34
34
import board
35
35
import adafruit_dotstar as dotstar
36
36
37
- class DotStarFeatherWing :
38
- """Class representing a `DotStar FeatherWing
39
- <https://www.adafruit.com/product/3449>`_.
37
+ class PixelDisplayFeatherWing :
38
+ """Base Class for DotStar and NeoPixel FeatherWings
40
39
41
40
The feather uses pins D13 and D11"""
42
- def __init__ (self , clock = board .D13 , data = board .D11 , brightness = 0.2 ):
43
- """
44
- :param pin clock: The clock pin for the featherwing
45
- :param pin data: The data pin for the featherwing
46
- :param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
47
- """
48
- self .rows = 6
49
- self .columns = 12
50
- self ._brightness = brightness
41
+ def __init__ (self ):
51
42
self ._auto_write = True
52
- self ._display = dotstar .DotStar (clock , data , self .rows * self .columns ,
53
- brightness = self ._brightness , auto_write = False )
54
43
55
44
def __setitem__ (self , indices , value ):
56
45
"""
@@ -96,6 +85,88 @@ def _get_index(self, indices):
96
85
else :
97
86
raise ValueError ('Index must be 1 or 2 number' )
98
87
88
+ def _update (self ):
89
+ """
90
+ Update the Display automatically if auto_write is set to True
91
+ """
92
+ if self ._auto_write :
93
+ self ._display .show ()
94
+
95
+ def _fill (self , color = 0 ):
96
+ """
97
+ Fills all of the Pixels with a color or unlit if empty.
98
+ """
99
+ self ._display .fill (color )
100
+ self ._update ()
101
+
102
+ def _show (self ):
103
+ """
104
+ Update the Pixels. This is only needed if auto_write is set to False
105
+ This can be very useful for more advanced graphics effects.
106
+ """
107
+ self ._display .show ()
108
+
109
+ def _shift_right (self , rotate = False ):
110
+ """
111
+ Shift all pixels right
112
+ """
113
+ for y in range (0 , self .rows ):
114
+ last_pixel = self ._display [(y + 1 ) * self .columns - 1 ] if rotate else 0
115
+ for x in range (self .columns - 1 , 0 , - 1 ):
116
+ self ._display [y * self .columns + x ] = self ._display [y * self .columns + x - 1 ]
117
+ self ._display [y * self .columns ] = last_pixel
118
+ self ._update ()
119
+
120
+ def _shift_left (self , rotate = False ):
121
+ """
122
+ Shift all pixels left
123
+ """
124
+ for y in range (0 , self .rows ):
125
+ last_pixel = self ._display [y * self .columns ] if rotate else 0
126
+ for x in range (0 , self .columns - 1 ):
127
+ self ._display [y * self .columns + x ] = self ._display [y * self .columns + x + 1 ]
128
+ self ._display [(y + 1 ) * self .columns - 1 ] = last_pixel
129
+ self ._update ()
130
+
131
+ def _shift_up (self , rotate = False ):
132
+ """
133
+ Shift all pixels up
134
+ """
135
+ for x in range (0 , self .columns ):
136
+ last_pixel = self ._display [(self .rows - 1 ) * self .columns + x ] if rotate else 0
137
+ for y in range (self .rows - 1 , 0 , - 1 ):
138
+ self ._display [y * self .columns + x ] = self ._display [(y - 1 ) * self .columns + x ]
139
+ self ._display [x ] = last_pixel
140
+ self ._update ()
141
+
142
+ def _shift_down (self , rotate = False ):
143
+ """
144
+ Shift all pixels down
145
+ """
146
+ for x in range (0 , self .columns ):
147
+ last_pixel = self ._display [x ] if rotate else 0
148
+ for y in range (0 , self .rows - 1 ):
149
+ self ._display [y * self .columns + x ] = self ._display [(y + 1 ) * self .columns + x ]
150
+ self ._display [(self .rows - 1 ) * self .columns + x ] = last_pixel
151
+ self ._update ()
152
+
153
+ class DotStarFeatherWing (PixelDisplayFeatherWing ):
154
+ """Class representing a `DotStar FeatherWing
155
+ <https://www.adafruit.com/product/3449>`_.
156
+
157
+ The feather uses pins D13 and D11"""
158
+ def __init__ (self , clock = board .D13 , data = board .D11 , brightness = 0.2 ):
159
+ """
160
+ :param pin clock: The clock pin for the featherwing
161
+ :param pin data: The data pin for the featherwing
162
+ :param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
163
+ """
164
+ self .rows = 6
165
+ self .columns = 12
166
+ self ._display = dotstar .DotStar (clock , data , self .rows * self .columns ,
167
+ brightness = brightness , auto_write = False )
168
+ super ().__init__ ()
169
+
99
170
def fill (self , color = 0 ):
100
171
"""
101
172
Fills all of the DotStars with a color or unlit if empty.
@@ -120,8 +191,7 @@ def fill(self, color=0):
120
191
dotstar.fill() # Clear all lit DotStars
121
192
122
193
"""
123
- self ._display .fill (color )
124
- self ._update ()
194
+ super ()._fill (color )
125
195
126
196
def show (self ):
127
197
"""
@@ -143,7 +213,7 @@ def show(self):
143
213
dotstar.show() # Update the DotStars
144
214
145
215
"""
146
- self . _display . show ()
216
+ super (). _show ()
147
217
148
218
def shift_right (self , rotate = False ):
149
219
"""
@@ -176,12 +246,7 @@ def shift_right(self, rotate=False):
176
246
time.sleep(.1)
177
247
178
248
"""
179
- for y in range (0 , self .rows ):
180
- last_pixel = self ._display [(y + 1 ) * self .columns - 1 ] if rotate else 0
181
- for x in range (self .columns - 1 , 0 , - 1 ):
182
- self ._display [y * self .columns + x ] = self ._display [y * self .columns + x - 1 ]
183
- self ._display [y * self .columns ] = last_pixel
184
- self ._update ()
249
+ super ()._shift_right (rotate )
185
250
186
251
def shift_left (self , rotate = False ):
187
252
"""
@@ -214,12 +279,7 @@ def shift_left(self, rotate=False):
214
279
time.sleep(.1)
215
280
216
281
"""
217
- for y in range (0 , self .rows ):
218
- last_pixel = self ._display [y * self .columns ] if rotate else 0
219
- for x in range (0 , self .columns - 1 ):
220
- self ._display [y * self .columns + x ] = self ._display [y * self .columns + x + 1 ]
221
- self ._display [(y + 1 ) * self .columns - 1 ] = last_pixel
222
- self ._update ()
282
+ super ()._shift_left (rotate )
223
283
224
284
def shift_up (self , rotate = False ):
225
285
"""
@@ -252,12 +312,7 @@ def shift_up(self, rotate=False):
252
312
time.sleep(.1)
253
313
254
314
"""
255
- for x in range (0 , self .columns ):
256
- last_pixel = self ._display [(self .rows - 1 ) * self .columns + x ] if rotate else 0
257
- for y in range (self .rows - 1 , 0 , - 1 ):
258
- self ._display [y * self .columns + x ] = self ._display [(y - 1 ) * self .columns + x ]
259
- self ._display [x ] = last_pixel
260
- self ._update ()
315
+ super ()._shift_up (rotate )
261
316
262
317
def shift_down (self , rotate = False ):
263
318
"""
@@ -290,19 +345,7 @@ def shift_down(self, rotate=False):
290
345
time.sleep(.1)
291
346
292
347
"""
293
- for x in range (0 , self .columns ):
294
- last_pixel = self ._display [x ] if rotate else 0
295
- for y in range (0 , self .rows - 1 ):
296
- self ._display [y * self .columns + x ] = self ._display [(y + 1 ) * self .columns + x ]
297
- self ._display [(self .rows - 1 ) * self .columns + x ] = last_pixel
298
- self ._update ()
299
-
300
- def _update (self ):
301
- """
302
- Update the Display automatically if auto_write is set to True
303
- """
304
- if self ._auto_write :
305
- self ._display .show ()
348
+ super ()._shift_down (rotate )
306
349
307
350
@property
308
351
def auto_write (self ):
0 commit comments