Skip to content

Commit

Permalink
Merge pull request #92 from tekktrik/feature/add-typing
Browse files Browse the repository at this point in the history
Add typing
  • Loading branch information
FoamyGuy authored Oct 30, 2021
2 parents 972fb38 + 8d6ac56 commit 97c5c58
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 48 deletions.
11 changes: 8 additions & 3 deletions adafruit_ht16k33/bargraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

from adafruit_ht16k33.ht16k33 import HT16K33

try:
from typing import Optional
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

Expand All @@ -24,14 +29,14 @@ class Bicolor24(HT16K33):
LED_GREEN = 2
LED_YELLOW = 3

def __getitem__(self, key):
def __getitem__(self, key: int) -> Optional[bool]:
# map to HT16K33 row (x) and column (y), see schematic
x = key % 4 + 4 * (key // 12)
y = key // 4 - 3 * (key // 12)
# construct the color value and return it
return self._pixel(x, y) | self._pixel(x + 8, y) << 1

def __setitem__(self, key, value):
def __setitem__(self, key: int, value: bool):
# map to HT16K33 row (x) and column (y), see schematic
x = key % 4 + 4 * (key // 12)
y = key // 4 - 3 * (key // 12)
Expand All @@ -40,7 +45,7 @@ def __setitem__(self, key, value):
# conditionally turn on green LED
self._pixel(x + 8, y, value >> 1)

def fill(self, color):
def fill(self, color: bool):
"""Fill the whole display with the given color."""
what_it_was = self.auto_write
self.auto_write = False
Expand Down
30 changes: 21 additions & 9 deletions adafruit_ht16k33/ht16k33.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
from adafruit_bus_device import i2c_device
from micropython import const

try:
from typing import Optional
from busio import I2C
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

Expand All @@ -33,7 +39,13 @@ class HT16K33:
:param float brightness: 0.0 - 1.0 default brightness level.
"""

def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
def __init__(
self,
i2c: I2C,
address: int = 0x70,
auto_write: bool = True,
brightness: float = 1.0,
):
self.i2c_device = i2c_device.I2CDevice(i2c, address)
self._temp = bytearray(1)
self._buffer = bytearray(17)
Expand All @@ -45,7 +57,7 @@ def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
self.blink_rate = 0
self.brightness = brightness

def _write_cmd(self, byte):
def _write_cmd(self, byte: bytearray):
self._temp[0] = byte
with self.i2c_device:
self.i2c_device.write(self._temp)
Expand All @@ -56,7 +68,7 @@ def blink_rate(self):
return self._blink_rate

@blink_rate.setter
def blink_rate(self, rate=None):
def blink_rate(self, rate: int = None):
if not 0 <= rate <= 3:
raise ValueError("Blink rate must be an integer in the range: 0-3")
rate = rate & 0x03
Expand All @@ -69,7 +81,7 @@ def brightness(self):
return self._brightness

@brightness.setter
def brightness(self, brightness):
def brightness(self, brightness: float):
if not 0.0 <= brightness <= 1.0:
raise ValueError(
"Brightness must be a decimal number in the range: 0.0-1.0"
Expand All @@ -86,7 +98,7 @@ def auto_write(self):
return self._auto_write

@auto_write.setter
def auto_write(self, auto_write):
def auto_write(self, auto_write: bool):
if isinstance(auto_write, bool):
self._auto_write = auto_write
else:
Expand All @@ -99,15 +111,15 @@ def show(self):
# bytes are the display register data to set.
self.i2c_device.write(self._buffer)

def fill(self, color):
def fill(self, color: bool):
"""Fill the whole display with the given color."""
fill = 0xFF if color else 0x00
for i in range(16):
self._buffer[i + 1] = fill
if self._auto_write:
self.show()

def _pixel(self, x, y, color=None):
def _pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
addr = 2 * y + x // 8
mask = 1 << x % 8
if color is None:
Expand All @@ -122,8 +134,8 @@ def _pixel(self, x, y, color=None):
self.show()
return None

def _set_buffer(self, i, value):
def _set_buffer(self, i: int, value: bool):
self._buffer[i + 1] = value # Offset by 1 to move past register address.

def _get_buffer(self, i):
def _get_buffer(self, i: int) -> bool:
return self._buffer[i + 1] # Offset by 1 to move past register address.
24 changes: 15 additions & 9 deletions adafruit_ht16k33/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"""
from adafruit_ht16k33.ht16k33 import HT16K33

try:
from typing import Optional
from PIL import Image
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

Expand All @@ -20,7 +26,7 @@ class Matrix8x8(HT16K33):
_columns = 8
_rows = 8

def pixel(self, x, y, color=None):
def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
"""Get or set the color of a given pixel."""
if not 0 <= x <= 7:
return None
Expand All @@ -29,16 +35,16 @@ def pixel(self, x, y, color=None):
x = (x - 1) % 8
return super()._pixel(x, y, color)

def __getitem__(self, key):
def __getitem__(self, key: int) -> Optional[bool]:
x, y = key
return self.pixel(x, y)

def __setitem__(self, key, value):
def __setitem__(self, key: int, value: bool):
x, y = key
self.pixel(x, y, value)

# pylint: disable=too-many-branches
def shift(self, x, y, rotate=False):
def shift(self, x: int, y: int, rotate: bool = False):
"""
Shift pixels by x and y
Expand Down Expand Up @@ -80,31 +86,31 @@ def shift(self, x, y, rotate=False):

# pylint: enable=too-many-branches

def shift_right(self, rotate=False):
def shift_right(self, rotate: bool = False):
"""
Shift all pixels right
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
"""
self.shift(1, 0, rotate)

def shift_left(self, rotate=False):
def shift_left(self, rotate: bool = False):
"""
Shift all pixels left
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
"""
self.shift(-1, 0, rotate)

def shift_up(self, rotate=False):
def shift_up(self, rotate: bool = False):
"""
Shift all pixels up
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
"""
self.shift(0, 1, rotate)

def shift_down(self, rotate=False):
def shift_down(self, rotate: bool = False):
"""
Shift all pixels down
Expand Down Expand Up @@ -205,7 +211,7 @@ def fill(self, color):
if self._auto_write:
self.show()

def image(self, img):
def image(self, img: Image):
"""Set buffer to value of Python Imaging Library image. The image should
be a size equal to the display size."""
imwidth, imheight = img.size
Expand Down
Loading

0 comments on commit 97c5c58

Please sign in to comment.