Skip to content

Commit a6bbcca

Browse files
author
Melissa LeBlanc-Williams
committed
Restructured DotStar and NeoPixel to use common base class
1 parent 814b0af commit a6bbcca

File tree

4 files changed

+165
-61
lines changed

4 files changed

+165
-61
lines changed

adafruit_featherwing/dotstar_featherwing.py

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,12 @@
3434
import board
3535
import adafruit_dotstar as dotstar
3636

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
4039
4140
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):
5142
self._auto_write = True
52-
self._display = dotstar.DotStar(clock, data, self.rows * self.columns,
53-
brightness=self._brightness, auto_write=False)
5443

5544
def __setitem__(self, indices, value):
5645
"""
@@ -96,6 +85,88 @@ def _get_index(self, indices):
9685
else:
9786
raise ValueError('Index must be 1 or 2 number')
9887

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+
99170
def fill(self, color=0):
100171
"""
101172
Fills all of the DotStars with a color or unlit if empty.
@@ -120,8 +191,7 @@ def fill(self, color=0):
120191
dotstar.fill() # Clear all lit DotStars
121192
122193
"""
123-
self._display.fill(color)
124-
self._update()
194+
super()._fill(color)
125195

126196
def show(self):
127197
"""
@@ -143,7 +213,7 @@ def show(self):
143213
dotstar.show() # Update the DotStars
144214
145215
"""
146-
self._display.show()
216+
super()._show()
147217

148218
def shift_right(self, rotate=False):
149219
"""
@@ -176,12 +246,7 @@ def shift_right(self, rotate=False):
176246
time.sleep(.1)
177247
178248
"""
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)
185250

186251
def shift_left(self, rotate=False):
187252
"""
@@ -214,12 +279,7 @@ def shift_left(self, rotate=False):
214279
time.sleep(.1)
215280
216281
"""
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)
223283

224284
def shift_up(self, rotate=False):
225285
"""
@@ -252,12 +312,7 @@ def shift_up(self, rotate=False):
252312
time.sleep(.1)
253313
254314
"""
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)
261316

262317
def shift_down(self, rotate=False):
263318
"""
@@ -290,19 +345,7 @@ def shift_down(self, rotate=False):
290345
time.sleep(.1)
291346
292347
"""
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)
306349

307350
@property
308351
def auto_write(self):

adafruit_featherwing/neopixel_featherwing.py

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434
import board
3535
import neopixel
36-
from adafruit_featherwing.dotstar_featherwing import DotStarFeatherWing
36+
from adafruit_featherwing.dotstar_featherwing import PixelDisplayFeatherWing
3737

38-
class NeoPixelFeatherWing(DotStarFeatherWing):
38+
class NeoPixelFeatherWing(PixelDisplayFeatherWing):
3939
"""Class representing a `NeoPixel FeatherWing
4040
<https://www.adafruit.com/product/2945>`_.
4141
@@ -51,9 +51,10 @@ def __init__(self, pixel_pin=board.D6, brightness=0.1):
5151

5252
self.rows = 4
5353
self.columns = 8
54-
self._auto_write = True
55-
self._display = neopixel.NeoPixel(pixel_pin, self.rows * self.columns, brightness=brightness,
56-
auto_write=False, pixel_order=neopixel.GRB)
54+
self._display = neopixel.NeoPixel(pixel_pin, self.rows * self.columns,
55+
brightness=brightness, auto_write=False,
56+
pixel_order=neopixel.GRB)
57+
super().__init__()
5758

5859
def fill(self, color=0):
5960
"""
@@ -77,7 +78,7 @@ def fill(self, color=0):
7778
neopixel.fill() # Clear all lit NeoPixels
7879
7980
"""
80-
super().fill(color)
81+
super()._fill(color)
8182

8283
def show(self):
8384
"""
@@ -99,7 +100,7 @@ def show(self):
99100
neopixel.show() # Update the NeoPixels
100101
101102
"""
102-
super().show()
103+
super()._show()
103104

104105
def shift_right(self, rotate=False):
105106
"""
@@ -132,7 +133,7 @@ def shift_right(self, rotate=False):
132133
time.sleep(.1)
133134
134135
"""
135-
super().shift_right(rotate)
136+
super()._shift_right(rotate)
136137

137138
def shift_left(self, rotate=False):
138139
"""
@@ -165,7 +166,7 @@ def shift_left(self, rotate=False):
165166
time.sleep(.1)
166167
167168
"""
168-
super().shift_left(rotate)
169+
super()._shift_left(rotate)
169170

170171
def shift_up(self, rotate=False):
171172
"""
@@ -198,7 +199,7 @@ def shift_up(self, rotate=False):
198199
time.sleep(.1)
199200
200201
"""
201-
super().shift_down(rotate) # Up and down are reversed
202+
super()._shift_down(rotate) # Up and down are reversed
202203

203204
def shift_down(self, rotate=False):
204205
"""
@@ -231,4 +232,64 @@ def shift_down(self, rotate=False):
231232
time.sleep(.1)
232233
233234
"""
234-
super().shift_up(rotate) # Up and down are reversed
235+
super()._shift_up(rotate) # Up and down are reversed
236+
237+
@property
238+
def auto_write(self):
239+
"""
240+
Whether or not we are automatically updating
241+
If set to false, be sure to call show() to update
242+
243+
This lights NeoPixels with and without auto_write
244+
245+
.. code-block:: python
246+
247+
import time
248+
from adafruit_featherwing import neopixel_featherwing
249+
250+
neopixel = neopixel_featherwing.NeoPixelFeatherWing()
251+
neopixel.fill() # Clear any lit NeoPixels
252+
neopixel[0, 0] = (255, 255, 255) # Set White
253+
time.sleep(1)
254+
255+
neopixel.auto_write = False
256+
neopixel[1, 0] = (255, 255, 255) # Set White
257+
time.sleep(1)
258+
neopixel.show() # Update the NeoPixels
259+
260+
"""
261+
return self._auto_write
262+
263+
@auto_write.setter
264+
def auto_write(self, write):
265+
if isinstance(write, bool):
266+
self._auto_write = write
267+
268+
@property
269+
def brightness(self):
270+
"""
271+
Overall brightness of the display
272+
273+
This example changes the brightness
274+
275+
.. code-block:: python
276+
277+
import time
278+
from adafruit_featherwing import neopixel_featherwing
279+
280+
neopixel = neopixel_featherwing.NeoPixelFeatherWing()
281+
neopixel.brightness = 0
282+
neopixel.fill(0xFFFFFF)
283+
for i in range(0, 6):
284+
neopixel.brightness = (i / 10)
285+
time.sleep(.2)
286+
287+
neopixel.brightness = 0.3
288+
289+
"""
290+
return self._display.brightness
291+
292+
@brightness.setter
293+
def brightness(self, brightness):
294+
self._display.brightness = min(max(brightness, 0.0), 1.0)
295+
self._update()

docs/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ Other tests
3030
:caption: examples/featherwing_dotstar_palette_example.py
3131
:linenos:
3232

33-
.. literalinclude:: ../examples/featherwing_neopixel_palettetest.py
33+
.. literalinclude:: ../examples/featherwing_neopixel_palette_example.py
3434
:caption: examples/featherwing_neopixel_palette_example.py
3535
:linenos:

examples/featherwing_dotstar_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ def random_color():
5757
x = random.randrange(0, dotstar.columns)
5858
y = random.randrange(0, dotstar.rows)
5959
dotstar[x, y] = (random_color(), random_color(), random_color())
60-
sleep(.1)
60+
sleep(.1)

0 commit comments

Comments
 (0)