2021-11-02 16:47:01 +00:00
|
|
|
from machine import Pin, Timer
|
|
|
|
import time
|
|
|
|
|
|
|
|
led = Pin(25, Pin.OUT)
|
|
|
|
|
2021-11-04 12:44:19 +00:00
|
|
|
btn_blu = Pin(5, Pin.IN, Pin.PULL_UP)
|
2021-11-02 16:47:01 +00:00
|
|
|
btn_red = Pin(3, Pin.IN, Pin.PULL_UP)
|
2021-11-04 12:44:19 +00:00
|
|
|
led_blu = Pin(4, Pin.OUT)
|
|
|
|
led_red = Pin(2, Pin.OUT)
|
2021-11-02 16:47:01 +00:00
|
|
|
|
|
|
|
def user_interaction(timeout):
|
|
|
|
def _toggle_leds(timer):
|
2021-11-04 12:44:19 +00:00
|
|
|
led_blu.toggle()
|
|
|
|
led_red.toggle()
|
2021-11-02 16:47:01 +00:00
|
|
|
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()
|
2021-11-04 12:44:19 +00:00
|
|
|
led_blu.value(0)
|
|
|
|
led_red.value(0)
|
2021-11-02 16:47:01 +00:00
|
|
|
return 'T' # Timeout
|
|
|
|
|
2021-11-03 12:54:46 +00:00
|
|
|
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'
|
|
|
|
|
2021-11-02 16:47:01 +00:00
|
|
|
if __name__ == '__main__':
|
2021-11-04 12:44:19 +00:00
|
|
|
print(user_interaction(20))
|
|
|
|
#print(recording(20))
|