@@ -18,42 +18,6 @@ def Color(red, green, blue, white=0):
18
18
return (white << 24 ) | (red << 16 ) | (green << 8 ) | blue
19
19
20
20
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
-
57
21
class PixelStrip (object ):
58
22
def __init__ (self , num , pin , freq_hz = 800000 , dma = 10 , invert = False ,
59
23
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,
91
55
92
56
# Initialize the channel in use
93
57
self ._channel = ws .ws2811_channel_get (self ._leds , channel )
58
+
59
+ super (PixelStrip , self ).__init__ (self ._channel , num )
60
+
94
61
ws .ws2811_channel_t_gamma_set (self ._channel , gamma )
95
62
ws .ws2811_channel_t_count_set (self ._channel , num )
96
63
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,
102
69
ws .ws2811_t_freq_set (self ._leds , freq_hz )
103
70
ws .ws2811_t_dmanum_set (self ._leds , dma )
104
71
105
- # Grab the led data array.
106
- self ._led_data = _LED_Data (self ._channel , num )
72
+ self .size = num
107
73
108
74
# Substitute for __del__, traps an exit condition and cleans up properly
109
75
atexit .register (self ._cleanup )
110
76
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
+
111
102
def _cleanup (self ):
112
103
# Clean up memory used by the library when not needed anymore.
113
104
if self ._leds is not None :
@@ -140,7 +131,7 @@ def show(self):
140
131
def setPixelColor (self , n , color ):
141
132
"""Set LED at position n to the provided 24-bit color value (in RGB order).
142
133
"""
143
- self . _led_data [n ] = color
134
+ self [n ] = color
144
135
145
136
def setPixelColorRGB (self , n , red , green , blue , white = 0 ):
146
137
"""Set LED at position n to the provided red, green, and blue color.
@@ -162,29 +153,29 @@ def getPixels(self):
162
153
"""Return an object which allows access to the LED display data as if
163
154
it were a sequence of 24-bit RGB values.
164
155
"""
165
- return self . _led_data
156
+ return self [:]
166
157
167
158
def numPixels (self ):
168
159
"""Return the number of pixels in the display."""
169
160
return ws .ws2811_channel_t_count_get (self ._channel )
170
161
171
162
def getPixelColor (self , n ):
172
163
"""Get the 24-bit RGB color value for the LED at position n."""
173
- return self . _led_data [n ]
164
+ return self [n ]
174
165
175
166
def getPixelColorRGB (self , n ):
176
167
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 )
180
171
return c
181
-
172
+
182
173
def getPixelColorRGBW (self , n ):
183
174
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 )
188
179
return c
189
180
190
181
# Shim for back-compatibility
0 commit comments