Skip to content

Commit e13a024

Browse files
committed
additional callback typing, enums, mypy
1 parent e76ddf6 commit e13a024

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

adafruit_neotrellis/multitrellis.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,33 @@
2929
__version__ = "0.0.0-auto.0"
3030
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
3131

32-
from typing import List, Optional, Sequence
32+
from dataclasses import dataclass
33+
from typing import Callable, List, Optional, Sequence, TypeAlias
3334

34-
from adafruit_neotrellis.neotrellis import CallbackType, NeoTrellis
35+
from adafruit_neotrellis.neotrellis import KeyEvent as SeesawKeyEvent
36+
from adafruit_neotrellis.neotrellis import KeypadEdge # noqa: F401
37+
from adafruit_neotrellis.neotrellis import NeoTrellis
3538
from adafruit_seesaw.neopixel import ColorType
3639

3740

41+
@dataclass
42+
class KeyEvent:
43+
x: int
44+
y: int
45+
edge: int # KeypadEdge
46+
47+
48+
CallbackType: TypeAlias = Callable[[KeyEvent], None]
49+
50+
3851
class MultiTrellis:
3952
"""Driver for multiple connected Adafruit NeoTrellis boards."""
4053

4154
_trelli: List[List[NeoTrellis]]
4255
_rows: int
4356
_cols: int
4457
_key_pads: List[List[NeoTrellis]]
58+
_callbacks: List[List[Optional[CallbackType]]]
4559

4660
def __init__(self, neotrellis_array: List[List[NeoTrellis]]):
4761
self._trelli = neotrellis_array
@@ -76,15 +90,23 @@ def __init__(self, neotrellis_array: List[List[NeoTrellis]]):
7690
self._height = row_size_sum[self._rows - 1]
7791
self._key_pads: List[List[NeoTrellis]] = []
7892

93+
for y in range(self._height):
94+
self._key_pads.append([])
7995
for py in range(self._rows):
8096
for px in range(self._cols):
8197
t = self._trelli[py][px]
8298
for ky in range(t.height):
8399
y = t.y_base + ky
84-
self._key_pads.append([])
85100
for kx in range(t.width):
86101
self._key_pads[y].append(t)
87102

103+
@staticmethod
104+
def _callback_wrapper(t: NeoTrellis,
105+
event: SeesawKeyEvent,
106+
cb: CallbackType) -> None:
107+
x, y = t.key_xy(event.number)
108+
cb(KeyEvent(x=x, y=y, edge=event.edge))
109+
88110
@property
89111
def width(self):
90112
return self._width
@@ -110,26 +132,28 @@ def __getitem__(self, subscript: int) -> Sequence[NeoTrellis]:
110132
def get_keypad(self, x: int, y: int) -> NeoTrellis:
111133
return self._key_pads[y][x]
112134

113-
def activate_key(self, x: int, y: int, edge: int, enable: bool = True):
135+
def activate_key(self, x: int, y:
136+
int, edge: # KeypadEdge
137+
int, enable: bool = True):
114138
"""Activate or deactivate a key on the trellis. x and y are the index
115139
of the key measured from the top lefthand corner. Edge specifies what
116140
edge to register an event on and can be NeoTrellis.EDGE_FALLING or
117141
NeoTrellis.EDGE_RISING. enable should be set to True if the event is
118142
to be enabled, or False if the event is to be disabled."""
119143
pad = self._key_pads[y][x]
120-
pad.activate_key(pad.key_index(x, y), enable)
144+
pad.activate_key(pad.key_index(x, y), edge, enable)
121145

122146
def set_callback(self, x: int, y: int, function: CallbackType):
123147
"""Set a callback function for when an event for the key at index x, y
124148
(measured from the top lefthand corner) is detected."""
125149
pad = self._key_pads[y][x]
126-
pad.callbacks[pad.key_index(x, y)] = function
150+
self._callbacks[y][x] = function
151+
pad.callbacks[pad.key_index(x, y)] = lambda t, e: self._callback_wrapper(t, e, function)
127152

128153
def get_callback(self, x: int, y: int) -> Optional[CallbackType]:
129154
"""Get a callback function for when an event for the key at index x, y
130155
(measured from the top lefthand corner) is detected."""
131-
pad = self._key_pads[y][x]
132-
return pad.callbacks[pad.key_index(x, y)]
156+
return self._callbacks[y][x]
133157

134158
def color(self, x: int, y: int, color: ColorType):
135159
"""Set the color of the pixel at index x, y measured from the top

adafruit_neotrellis/neotrellis.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from time import sleep
5050
from typing import Callable, List, Optional, Sequence, Tuple, TypeAlias
5151

52+
from adafruit_seesaw.keypad import KeypadEdge # noqa: F401
5253
from adafruit_seesaw.keypad import KeyEvent, Keypad, ResponseType
5354
from adafruit_seesaw.neopixel import ColorType, NeoPixel
5455
from micropython import const
@@ -65,7 +66,7 @@
6566
SYNC_DELAY = const(0.0005)
6667
INIT_DELAY = const(0.0005)
6768

68-
CallbackType: TypeAlias = Callable[[KeyEvent], None]
69+
CallbackType: TypeAlias = Callable[['NeoTrellis', KeyEvent], None]
6970

7071

7172
class NeoTrellis(Keypad):
@@ -97,7 +98,9 @@ def __init__(self, i2c_bus, interrupt: bool = False,
9798
self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, self.width * self.height)
9899
sleep(INIT_DELAY)
99100

100-
def activate_key(self, key: int, edge: int, enable: bool = True) -> None:
101+
def activate_key(self, key:
102+
int, edge: # KeypadEdge
103+
int, enable: bool = True) -> None:
101104
"""Activate or deactivate a key on the trellis. Key is the key number from
102105
0 to 16. Edge specifies what edge to register an event on and can be
103106
NeoTrellis.EDGE_FALLING or NeoTrellis.EDGE_RISING. enable should be set
@@ -128,18 +131,22 @@ def sync(self) -> None:
128131
buf = self.read_keypad(available)
129132
for r in buf:
130133
if r.response_type == ResponseType.TYPE_KEY:
131-
(e, n) = r.data_edge_num()
132-
evt = KeyEvent(n, e)
134+
evt = r.data_keyevent()
133135
callback = self.callbacks[evt.number]
134136
if (
135137
callback is not None
136138
and evt.number < _NEO_TRELLIS_NUM_KEYS
137139
):
138-
callback(evt)
140+
callback(self, evt)
139141

140142
def local_key_index(self, x: int, y: int) -> int:
141143
return int(y * self.width + x)
142144

143145
def key_index(self, x: int, y: int) -> int:
144146
return int((y - self.y_base) * self.width + (x - self.x_base))
145147

148+
def local_key_xy(self, key: int) -> Tuple[int, int]:
149+
return key // self.width, key % self.width
150+
151+
def key_xy(self, key: int) -> Tuple[int, int]:
152+
return self.y_base + key // self.width, self.x_base + key % self.width

0 commit comments

Comments
 (0)