30
30
import neopixel
31
31
import adafruit_matrixkeypad
32
32
33
+ try :
34
+ from typing import List , Optional , Tuple , Union
35
+ from typing_extensions import Literal
36
+ from microcontroller import Pin
37
+ except ImportError :
38
+ pass
39
+
33
40
__version__ = "0.0.0+auto.0"
34
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4.git"
35
42
36
43
37
44
class _NeoPixelArray :
38
45
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
39
46
40
- def __init__ (self , pin , * , width , height , rotation = 0 ):
47
+ def __init__ (
48
+ self ,
49
+ pin : Pin ,
50
+ * ,
51
+ width : int ,
52
+ height : int ,
53
+ rotation : Literal [0 , 90 , 180 , 270 ] = 0 ,
54
+ ) -> None :
41
55
self ._neopixel = neopixel .NeoPixel (pin , width * height , auto_write = True )
42
56
if rotation % 90 != 0 :
43
57
raise ValueError ("Only 90 degree rotations supported" )
@@ -47,7 +61,7 @@ def __init__(self, pin, *, width, height, rotation=0):
47
61
self ._width = width
48
62
self ._height = height
49
63
50
- def __setitem__ (self , index , value ) :
64
+ def __setitem__ (self , index : Tuple [ int , int ], value : int ) -> None :
51
65
if not isinstance (index , tuple ) or len (index ) != 2 :
52
66
raise IndexError ("Index must be tuple" )
53
67
if index [0 ] >= self .width or index [1 ] >= self .height :
@@ -57,7 +71,7 @@ def __setitem__(self, index, value):
57
71
58
72
self ._neopixel [offset ] = value
59
73
60
- def __getitem__ (self , index ) :
74
+ def __getitem__ (self , index : Tuple [ int , int ]) -> int :
61
75
if not isinstance (index , tuple ) or len (index ) != 2 :
62
76
raise IndexError ("Index must be tuple" )
63
77
if index [0 ] >= self .width or index [1 ] >= self .height :
@@ -67,7 +81,7 @@ def __getitem__(self, index):
67
81
68
82
return self ._neopixel [offset ]
69
83
70
- def _calculate_pixel_offset (self , index ) :
84
+ def _calculate_pixel_offset (self , index : Tuple [ int , int ]) -> Optional [ int ] :
71
85
if self ._rotation in (0 , 180 ):
72
86
offset = self .width * index [1 ] + index [0 ]
73
87
if self ._rotation == 180 :
@@ -82,7 +96,7 @@ def _calculate_pixel_offset(self, index):
82
96
83
97
return offset
84
98
85
- def show (self ):
99
+ def show (self ) -> None :
86
100
"""
87
101
Shows the new colors on the pixels themselves if they haven't already
88
102
been autowritten.
@@ -95,7 +109,7 @@ def show(self):
95
109
self ._neopixel .show ()
96
110
97
111
@property
98
- def auto_write (self ):
112
+ def auto_write (self ) -> bool :
99
113
"""
100
114
True if the neopixels should immediately change when set. If False,
101
115
``show`` must be called explicitly.
@@ -122,11 +136,11 @@ def auto_write(self):
122
136
return self ._neopixel .auto_write
123
137
124
138
@auto_write .setter
125
- def auto_write (self , val ) :
139
+ def auto_write (self , val : bool ) -> None :
126
140
self ._neopixel .auto_write = val
127
141
128
142
@property
129
- def brightness (self ):
143
+ def brightness (self ) -> float :
130
144
"""
131
145
The overall brightness of the pixel. Must be a number between 0 and
132
146
1, where the number represents a percentage between 0 and 100, i.e. ``0.3`` is 30%.
@@ -146,10 +160,10 @@ def brightness(self):
146
160
return self ._neopixel .brightness
147
161
148
162
@brightness .setter
149
- def brightness (self , brightness ) :
163
+ def brightness (self , brightness : float ) -> None :
150
164
self ._neopixel .brightness = brightness
151
165
152
- def fill (self , color ) :
166
+ def fill (self , color : Union [ Tuple [ int , int , int ], int ]) -> None :
153
167
"""
154
168
Colors all the pixels a given color.
155
169
@@ -168,7 +182,7 @@ def fill(self, color):
168
182
self ._neopixel .fill (color )
169
183
170
184
@property
171
- def width (self ):
185
+ def width (self ) -> int :
172
186
"""
173
187
The width of the grid. When ``rotation`` is 0, ``width`` is 8.
174
188
@@ -185,7 +199,7 @@ def width(self):
185
199
return self ._width
186
200
187
201
@property
188
- def height (self ):
202
+ def height (self ) -> int :
189
203
"""The height of the grid. When ``rotation`` is 0, ``height`` is 4.
190
204
191
205
.. code-block:: python
@@ -228,7 +242,7 @@ class TrellisM4Express:
228
242
current_press = pressed
229
243
"""
230
244
231
- def __init__ (self , rotation = 0 ) :
245
+ def __init__ (self , rotation : Literal [ 0 , 90 , 180 , 270 ] = 0 ) -> None :
232
246
self ._rotation = rotation
233
247
234
248
# Define NeoPixels
@@ -296,12 +310,12 @@ def __init__(self, rotation=0):
296
310
297
311
cols = []
298
312
for x in range (8 ):
299
- col = digitalio .DigitalInOut (getattr (board , "COL{}" . format ( x ) ))
313
+ col = digitalio .DigitalInOut (getattr (board , f "COL{ x } " ))
300
314
cols .append (col )
301
315
302
316
rows = []
303
317
for y in range (4 ):
304
- row = digitalio .DigitalInOut (getattr (board , "ROW{}" . format ( y ) ))
318
+ row = digitalio .DigitalInOut (getattr (board , f "ROW{ y } " ))
305
319
rows .append (row )
306
320
307
321
key_names = []
@@ -322,7 +336,7 @@ def __init__(self, rotation=0):
322
336
self ._matrix = adafruit_matrixkeypad .Matrix_Keypad (cols , rows , key_names )
323
337
324
338
@property
325
- def pressed_keys (self ):
339
+ def pressed_keys (self ) -> List [ Tuple [ int , int ]] :
326
340
"""A list of tuples of currently pressed button coordinates.
327
341
328
342
.. code-block:: python
0 commit comments