Skip to content

Commit e84346b

Browse files
committed
fixes #21 - add type hints
1 parent 4704c69 commit e84346b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

adafruit_ws2801.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
import busio
1717
import digitalio
1818

19+
try:
20+
from typing import Any, Union, Tuple, List
21+
except ImportError:
22+
pass
23+
1924
__version__ = "0.0.0-auto.0"
2025
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_WS2801.git"
2126

22-
### based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
27+
# based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
2328

2429

2530
class WS2801:
@@ -49,7 +54,9 @@ class WS2801:
4954
time.sleep(2)
5055
"""
5156

52-
def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
57+
def __init__(
58+
self, clock, data, n: int, *, brightness: float = 1.0, auto_write: bool = True
59+
) -> None:
5360
self._spi = None
5461
try:
5562
self._spi = busio.SPI(clock, MOSI=data)
@@ -64,15 +71,15 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
6471
self.cpin.value = False
6572
self._n = n
6673
self._buf = bytearray(n * 3)
67-
self._brightness = 1.0 ### keeps pylint happy
74+
self._brightness = 1.0 # keeps pylint happy
6875
# Set auto_write to False temporarily so brightness setter does _not_
6976
# call show() while in __init__.
7077
self.auto_write = False
7178
self.brightness = brightness
7279
self.auto_write = auto_write
73-
### TODO - review/consider adding GRB support like that in c++ version
80+
# TODO - review/consider adding GRB support like that in c++ version
7481

75-
def deinit(self):
82+
def deinit(self) -> None:
7683
"""Blank out the DotStars and release the resources."""
7784
self.auto_write = False
7885
black = (0, 0, 0)
@@ -84,16 +91,18 @@ def deinit(self):
8491
self.dpin.deinit()
8592
self.cpin.deinit()
8693

87-
def __enter__(self):
94+
def __enter__(self) -> "WS2801":
8895
return self
8996

90-
def __exit__(self, exception_type, exception_value, traceback):
97+
def __exit__(
98+
self, exception_type: Any, exception_value: Any, traceback: Any
99+
) -> None:
91100
self.deinit()
92101

93102
def __repr__(self):
94103
return "[" + ", ".join([str(x) for x in self]) + "]"
95104

96-
def _set_item(self, index, value):
105+
def _set_item(self, index: int, value: Union[Tuple[int, ...], int]):
97106
offset = index * 3
98107
if isinstance(value, int):
99108
r = value >> 16
@@ -106,7 +115,7 @@ def _set_item(self, index, value):
106115
self._buf[offset + 1] = g
107116
self._buf[offset + 2] = b
108117

109-
def __setitem__(self, index, val):
118+
def __setitem__(self, index: int, val: Union[Tuple[int, ...], int]):
110119
if isinstance(index, slice):
111120
start, stop, step = index.indices(self._n)
112121
length = stop - start
@@ -122,7 +131,9 @@ def __setitem__(self, index, val):
122131
if self.auto_write:
123132
self.show()
124133

125-
def __getitem__(self, index):
134+
def __getitem__(
135+
self, index: Union[slice, int]
136+
) -> Union[Tuple[int, ...], List[Tuple[int, ...]]]:
126137
if isinstance(index, slice):
127138
out = []
128139
for in_i in range(*index.indices(self._n)):
@@ -135,21 +146,21 @@ def __getitem__(self, index):
135146
offset = index * 3
136147
return tuple(self._buf[offset + i] for i in range(3))
137148

138-
def __len__(self):
149+
def __len__(self) -> int:
139150
return self._n
140151

141152
@property
142-
def brightness(self):
153+
def brightness(self) -> float:
143154
"""Overall brightness of the pixel"""
144155
return self._brightness
145156

146157
@brightness.setter
147-
def brightness(self, brightness):
158+
def brightness(self, brightness: float) -> None:
148159
self._brightness = min(max(brightness, 0.0), 1.0)
149160
if self.auto_write:
150161
self.show()
151162

152-
def fill(self, color):
163+
def fill(self, color: Union[Tuple[int, ...], int]) -> None:
153164
"""Colors all pixels the given ***color***."""
154165
auto_write = self.auto_write
155166
self.auto_write = False
@@ -159,15 +170,15 @@ def fill(self, color):
159170
self.show()
160171
self.auto_write = auto_write
161172

162-
def _ds_writebytes(self, buf):
173+
def _ds_writebytes(self, buf: bytearray) -> None:
163174
for b in buf:
164175
for _ in range(8):
165176
self.dpin.value = b & 0x80
166177
self.cpin.value = True
167178
self.cpin.value = False
168179
b = b << 1
169180

170-
def show(self):
181+
def show(self) -> None:
171182
"""Shows the new colors on the pixels themselves if they haven't already
172183
been autowritten.
173184

0 commit comments

Comments
 (0)