Skip to content

Commit a214d70

Browse files
committed
Bugfixes, len support and new RGBW class.
1 parent 4fcbadb commit a214d70

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

library/rpi_ws281x/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# New canonical package, to support `import rpi_ws281x`
2-
from .rpi_ws281x import PixelStrip, Adafruit_NeoPixel, Color, ws
2+
from .rpi_ws281x import PixelStrip, Adafruit_NeoPixel, Color, RGBW, ws
33
from _rpi_ws281x import *
44

55
__version__ = '5.0.0'

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,41 @@
44
import atexit
55

66

7+
class RGBW(int):
8+
def __new__(self, r, g=None, b=None, w=None):
9+
if (g, b, w) == (None, None, None):
10+
return int.__new__(self, r)
11+
else:
12+
if w is None:
13+
w = 0
14+
return int.__new__(self, (w << 24) | (r << 16) | (g << 8) | b)
15+
16+
@property
17+
def r(self):
18+
return (self >> 16) & 0xff
19+
20+
@property
21+
def g(self):
22+
return (self >> 8) & 0xff
23+
24+
@property
25+
def b(self):
26+
return (self) & 0xff
27+
28+
@property
29+
def w(self):
30+
return (self >> 24) & 0xff
31+
32+
733
def Color(red, green, blue, white=0):
834
"""Convert the provided red, green, blue color to a 24-bit color value.
935
Each color component should be a value 0-255 where 0 is the lowest intensity
1036
and 255 is the highest intensity.
1137
"""
12-
return (white << 24) | (red << 16) | (green << 8) | blue
38+
return RGBW(red, green, blue, white)
1339

1440

15-
class PixelStrip(object):
41+
class PixelStrip:
1642
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
1743
brightness=255, channel=0, strip_type=None, gamma=None):
1844
"""Class to represent a SK6812/WS281x LED display. Num should be the
@@ -50,8 +76,6 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
5076
# Initialize the channel in use
5177
self._channel = ws.ws2811_channel_get(self._leds, channel)
5278

53-
super(PixelStrip, self).__init__(self._channel, num)
54-
5579
ws.ws2811_channel_t_gamma_set(self._channel, gamma)
5680
ws.ws2811_channel_t_count_set(self._channel, num)
5781
ws.ws2811_channel_t_gpionum_set(self._channel, pin)
@@ -93,6 +117,9 @@ def __setitem__(self, pos, value):
93117
else:
94118
return ws.ws2811_led_set(self._channel, pos, value)
95119

120+
def __len__(self):
121+
return ws.ws2811_channel_t_count_get(self._channel)
122+
96123
def _cleanup(self):
97124
# Clean up memory used by the library when not needed anymore.
98125
if self._leds is not None:
@@ -151,26 +178,17 @@ def getPixels(self):
151178

152179
def numPixels(self):
153180
"""Return the number of pixels in the display."""
154-
return ws.ws2811_channel_t_count_get(self._channel)
181+
return len(self)
155182

156183
def getPixelColor(self, n):
157184
"""Get the 24-bit RGB color value for the LED at position n."""
158185
return self[n]
159186

160187
def getPixelColorRGB(self, n):
161-
c = lambda: None
162-
setattr(c, 'r', self[n] >> 16 & 0xff)
163-
setattr(c, 'g', self[n] >> 8 & 0xff)
164-
setattr(c, 'b', self[n] & 0xff)
165-
return c
188+
return RGBW(self[n])
166189

167190
def getPixelColorRGBW(self, n):
168-
c = lambda: None
169-
setattr(c, 'w', self[n] >> 24 & 0xff)
170-
setattr(c, 'r', self[n] >> 16 & 0xff)
171-
setattr(c, 'g', self[n] >> 8 & 0xff)
172-
setattr(c, 'b', self[n] & 0xff)
173-
return c
191+
return RGBW(self[n])
174192

175193
# Shim for back-compatibility
176194
class Adafruit_NeoPixel(PixelStrip):

0 commit comments

Comments
 (0)