30
30
import neopixel
31
31
import adafruit_matrixkeypad
32
32
33
+ try :
34
+ from typing import List , Optional , Tuple , Union
35
+ from microcontroller import Pin
36
+ except ImportError :
37
+ pass
38
+
33
39
__version__ = "0.0.0+auto.0"
34
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4.git"
35
41
36
42
37
43
class _NeoPixelArray :
38
44
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
39
45
40
- def __init__ (self , pin , * , width , height , rotation = 0 ) :
46
+ def __init__ (self , pin : Pin , * , width : int , height : int , rotation : int = 0 ) -> None :
41
47
self ._neopixel = neopixel .NeoPixel (pin , width * height , auto_write = True )
42
48
if rotation % 90 != 0 :
43
49
raise ValueError ("Only 90 degree rotations supported" )
@@ -47,7 +53,7 @@ def __init__(self, pin, *, width, height, rotation=0):
47
53
self ._width = width
48
54
self ._height = height
49
55
50
- def __setitem__ (self , index , value ) :
56
+ def __setitem__ (self , index : Tuple [ int , int ], value : int ) -> None :
51
57
if not isinstance (index , tuple ) or len (index ) != 2 :
52
58
raise IndexError ("Index must be tuple" )
53
59
if index [0 ] >= self .width or index [1 ] >= self .height :
@@ -57,7 +63,7 @@ def __setitem__(self, index, value):
57
63
58
64
self ._neopixel [offset ] = value
59
65
60
- def __getitem__ (self , index ) :
66
+ def __getitem__ (self , index : Tuple [ int , int ]) -> int :
61
67
if not isinstance (index , tuple ) or len (index ) != 2 :
62
68
raise IndexError ("Index must be tuple" )
63
69
if index [0 ] >= self .width or index [1 ] >= self .height :
@@ -67,7 +73,7 @@ def __getitem__(self, index):
67
73
68
74
return self ._neopixel [offset ]
69
75
70
- def _calculate_pixel_offset (self , index ) :
76
+ def _calculate_pixel_offset (self , index : Tuple [ int , int ]) -> Optional [ int ] :
71
77
if self ._rotation in (0 , 180 ):
72
78
offset = self .width * index [1 ] + index [0 ]
73
79
if self ._rotation == 180 :
@@ -82,7 +88,7 @@ def _calculate_pixel_offset(self, index):
82
88
83
89
return offset
84
90
85
- def show (self ):
91
+ def show (self ) -> None :
86
92
"""
87
93
Shows the new colors on the pixels themselves if they haven't already
88
94
been autowritten.
@@ -95,7 +101,7 @@ def show(self):
95
101
self ._neopixel .show ()
96
102
97
103
@property
98
- def auto_write (self ):
104
+ def auto_write (self ) -> bool :
99
105
"""
100
106
True if the neopixels should immediately change when set. If False,
101
107
``show`` must be called explicitly.
@@ -122,11 +128,11 @@ def auto_write(self):
122
128
return self ._neopixel .auto_write
123
129
124
130
@auto_write .setter
125
- def auto_write (self , val ) :
131
+ def auto_write (self , val : bool ) -> None :
126
132
self ._neopixel .auto_write = val
127
133
128
134
@property
129
- def brightness (self ):
135
+ def brightness (self ) -> float :
130
136
"""
131
137
The overall brightness of the pixel. Must be a number between 0 and
132
138
1, where the number represents a percentage between 0 and 100, i.e. ``0.3`` is 30%.
@@ -146,10 +152,10 @@ def brightness(self):
146
152
return self ._neopixel .brightness
147
153
148
154
@brightness .setter
149
- def brightness (self , brightness ) :
155
+ def brightness (self , brightness : float ) -> None :
150
156
self ._neopixel .brightness = brightness
151
157
152
- def fill (self , color ) :
158
+ def fill (self , color : Union [ Tuple [ int , int , int ], int ]) -> None :
153
159
"""
154
160
Colors all the pixels a given color.
155
161
@@ -168,7 +174,7 @@ def fill(self, color):
168
174
self ._neopixel .fill (color )
169
175
170
176
@property
171
- def width (self ):
177
+ def width (self ) -> int :
172
178
"""
173
179
The width of the grid. When ``rotation`` is 0, ``width`` is 8.
174
180
@@ -185,7 +191,7 @@ def width(self):
185
191
return self ._width
186
192
187
193
@property
188
- def height (self ):
194
+ def height (self ) -> int :
189
195
"""The height of the grid. When ``rotation`` is 0, ``height`` is 4.
190
196
191
197
.. code-block:: python
@@ -228,7 +234,7 @@ class TrellisM4Express:
228
234
current_press = pressed
229
235
"""
230
236
231
- def __init__ (self , rotation = 0 ) :
237
+ def __init__ (self , rotation : int = 0 ) -> None :
232
238
self ._rotation = rotation
233
239
234
240
# Define NeoPixels
@@ -296,12 +302,12 @@ def __init__(self, rotation=0):
296
302
297
303
cols = []
298
304
for x in range (8 ):
299
- col = digitalio .DigitalInOut (getattr (board , "COL{}" . format ( x ) ))
305
+ col = digitalio .DigitalInOut (getattr (board , f "COL{ x } " ))
300
306
cols .append (col )
301
307
302
308
rows = []
303
309
for y in range (4 ):
304
- row = digitalio .DigitalInOut (getattr (board , "ROW{}" . format ( y ) ))
310
+ row = digitalio .DigitalInOut (getattr (board , f "ROW{ y } " ))
305
311
rows .append (row )
306
312
307
313
key_names = []
@@ -322,7 +328,7 @@ def __init__(self, rotation=0):
322
328
self ._matrix = adafruit_matrixkeypad .Matrix_Keypad (cols , rows , key_names )
323
329
324
330
@property
325
- def pressed_keys (self ):
331
+ def pressed_keys (self ) -> List [ Tuple [ int , int ]] :
326
332
"""A list of tuples of currently pressed button coordinates.
327
333
328
334
.. code-block:: python
0 commit comments