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