|
4 | 4 | import atexit
|
5 | 5 |
|
6 | 6 |
|
| 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 | + |
7 | 33 | def Color(red, green, blue, white=0):
|
8 | 34 | """Convert the provided red, green, blue color to a 24-bit color value.
|
9 | 35 | Each color component should be a value 0-255 where 0 is the lowest intensity
|
10 | 36 | and 255 is the highest intensity.
|
11 | 37 | """
|
12 |
| - return (white << 24) | (red << 16) | (green << 8) | blue |
| 38 | + return RGBW(red, green, blue, white) |
13 | 39 |
|
14 | 40 |
|
15 |
| -class PixelStrip(object): |
| 41 | +class PixelStrip: |
16 | 42 | def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
|
17 | 43 | brightness=255, channel=0, strip_type=None, gamma=None):
|
18 | 44 | """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,
|
50 | 76 | # Initialize the channel in use
|
51 | 77 | self._channel = ws.ws2811_channel_get(self._leds, channel)
|
52 | 78 |
|
53 |
| - super(PixelStrip, self).__init__(self._channel, num) |
54 |
| - |
55 | 79 | ws.ws2811_channel_t_gamma_set(self._channel, gamma)
|
56 | 80 | ws.ws2811_channel_t_count_set(self._channel, num)
|
57 | 81 | ws.ws2811_channel_t_gpionum_set(self._channel, pin)
|
@@ -93,6 +117,9 @@ def __setitem__(self, pos, value):
|
93 | 117 | else:
|
94 | 118 | return ws.ws2811_led_set(self._channel, pos, value)
|
95 | 119 |
|
| 120 | + def __len__(self): |
| 121 | + return ws.ws2811_channel_t_count_get(self._channel) |
| 122 | + |
96 | 123 | def _cleanup(self):
|
97 | 124 | # Clean up memory used by the library when not needed anymore.
|
98 | 125 | if self._leds is not None:
|
@@ -151,26 +178,17 @@ def getPixels(self):
|
151 | 178 |
|
152 | 179 | def numPixels(self):
|
153 | 180 | """Return the number of pixels in the display."""
|
154 |
| - return ws.ws2811_channel_t_count_get(self._channel) |
| 181 | + return len(self) |
155 | 182 |
|
156 | 183 | def getPixelColor(self, n):
|
157 | 184 | """Get the 24-bit RGB color value for the LED at position n."""
|
158 | 185 | return self[n]
|
159 | 186 |
|
160 | 187 | 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]) |
166 | 189 |
|
167 | 190 | 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]) |
174 | 192 |
|
175 | 193 | # Shim for back-compatibility
|
176 | 194 | class Adafruit_NeoPixel(PixelStrip):
|
|
0 commit comments