Skip to content

Commit dd29815

Browse files
committed
subclass from Pixelbuf and add changes from master
1 parent 6efcd01 commit dd29815

File tree

1 file changed

+38
-86
lines changed

1 file changed

+38
-86
lines changed

adafruit_dotstar.py

Lines changed: 38 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,33 @@
2424
# THE SOFTWARE.
2525

2626
"""
27-
`adafruit_dotstar` - DotStar strip driver (for CircuitPython 4.0+ with _pixelbuf)
27+
`adafruit_dotstar` - DotStar strip driver (for CircuitPython 5.0+ with _pixelbuf)
2828
=================================================================================
2929
30-
* Author(s): Damien P. George, Limor Fried, Scott Shawcroft, Roy Hooper
30+
* Author(s): Damien P. George, Limor Fried, Scott Shawcroft & Roy Hooper
3131
"""
3232
import busio
3333
import digitalio
3434
try:
35-
from _pixelbuf import PixelBuf, RGBD as RGB, RBGD as RBG, GRBD as GRB, GBRD as GBR, BRGD as BRG, BGRD as BGR
35+
from _pixelbuf import PixelBuf
3636
except ImportError:
37-
from pypixelbuf import PixelBuf, RGB, RBG, GRB, GBR, BRG, BGR, LRGB, LRBG, LGRB, LGBR, LBRG, LBGR
37+
from pypixelbuf import PixelBuf
3838

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

4242
START_HEADER_SIZE = 4
4343

44+
RBG = 'PRBG'
45+
RGB = 'PRGB'
46+
GRB = 'PGRB'
47+
GBR = 'PGBR'
48+
BRG = 'PBRG'
49+
BGR = 'PBGR'
50+
BGR = 'PBGR'
4451

45-
class DotStar:
52+
53+
class DotStar(PixelBuf):
4654
"""
4755
A sequence of dotstars.
4856
@@ -52,10 +60,13 @@ class DotStar:
5260
:param float brightness: Brightness of the pixels between 0.0 and 1.0
5361
:param bool auto_write: True if the dotstars should immediately change when
5462
set. If False, `show` must be called explicitly.
55-
:param ByteOrder pixel_order: Set the pixel order on the strip - different
63+
:param str pixel_order: Set the pixel order on the strip - different
5664
strips implement this differently. If you send red, and it looks blue
57-
or green on the strip, modify this! It should be one of the values above
58-
65+
or green on the strip, modify this! It should be one of the values above.
66+
:param int baudrate: Desired clock rate if using hardware SPI (ignored if
67+
using 'soft' SPI). This is only a recommendation; the actual clock
68+
rate may be slightly different depending on what the system hardware
69+
can provide.
5970
6071
Example for Gemma M0:
6172
@@ -88,6 +99,7 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True,
8899
self.cpin.direction = digitalio.Direction.OUTPUT
89100
self.cpin.value = False
90101
self.n = n
102+
91103
# Supply one extra clock cycle for each two pixels in the strip.
92104
end_header_size = n // 16
93105
if n % 16 != 0:
@@ -106,20 +118,28 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True,
106118
for i in range(end_header_index, bufsize):
107119
self._rawbuf[i] = 0xff
108120
# Mark the beginnings of each pixel.
109-
# for i in range(START_HEADER_SIZE, end_header_index, 4):
110-
# self._rawbuf[i] = 0xff
121+
for i in range(START_HEADER_SIZE, end_header_index, 4):
122+
self._rawbuf[i] = 0xff
111123
self._buf[:] = self._rawbuf[:]
112124

113-
writer = self._spi.write if self._spi else self._ds_writebytes
114-
def write_fn(*_):
115-
self._spi.write(self._buf)
125+
super(DotStar, self).__init__(n, self._buf, byteorder=pixel_order,
126+
rawbuf=self._rawbuf, offset=START_HEADER_SIZE,
127+
brightness=brightness, auto_write=auto_write)
116128

117-
self._pb = PixelBuf(n, self._buf, byteorder=pixel_order,
118-
rawbuf=self._rawbuf, write_function=write_fn, offset=START_HEADER_SIZE,
119-
write_args=(), brightness=brightness, auto_write=auto_write)
129+
def show(self):
130+
if self._spi:
131+
self._spi.write(self._buf)
132+
else:
133+
self.ds_writebytes()
120134

121-
if self.auto_write:
122-
self.show()
135+
def _ds_writebytes(self):
136+
for b in self.buf:
137+
for _ in range(8):
138+
self.dpin.value = (b & 0x80)
139+
self.cpin.value = True
140+
self.cpin.value = False
141+
b = b << 1
142+
self.cpin.value = False
123143

124144
def deinit(self):
125145
"""Blank out the DotStars and release the resources."""
@@ -139,71 +159,3 @@ def __exit__(self, exception_type, exception_value, traceback):
139159

140160
def __repr__(self):
141161
return "[" + ", ".join([str(x) for x in self]) + "]"
142-
143-
def _set_item(self, index, value):
144-
if isinstance(index, slice):
145-
start, stop, step = index.indices(self.n)
146-
self._pb[start:stop:step] = value
147-
self._pb[index] = value
148-
149-
def __setitem__(self, index, value):
150-
"""
151-
value can be one of three things:
152-
a (r,g,b) list/tuple
153-
a (r,g,b, brightness) list/tuple (if bpp=4, pixel_order=LBGR)
154-
a single, longer int that contains RGB values, like 0xFFFFFF
155-
brightness, if specified should be a float 0-1
156-
157-
Set a pixel value. You can set per-pixel brightness here, if it's not passed it
158-
will use the max value for pixel brightness value, which is a good default.
159-
160-
Important notes about the per-pixel brightness - it's accomplished by
161-
PWMing the entire output of the LED, and that PWM is at a much
162-
slower clock than the rest of the LEDs. This can cause problems in
163-
Persistence of Vision Applications
164-
"""
165-
if isinstance(index, slice):
166-
start, stop, step = index.indices(self.n)
167-
self._pb[start:stop:step] = value
168-
self._pb[index] = value
169-
170-
def __getitem__(self, index):
171-
if isinstance(index, slice):
172-
start, stop, step = index.indices(self.n)
173-
return self._pb[start:stop:step]
174-
return self._pb[index]
175-
176-
@property
177-
def brightness(self):
178-
return self._pb.brightness
179-
180-
@brightness.setter
181-
def brightness(self, brightness):
182-
self._pb.brightness = brightness
183-
184-
@property
185-
def auto_write(self):
186-
return self._pb.auto_write
187-
188-
@auto_write.setter
189-
def auto_write(self, auto_write):
190-
self._pb.auto_write = auto_write
191-
192-
def show(self):
193-
self._pb.show()
194-
195-
def __len__(self):
196-
return self.n
197-
198-
def fill(self, color):
199-
"""Colors all pixels the given ***color***."""
200-
self._pb[0:self.n] = (color, ) * self.n
201-
202-
def _ds_writebytes(self, buf):
203-
for b in buf:
204-
for _ in range(8):
205-
self.dpin.value = (b & 0x80)
206-
self.cpin.value = True
207-
self.cpin.value = False
208-
b = b << 1
209-
self.cpin.value = False

0 commit comments

Comments
 (0)