pizzabox-ctrl/micropizza/test.py
2021-11-03 13:54:46 +01:00

39 lines
No EOL
1.1 KiB
Python

from machine import Pin, Timer
import time
led = Pin(25, Pin.OUT)
btn_blu = Pin(2, Pin.IN, Pin.PULL_UP)
btn_red = Pin(3, Pin.IN, Pin.PULL_UP)
def user_interaction(timeout):
def _toggle_leds(timer):
led.toggle()
tmr = Timer()
tmr.init(freq=2, mode=Timer.PERIODIC, callback=_toggle_leds)
startime = time.ticks_ms()
timeout *= 1000
while time.ticks_diff(time.ticks_ms(), startime) < timeout:
b, r = btn_blu.value(), btn_red.value()
if (b + r) < 2:
tmr.deinit()
return 'B' if not b else 'R'
tmr.deinit()
return 'T' # Timeout
def recording(timeout):
timeout *= 1000
startime = time.ticks_ms()
endtime = time.ticks_add(startime, timeout)
while time.ticks_diff(time.ticks_ms(), startime) < timeout:
if not btn_blu.value():
led.value(0)
return 'I'
restime = time.ticks_diff(endtime, time.ticks_ms())
if restime < 5000:
led.value((restime // 500) % 2)
time.sleep_ms(50)
return 'OK'
if __name__ == '__main__':
print(recording(20))