-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_handler.py
More file actions
28 lines (25 loc) · 921 Bytes
/
button_handler.py
File metadata and controls
28 lines (25 loc) · 921 Bytes
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
import machine
import asyncio
import sys
class ButtonHandler:
def __init__(self, pin, callback, debounce=0.04):
self._downCounter = 0
self.pin = machine.Pin(pin, machine.Pin.IN)
self.debounce = debounce
self.callback = callback
self.reset_counter = 10 / debounce
async def poll(self):
while True:
if self._downCounter >= self.reset_counter:
print("Resetting...")
machine.reset()
elif self._downCounter > 0 and self.pin.value() == 1:
self._downCounter = 0
try:
await self.callback()
except Exception as e:
print("Uncaught exception in callback:", e)
sys.print_exception(e)
elif self.pin.value() == 0:
self._downCounter += 1
await asyncio.sleep(self.debounce)