Skip to content

Commit 0ea2c66

Browse files
authored
Merge pull request #36 from RossK1/adding_type_hints
Adding type annotations and optional typing-related imports
2 parents bcabc4b + 62560a6 commit 0ea2c66

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

neopixel_spi.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@
2525
2626
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2727
"""
28+
try:
29+
from typing import Optional, Tuple, Union
2830

29-
import adafruit_pixelbuf
31+
from busio import SPI
32+
except ImportError:
33+
pass
3034

35+
import adafruit_pixelbuf
3136
from adafruit_bus_device.spi_device import SPIDevice
3237

33-
3438
__version__ = "0.0.0+auto.0"
3539
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
3640

3741
# Pixel color order constants
38-
RGB = "RGB"
42+
RGB: str = "RGB"
3943
"""Red Green Blue"""
40-
GRB = "GRB"
44+
GRB: str = "GRB"
4145
"""Green Red Blue"""
42-
RGBW = "RGBW"
46+
RGBW: str = "RGBW"
4347
"""Red Green Blue White"""
44-
GRBW = "GRBW"
48+
GRBW: str = "GRBW"
4549
"""Green Red Blue White"""
4650

4751

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

7782
def __init__(
7883
self,
79-
spi,
80-
n,
84+
spi: SPI,
85+
n: int,
8186
*,
82-
bpp=3,
83-
brightness=1.0,
84-
auto_write=True,
85-
pixel_order=None,
86-
frequency=6400000,
87-
reset_time=80e-6,
88-
bit0=0b11000000,
89-
bit1=0b11110000
90-
):
91-
87+
bpp: int = 3,
88+
brightness: float = 1.0,
89+
auto_write: bool = True,
90+
pixel_order: Optional[Union[str, Tuple[int, ...]]] = None,
91+
frequency: int = 6400000,
92+
reset_time: float = 80e-6,
93+
bit0: int = 0b11000000,
94+
bit1: int = 0b11110000
95+
) -> None:
9296
# configure bpp and pixel_order
9397
if not pixel_order:
9498
pixel_order = GRB if bpp == 3 else GRBW
@@ -117,25 +121,25 @@ def __init__(
117121

118122
# everything else taken care of by base class
119123
super().__init__(
120-
n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write
124+
size=n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write
121125
)
122126

123-
def deinit(self):
127+
def deinit(self) -> None:
124128
"""Blank out the NeoPixels."""
125129
self.fill(0)
126130
self.show()
127131

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

131135
@property
132-
def n(self):
136+
def n(self) -> int:
133137
"""
134138
The number of neopixels in the chain (read-only)
135139
"""
136140
return len(self)
137141

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

148-
def _transmogrify(self, buffer):
152+
def _transmogrify(self, buffer: bytearray) -> None:
149153
"""Turn every BIT of buf into a special BYTE pattern."""
150154
k = 0
151155
for byte in buffer:

0 commit comments

Comments
 (0)