Skip to content

Add type annotations #59

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

Merged
merged 3 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
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
43 changes: 28 additions & 15 deletions adafruit_dotstar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
except ImportError:
import adafruit_pypixelbuf as adafruit_pixelbuf

try:
from typing import Optional, Type
from types import TracebackType
from circuitpython_typing import ReadableBuffer
from microcontroller import Pin
except ImportError:
pass

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

Expand Down Expand Up @@ -95,15 +103,15 @@ class DotStar(adafruit_pixelbuf.PixelBuf):

def __init__(
self,
clock,
data,
n,
clock: Pin,
data: Pin,
n: int,
*,
brightness=1.0,
auto_write=True,
pixel_order=BGR,
baudrate=4000000
):
brightness: float = 1.0,
auto_write: bool = True,
pixel_order: str = BGR,
baudrate: int = 4000000
) -> None:
self._spi = None
try:
self._spi = busio.SPI(clock, MOSI=data)
Expand Down Expand Up @@ -137,7 +145,7 @@ def __init__(
trailer=trailer,
)

def deinit(self):
def deinit(self) -> None:
"""Blank out the DotStars and release the resources."""
self.fill(0)
self.show()
Expand All @@ -147,29 +155,34 @@ def deinit(self):
self.dpin.deinit()
self.cpin.deinit()

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

def __exit__(self, exception_type, exception_value, traceback):
def __exit__(
self,
exception_type: Optional[Type[type]],
exception_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
self.deinit()

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

@property
def n(self):
def n(self) -> int:
"""
The number of dotstars in the chain (read-only)
"""
return len(self)

def _transmit(self, buffer):
def _transmit(self, buffer: ReadableBuffer) -> None:
if self._spi:
self._spi.write(buffer)
else:
self._ds_writebytes(buffer)

def _ds_writebytes(self, buffer):
def _ds_writebytes(self, buffer: ReadableBuffer) -> None:
for b in buffer:
for _ in range(8):
self.dpin.value = b & 0x80
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: Unlicense

Adafruit-Blinka
Adafruit-Blinka>=7.0.0
adafruit-circuitpython-typing
adafruit-circuitpython-busdevice
adafruit-circuitpython-pixelbuf
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
author="Adafruit Industries",
author_email="[email protected]",
install_requires=[
"Adafruit-Blinka",
"Adafruit-Blinka>=7.0.0",
"adafruit-circuitpython-typing",
"adafruit-circuitpython-busdevice",
"adafruit-circuitpython-pixelbuf",
],
Expand Down