import machine
from machine import Pin
from time import ticks_us, sleep
import ssd1306
# --- PIN SETUP ---
s1 = Pin(26, Pin.IN)
s2 = Pin(27, Pin.IN)
key = Pin(25, Pin.IN, Pin.PULL_UP)
# --- OLED SETUP ---
sda = machine.Pin(21)
scl = machine.Pin(22)
i2c = machine.I2C(0, sda=sda, scl=scl, freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# --- VARIABELEN ---
code = 1234
time_out = 30
time_in = 30
time_siren = 60
# Globale variabelen voor rotary
counter = 0
last_state = s1.value()
last_interrupt_time = 0
debounce_period = 2000
# Globale variabelen voor menu
state_settings = 0
last_state_settings = 0
lijn_vorige = 0
last_key_state = 1
last_key_interrupt_time = 0
# --- FUNCTIES VOOR SCHERM ---
# Deze functie update 1 specifiek getal op het scherm
def update_waarde(lijn, waarde):
y_pos = 5 + 15 * (lijn - 1)
# 1. Teken een zwart blokje over het oude getal (wissen)
# x=90, y=y_pos, breedte=38, hoogte=8, kleur=0 (zwart)
oled.fill_rect(95, y_pos, 38, 8, 0)
# 2. Schrijf het nieuwe getal
oled.text(str(waarde), 95, y_pos)
oled.show()
def oled_setup_scherm():
oled.fill(0)
# Teksten
oled.text("code", 10, 5)
oled.text("time_out", 10, 20)
oled.text("time_in", 10, 35)
oled.text("time_siren", 10, 50)
# Waarden direct tonen bij start
oled.text(str(code), 95, 5)
oled.text(str(time_out), 95, 20)
oled.text(str(time_in), 95, 35)
oled.text(str(time_siren), 95, 50)
oled.show()
def oled_pijl(lijn):
global lijn_vorige
# Oude pijl wissen (schrijf zwart)
if lijn_vorige != 0:
oled.text(">", 0, 5 + 15 * (lijn_vorige-1), 0)
# Nieuwe pijl tekenen (schrijf wit) als lijn > 0
if lijn > 0:
oled.text(">", 0, 5 + 15 * (lijn-1), 1)
lijn_vorige = lijn
oled.show()
# --- INTERRUPT HANDLERS ---
def s1_int_handler(p):
global counter, last_interrupt_time, last_state
current_time = ticks_us()
if current_time - last_interrupt_time > debounce_period:
last_interrupt_time = current_time
state = s1.value()
if state != last_state:
last_state = state
if state == 1:
if s2.value() != state:
counter += 1
else:
counter -= 1
def key_int_handler(p):
global last_key_interrupt_time, last_key_state, state_settings
current_time = ticks_us()
state = key.value()
if state != last_key_state:
last_key_state = state
if current_time - last_key_interrupt_time > 10000:
last_key_interrupt_time = current_time
if state == 0:
state_settings +=1
if state_settings >= 5:
state_settings = 0
# --- START ---
s1.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=s1_int_handler)
key.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=key_int_handler)
oled_setup_scherm()
print("Systeem start...")
while True:
# Als er aan de knop gedraaid is (counter != 0)
if counter != 0:
print("counter =", counter)
update_nodig = False
if state_settings == 1:
code += counter
print("code =", code)
update_waarde(1, code) # Update scherm direct
elif state_settings == 2:
time_out += counter
print("time_out =", time_out)
update_waarde(2, time_out)
elif state_settings == 3:
time_in += counter
print("time_in =", time_in)
update_waarde(3, time_in)
elif state_settings == 4:
time_siren += counter
print("time_siren =", time_siren)
update_waarde(4, time_siren)
counter = 0 # Reset counter na verwerking
# Als er op de knop gedrukt is (menu wissel)
if state_settings != last_state_settings:
last_state_settings = state_settings
if state_settings == 0:
print("terug naar normale werking")
elif state_settings == 1:
print("settings code")
elif state_settings == 2:
print("settings time out")
elif state_settings == 3:
print("settings time in")
elif state_settings == 4:
print("settings sirene")
oled_pijl(state_settings)
sleep(2)