Skip to content

Commit

Permalink
Merge pull request #59 from tekktrik/doc/add-typing
Browse files Browse the repository at this point in the history
Add type annotations
  • Loading branch information
FoamyGuy authored Feb 26, 2022
2 parents 8f20f73 + 9feda92 commit a313e87
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
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

0 comments on commit a313e87

Please sign in to comment.