Skip to content

Commit 83b2519

Browse files
committed
I realized rebuilding the buffer on every show is an important part of the behavior
1 parent 609b012 commit 83b2519

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

adafruit_dotstar.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git"
3838

3939
START_HEADER_SIZE = 4
40+
LED_START = 0b11100000 # Three "1" bits, followed by 5 brightness bits
4041

4142
# Pixel color order constants
4243
RGB = (0, 1, 2)
@@ -171,14 +172,14 @@ def _set_item(self, index, value):
171172
else:
172173
brightness = 100
173174

174-
brightness_byte = ceil(brightness * 31) & 0b00011111
175+
brightness_byte = math.ceil(brightness * 31) & 0b00011111
175176
# LED startframe is three "1" bits, followed by 5 brightness bits
176177
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
177178
# vary based on hardware
178179
self._buf[offset] = brightness_byte | LED_START
179-
self._buf[offset + 1] = int(rgb[self.pixel_order[0]] * self.brightness)
180-
self._buf[offset + 2] = int(rgb[self.pixel_order[1]] * self.brightness)
181-
self._buf[offset + 3] = (rgb[self.pixel_order[2]] * self.brightness)
180+
self._buf[offset + 1] = int(rgb[self.pixel_order[0]])
181+
self._buf[offset + 2] = int(rgb[self.pixel_order[1]])
182+
self._buf[offset + 3] = (rgb[self.pixel_order[2]])
182183

183184
def __setitem__(self, index, val):
184185
if isinstance(index, slice):
@@ -249,7 +250,23 @@ def show(self):
249250
250251
The colors may or may not be showing after this function returns because
251252
it may be done asynchronously."""
253+
# Create a second output buffer if we need to compute brightness
254+
buf = self._buf
255+
if self.brightness < 1.0:
256+
buf = bytearray(self._buf)
257+
# Four empty bytes to start.
258+
for i in range(START_HEADER_SIZE):
259+
buf[i] = 0x00
260+
for i in range(START_HEADER_SIZE, self.end_header_index):
261+
buf[i] = self._buf[i] if i % 4 == 0 else math.ceil(self._buf[i] * self._brightness)
262+
# Four 0xff bytes at the end.
263+
for i in range(self.end_header_index, len(buf)):
264+
buf[i] = 0xff
252265

266+
if self._spi:
267+
self._spi.write(buf)
268+
else:
269+
self._ds_writebytes(buf)
253270
if self._spi:
254271
self._spi.write(self._buf)
255272
else:

0 commit comments

Comments
 (0)