Skip to content

Commit e5de263

Browse files
committed
Add missing type annotations
1 parent 43da703 commit e5de263

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

adafruit_trellism4.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@
3030
import neopixel
3131
import adafruit_matrixkeypad
3232

33+
try:
34+
from typing import List, Optional, Tuple, Union
35+
from microcontroller import Pin
36+
except ImportError:
37+
pass
38+
3339
__version__ = "0.0.0+auto.0"
3440
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4.git"
3541

3642

3743
class _NeoPixelArray:
3844
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
3945

40-
def __init__(self, pin, *, width, height, rotation=0):
46+
def __init__(self, pin: Pin, *, width: int, height: int, rotation: int = 0) -> None:
4147
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=True)
4248
if rotation % 90 != 0:
4349
raise ValueError("Only 90 degree rotations supported")
@@ -47,7 +53,7 @@ def __init__(self, pin, *, width, height, rotation=0):
4753
self._width = width
4854
self._height = height
4955

50-
def __setitem__(self, index, value):
56+
def __setitem__(self, index: Tuple[int, int], value: int) -> None:
5157
if not isinstance(index, tuple) or len(index) != 2:
5258
raise IndexError("Index must be tuple")
5359
if index[0] >= self.width or index[1] >= self.height:
@@ -57,7 +63,7 @@ def __setitem__(self, index, value):
5763

5864
self._neopixel[offset] = value
5965

60-
def __getitem__(self, index):
66+
def __getitem__(self, index: Tuple[int, int]) -> int:
6167
if not isinstance(index, tuple) or len(index) != 2:
6268
raise IndexError("Index must be tuple")
6369
if index[0] >= self.width or index[1] >= self.height:
@@ -67,7 +73,7 @@ def __getitem__(self, index):
6773

6874
return self._neopixel[offset]
6975

70-
def _calculate_pixel_offset(self, index):
76+
def _calculate_pixel_offset(self, index: Tuple[int, int]) -> Optional[int]:
7177
if self._rotation in (0, 180):
7278
offset = self.width * index[1] + index[0]
7379
if self._rotation == 180:
@@ -82,7 +88,7 @@ def _calculate_pixel_offset(self, index):
8288

8389
return offset
8490

85-
def show(self):
91+
def show(self) -> None:
8692
"""
8793
Shows the new colors on the pixels themselves if they haven't already
8894
been autowritten.
@@ -95,7 +101,7 @@ def show(self):
95101
self._neopixel.show()
96102

97103
@property
98-
def auto_write(self):
104+
def auto_write(self) -> bool:
99105
"""
100106
True if the neopixels should immediately change when set. If False,
101107
``show`` must be called explicitly.
@@ -122,11 +128,11 @@ def auto_write(self):
122128
return self._neopixel.auto_write
123129

124130
@auto_write.setter
125-
def auto_write(self, val):
131+
def auto_write(self, val: bool) -> None:
126132
self._neopixel.auto_write = val
127133

128134
@property
129-
def brightness(self):
135+
def brightness(self) -> float:
130136
"""
131137
The overall brightness of the pixel. Must be a number between 0 and
132138
1, where the number represents a percentage between 0 and 100, i.e. ``0.3`` is 30%.
@@ -146,10 +152,10 @@ def brightness(self):
146152
return self._neopixel.brightness
147153

148154
@brightness.setter
149-
def brightness(self, brightness):
155+
def brightness(self, brightness: float) -> None:
150156
self._neopixel.brightness = brightness
151157

152-
def fill(self, color):
158+
def fill(self, color: Union[Tuple[int, int, int], int]) -> None:
153159
"""
154160
Colors all the pixels a given color.
155161
@@ -168,7 +174,7 @@ def fill(self, color):
168174
self._neopixel.fill(color)
169175

170176
@property
171-
def width(self):
177+
def width(self) -> int:
172178
"""
173179
The width of the grid. When ``rotation`` is 0, ``width`` is 8.
174180
@@ -185,7 +191,7 @@ def width(self):
185191
return self._width
186192

187193
@property
188-
def height(self):
194+
def height(self) -> int:
189195
"""The height of the grid. When ``rotation`` is 0, ``height`` is 4.
190196
191197
.. code-block:: python
@@ -228,7 +234,7 @@ class TrellisM4Express:
228234
current_press = pressed
229235
"""
230236

231-
def __init__(self, rotation=0):
237+
def __init__(self, rotation: int = 0) -> None:
232238
self._rotation = rotation
233239

234240
# Define NeoPixels
@@ -296,12 +302,12 @@ def __init__(self, rotation=0):
296302

297303
cols = []
298304
for x in range(8):
299-
col = digitalio.DigitalInOut(getattr(board, "COL{}".format(x)))
305+
col = digitalio.DigitalInOut(getattr(board, f"COL{x}"))
300306
cols.append(col)
301307

302308
rows = []
303309
for y in range(4):
304-
row = digitalio.DigitalInOut(getattr(board, "ROW{}".format(y)))
310+
row = digitalio.DigitalInOut(getattr(board, f"ROW{y}"))
305311
rows.append(row)
306312

307313
key_names = []
@@ -322,7 +328,7 @@ def __init__(self, rotation=0):
322328
self._matrix = adafruit_matrixkeypad.Matrix_Keypad(cols, rows, key_names)
323329

324330
@property
325-
def pressed_keys(self):
331+
def pressed_keys(self) -> List[Tuple[int, int]]:
326332
"""A list of tuples of currently pressed button coordinates.
327333
328334
.. code-block:: python

0 commit comments

Comments
 (0)