Skip to content

Commit 97c5c58

Browse files
authored
Merge pull request #92 from tekktrik/feature/add-typing
Add typing
2 parents 972fb38 + 8d6ac56 commit 97c5c58

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

adafruit_ht16k33/bargraph.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
from adafruit_ht16k33.ht16k33 import HT16K33
1414

15+
try:
16+
from typing import Optional
17+
except ImportError:
18+
pass
19+
1520
__version__ = "0.0.0-auto.0"
1621
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
1722

@@ -24,14 +29,14 @@ class Bicolor24(HT16K33):
2429
LED_GREEN = 2
2530
LED_YELLOW = 3
2631

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

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

43-
def fill(self, color):
48+
def fill(self, color: bool):
4449
"""Fill the whole display with the given color."""
4550
what_it_was = self.auto_write
4651
self.auto_write = False

adafruit_ht16k33/ht16k33.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
from adafruit_bus_device import i2c_device
1515
from micropython import const
1616

17+
try:
18+
from typing import Optional
19+
from busio import I2C
20+
except ImportError:
21+
pass
22+
1723
__version__ = "0.0.0-auto.0"
1824
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
1925

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

36-
def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
42+
def __init__(
43+
self,
44+
i2c: I2C,
45+
address: int = 0x70,
46+
auto_write: bool = True,
47+
brightness: float = 1.0,
48+
):
3749
self.i2c_device = i2c_device.I2CDevice(i2c, address)
3850
self._temp = bytearray(1)
3951
self._buffer = bytearray(17)
@@ -45,7 +57,7 @@ def __init__(self, i2c, address=0x70, auto_write=True, brightness=1.0):
4557
self.blink_rate = 0
4658
self.brightness = brightness
4759

48-
def _write_cmd(self, byte):
60+
def _write_cmd(self, byte: bytearray):
4961
self._temp[0] = byte
5062
with self.i2c_device:
5163
self.i2c_device.write(self._temp)
@@ -56,7 +68,7 @@ def blink_rate(self):
5668
return self._blink_rate
5769

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

7183
@brightness.setter
72-
def brightness(self, brightness):
84+
def brightness(self, brightness: float):
7385
if not 0.0 <= brightness <= 1.0:
7486
raise ValueError(
7587
"Brightness must be a decimal number in the range: 0.0-1.0"
@@ -86,7 +98,7 @@ def auto_write(self):
8698
return self._auto_write
8799

88100
@auto_write.setter
89-
def auto_write(self, auto_write):
101+
def auto_write(self, auto_write: bool):
90102
if isinstance(auto_write, bool):
91103
self._auto_write = auto_write
92104
else:
@@ -99,15 +111,15 @@ def show(self):
99111
# bytes are the display register data to set.
100112
self.i2c_device.write(self._buffer)
101113

102-
def fill(self, color):
114+
def fill(self, color: bool):
103115
"""Fill the whole display with the given color."""
104116
fill = 0xFF if color else 0x00
105117
for i in range(16):
106118
self._buffer[i + 1] = fill
107119
if self._auto_write:
108120
self.show()
109121

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

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

128-
def _get_buffer(self, i):
140+
def _get_buffer(self, i: int) -> bool:
129141
return self._buffer[i + 1] # Offset by 1 to move past register address.

adafruit_ht16k33/matrix.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"""
1111
from adafruit_ht16k33.ht16k33 import HT16K33
1212

13+
try:
14+
from typing import Optional
15+
from PIL import Image
16+
except ImportError:
17+
pass
18+
1319
__version__ = "0.0.0-auto.0"
1420
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
1521

@@ -20,7 +26,7 @@ class Matrix8x8(HT16K33):
2026
_columns = 8
2127
_rows = 8
2228

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

32-
def __getitem__(self, key):
38+
def __getitem__(self, key: int) -> Optional[bool]:
3339
x, y = key
3440
return self.pixel(x, y)
3541

36-
def __setitem__(self, key, value):
42+
def __setitem__(self, key: int, value: bool):
3743
x, y = key
3844
self.pixel(x, y, value)
3945

4046
# pylint: disable=too-many-branches
41-
def shift(self, x, y, rotate=False):
47+
def shift(self, x: int, y: int, rotate: bool = False):
4248
"""
4349
Shift pixels by x and y
4450
@@ -80,31 +86,31 @@ def shift(self, x, y, rotate=False):
8086

8187
# pylint: enable=too-many-branches
8288

83-
def shift_right(self, rotate=False):
89+
def shift_right(self, rotate: bool = False):
8490
"""
8591
Shift all pixels right
8692
8793
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
8894
"""
8995
self.shift(1, 0, rotate)
9096

91-
def shift_left(self, rotate=False):
97+
def shift_left(self, rotate: bool = False):
9298
"""
9399
Shift all pixels left
94100
95101
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
96102
"""
97103
self.shift(-1, 0, rotate)
98104

99-
def shift_up(self, rotate=False):
105+
def shift_up(self, rotate: bool = False):
100106
"""
101107
Shift all pixels up
102108
103109
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
104110
"""
105111
self.shift(0, 1, rotate)
106112

107-
def shift_down(self, rotate=False):
113+
def shift_down(self, rotate: bool = False):
108114
"""
109115
Shift all pixels down
110116
@@ -205,7 +211,7 @@ def fill(self, color):
205211
if self._auto_write:
206212
self.show()
207213

208-
def image(self, img):
214+
def image(self, img: Image):
209215
"""Set buffer to value of Python Imaging Library image. The image should
210216
be a size equal to the display size."""
211217
imwidth, imheight = img.size

0 commit comments

Comments
 (0)