From 5dfabcbe7136e7fa468d67ec368c33f73e1a9801 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Wed, 7 Sep 2022 11:35:05 -0400 Subject: [PATCH 1/2] Add Missing Type Annotations --- adafruit_trellis.py | 45 +++++++++++++++++++++++++++------------------ requirements.txt | 1 + 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/adafruit_trellis.py b/adafruit_trellis.py index 9e47212..ef84625 100644 --- a/adafruit_trellis.py +++ b/adafruit_trellis.py @@ -49,6 +49,13 @@ from micropython import const from adafruit_bus_device import i2c_device +try: + from typing import List, Optional, Tuple + from typing_extensions import Literal + from busio import I2C +except ImportError: + pass + # HT16K33 Command Contstants _HT16K33_OSCILATOR_ON = const(0x21) _HT16K33_BLINK_CMD = const(0x80) @@ -97,10 +104,10 @@ ) # pylint: disable=missing-docstring, protected-access class TrellisLEDs: - def __init__(self, trellis_obj): + def __init__(self, trellis_obj: "Trellis") -> None: self._parent = trellis_obj - def __getitem__(self, x): + def __getitem__(self, x: int) -> None: if 0 < x >= self._parent._num_leds: raise ValueError( ("LED number must be between 0 -", self._parent._num_leds - 1) @@ -118,7 +125,7 @@ def __getitem__(self, x): > 0 ) - def __setitem__(self, x, value): + def __setitem__(self, x: int, value: bool) -> None: if 0 < x >= self._parent._num_leds: raise ValueError( ("LED number must be between 0 -", self._parent._num_leds - 1) @@ -138,7 +145,7 @@ def __setitem__(self, x, value): self._parent.show() # pylint: disable=invalid-name - def fill(self, on): + def fill(self, on: bool) -> None: fill = 0xFF if on else 0x00 for buff in range(len(self._parent._i2c_devices)): for i in range(1, 17): @@ -168,7 +175,7 @@ class Trellis: """ - def __init__(self, i2c, addresses=None): + def __init__(self, i2c: I2C, addresses: Optional[int] = None) -> None: if addresses is None: addresses = [0x70] self._i2c_devices = [] @@ -197,21 +204,21 @@ def __init__(self, i2c, addresses=None): self.blink_rate = 0 self.brightness = 15 - def _write_cmd(self, byte): + def _write_cmd(self, byte: int) -> None: self._temp[0] = byte for device in self._i2c_devices: with device: device.write(self._temp) @property - def blink_rate(self): + def blink_rate(self) -> int: """ The current blink rate as an integer range 0-3. """ return self._blink_rate @blink_rate.setter - def blink_rate(self, rate): + def blink_rate(self, rate: Literal[0, 1, 2, 3]) -> None: if not 0 <= rate <= 3: raise ValueError("Blink rate must be an integer in the range: 0-3") rate = rate & 0x03 @@ -219,21 +226,23 @@ def blink_rate(self, rate): self._write_cmd(_HT16K33_BLINK_CMD | _HT16K33_BLINK_DISPLAYON | rate << 1) @property - def brightness(self): + def brightness(self) -> int: """ The current brightness as an integer range 0-15. """ return self._brightness @brightness.setter - def brightness(self, brightness): + def brightness( + self, brightness: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + ) -> None: if not 0 <= brightness <= 15: raise ValueError("Brightness must be an integer in the range: 0-15") brightness = brightness & 0x0F self._brightness = brightness self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness) - def show(self): + def show(self) -> None: """Refresh the LED buffer and show the changes.""" pos = 0 for device in self._i2c_devices: @@ -243,7 +252,7 @@ def show(self): pos += 1 @property - def auto_show(self): + def auto_show(self) -> bool: """ Current state of sending LED updates directly the Trellis board(s). ``True`` or ``False``. @@ -251,12 +260,12 @@ def auto_show(self): return self._auto_show @auto_show.setter - def auto_show(self, value): + def auto_show(self, value: bool) -> None: if value not in (True, False): raise ValueError("Auto show value must be True or False") self._auto_show = value - def read_buttons(self): + def read_buttons(self) -> Tuple[List[int], List[int]]: """ Read the button matrix register on the Trellis board(s). Returns two lists: 1 for new button presses, 1 for button relases. @@ -279,18 +288,18 @@ def read_buttons(self): return pressed, released - def _is_pressed(self, button): + def _is_pressed(self, button: int) -> bool: mask = 1 << (buttonLUT[button % 16] & 0x0F) return self._buttons[button // 16][1][(buttonLUT[button % 16] >> 4)] & mask - def _was_pressed(self, button): + def _was_pressed(self, button: int) -> bool: mask = 1 << (buttonLUT[button % 16] & 0x0F) return self._buttons[button // 16][0][(buttonLUT[button % 16] >> 4)] & mask - def _just_pressed(self, button): + def _just_pressed(self, button: int) -> bool: # pylint: disable=invalid-unary-operand-type return self._is_pressed(button) & ~self._was_pressed(button) - def _just_released(self, button): + def _just_released(self, button: int) -> bool: # pylint: disable=invalid-unary-operand-type return ~self._is_pressed(button) & self._was_pressed(button) diff --git a/requirements.txt b/requirements.txt index a45c547..29c73c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ Adafruit-Blinka adafruit-circuitpython-busdevice +typing-extensions~=4.0 From 39c80c7d22f02944bf08ec8e9a47e3f0936c1567 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Wed, 7 Sep 2022 23:01:24 -0400 Subject: [PATCH 2/2] Add Missing Type Annotations --- adafruit_trellis.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/adafruit_trellis.py b/adafruit_trellis.py index ef84625..30620f9 100644 --- a/adafruit_trellis.py +++ b/adafruit_trellis.py @@ -107,7 +107,7 @@ class TrellisLEDs: def __init__(self, trellis_obj: "Trellis") -> None: self._parent = trellis_obj - def __getitem__(self, x: int) -> None: + def __getitem__(self, x: int) -> bool: if 0 < x >= self._parent._num_leds: raise ValueError( ("LED number must be between 0 -", self._parent._num_leds - 1) @@ -175,7 +175,7 @@ class Trellis: """ - def __init__(self, i2c: I2C, addresses: Optional[int] = None) -> None: + def __init__(self, i2c: I2C, addresses: Optional[List[int]] = None) -> None: if addresses is None: addresses = [0x70] self._i2c_devices = [] @@ -211,7 +211,7 @@ def _write_cmd(self, byte: int) -> None: device.write(self._temp) @property - def blink_rate(self) -> int: + def blink_rate(self) -> Literal[0, 1, 2, 3]: """ The current blink rate as an integer range 0-3. """ @@ -233,9 +233,7 @@ def brightness(self) -> int: return self._brightness @brightness.setter - def brightness( - self, brightness: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] - ) -> None: + def brightness(self, brightness: int) -> None: if not 0 <= brightness <= 15: raise ValueError("Brightness must be an integer in the range: 0-15") brightness = brightness & 0x0F