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
3538from 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+
3851class 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
0 commit comments