-
Notifications
You must be signed in to change notification settings - Fork 0
/
neokey_debounced.py
51 lines (41 loc) · 1.54 KB
/
neokey_debounced.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from micropython import const
from adafruit_seesaw import neopixel, digitalio
from adafruit_seesaw.seesaw import Seesaw
from adafruit_debouncer import Debouncer
try:
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass
_NEOKEY1X4_ADDR = const(0x30)
_NEOKEY1X4_NEOPIX_PIN = const(3)
_NEOKEY1X4_NUM_ROWS = const(1)
_NEOKEY1X4_NUM_COLS = const(4)
_NEOKEY1X4_NUM_KEYS = const(4)
class NeoKey1x4Debounced(Seesaw):
"""Driver for the Adafruit NeoKey 1x4."""
def __init__(
self, i2c_bus: I2C, interrupt: bool = False, addr: int = _NEOKEY1X4_ADDR
) -> None:
super().__init__(i2c_bus, addr)
self.interrupt_enabled = interrupt
self.pixels = neopixel.NeoPixel(
self,
_NEOKEY1X4_NEOPIX_PIN,
_NEOKEY1X4_NUM_KEYS,
brightness=0.3,
pixel_order=neopixel.GRB,
)
# set the pins to inputs, pullups
self.debounced_switches = []
for pin_number in range(4, 8):
self.pin_mode(pin_number, self.INPUT_PULLUP)
pin = digitalio.DigitalIO(self, pin_number)
self.debounced_switches.append(Debouncer(pin, 0.05))
def debounce_update(self):
for key in self.debounced_switches:
key.update()
def __getitem__(self, index: int) -> bool:
if not isinstance(index, int) or (index < 0) or (index > 3):
raise RuntimeError("Index must be 0 thru 3")
return not self.debounced_switches[index].value