-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
31 lines (25 loc) · 964 Bytes
/
helpers.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
31
import time
from micropython import const
from machine import Pin, Timer
class Button:
"""
Debounced pin handler
usage e.g.:
def button_callback(pin):
print("Button (%s) changed to: %r" % (pin, pin.value()))
button_handler = Button(pin=Pin(32, mode=Pin.IN, pull=Pin.PULL_UP), callback=button_callback)
"""
def __init__(self, pin, callback, trigger=Pin.IRQ_FALLING, min_ago=300):
self.callback = callback
self.min_ago = min_ago
self._blocked = False
self._next_call = time.ticks_ms() + self.min_ago
pin.irq(trigger=trigger, handler=self.debounce_handler)
def call_callback(self, pin):
self.callback(pin)
def debounce_handler(self, pin):
if time.ticks_ms() > self._next_call:
self._next_call = time.ticks_ms() + self.min_ago
self.call_callback(pin)
#else:
# print("debounce: %s" % (self._next_call - time.ticks_ms()))