Skip to content

Commit 7889f2a

Browse files
authored
Merge pull request #23 from rhooper/main
fixes #21 - add type hints
2 parents 4704c69 + 84a329b commit 7889f2a

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

adafruit_ws2801.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
import busio
1717
import digitalio
1818

19+
try:
20+
from typing import Any, Union, Tuple, List
21+
from microcontroller import Pin
22+
except ImportError:
23+
pass
24+
1925
__version__ = "0.0.0-auto.0"
2026
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_WS2801.git"
2127

22-
### based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
28+
# based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
2329

2430

2531
class WS2801:
@@ -49,7 +55,15 @@ class WS2801:
4955
time.sleep(2)
5056
"""
5157

52-
def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
58+
def __init__(
59+
self,
60+
clock: Pin,
61+
data: Pin,
62+
n: int,
63+
*,
64+
brightness: float = 1.0,
65+
auto_write: bool = True
66+
) -> None:
5367
self._spi = None
5468
try:
5569
self._spi = busio.SPI(clock, MOSI=data)
@@ -64,15 +78,15 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
6478
self.cpin.value = False
6579
self._n = n
6680
self._buf = bytearray(n * 3)
67-
self._brightness = 1.0 ### keeps pylint happy
81+
self._brightness = 1.0 # keeps pylint happy
6882
# Set auto_write to False temporarily so brightness setter does _not_
6983
# call show() while in __init__.
7084
self.auto_write = False
7185
self.brightness = brightness
7286
self.auto_write = auto_write
73-
### TODO - review/consider adding GRB support like that in c++ version
87+
# TODO - review/consider adding GRB support like that in c++ version
7488

75-
def deinit(self):
89+
def deinit(self) -> None:
7690
"""Blank out the DotStars and release the resources."""
7791
self.auto_write = False
7892
black = (0, 0, 0)
@@ -84,16 +98,18 @@ def deinit(self):
8498
self.dpin.deinit()
8599
self.cpin.deinit()
86100

87-
def __enter__(self):
101+
def __enter__(self) -> "WS2801":
88102
return self
89103

90-
def __exit__(self, exception_type, exception_value, traceback):
104+
def __exit__(
105+
self, exception_type: Any, exception_value: Any, traceback: Any
106+
) -> None:
91107
self.deinit()
92108

93109
def __repr__(self):
94110
return "[" + ", ".join([str(x) for x in self]) + "]"
95111

96-
def _set_item(self, index, value):
112+
def _set_item(self, index: int, value: Union[Tuple[int, ...], int]):
97113
offset = index * 3
98114
if isinstance(value, int):
99115
r = value >> 16
@@ -106,7 +122,7 @@ def _set_item(self, index, value):
106122
self._buf[offset + 1] = g
107123
self._buf[offset + 2] = b
108124

109-
def __setitem__(self, index, val):
125+
def __setitem__(self, index: int, val: Union[Tuple[int, ...], int]):
110126
if isinstance(index, slice):
111127
start, stop, step = index.indices(self._n)
112128
length = stop - start
@@ -122,7 +138,9 @@ def __setitem__(self, index, val):
122138
if self.auto_write:
123139
self.show()
124140

125-
def __getitem__(self, index):
141+
def __getitem__(
142+
self, index: Union[slice, int]
143+
) -> Union[Tuple[int, ...], List[Tuple[int, ...]]]:
126144
if isinstance(index, slice):
127145
out = []
128146
for in_i in range(*index.indices(self._n)):
@@ -135,21 +153,21 @@ def __getitem__(self, index):
135153
offset = index * 3
136154
return tuple(self._buf[offset + i] for i in range(3))
137155

138-
def __len__(self):
156+
def __len__(self) -> int:
139157
return self._n
140158

141159
@property
142-
def brightness(self):
160+
def brightness(self) -> float:
143161
"""Overall brightness of the pixel"""
144162
return self._brightness
145163

146164
@brightness.setter
147-
def brightness(self, brightness):
165+
def brightness(self, brightness: float) -> None:
148166
self._brightness = min(max(brightness, 0.0), 1.0)
149167
if self.auto_write:
150168
self.show()
151169

152-
def fill(self, color):
170+
def fill(self, color: Union[Tuple[int, ...], int]) -> None:
153171
"""Colors all pixels the given ***color***."""
154172
auto_write = self.auto_write
155173
self.auto_write = False
@@ -159,15 +177,15 @@ def fill(self, color):
159177
self.show()
160178
self.auto_write = auto_write
161179

162-
def _ds_writebytes(self, buf):
180+
def _ds_writebytes(self, buf: bytearray) -> None:
163181
for b in buf:
164182
for _ in range(8):
165183
self.dpin.value = b & 0x80
166184
self.cpin.value = True
167185
self.cpin.value = False
168186
b = b << 1
169187

170-
def show(self):
188+
def show(self) -> None:
171189
"""Shows the new colors on the pixels themselves if they haven't already
172190
been autowritten.
173191

0 commit comments

Comments
 (0)