Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit f22b300

Browse files
committed
Allow setting RGBW pixels with RGB tuples
1 parent ca839ea commit f22b300

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

adafruit_pypixelbuf.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,39 @@ def _parse_color(self, value):
227227
g = (value >> 8) & 0xFF
228228
b = value & 0xFF
229229
w = 0
230-
# If all components are the same and we have a white pixel then use it
231-
# instead of the individual components.
232-
if self._bpp == 4 and self._has_white and r == g and g == b:
230+
231+
if self._dotstar_mode:
232+
w = 1.0
233+
else:
234+
if len(value) < 3 or len(value) > 4:
235+
raise ValueError(
236+
"Expected tuple of length {}, got {}".format(self._bpp, len(value))
237+
)
238+
if len(value) == self._bpp:
239+
if self._bpp == 3:
240+
r, g, b = value
241+
else:
242+
r, g, b, w = value
243+
elif len(value) == 3:
244+
r, g, b = value
245+
if self._dotstar_mode:
246+
w = 1.0
247+
248+
if self._bpp == 4:
249+
if self._dotstar_mode:
250+
# LED startframe is three "1" bits, followed by 5 brightness bits
251+
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
252+
# vary based on hardware
253+
# same as math.ceil(brightness * 31) & 0b00011111
254+
# Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions
255+
w = (32 - int(32 - w * 31) & 0b00011111) | DOTSTAR_LED_START
256+
elif self._has_white and r == g and g == b:
257+
# If all components are the same and we have a white pixel then use it
258+
# instead of the individual components.
233259
w = r
234260
r = 0
235261
g = 0
236262
b = 0
237-
elif self._dotstar_mode:
238-
w = 1.0
239-
elif len(value) == self._bpp:
240-
if self._bpp == 3:
241-
r, g, b = value
242-
else:
243-
r, g, b, w = value
244-
elif len(value) == 3 and self._dotstar_mode:
245-
r, g, b = value
246-
w = 1.0
247-
248-
if self._bpp == 4 and self._dotstar_mode:
249-
# LED startframe is three "1" bits, followed by 5 brightness bits
250-
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and
251-
# vary based on hardware
252-
# same as math.ceil(brightness * 31) & 0b00011111
253-
# Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions
254-
w = (32 - int(32 - w * 31) & 0b00011111) | DOTSTAR_LED_START
255263

256264
return (r, g, b, w)
257265

examples/pypixelbuf_simpletest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
class TestBuf(adafruit_pypixelbuf.PixelBuf):
55
called = False
66

7-
def _transmit(self, buffer):
7+
@property
8+
def n(self):
9+
return len(self)
10+
11+
def _transmit(self, buffer): # pylint: disable=unused-argument
812
self.called = True
913

1014

0 commit comments

Comments
 (0)