import _thread
from machine import Pin
from neopixel import NeoPixel
from time import sleep_ms, ticks_ms, ticks_diff
# --- HARDWARE SETUP ---
pin1 = Pin(26, Pin.OUT)
np1 = NeoPixel(pin1, 8) # FRONT wagen
pin2 = Pin(27, Pin.OUT)
np2 = NeoPixel(pin2, 8) # REAR wagen
# --- KLEUREN (R, G, B) ---
UIT = (0, 0, 0)
WIT = (255, 255, 255)
DIM_WIT = (80, 80, 80)
ROOD = (255, 0, 0)
DIM_ROOD = (60, 0, 0)
BLAUW = (0, 0, 255)
ORANJE = (255, 100, 0)
# --- STATUS VARIABELEN (Global) ---
status_dimlicht = 0
status_remlicht = 0
status_zwaailicht = 0
status_pinker_l = 0
status_pinker_r = 0
# --- TIMERS VOOR ANIMATIES ---
laatste_tijd_zwaai = 0
zwaai_toggle = False
zwaai_snelheid = 150
laatste_tijd_pinker = 0
pinker_toggle = False
pinker_snelheid = 400
# --- FUNCTIES ---
def parse_commando(tekst):
"""Vertaalt inkomende tekst naar acties (past de globals aan)"""
global status_dimlicht, status_remlicht, status_zwaailicht, status_pinker_l, status_pinker_r
try:
delen = tekst.split('/')
if len(delen) == 2:
commando = delen[0].lower()
waarde = int(delen[1])
print(f"Ontvangen -> Commando: {commando}, Waarde: {waarde}")
if commando == "dimlicht": status_dimlicht = waarde
elif commando == "remlicht": status_remlicht = waarde
elif commando == "zwaailicht": status_zwaailicht = waarde
elif commando == "pinker_l": status_pinker_l = waarde
elif commando == "pinker_r": status_pinker_r = waarde
elif commando == "gevaren":
status_pinker_l = waarde
status_pinker_r = waarde
else:
print("Onbekend commando!")
except:
pass # Negeer foute invoer (zoals lege enters)
def update_leds():
"""Berekent welke LED welke kleur moet hebben"""
global zwaai_toggle, laatste_tijd_zwaai, pinker_toggle, laatste_tijd_pinker
huidige_tijd = ticks_ms()
# 1. Update Timers
if ticks_diff(huidige_tijd, laatste_tijd_zwaai) > zwaai_snelheid:
zwaai_toggle = not zwaai_toggle
laatste_tijd_zwaai = huidige_tijd
if ticks_diff(huidige_tijd, laatste_tijd_pinker) > pinker_snelheid:
pinker_toggle = not pinker_toggle
laatste_tijd_pinker = huidige_tijd
# 2. Maak strips leeg
for i in range(8):
np1[i] = UIT
np2[i] = UIT
# 3. Bouw de verlichting op
if status_dimlicht == 1:
np1[1], np1[2], np1[5], np1[6] = DIM_WIT, DIM_WIT, DIM_WIT, DIM_WIT
np2[1], np2[2], np2[5], np2[6] = DIM_ROOD, DIM_ROOD, DIM_ROOD, DIM_ROOD
if status_remlicht == 1:
np2[1], np2[2], np2[5], np2[6] = ROOD, ROOD, ROOD, ROOD
if status_zwaailicht == 1:
if zwaai_toggle:
np1[3], np1[4] = BLAUW, UIT
np2[3], np2[4] = BLAUW, UIT
else:
np1[3], np1[4] = UIT, BLAUW
np2[3], np2[4] = UIT, BLAUW
if status_pinker_l == 1 and pinker_toggle:
np1[0], np2[0] = ORANJE, ORANJE
if status_pinker_r == 1 and pinker_toggle:
np1[7], np2[7] = ORANJE, ORANJE
# 4. Schrijf de data
np1.write()
np2.write()
# --- THREAD FUNCTIE VOOR INPUT ---
def input_luisteraar():
"""Deze functie draait als een apart proces op de achtergrond"""
while True:
try:
# Hier blokkeert DEZE thread tot je op Enter drukt.
# Maar de LEDs in de hoofd-lus draaien gewoon door!
inkomende_tekst = input()
if inkomende_tekst:
parse_commando(inkomende_tekst.strip())
except Exception as e:
print("Fout in input thread:", e)
# --- MAIN LOOP ---
print("--- Politie LED Simulator (THREADING VERSIE) Gestart ---")
print("Typ commando's in de terminal (bijv: dimlicht/1, zwaailicht/1) en druk op Enter.")
# Start de thread op de achtergrond!
_thread.start_new_thread(input_luisteraar, ())
# Dit is de 'hoofd' lus. Deze blijft eindeloos de leds updaten.
# Omdat we input() hebben verbannen naar de thread, loopt dit soepel.
while True:
update_leds()
sleep_ms(10) # Voorkomt dat de loop té veel CPU vreet