Skip to content

Commit 47fcb15

Browse files
authored
Merge pull request #29 from tcfranks/main
Add missing type annotations
2 parents 43da703 + 69f44f9 commit 47fcb15

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

adafruit_trellism4.py

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

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

3643

3744
class _NeoPixelArray:
3845
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
3946

40-
def __init__(self, pin, *, width, height, rotation=0):
47+
def __init__(
48+
self,
49+
pin: Pin,
50+
*,
51+
width: int,
52+
height: int,
53+
rotation: Literal[0, 90, 180, 270] = 0,
54+
) -> None:
4155
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=True)
4256
if rotation % 90 != 0:
4357
raise ValueError("Only 90 degree rotations supported")
@@ -47,7 +61,7 @@ def __init__(self, pin, *, width, height, rotation=0):
4761
self._width = width
4862
self._height = height
4963

50-
def __setitem__(self, index, value):
64+
def __setitem__(self, index: Tuple[int, int], value: int) -> None:
5165
if not isinstance(index, tuple) or len(index) != 2:
5266
raise IndexError("Index must be tuple")
5367
if index[0] >= self.width or index[1] >= self.height:
@@ -57,7 +71,7 @@ def __setitem__(self, index, value):
5771

5872
self._neopixel[offset] = value
5973

60-
def __getitem__(self, index):
74+
def __getitem__(self, index: Tuple[int, int]) -> int:
6175
if not isinstance(index, tuple) or len(index) != 2:
6276
raise IndexError("Index must be tuple")
6377
if index[0] >= self.width or index[1] >= self.height:
@@ -67,7 +81,7 @@ def __getitem__(self, index):
6781

6882
return self._neopixel[offset]
6983

70-
def _calculate_pixel_offset(self, index):
84+
def _calculate_pixel_offset(self, index: Tuple[int, int]) -> Optional[int]:
7185
if self._rotation in (0, 180):
7286
offset = self.width * index[1] + index[0]
7387
if self._rotation == 180:
@@ -82,7 +96,7 @@ def _calculate_pixel_offset(self, index):
8296

8397
return offset
8498

85-
def show(self):
99+
def show(self) -> None:
86100
"""
87101
Shows the new colors on the pixels themselves if they haven't already
88102
been autowritten.
@@ -95,7 +109,7 @@ def show(self):
95109
self._neopixel.show()
96110

97111
@property
98-
def auto_write(self):
112+
def auto_write(self) -> bool:
99113
"""
100114
True if the neopixels should immediately change when set. If False,
101115
``show`` must be called explicitly.
@@ -122,11 +136,11 @@ def auto_write(self):
122136
return self._neopixel.auto_write
123137

124138
@auto_write.setter
125-
def auto_write(self, val):
139+
def auto_write(self, val: bool) -> None:
126140
self._neopixel.auto_write = val
127141

128142
@property
129-
def brightness(self):
143+
def brightness(self) -> float:
130144
"""
131145
The overall brightness of the pixel. Must be a number between 0 and
132146
1, where the number represents a percentage between 0 and 100, i.e. ``0.3`` is 30%.
@@ -146,10 +160,10 @@ def brightness(self):
146160
return self._neopixel.brightness
147161

148162
@brightness.setter
149-
def brightness(self, brightness):
163+
def brightness(self, brightness: float) -> None:
150164
self._neopixel.brightness = brightness
151165

152-
def fill(self, color):
166+
def fill(self, color: Union[Tuple[int, int, int], int]) -> None:
153167
"""
154168
Colors all the pixels a given color.
155169
@@ -168,7 +182,7 @@ def fill(self, color):
168182
self._neopixel.fill(color)
169183

170184
@property
171-
def width(self):
185+
def width(self) -> int:
172186
"""
173187
The width of the grid. When ``rotation`` is 0, ``width`` is 8.
174188
@@ -185,7 +199,7 @@ def width(self):
185199
return self._width
186200

187201
@property
188-
def height(self):
202+
def height(self) -> int:
189203
"""The height of the grid. When ``rotation`` is 0, ``height`` is 4.
190204
191205
.. code-block:: python
@@ -228,7 +242,7 @@ class TrellisM4Express:
228242
current_press = pressed
229243
"""
230244

231-
def __init__(self, rotation=0):
245+
def __init__(self, rotation: Literal[0, 90, 180, 270] = 0) -> None:
232246
self._rotation = rotation
233247

234248
# Define NeoPixels
@@ -296,12 +310,12 @@ def __init__(self, rotation=0):
296310

297311
cols = []
298312
for x in range(8):
299-
col = digitalio.DigitalInOut(getattr(board, "COL{}".format(x)))
313+
col = digitalio.DigitalInOut(getattr(board, f"COL{x}"))
300314
cols.append(col)
301315

302316
rows = []
303317
for y in range(4):
304-
row = digitalio.DigitalInOut(getattr(board, "ROW{}".format(y)))
318+
row = digitalio.DigitalInOut(getattr(board, f"ROW{y}"))
305319
rows.append(row)
306320

307321
key_names = []
@@ -322,7 +336,7 @@ def __init__(self, rotation=0):
322336
self._matrix = adafruit_matrixkeypad.Matrix_Keypad(cols, rows, key_names)
323337

324338
@property
325-
def pressed_keys(self):
339+
def pressed_keys(self) -> List[Tuple[int, int]]:
326340
"""A list of tuples of currently pressed button coordinates.
327341
328342
.. code-block:: python

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-matrixkeypad
77
adafruit-circuitpython-neopixel
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)