49
49
from micropython import const
50
50
from adafruit_bus_device import i2c_device
51
51
52
+ try :
53
+ from typing import List , Optional , Tuple
54
+ from typing_extensions import Literal
55
+ from busio import I2C
56
+ except ImportError :
57
+ pass
58
+
52
59
# HT16K33 Command Contstants
53
60
_HT16K33_OSCILATOR_ON = const (0x21 )
54
61
_HT16K33_BLINK_CMD = const (0x80 )
97
104
)
98
105
# pylint: disable=missing-docstring, protected-access
99
106
class TrellisLEDs :
100
- def __init__ (self , trellis_obj ) :
107
+ def __init__ (self , trellis_obj : "Trellis" ) -> None :
101
108
self ._parent = trellis_obj
102
109
103
- def __getitem__ (self , x ) :
110
+ def __getitem__ (self , x : int ) -> None :
104
111
if 0 < x >= self ._parent ._num_leds :
105
112
raise ValueError (
106
113
("LED number must be between 0 -" , self ._parent ._num_leds - 1 )
@@ -118,7 +125,7 @@ def __getitem__(self, x):
118
125
> 0
119
126
)
120
127
121
- def __setitem__ (self , x , value ) :
128
+ def __setitem__ (self , x : int , value : bool ) -> None :
122
129
if 0 < x >= self ._parent ._num_leds :
123
130
raise ValueError (
124
131
("LED number must be between 0 -" , self ._parent ._num_leds - 1 )
@@ -138,7 +145,7 @@ def __setitem__(self, x, value):
138
145
self ._parent .show ()
139
146
140
147
# pylint: disable=invalid-name
141
- def fill (self , on ) :
148
+ def fill (self , on : bool ) -> None :
142
149
fill = 0xFF if on else 0x00
143
150
for buff in range (len (self ._parent ._i2c_devices )):
144
151
for i in range (1 , 17 ):
@@ -168,7 +175,7 @@ class Trellis:
168
175
169
176
"""
170
177
171
- def __init__ (self , i2c , addresses = None ):
178
+ def __init__ (self , i2c : I2C , addresses : Optional [ int ] = None ) -> None :
172
179
if addresses is None :
173
180
addresses = [0x70 ]
174
181
self ._i2c_devices = []
@@ -197,43 +204,45 @@ def __init__(self, i2c, addresses=None):
197
204
self .blink_rate = 0
198
205
self .brightness = 15
199
206
200
- def _write_cmd (self , byte ) :
207
+ def _write_cmd (self , byte : int ) -> None :
201
208
self ._temp [0 ] = byte
202
209
for device in self ._i2c_devices :
203
210
with device :
204
211
device .write (self ._temp )
205
212
206
213
@property
207
- def blink_rate (self ):
214
+ def blink_rate (self ) -> int :
208
215
"""
209
216
The current blink rate as an integer range 0-3.
210
217
"""
211
218
return self ._blink_rate
212
219
213
220
@blink_rate .setter
214
- def blink_rate (self , rate ) :
221
+ def blink_rate (self , rate : Literal [ 0 , 1 , 2 , 3 ]) -> None :
215
222
if not 0 <= rate <= 3 :
216
223
raise ValueError ("Blink rate must be an integer in the range: 0-3" )
217
224
rate = rate & 0x03
218
225
self ._blink_rate = rate
219
226
self ._write_cmd (_HT16K33_BLINK_CMD | _HT16K33_BLINK_DISPLAYON | rate << 1 )
220
227
221
228
@property
222
- def brightness (self ):
229
+ def brightness (self ) -> int :
223
230
"""
224
231
The current brightness as an integer range 0-15.
225
232
"""
226
233
return self ._brightness
227
234
228
235
@brightness .setter
229
- def brightness (self , brightness ):
236
+ def brightness (
237
+ self , brightness : Literal [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ]
238
+ ) -> None :
230
239
if not 0 <= brightness <= 15 :
231
240
raise ValueError ("Brightness must be an integer in the range: 0-15" )
232
241
brightness = brightness & 0x0F
233
242
self ._brightness = brightness
234
243
self ._write_cmd (_HT16K33_CMD_BRIGHTNESS | brightness )
235
244
236
- def show (self ):
245
+ def show (self ) -> None :
237
246
"""Refresh the LED buffer and show the changes."""
238
247
pos = 0
239
248
for device in self ._i2c_devices :
@@ -243,20 +252,20 @@ def show(self):
243
252
pos += 1
244
253
245
254
@property
246
- def auto_show (self ):
255
+ def auto_show (self ) -> bool :
247
256
"""
248
257
Current state of sending LED updates directly the Trellis board(s). ``True``
249
258
or ``False``.
250
259
"""
251
260
return self ._auto_show
252
261
253
262
@auto_show .setter
254
- def auto_show (self , value ) :
263
+ def auto_show (self , value : bool ) -> None :
255
264
if value not in (True , False ):
256
265
raise ValueError ("Auto show value must be True or False" )
257
266
self ._auto_show = value
258
267
259
- def read_buttons (self ):
268
+ def read_buttons (self ) -> Tuple [ List [ int ], List [ int ]] :
260
269
"""
261
270
Read the button matrix register on the Trellis board(s). Returns two
262
271
lists: 1 for new button presses, 1 for button relases.
@@ -279,18 +288,18 @@ def read_buttons(self):
279
288
280
289
return pressed , released
281
290
282
- def _is_pressed (self , button ) :
291
+ def _is_pressed (self , button : int ) -> bool :
283
292
mask = 1 << (buttonLUT [button % 16 ] & 0x0F )
284
293
return self ._buttons [button // 16 ][1 ][(buttonLUT [button % 16 ] >> 4 )] & mask
285
294
286
- def _was_pressed (self , button ) :
295
+ def _was_pressed (self , button : int ) -> bool :
287
296
mask = 1 << (buttonLUT [button % 16 ] & 0x0F )
288
297
return self ._buttons [button // 16 ][0 ][(buttonLUT [button % 16 ] >> 4 )] & mask
289
298
290
- def _just_pressed (self , button ) :
299
+ def _just_pressed (self , button : int ) -> bool :
291
300
# pylint: disable=invalid-unary-operand-type
292
301
return self ._is_pressed (button ) & ~ self ._was_pressed (button )
293
302
294
- def _just_released (self , button ) :
303
+ def _just_released (self , button : int ) -> bool :
295
304
# pylint: disable=invalid-unary-operand-type
296
305
return ~ self ._is_pressed (button ) & self ._was_pressed (button )
0 commit comments