Skip to content

Commit 7404caa

Browse files
authored
Merge pull request #91 from Viicos/90-refactor-led-data
Make `PixelStrip` a subclass of `_LED_Data`
2 parents 3f7f031 + c5c25f3 commit 7404caa

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,6 @@ def Color(red, green, blue, white=0):
1818
return (white << 24) | (red << 16) | (green << 8) | blue
1919

2020

21-
class _LED_Data(object):
22-
"""Wrapper class which makes a SWIG LED color data array look and feel like
23-
a Python list of integers.
24-
"""
25-
def __init__(self, channel, size):
26-
self.size = size
27-
self.channel = channel
28-
29-
def __getitem__(self, pos):
30-
"""Return the 24-bit RGB color value at the provided position or slice
31-
of positions.
32-
"""
33-
# Handle if a slice of positions are passed in by grabbing all the values
34-
# and returning them in a list.
35-
if isinstance(pos, slice):
36-
return [ws.ws2811_led_get(self.channel, n) for n in xrange(*pos.indices(self.size))]
37-
# Else assume the passed in value is a number to the position.
38-
else:
39-
return ws.ws2811_led_get(self.channel, pos)
40-
41-
def __setitem__(self, pos, value):
42-
"""Set the 24-bit RGB color value at the provided position or slice of
43-
positions.
44-
"""
45-
# Handle if a slice of positions are passed in by setting the appropriate
46-
# LED data values to the provided values.
47-
if isinstance(pos, slice):
48-
index = 0
49-
for n in xrange(*pos.indices(self.size)):
50-
ws.ws2811_led_set(self.channel, n, value[index])
51-
index += 1
52-
# Else assume the passed in value is a number to the position.
53-
else:
54-
return ws.ws2811_led_set(self.channel, pos, value)
55-
56-
5721
class PixelStrip(object):
5822
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
5923
brightness=255, channel=0, strip_type=None, gamma=None):
@@ -91,6 +55,9 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
9155

9256
# Initialize the channel in use
9357
self._channel = ws.ws2811_channel_get(self._leds, channel)
58+
59+
super(PixelStrip, self).__init__(self._channel, num)
60+
9461
ws.ws2811_channel_t_gamma_set(self._channel, gamma)
9562
ws.ws2811_channel_t_count_set(self._channel, num)
9663
ws.ws2811_channel_t_gpionum_set(self._channel, pin)
@@ -102,12 +69,36 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
10269
ws.ws2811_t_freq_set(self._leds, freq_hz)
10370
ws.ws2811_t_dmanum_set(self._leds, dma)
10471

105-
# Grab the led data array.
106-
self._led_data = _LED_Data(self._channel, num)
72+
self.size = num
10773

10874
# Substitute for __del__, traps an exit condition and cleans up properly
10975
atexit.register(self._cleanup)
11076

77+
def __getitem__(self, pos):
78+
"""Return the 24-bit RGB color value at the provided position or slice
79+
of positions.
80+
"""
81+
# Handle if a slice of positions are passed in by grabbing all the values
82+
# and returning them in a list.
83+
if isinstance(pos, slice):
84+
return [ws.ws2811_led_get(self._channel, n) for n in xrange(*pos.indices(self.size))]
85+
# Else assume the passed in value is a number to the position.
86+
else:
87+
return ws.ws2811_led_get(self._channel, pos)
88+
89+
def __setitem__(self, pos, value):
90+
"""Set the 24-bit RGB color value at the provided position or slice of
91+
positions.
92+
"""
93+
# Handle if a slice of positions are passed in by setting the appropriate
94+
# LED data values to the provided value.
95+
if isinstance(pos, slice):
96+
for n in xrange(*pos.indices(self.size)):
97+
ws.ws2811_led_set(self._channel, n, value)
98+
# Else assume the passed in value is a number to the position.
99+
else:
100+
return ws.ws2811_led_set(self._channel, pos, value)
101+
111102
def _cleanup(self):
112103
# Clean up memory used by the library when not needed anymore.
113104
if self._leds is not None:
@@ -140,7 +131,7 @@ def show(self):
140131
def setPixelColor(self, n, color):
141132
"""Set LED at position n to the provided 24-bit color value (in RGB order).
142133
"""
143-
self._led_data[n] = color
134+
self[n] = color
144135

145136
def setPixelColorRGB(self, n, red, green, blue, white=0):
146137
"""Set LED at position n to the provided red, green, and blue color.
@@ -162,29 +153,29 @@ def getPixels(self):
162153
"""Return an object which allows access to the LED display data as if
163154
it were a sequence of 24-bit RGB values.
164155
"""
165-
return self._led_data
156+
return self[:]
166157

167158
def numPixels(self):
168159
"""Return the number of pixels in the display."""
169160
return ws.ws2811_channel_t_count_get(self._channel)
170161

171162
def getPixelColor(self, n):
172163
"""Get the 24-bit RGB color value for the LED at position n."""
173-
return self._led_data[n]
164+
return self[n]
174165

175166
def getPixelColorRGB(self, n):
176167
c = lambda: None
177-
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
178-
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
179-
setattr(c, 'b', self._led_data[n] & 0xff)
168+
setattr(c, 'r', self[n] >> 16 & 0xff)
169+
setattr(c, 'g', self[n] >> 8 & 0xff)
170+
setattr(c, 'b', self[n] & 0xff)
180171
return c
181-
172+
182173
def getPixelColorRGBW(self, n):
183174
c = lambda: None
184-
setattr(c, 'w', self._led_data[n] >> 24 & 0xff)
185-
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
186-
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
187-
setattr(c, 'b', self._led_data[n] & 0xff)
175+
setattr(c, 'w', self[n] >> 24 & 0xff)
176+
setattr(c, 'r', self[n] >> 16 & 0xff)
177+
setattr(c, 'g', self[n] >> 8 & 0xff)
178+
setattr(c, 'b', self[n] & 0xff)
188179
return c
189180

190181
# Shim for back-compatibility

0 commit comments

Comments
 (0)