Skip to content

Commit c5c25f3

Browse files
committed
Remove _LED_Data and move dunders to PixelStrip
1 parent f80276b commit c5c25f3

File tree

1 file changed

+38
-48
lines changed

1 file changed

+38
-48
lines changed

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,7 @@ 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-
for n in xrange(*pos.indices(self.size)):
49-
ws.ws2811_led_set(self.channel, n, value)
50-
# Else assume the passed in value is a number to the position.
51-
else:
52-
return ws.ws2811_led_set(self.channel, pos, value)
53-
54-
55-
class PixelStrip(_LED_Data):
21+
class PixelStrip(object):
5622
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
5723
brightness=255, channel=0, strip_type=None, gamma=None):
5824
"""Class to represent a SK6812/WS281x LED display. Num should be the
@@ -103,12 +69,36 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
10369
ws.ws2811_t_freq_set(self._leds, freq_hz)
10470
ws.ws2811_t_dmanum_set(self._leds, dma)
10571

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

10974
# Substitute for __del__, traps an exit condition and cleans up properly
11075
atexit.register(self._cleanup)
11176

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+
112102
def _cleanup(self):
113103
# Clean up memory used by the library when not needed anymore.
114104
if self._leds is not None:
@@ -141,7 +131,7 @@ def show(self):
141131
def setPixelColor(self, n, color):
142132
"""Set LED at position n to the provided 24-bit color value (in RGB order).
143133
"""
144-
self._led_data[n] = color
134+
self[n] = color
145135

146136
def setPixelColorRGB(self, n, red, green, blue, white=0):
147137
"""Set LED at position n to the provided red, green, and blue color.
@@ -163,29 +153,29 @@ def getPixels(self):
163153
"""Return an object which allows access to the LED display data as if
164154
it were a sequence of 24-bit RGB values.
165155
"""
166-
return self._led_data
156+
return self[:]
167157

168158
def numPixels(self):
169159
"""Return the number of pixels in the display."""
170160
return ws.ws2811_channel_t_count_get(self._channel)
171161

172162
def getPixelColor(self, n):
173163
"""Get the 24-bit RGB color value for the LED at position n."""
174-
return self._led_data[n]
164+
return self[n]
175165

176166
def getPixelColorRGB(self, n):
177167
c = lambda: None
178-
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
179-
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
180-
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)
181171
return c
182-
172+
183173
def getPixelColorRGBW(self, n):
184174
c = lambda: None
185-
setattr(c, 'w', self._led_data[n] >> 24 & 0xff)
186-
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
187-
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
188-
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)
189179
return c
190180

191181
# Shim for back-compatibility

0 commit comments

Comments
 (0)