-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuttons.py
30 lines (26 loc) · 1.04 KB
/
buttons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from alarm import pin
from digitalio import DigitalInOut, Pull
class Buttons():
def __init__(self, pins_to_pressed_bool_map):
self.pins_to_pressed_bool_map = pins_to_pressed_bool_map
self.callbacks = {}
def get_alarms(self):
alarms = []
for _pin, pressed_val in self.pins_to_pressed_bool_map.items():
alarms.append(pin.PinAlarm(pin=_pin, value=pressed_val, pull=True))
return alarms
def set_callback(self, pin, callback):
self.callbacks[pin] = callback
def loop(self):
for pin, callback in self.callbacks.items():
button = DigitalInOut(pin)
pull = Pull.DOWN if self.pins_to_pressed_bool_map[pin] else Pull.UP
button.switch_to_input(pull=pull)
if button.value == self.pins_to_pressed_bool_map[pin]:
callback()
while button.value == self.pins_to_pressed_bool_map[pin]:
pass
button.deinit()
return
else:
button.deinit()