Skip to content

Commit

Permalink
Merge pull request #36 from RossK1/adding_type_hints
Browse files Browse the repository at this point in the history
Adding type annotations and optional typing-related imports
  • Loading branch information
FoamyGuy authored Apr 28, 2023
2 parents bcabc4b + 62560a6 commit 0ea2c66
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions neopixel_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,27 @@
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
try:
from typing import Optional, Tuple, Union

import adafruit_pixelbuf
from busio import SPI
except ImportError:
pass

import adafruit_pixelbuf
from adafruit_bus_device.spi_device import SPIDevice


__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"

# Pixel color order constants
RGB = "RGB"
RGB: str = "RGB"
"""Red Green Blue"""
GRB = "GRB"
GRB: str = "GRB"
"""Green Red Blue"""
RGBW = "RGBW"
RGBW: str = "RGBW"
"""Red Green Blue White"""
GRBW = "GRBW"
GRBW: str = "GRBW"
"""Green Red Blue White"""


Expand All @@ -57,6 +61,7 @@ class NeoPixel_SPI(adafruit_pixelbuf.PixelBuf):
:param bool auto_write: True if the neopixels should immediately change when set. If False,
``show`` must be called explicitly.
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
pixel_order may be a string or a tuple of integers with values between 0 and 3.
:param int frequency: SPI bus frequency. For 800kHz NeoPixels, use 6400000 (default).
For 400kHz, use 3200000.
:param float reset_time: Reset low level time in seconds. Default is 80e-6.
Expand All @@ -76,19 +81,18 @@ class NeoPixel_SPI(adafruit_pixelbuf.PixelBuf):

def __init__(
self,
spi,
n,
spi: SPI,
n: int,
*,
bpp=3,
brightness=1.0,
auto_write=True,
pixel_order=None,
frequency=6400000,
reset_time=80e-6,
bit0=0b11000000,
bit1=0b11110000
):

bpp: int = 3,
brightness: float = 1.0,
auto_write: bool = True,
pixel_order: Optional[Union[str, Tuple[int, ...]]] = None,
frequency: int = 6400000,
reset_time: float = 80e-6,
bit0: int = 0b11000000,
bit1: int = 0b11110000
) -> None:
# configure bpp and pixel_order
if not pixel_order:
pixel_order = GRB if bpp == 3 else GRBW
Expand Down Expand Up @@ -117,25 +121,25 @@ def __init__(

# everything else taken care of by base class
super().__init__(
n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write
size=n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write
)

def deinit(self):
def deinit(self) -> None:
"""Blank out the NeoPixels."""
self.fill(0)
self.show()

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 neopixels in the chain (read-only)
"""
return len(self)

def _transmit(self, buffer):
def _transmit(self, buffer: bytearray) -> None:
"""Shows the new colors on the pixels themselves if they haven't already
been autowritten."""
self._transmogrify(buffer)
Expand All @@ -145,7 +149,7 @@ def _transmit(self, buffer):
# leading RESET needed for cases where MOSI rests HI
spi.write(self._reset + self._spibuf + self._reset)

def _transmogrify(self, buffer):
def _transmogrify(self, buffer: bytearray) -> None:
"""Turn every BIT of buf into a special BYTE pattern."""
k = 0
for byte in buffer:
Expand Down

0 comments on commit 0ea2c66

Please sign in to comment.