Skip to content

Commit 162daa3

Browse files
authored
Merge pull request #105 from tekktrik/doc/fix-type-annotatations
Fix type annotatations, minor tweaks
2 parents 0f283d2 + 694cea5 commit 162daa3

File tree

6 files changed

+94
-86
lines changed

6 files changed

+94
-86
lines changed

adafruit_ht16k33/animations.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
from time import sleep
1818

19+
try:
20+
from typing import List
21+
from adafruit_ht16k33.segments import Seg14x4
22+
except ImportError:
23+
pass
24+
25+
1926
N = 16384
2027
M = 8192
2128
L = 4096
@@ -45,11 +52,17 @@ class Animation:
4552
4653
"""
4754

48-
def __init__(self, display):
55+
def __init__(self, display: Seg14x4) -> None:
4956

5057
self._display = display
5158

52-
def animate(self, digits, bitmasks, delay=0.2, auto_write=True):
59+
def animate(
60+
self,
61+
digits: List[int],
62+
bitmasks: List[int],
63+
delay: float = 0.2,
64+
auto_write: bool = True,
65+
) -> None:
5366
"""Animate function
5467
5568
@@ -84,12 +97,10 @@ def animate(self, digits, bitmasks, delay=0.2, auto_write=True):
8497
self._display.show()
8598
sleep(delay)
8699

87-
def chase_forward_and_reverse(self, delay=0.2, cycles=5):
100+
def chase_forward_and_reverse(self, delay: float = 0.2, cycles: int = 5):
88101
"""Chase Forward and Reverse Animation"""
89102

90-
cy = 0
91-
92-
while cy < cycles:
103+
for _ in range(cycles):
93104
self.animate([0, 1, 2, 3], [A, 0], delay)
94105
self.animate([3], [B, C, D, 0], delay)
95106
self.animate([2, 1, 0], [D, 0], delay)
@@ -103,15 +114,12 @@ def chase_forward_and_reverse(self, delay=0.2, cycles=5):
103114
self.animate([2, 1], [G2, G1, 0], delay)
104115
self.animate([0], [H, 0], delay)
105116

106-
cy += 1
107-
108-
def prelude_to_spinners(self, delay=0.2, cycles=5):
117+
def prelude_to_spinners(self, delay: float = 0.2, cycles: int = 5) -> None:
109118
"""Prelude to Spinners Animation"""
110119

111-
cy = 0
112120
auto_write = False
113121

114-
while cy < cycles:
122+
for _ in range(cycles):
115123
self.animate([1, 2], [A], 0, auto_write)
116124
self._display.show()
117125
sleep(delay)
@@ -164,15 +172,12 @@ def prelude_to_spinners(self, delay=0.2, cycles=5):
164172
self._display.show()
165173
sleep(delay)
166174

167-
cy += 1
168-
169-
def spinners(self, delay=0.2, cycles=5):
175+
def spinners(self, delay: float = 0.2, cycles: int = 5) -> None:
170176
"""Spinners Animation"""
171177

172-
cy = 0
173178
auto_write = False
174179

175-
while cy < cycles:
180+
for _ in range(cycles):
176181
self.animate([0], [H + M], 0, auto_write)
177182
self.animate([1], [J + K], 0, auto_write)
178183
self.animate([2], [H + M], 0, auto_write)
@@ -194,16 +199,14 @@ def spinners(self, delay=0.2, cycles=5):
194199
self._display.show()
195200
sleep(delay)
196201

197-
cy += 1
198-
199202
self._display.fill(0)
200203

201-
def enclosed_spinners(self, delay=0.2, cycles=5):
204+
def enclosed_spinners(self, delay: float = 0.2, cycles: int = 5) -> None:
202205
"""Enclosed Spinner Animation"""
203-
cy = 0
206+
204207
auto_write = False
205208

206-
while cy < cycles:
209+
for _ in range(cycles):
207210
self.animate([0], [A + D + E + F + H + M], 0, auto_write)
208211
self.animate([1], [A + D + J + K], 0, auto_write)
209212
self.animate([2], [A + D + H + M], 0, auto_write)
@@ -225,30 +228,26 @@ def enclosed_spinners(self, delay=0.2, cycles=5):
225228
self._display.show()
226229
sleep(delay)
227230

228-
cy += 1
229-
230231
self._display.fill(0)
231232

232-
def count_down(self):
233+
def count_down(self) -> None:
233234
"""Countdown Method"""
235+
234236
auto_write = False
235237
numbers = [
236238
[A + B + C + D + G1 + G2 + N],
237239
[A + B + D + E + G1 + G2 + N],
238240
[B + C + N],
239241
]
240-
index = 0
241242

242243
self._display.fill(0)
243244

244-
while index < len(numbers):
245-
self.animate([index], numbers[index], 0, auto_write)
245+
for index, number in enumerate(numbers):
246+
self.animate([index], number, 0, auto_write)
246247
self._display.show()
247248
sleep(1)
248249
self._display.fill(0)
249250
sleep(0.5)
250251

251-
index += 1
252-
253252
sleep(1)
254253
self._display.fill(0)

adafruit_ht16k33/bargraph.py

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

1313
from adafruit_ht16k33.ht16k33 import HT16K33
1414

15-
try:
16-
from typing import Optional
17-
except ImportError:
18-
pass
1915

2016
__version__ = "0.0.0+auto.0"
2117
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
@@ -29,14 +25,14 @@ class Bicolor24(HT16K33):
2925
LED_GREEN = 2
3026
LED_YELLOW = 3
3127

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

39-
def __setitem__(self, key: int, value: bool) -> None:
35+
def __setitem__(self, key: int, value: int) -> None:
4036
# map to HT16K33 row (x) and column (y), see schematic
4137
x = key % 4 + 4 * (key // 12)
4238
y = key // 4 - 3 * (key // 12)
@@ -45,10 +41,12 @@ def __setitem__(self, key: int, value: bool) -> None:
4541
# conditionally turn on green LED
4642
self._pixel(x + 8, y, value >> 1)
4743

48-
def fill(self, color: bool) -> None:
44+
def fill(self, color: int) -> None:
4945
"""Fill the whole display with the given color.
5046
51-
:param bool color: Whether to fill the display
47+
:param int color: Whether to fill the display, where 0 is no
48+
color, 1 is red, 2 is green, and 3 is yellow (red +
49+
green)
5250
"""
5351

5452
what_it_was = self.auto_write

adafruit_ht16k33/ht16k33.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
except ImportError:
2121
pass
2222

23+
2324
__version__ = "0.0.0+auto.0"
2425
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
2526

@@ -33,8 +34,8 @@ class HT16K33:
3334
"""
3435
The base class for all displays. Contains common methods.
3536
36-
:param I2C i2c: The I2C bus object
37-
:param int address: The I2C addess of the HT16K33.
37+
:param ~busio.I2C i2c: The I2C bus object
38+
:param int|list|tuple address: The I2C addess(es) of the HT16K33.
3839
:param bool auto_write: True if the display should immediately change when
3940
set. If False, `show` must be called explicitly.
4041
:param float brightness: 0.0 - 1.0 default brightness level.
@@ -43,7 +44,7 @@ class HT16K33:
4344
def __init__(
4445
self,
4546
i2c: I2C,
46-
address: Union[int, Tuple, List] = 0x70,
47+
address: Union[int, List[int], Tuple[int, ...]] = 0x70,
4748
auto_write: bool = True,
4849
brightness: float = 1.0,
4950
) -> None:
@@ -65,7 +66,7 @@ def __init__(
6566
self.blink_rate = 0
6667
self.brightness = brightness
6768

68-
def _write_cmd(self, byte: bytearray, i2c_index: int = 0) -> None:
69+
def _write_cmd(self, byte: int, i2c_index: int = 0) -> None:
6970
self._temp[0] = byte
7071
with self.i2c_device[i2c_index]:
7172
self.i2c_device[i2c_index].write(self._temp)
@@ -76,7 +77,7 @@ def blink_rate(self) -> int:
7677
return self._blink_rate
7778

7879
@blink_rate.setter
79-
def blink_rate(self, rate: Optional[int] = None) -> None:
80+
def blink_rate(self, rate: int) -> None:
8081
if not 0 <= rate <= 3:
8182
raise ValueError("Blink rate must be an integer in the range: 0-3")
8283
rate = rate & 0x03
@@ -156,8 +157,8 @@ def _pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]
156157
self.show()
157158
return None
158159

159-
def _set_buffer(self, i: int, value: bool) -> None:
160+
def _set_buffer(self, i: int, value: int) -> None:
160161
self._buffer[i + 1] = value # Offset by 1 to move past register address.
161162

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

adafruit_ht16k33/matrix.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
from adafruit_ht16k33.ht16k33 import HT16K33
1212

1313
try:
14-
from typing import Union, List, Tuple, Optional
14+
from typing import Optional, Tuple, Union, List
15+
from circuitpython_typing.pil import Image
1516
from busio import I2C
16-
from PIL import Image
1717
except ImportError:
1818
pass
1919

20+
2021
__version__ = "0.0.0+auto.0"
2122
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
2223

@@ -43,11 +44,11 @@ def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
4344
x = (x - 1) % 8
4445
return super()._pixel(x, y, color)
4546

46-
def __getitem__(self, key: int) -> Optional[bool]:
47+
def __getitem__(self, key: Tuple[int, int]) -> Optional[bool]:
4748
x, y = key
4849
return self.pixel(x, y)
4950

50-
def __setitem__(self, key: int, value: bool) -> None:
51+
def __setitem__(self, key: Tuple[int, int], value: Optional[bool]) -> None:
5152
x, y = key
5253
self.pixel(x, y, value)
5354

@@ -173,7 +174,7 @@ class Matrix16x8(Matrix8x8):
173174
def __init__(
174175
self,
175176
i2c: I2C,
176-
address: Union[int, Tuple, List] = 0x70,
177+
address: Union[int, List[int], Tuple[int, ...]] = 0x70,
177178
auto_write: bool = True,
178179
brightness: float = 1.0,
179180
) -> None:
@@ -229,14 +230,14 @@ class Matrix8x8x2(Matrix8x8):
229230
LED_GREEN = 2
230231
LED_YELLOW = 3
231232

232-
def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
233+
def pixel(self, x: int, y: int, color: Optional[int] = None) -> Optional[int]:
233234
"""Get or set the color of a given pixel.
234235
235236
:param int x: The x coordinate of the pixel
236237
:param int y: The y coordinate of the pixel
237-
:param bool color: (Optional) The state to set the pixel
238+
:param int color: (Optional) The color to set the pixel
238239
:return: If ``color`` was not set, this returns the state of the pixel
239-
:rtype: bool
240+
:rtype: int
240241
"""
241242
if not 0 <= x <= 7:
242243
return None
@@ -249,10 +250,10 @@ def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
249250
return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
250251
return None
251252

252-
def fill(self, color: bool) -> None:
253+
def fill(self, color: int) -> None:
253254
"""Fill the whole display with the given color.
254255
255-
:param bool color: Whether to fill the display
256+
:param int color: The color to fill the display
256257
"""
257258

258259
fill1 = 0xFF if color & 0x01 else 0x00

0 commit comments

Comments
 (0)