Skip to content

Commit 9fc9545

Browse files
authored
Merge pull request #20 from tcfranks/main
Add Missing Type Annotations
2 parents f85266c + 39c80c7 commit 9fc9545

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

adafruit_trellis.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
from micropython import const
5050
from adafruit_bus_device import i2c_device
5151

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+
5259
# HT16K33 Command Contstants
5360
_HT16K33_OSCILATOR_ON = const(0x21)
5461
_HT16K33_BLINK_CMD = const(0x80)
@@ -97,10 +104,10 @@
97104
)
98105
# pylint: disable=missing-docstring, protected-access
99106
class TrellisLEDs:
100-
def __init__(self, trellis_obj):
107+
def __init__(self, trellis_obj: "Trellis") -> None:
101108
self._parent = trellis_obj
102109

103-
def __getitem__(self, x):
110+
def __getitem__(self, x: int) -> bool:
104111
if 0 < x >= self._parent._num_leds:
105112
raise ValueError(
106113
("LED number must be between 0 -", self._parent._num_leds - 1)
@@ -118,7 +125,7 @@ def __getitem__(self, x):
118125
> 0
119126
)
120127

121-
def __setitem__(self, x, value):
128+
def __setitem__(self, x: int, value: bool) -> None:
122129
if 0 < x >= self._parent._num_leds:
123130
raise ValueError(
124131
("LED number must be between 0 -", self._parent._num_leds - 1)
@@ -138,7 +145,7 @@ def __setitem__(self, x, value):
138145
self._parent.show()
139146

140147
# pylint: disable=invalid-name
141-
def fill(self, on):
148+
def fill(self, on: bool) -> None:
142149
fill = 0xFF if on else 0x00
143150
for buff in range(len(self._parent._i2c_devices)):
144151
for i in range(1, 17):
@@ -168,7 +175,7 @@ class Trellis:
168175
169176
"""
170177

171-
def __init__(self, i2c, addresses=None):
178+
def __init__(self, i2c: I2C, addresses: Optional[List[int]] = None) -> None:
172179
if addresses is None:
173180
addresses = [0x70]
174181
self._i2c_devices = []
@@ -197,43 +204,43 @@ def __init__(self, i2c, addresses=None):
197204
self.blink_rate = 0
198205
self.brightness = 15
199206

200-
def _write_cmd(self, byte):
207+
def _write_cmd(self, byte: int) -> None:
201208
self._temp[0] = byte
202209
for device in self._i2c_devices:
203210
with device:
204211
device.write(self._temp)
205212

206213
@property
207-
def blink_rate(self):
214+
def blink_rate(self) -> Literal[0, 1, 2, 3]:
208215
"""
209216
The current blink rate as an integer range 0-3.
210217
"""
211218
return self._blink_rate
212219

213220
@blink_rate.setter
214-
def blink_rate(self, rate):
221+
def blink_rate(self, rate: Literal[0, 1, 2, 3]) -> None:
215222
if not 0 <= rate <= 3:
216223
raise ValueError("Blink rate must be an integer in the range: 0-3")
217224
rate = rate & 0x03
218225
self._blink_rate = rate
219226
self._write_cmd(_HT16K33_BLINK_CMD | _HT16K33_BLINK_DISPLAYON | rate << 1)
220227

221228
@property
222-
def brightness(self):
229+
def brightness(self) -> int:
223230
"""
224231
The current brightness as an integer range 0-15.
225232
"""
226233
return self._brightness
227234

228235
@brightness.setter
229-
def brightness(self, brightness):
236+
def brightness(self, brightness: int) -> None:
230237
if not 0 <= brightness <= 15:
231238
raise ValueError("Brightness must be an integer in the range: 0-15")
232239
brightness = brightness & 0x0F
233240
self._brightness = brightness
234241
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
235242

236-
def show(self):
243+
def show(self) -> None:
237244
"""Refresh the LED buffer and show the changes."""
238245
pos = 0
239246
for device in self._i2c_devices:
@@ -243,20 +250,20 @@ def show(self):
243250
pos += 1
244251

245252
@property
246-
def auto_show(self):
253+
def auto_show(self) -> bool:
247254
"""
248255
Current state of sending LED updates directly the Trellis board(s). ``True``
249256
or ``False``.
250257
"""
251258
return self._auto_show
252259

253260
@auto_show.setter
254-
def auto_show(self, value):
261+
def auto_show(self, value: bool) -> None:
255262
if value not in (True, False):
256263
raise ValueError("Auto show value must be True or False")
257264
self._auto_show = value
258265

259-
def read_buttons(self):
266+
def read_buttons(self) -> Tuple[List[int], List[int]]:
260267
"""
261268
Read the button matrix register on the Trellis board(s). Returns two
262269
lists: 1 for new button presses, 1 for button relases.
@@ -279,18 +286,18 @@ def read_buttons(self):
279286

280287
return pressed, released
281288

282-
def _is_pressed(self, button):
289+
def _is_pressed(self, button: int) -> bool:
283290
mask = 1 << (buttonLUT[button % 16] & 0x0F)
284291
return self._buttons[button // 16][1][(buttonLUT[button % 16] >> 4)] & mask
285292

286-
def _was_pressed(self, button):
293+
def _was_pressed(self, button: int) -> bool:
287294
mask = 1 << (buttonLUT[button % 16] & 0x0F)
288295
return self._buttons[button // 16][0][(buttonLUT[button % 16] >> 4)] & mask
289296

290-
def _just_pressed(self, button):
297+
def _just_pressed(self, button: int) -> bool:
291298
# pylint: disable=invalid-unary-operand-type
292299
return self._is_pressed(button) & ~self._was_pressed(button)
293300

294-
def _just_released(self, button):
301+
def _just_released(self, button: int) -> bool:
295302
# pylint: disable=invalid-unary-operand-type
296303
return ~self._is_pressed(button) & self._was_pressed(button)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
typing-extensions~=4.0

0 commit comments

Comments
 (0)