Skip to content

Commit 5dfabcb

Browse files
committed
Add Missing Type Annotations
1 parent f85266c commit 5dfabcb

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

adafruit_trellis.py

Lines changed: 27 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) -> None:
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[int] = None) -> None:
172179
if addresses is None:
173180
addresses = [0x70]
174181
self._i2c_devices = []
@@ -197,43 +204,45 @@ 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) -> int:
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(
237+
self, brightness: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
238+
) -> None:
230239
if not 0 <= brightness <= 15:
231240
raise ValueError("Brightness must be an integer in the range: 0-15")
232241
brightness = brightness & 0x0F
233242
self._brightness = brightness
234243
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
235244

236-
def show(self):
245+
def show(self) -> None:
237246
"""Refresh the LED buffer and show the changes."""
238247
pos = 0
239248
for device in self._i2c_devices:
@@ -243,20 +252,20 @@ def show(self):
243252
pos += 1
244253

245254
@property
246-
def auto_show(self):
255+
def auto_show(self) -> bool:
247256
"""
248257
Current state of sending LED updates directly the Trellis board(s). ``True``
249258
or ``False``.
250259
"""
251260
return self._auto_show
252261

253262
@auto_show.setter
254-
def auto_show(self, value):
263+
def auto_show(self, value: bool) -> None:
255264
if value not in (True, False):
256265
raise ValueError("Auto show value must be True or False")
257266
self._auto_show = value
258267

259-
def read_buttons(self):
268+
def read_buttons(self) -> Tuple[List[int], List[int]]:
260269
"""
261270
Read the button matrix register on the Trellis board(s). Returns two
262271
lists: 1 for new button presses, 1 for button relases.
@@ -279,18 +288,18 @@ def read_buttons(self):
279288

280289
return pressed, released
281290

282-
def _is_pressed(self, button):
291+
def _is_pressed(self, button: int) -> bool:
283292
mask = 1 << (buttonLUT[button % 16] & 0x0F)
284293
return self._buttons[button // 16][1][(buttonLUT[button % 16] >> 4)] & mask
285294

286-
def _was_pressed(self, button):
295+
def _was_pressed(self, button: int) -> bool:
287296
mask = 1 << (buttonLUT[button % 16] & 0x0F)
288297
return self._buttons[button // 16][0][(buttonLUT[button % 16] >> 4)] & mask
289298

290-
def _just_pressed(self, button):
299+
def _just_pressed(self, button: int) -> bool:
291300
# pylint: disable=invalid-unary-operand-type
292301
return self._is_pressed(button) & ~self._was_pressed(button)
293302

294-
def _just_released(self, button):
303+
def _just_released(self, button: int) -> bool:
295304
# pylint: disable=invalid-unary-operand-type
296305
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)