Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #21 - add type hints #23

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions adafruit_ws2801.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
import busio
import digitalio

try:
from typing import Any, Union, Tuple, List
from microcontroller import Pin
except ImportError:
pass

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

### based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar
# based on https://github.com/adafruit/Adafruit_CircuitPython_DotStar


class WS2801:
Expand Down Expand Up @@ -49,7 +55,15 @@ class WS2801:
time.sleep(2)
"""

def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
def __init__(
self,
clock: Pin,
data: Pin,
n: int,
*,
brightness: float = 1.0,
auto_write: bool = True
) -> None:
self._spi = None
try:
self._spi = busio.SPI(clock, MOSI=data)
Expand All @@ -64,15 +78,15 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
self.cpin.value = False
self._n = n
self._buf = bytearray(n * 3)
self._brightness = 1.0 ### keeps pylint happy
self._brightness = 1.0 # keeps pylint happy
# Set auto_write to False temporarily so brightness setter does _not_
# call show() while in __init__.
self.auto_write = False
self.brightness = brightness
self.auto_write = auto_write
### TODO - review/consider adding GRB support like that in c++ version
# TODO - review/consider adding GRB support like that in c++ version

def deinit(self):
def deinit(self) -> None:
"""Blank out the DotStars and release the resources."""
self.auto_write = False
black = (0, 0, 0)
Expand All @@ -84,16 +98,18 @@ def deinit(self):
self.dpin.deinit()
self.cpin.deinit()

def __enter__(self):
def __enter__(self) -> "WS2801":
return self

def __exit__(self, exception_type, exception_value, traceback):
def __exit__(
self, exception_type: Any, exception_value: Any, traceback: Any
) -> None:
self.deinit()

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

def _set_item(self, index, value):
def _set_item(self, index: int, value: Union[Tuple[int, ...], int]):
offset = index * 3
if isinstance(value, int):
r = value >> 16
Expand All @@ -106,7 +122,7 @@ def _set_item(self, index, value):
self._buf[offset + 1] = g
self._buf[offset + 2] = b

def __setitem__(self, index, val):
def __setitem__(self, index: int, val: Union[Tuple[int, ...], int]):
if isinstance(index, slice):
start, stop, step = index.indices(self._n)
length = stop - start
Expand All @@ -122,7 +138,9 @@ def __setitem__(self, index, val):
if self.auto_write:
self.show()

def __getitem__(self, index):
def __getitem__(
self, index: Union[slice, int]
) -> Union[Tuple[int, ...], List[Tuple[int, ...]]]:
if isinstance(index, slice):
out = []
for in_i in range(*index.indices(self._n)):
Expand All @@ -135,21 +153,21 @@ def __getitem__(self, index):
offset = index * 3
return tuple(self._buf[offset + i] for i in range(3))

def __len__(self):
def __len__(self) -> int:
return self._n

@property
def brightness(self):
def brightness(self) -> float:
"""Overall brightness of the pixel"""
return self._brightness

@brightness.setter
def brightness(self, brightness):
def brightness(self, brightness: float) -> None:
self._brightness = min(max(brightness, 0.0), 1.0)
if self.auto_write:
self.show()

def fill(self, color):
def fill(self, color: Union[Tuple[int, ...], int]) -> None:
"""Colors all pixels the given ***color***."""
auto_write = self.auto_write
self.auto_write = False
Expand All @@ -159,15 +177,15 @@ def fill(self, color):
self.show()
self.auto_write = auto_write

def _ds_writebytes(self, buf):
def _ds_writebytes(self, buf: bytearray) -> None:
for b in buf:
for _ in range(8):
self.dpin.value = b & 0x80
self.cpin.value = True
self.cpin.value = False
b = b << 1

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

Expand Down