Skip to content

Commit c033bd4

Browse files
committed
Support Color Order setting
1 parent f9e5675 commit c033bd4

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

adafruit_dotstar.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
__version__ = "0.0.0-auto.0"
3737
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git"
3838

39+
# Pixel color order constants
40+
RGB = (0, 1, 2)
41+
RBG = (0, 2, 1)
42+
GRB = (1, 0, 2)
43+
GBR = (1, 2, 0)
44+
BRG = (2, 0, 1)
45+
BGR = (2, 1, 0)
46+
47+
3948
class DotStar:
4049
"""
4150
A sequence of dotstars.
@@ -46,6 +55,9 @@ class DotStar:
4655
:param float brightness: Brightness of the pixels between 0.0 and 1.0
4756
:param bool auto_write: True if the dotstars should immediately change when
4857
set. If False, `show` must be called explicitly.
58+
:param tuple pixel_order: Set the pixel order on the strip - different
59+
strips implement this differently. If you send red, and it looks blue
60+
or green on the strip, modify this! It should be one of the values above
4961
5062
5163
Example for Gemma M0:
@@ -63,7 +75,7 @@ class DotStar:
6375
time.sleep(2)
6476
"""
6577

66-
def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
78+
def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True, pixel_order=BGR):
6779
self._spi = None
6880
try:
6981
self._spi = busio.SPI(clock, MOSI=data)
@@ -84,7 +96,7 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True):
8496
self.end_header_size += 1
8597
self._buf = bytearray(n * 4 + self.start_header_size + self.end_header_size)
8698
self.end_header_index = len(self._buf) - self.end_header_size
87-
99+
self.pixel_order = pixel_order
88100
# Four empty bytes to start.
89101
for i in range(self.start_header_size):
90102
self._buf[i] = 0x00
@@ -125,23 +137,18 @@ def __repr__(self):
125137

126138
def _set_item(self, index, value):
127139
offset = index * 4 + self.start_header_size
128-
r = 0
129-
g = 0
130-
b = 0
140+
rbg = value
131141
if isinstance(value, int):
132-
r = value >> 16
133-
g = (value >> 8) & 0xff
134-
b = value & 0xff
135-
else:
136-
r, g, b = value
142+
rgb = (value >> 16, (value >> 8) & 0xff, value & 0xff)
143+
137144
# Each pixel starts with 0xFF, then red/green/blue. Although the data
138145
# sheet suggests using a global brightness in the first byte, we don't
139146
# do that because it causes further issues with persistence of vision
140147
# projects.
141148
self._buf[offset] = 0xff # redundant; should already be set
142-
self._buf[offset + 1] = b
143-
self._buf[offset + 2] = g
144-
self._buf[offset + 3] = r
149+
self._buf[offset + 1] = rgb[self.pixel_order[0]]
150+
self._buf[offset + 2] = rgb[self.pixel_order[1]]
151+
self._buf[offset + 3] = rgb[self.pixel_order[2]]
145152

146153
def __setitem__(self, index, val):
147154
if isinstance(index, slice):
@@ -220,7 +227,7 @@ def show(self):
220227
for i in range(self.start_header_size):
221228
buf[i] = 0x00
222229
for i in range(self.start_header_size, self.end_header_index):
223-
buf[i] = self._buf[i] if i %4 == 0 else int(self._buf[i] * self._brightness)
230+
buf[i] = self._buf[i] if i % 4 == 0 else int(self._buf[i] * self._brightness)
224231
# Four 0xff bytes at the end.
225232
for i in range(self.end_header_index, len(buf)):
226233
buf[i] = 0xff

0 commit comments

Comments
 (0)