Skip to content

Add missing type annotations #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions adafruit_trellism4.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@
import neopixel
import adafruit_matrixkeypad

try:
from typing import List, Optional, Tuple, Union
from typing_extensions import Literal
from microcontroller import Pin
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4.git"


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

def __init__(self, pin, *, width, height, rotation=0):
def __init__(
self,
pin: Pin,
*,
width: int,
height: int,
rotation: Literal[0, 90, 180, 270] = 0,
) -> None:
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=True)
if rotation % 90 != 0:
raise ValueError("Only 90 degree rotations supported")
Expand All @@ -47,7 +61,7 @@ def __init__(self, pin, *, width, height, rotation=0):
self._width = width
self._height = height

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

self._neopixel[offset] = value

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

return self._neopixel[offset]

def _calculate_pixel_offset(self, index):
def _calculate_pixel_offset(self, index: Tuple[int, int]) -> Optional[int]:
if self._rotation in (0, 180):
offset = self.width * index[1] + index[0]
if self._rotation == 180:
Expand All @@ -82,7 +96,7 @@ def _calculate_pixel_offset(self, index):

return offset

def show(self):
def show(self) -> None:
"""
Shows the new colors on the pixels themselves if they haven't already
been autowritten.
Expand All @@ -95,7 +109,7 @@ def show(self):
self._neopixel.show()

@property
def auto_write(self):
def auto_write(self) -> bool:
"""
True if the neopixels should immediately change when set. If False,
``show`` must be called explicitly.
Expand All @@ -122,11 +136,11 @@ def auto_write(self):
return self._neopixel.auto_write

@auto_write.setter
def auto_write(self, val):
def auto_write(self, val: bool) -> None:
self._neopixel.auto_write = val

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

@brightness.setter
def brightness(self, brightness):
def brightness(self, brightness: float) -> None:
self._neopixel.brightness = brightness

def fill(self, color):
def fill(self, color: Union[Tuple[int, int, int], int]) -> None:
"""
Colors all the pixels a given color.

Expand All @@ -168,7 +182,7 @@ def fill(self, color):
self._neopixel.fill(color)

@property
def width(self):
def width(self) -> int:
"""
The width of the grid. When ``rotation`` is 0, ``width`` is 8.

Expand All @@ -185,7 +199,7 @@ def width(self):
return self._width

@property
def height(self):
def height(self) -> int:
"""The height of the grid. When ``rotation`` is 0, ``height`` is 4.

.. code-block:: python
Expand Down Expand Up @@ -228,7 +242,7 @@ class TrellisM4Express:
current_press = pressed
"""

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

# Define NeoPixels
Expand Down Expand Up @@ -296,12 +310,12 @@ def __init__(self, rotation=0):

cols = []
for x in range(8):
col = digitalio.DigitalInOut(getattr(board, "COL{}".format(x)))
col = digitalio.DigitalInOut(getattr(board, f"COL{x}"))
cols.append(col)

rows = []
for y in range(4):
row = digitalio.DigitalInOut(getattr(board, "ROW{}".format(y)))
row = digitalio.DigitalInOut(getattr(board, f"ROW{y}"))
rows.append(row)

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

@property
def pressed_keys(self):
def pressed_keys(self) -> List[Tuple[int, int]]:
"""A list of tuples of currently pressed button coordinates.

.. code-block:: python
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
Adafruit-Blinka
adafruit-circuitpython-matrixkeypad
adafruit-circuitpython-neopixel
typing-extensions~=4.0