chore: initialize repository and staged implementation plan
This commit is contained in:
commit
cb0e9d156f
48 files changed
+7343
No files matched your search
@@ -0,0 +1,365 @@
|
||||
#!/usr/bin/env python3
|
||||
import RPi.GPIO as GPIO
|
||||
import serial
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
|
||||
from sim_clock import SimClock
|
||||
from encoder_handler import EncoderHandler
|
||||
from barrier import Barrier
|
||||
from turnstile import Turnstile
|
||||
from gate_controller import GateController
|
||||
from led_strip import LEDStrip
|
||||
from badusb import BadUSBManager
|
||||
|
||||
SERIAL_PORT = '/dev/serial0'
|
||||
BAUD = 115200
|
||||
HELLO_INTERVAL = 1.0
|
||||
HELLO_REPLY_COOLDOWN = 2.0
|
||||
RADIO_SAFE_DEBOUNCE = 0.1
|
||||
|
||||
# --- ОБНОВЛЕННЫЕ UID КАРТ ---
|
||||
AUTHORIZED_UIDS_1 = ['2EF588F2', 'CE0385F2'] # Турникет (канал 1)
|
||||
AUTHORIZED_UIDS_2 = ['CE0385F2'] # Второй сканер (канал 2)
|
||||
SHUTDOWN_UID = 'A1CE8B0D' # Карта выключения (если нужна)
|
||||
# ----------------------------
|
||||
|
||||
MOVE_IGNORE_DELAY = 0.2
|
||||
TURNSTILE_OPEN_TIME = 2.0
|
||||
TIME_SEND_MIN_INTERVAL = 0.5
|
||||
|
||||
STATE_WAITING = 0
|
||||
STATE_RUNNING = 1
|
||||
STATE_SHUTDOWN = 2
|
||||
state = STATE_WAITING
|
||||
last_hello_sent = 0.0
|
||||
last_hello_reply = 0.0
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
sim_clock = SimClock()
|
||||
barrier = Barrier(pin=12)
|
||||
turnstile = Turnstile(pin=13)
|
||||
gate = GateController(motor_pin1=5, motor_pin2=6, hallA0=22, hallA1=23)
|
||||
led_strip = LEDStrip()
|
||||
|
||||
badusb = BadUSBManager(time_provider=lambda: sim_clock.current_time)
|
||||
|
||||
ser = serial.Serial(SERIAL_PORT, BAUD, timeout=0.05, write_timeout=0.1)
|
||||
|
||||
direction = 1
|
||||
moving = False
|
||||
move_start_time = 0.0
|
||||
turnstile_open_time = 0.0
|
||||
barrier_open = False
|
||||
_last_minute = None
|
||||
_last_time_sent = 0.0
|
||||
|
||||
_last_radio_code = None
|
||||
_last_radio_time = 0.0
|
||||
|
||||
_current_time_mode = None
|
||||
|
||||
serial_lock = threading.Lock()
|
||||
|
||||
def send_json(obj):
|
||||
with serial_lock:
|
||||
try:
|
||||
ser.write((json.dumps(obj) + '\n').encode())
|
||||
except serial.SerialTimeoutException:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"Serial write error: {e}")
|
||||
|
||||
def send_hello():
|
||||
send_json({"type": "hello", "role": "rpi"})
|
||||
|
||||
def send_time():
|
||||
send_json({"type": "time", "value": sim_clock.time_string('%d.%m.%y %H:%M')})
|
||||
|
||||
def prev_mode():
|
||||
mode = led_strip.mode
|
||||
if mode >= LEDStrip.MODE_POLICE:
|
||||
led_strip.set_mode(LEDStrip.MODE_RUNNING_DOTS)
|
||||
else:
|
||||
new_mode = mode - 1
|
||||
if new_mode < LEDStrip.MODE_RAINBOW_RUN:
|
||||
new_mode = LEDStrip.MODE_RUNNING_DOTS
|
||||
led_strip.set_mode(new_mode)
|
||||
|
||||
def next_mode():
|
||||
mode = led_strip.mode
|
||||
if mode >= LEDStrip.MODE_POLICE:
|
||||
led_strip.set_mode(LEDStrip.MODE_RAINBOW_RUN)
|
||||
else:
|
||||
new_mode = mode + 1
|
||||
if new_mode > LEDStrip.MODE_RUNNING_DOTS:
|
||||
new_mode = LEDStrip.MODE_RAINBOW_RUN
|
||||
led_strip.set_mode(new_mode)
|
||||
|
||||
def _special_mode(target):
|
||||
if led_strip.mode != target:
|
||||
if led_strip.mode < LEDStrip.MODE_POLICE:
|
||||
led_strip.prev_mode = led_strip.mode
|
||||
led_strip.set_mode(target)
|
||||
else:
|
||||
led_strip.set_mode(led_strip.prev_mode)
|
||||
|
||||
def handle_ir(code):
|
||||
if isinstance(code, str):
|
||||
try:
|
||||
code = int(code, 16)
|
||||
except ValueError:
|
||||
return
|
||||
if code == 0xBA45FF00:
|
||||
led_strip.set_speed(led_strip.speed - 1)
|
||||
elif code == 0xB847FF00:
|
||||
led_strip.set_speed(led_strip.speed + 1)
|
||||
elif code == 0xB946FF00:
|
||||
led_strip.toggle_enabled()
|
||||
elif code == 0xBB44FF00:
|
||||
led_strip.set_direction(-1)
|
||||
elif code == 0xBF40FF00:
|
||||
led_strip.set_direction(1)
|
||||
elif code == 0xBC43FF00:
|
||||
led_strip.toggle_paused()
|
||||
elif code == 0xF807FF00:
|
||||
led_strip.set_brightness(led_strip.brightness - 1)
|
||||
elif code == 0xEA15FF00:
|
||||
led_strip.set_brightness(led_strip.brightness + 1)
|
||||
elif code == 0xE916FF00:
|
||||
led_strip.set_mode(LEDStrip.MODE_RAINBOW_RUN)
|
||||
led_strip.prev_mode = LEDStrip.MODE_RAINBOW_RUN
|
||||
led_strip.set_speed(5)
|
||||
led_strip.set_brightness(5)
|
||||
led_strip.set_direction(1)
|
||||
if led_strip.paused:
|
||||
led_strip.toggle_paused()
|
||||
led_strip.phase = 0.0
|
||||
led_strip.dot_pos = 0.0
|
||||
elif code == 0xE619FF00:
|
||||
prev_mode()
|
||||
elif code == 0xF20DFF00:
|
||||
next_mode()
|
||||
elif code == 0xF30CFF00:
|
||||
led_strip.set_color_balance(led_strip.red + 1, led_strip.green, led_strip.blue)
|
||||
elif code == 0xE718FF00:
|
||||
led_strip.set_color_balance(led_strip.red, led_strip.green + 1, led_strip.blue)
|
||||
elif code == 0xA15EFF00:
|
||||
led_strip.set_color_balance(led_strip.red, led_strip.green, led_strip.blue + 1)
|
||||
elif code == 0xF708FF00:
|
||||
_special_mode(LEDStrip.MODE_POLICE)
|
||||
elif code == 0xE31CFF00:
|
||||
_special_mode(LEDStrip.MODE_SERVICE)
|
||||
elif code == 0xA55AFF00:
|
||||
_special_mode(LEDStrip.MODE_ALARM)
|
||||
elif code == 0xBD42FF00:
|
||||
led_strip.set_color_balance(led_strip.red - 1, led_strip.green, led_strip.blue)
|
||||
elif code == 0xAD52FF00:
|
||||
led_strip.set_color_balance(led_strip.red, led_strip.green - 1, led_strip.blue)
|
||||
elif code == 0xB54AFF00:
|
||||
led_strip.set_color_balance(led_strip.red, led_strip.green, led_strip.blue - 1)
|
||||
|
||||
def handle_radio(code, burst_count=0):
|
||||
global direction, moving, move_start_time, barrier_open
|
||||
global _last_radio_code, _last_radio_time
|
||||
|
||||
if isinstance(code, str):
|
||||
try:
|
||||
code = int(code)
|
||||
except ValueError:
|
||||
return
|
||||
now = time.time()
|
||||
if code == _last_radio_code and (now - _last_radio_time) < RADIO_SAFE_DEBOUNCE:
|
||||
return
|
||||
_last_radio_code = code
|
||||
_last_radio_time = now
|
||||
print(f"Radio: {code} (burst={burst_count})")
|
||||
if code == 460770:
|
||||
direction = -direction
|
||||
moving = True
|
||||
move_start_time = time.time()
|
||||
gate.move(direction)
|
||||
elif code == 460769:
|
||||
if barrier_open:
|
||||
barrier.close()
|
||||
barrier_open = False
|
||||
else:
|
||||
barrier.open()
|
||||
barrier_open = True
|
||||
|
||||
def handle_rfid(channel, uid):
|
||||
global turnstile_open_time
|
||||
if uid == SHUTDOWN_UID:
|
||||
send_json({"type": "shutdown"})
|
||||
shutdown_system()
|
||||
return
|
||||
if channel == 1:
|
||||
auth = uid in AUTHORIZED_UIDS_1
|
||||
send_json({
|
||||
"type": "rfid_result",
|
||||
"channel": 1,
|
||||
"result": "granted" if auth else "denied",
|
||||
"uid": uid,
|
||||
})
|
||||
if auth:
|
||||
turnstile.open()
|
||||
turnstile_open_time = time.time()
|
||||
elif channel == 2:
|
||||
auth = uid in AUTHORIZED_UIDS_2
|
||||
send_json({
|
||||
"type": "rfid_result",
|
||||
"channel": 2,
|
||||
"result": "granted" if auth else "denied",
|
||||
"uid": uid,
|
||||
})
|
||||
|
||||
def process_message(msg):
|
||||
global state, _last_minute, last_hello_reply
|
||||
t = msg.get("type")
|
||||
|
||||
if t == "hello":
|
||||
now = time.time()
|
||||
if state == STATE_WAITING:
|
||||
state = STATE_RUNNING
|
||||
print("Handshake complete")
|
||||
try:
|
||||
ser.reset_input_buffer()
|
||||
except Exception:
|
||||
pass
|
||||
send_hello()
|
||||
send_time()
|
||||
_last_minute = sim_clock.time_string('%d.%m.%y %H:%M')
|
||||
last_hello_reply = now
|
||||
else:
|
||||
if now - last_hello_reply >= HELLO_REPLY_COOLDOWN:
|
||||
print("ESP hello in RUNNING, replying")
|
||||
send_hello()
|
||||
last_hello_reply = now
|
||||
send_time()
|
||||
_last_minute = sim_clock.time_string('%d.%m.%y %H:%M')
|
||||
return
|
||||
|
||||
if state != STATE_RUNNING:
|
||||
return
|
||||
|
||||
if t == "rfid":
|
||||
handle_rfid(msg.get("channel"), msg.get("uid"))
|
||||
elif t == "ir":
|
||||
handle_ir(msg.get("code"))
|
||||
elif t == "radio":
|
||||
handle_radio(msg.get("code"), msg.get("count", 0))
|
||||
|
||||
def on_encoder_speed(new_speed):
|
||||
sim_clock.set_acceleration(new_speed)
|
||||
|
||||
def on_short_press():
|
||||
sim_clock.stop()
|
||||
|
||||
def on_long_press():
|
||||
sim_clock.reset_to_system_time()
|
||||
|
||||
encoder = EncoderHandler(
|
||||
clk_pin=16, dt_pin=19, sw_pin=7,
|
||||
speed_callback=on_encoder_speed,
|
||||
short_press_callback=on_short_press,
|
||||
long_press_callback=on_long_press,
|
||||
long_press_sec=3.0
|
||||
)
|
||||
|
||||
def maybe_send_time():
|
||||
global _last_minute, _last_time_sent
|
||||
current = sim_clock.time_string('%d.%m.%y %H:%M')
|
||||
if current == _last_minute:
|
||||
return
|
||||
now = time.time()
|
||||
if (now - _last_time_sent) < TIME_SEND_MIN_INTERVAL:
|
||||
return
|
||||
send_time()
|
||||
_last_minute = current
|
||||
_last_time_sent = now
|
||||
|
||||
def shutdown_system():
|
||||
global state
|
||||
state = STATE_SHUTDOWN
|
||||
print("Shutting down...")
|
||||
time.sleep(0.3)
|
||||
badusb.stop()
|
||||
for fn in (led_strip.shutdown, turnstile.stop, barrier.stop,
|
||||
gate.stop, encoder.stop, ser.close):
|
||||
try:
|
||||
fn()
|
||||
except Exception:
|
||||
pass
|
||||
GPIO.cleanup()
|
||||
exit(0)
|
||||
|
||||
def main_loop():
|
||||
global moving, last_hello_sent, turnstile_open_time, _current_time_mode
|
||||
buf = b""
|
||||
while True:
|
||||
now = time.time()
|
||||
|
||||
encoder.check_timeout()
|
||||
|
||||
if state == STATE_WAITING:
|
||||
if now - last_hello_sent >= HELLO_INTERVAL:
|
||||
send_hello()
|
||||
last_hello_sent = now
|
||||
|
||||
if state == STATE_RUNNING:
|
||||
sim_clock.update()
|
||||
maybe_send_time()
|
||||
|
||||
if sim_clock.is_rewinding:
|
||||
mode = 'FWD' if sim_clock.speed > 1.0 else 'REV'
|
||||
if _current_time_mode != mode:
|
||||
badusb.set_time_mode(mode)
|
||||
_current_time_mode = mode
|
||||
else:
|
||||
if _current_time_mode is not None:
|
||||
badusb.set_time_mode('PLAY')
|
||||
_current_time_mode = None
|
||||
|
||||
try:
|
||||
n = ser.in_waiting
|
||||
if n:
|
||||
buf += ser.read(n)
|
||||
while b'\n' in buf:
|
||||
raw_line, buf = buf.split(b'\n', 1)
|
||||
raw_line = raw_line.strip(b'\r').strip()
|
||||
if not raw_line:
|
||||
continue
|
||||
if not (raw_line.startswith(b'{') and raw_line.endswith(b'}')):
|
||||
print(f"Skip garbage: {raw_line!r}")
|
||||
continue
|
||||
try:
|
||||
process_message(json.loads(raw_line.decode('utf-8')))
|
||||
except Exception as e:
|
||||
print(f"Parse error: {e} | raw={raw_line!r}")
|
||||
except Exception as e:
|
||||
print(f"Serial read error: {e}")
|
||||
|
||||
if moving:
|
||||
if now - move_start_time >= MOVE_IGNORE_DELAY:
|
||||
a0, a1 = gate.read_hall_sensors()
|
||||
stop_cond = (direction == 1 and a0 == 0) or (direction == -1 and a1 == 0)
|
||||
if stop_cond:
|
||||
gate.stop()
|
||||
moving = False
|
||||
|
||||
if turnstile_open_time and now - turnstile_open_time >= TURNSTILE_OPEN_TIME:
|
||||
turnstile.close()
|
||||
turnstile_open_time = 0
|
||||
|
||||
time.sleep(0.01)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
print(f"RPi starting on {SERIAL_PORT}, waiting for ESP handshake...")
|
||||
badusb.start()
|
||||
main_loop()
|
||||
except KeyboardInterrupt:
|
||||
shutdown_system()
|
||||
Reference in new issue
Block a user