Skip to content

Commit f16b992

Browse files
author
Melissa LeBlanc-Williams
committed
Merge branch 'neopixel2'
2 parents b3cd6e3 + 570bfe9 commit f16b992

File tree

2 files changed

+120
-65
lines changed

2 files changed

+120
-65
lines changed

adafruit_featherwing/dotstar_featherwing.py

Lines changed: 95 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,17 @@
3333

3434
import board
3535
import adafruit_dotstar as dotstar
36+
#pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation
3637

37-
class DotStarFeatherWing:
38-
"""Class representing a `DotStar FeatherWing
39-
<https://www.adafruit.com/product/3449>`_.
38+
class PixelMatrixFeatherWing:
39+
"""Base Class for DotStar and NeoPixel FeatherWings
4040
4141
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
42+
def __init__(self):
43+
self.rows = 0
44+
self.columns = 0
45+
self._matrix = None
5046
self._auto_write = True
51-
self._matrix = dotstar.DotStar(clock, data, self.rows * self.columns,
52-
brightness=brightness, auto_write=False)
5347

5448
def __setitem__(self, indices, value):
5549
"""
@@ -95,6 +89,88 @@ def _get_index(self, indices):
9589
else:
9690
raise ValueError('Index must be 1 or 2 number')
9791

92+
def _update(self):
93+
"""
94+
Update the Display automatically if auto_write is set to True
95+
"""
96+
if self._auto_write:
97+
self._matrix.show()
98+
99+
def _fill(self, color=0):
100+
"""
101+
Fills all of the Pixels with a color or unlit if empty.
102+
"""
103+
self._matrix.fill(color)
104+
self._update()
105+
106+
def _show(self):
107+
"""
108+
Update the Pixels. This is only needed if auto_write is set to False
109+
This can be very useful for more advanced graphics effects.
110+
"""
111+
self._matrix.show()
112+
113+
def _shift_right(self, rotate=False):
114+
"""
115+
Shift all pixels right
116+
"""
117+
for y in range(0, self.rows):
118+
last_pixel = self._matrix[(y + 1) * self.columns - 1] if rotate else 0
119+
for x in range(self.columns - 1, 0, -1):
120+
self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x - 1]
121+
self._matrix[y * self.columns] = last_pixel
122+
self._update()
123+
124+
def _shift_left(self, rotate=False):
125+
"""
126+
Shift all pixels left
127+
"""
128+
for y in range(0, self.rows):
129+
last_pixel = self._matrix[y * self.columns] if rotate else 0
130+
for x in range(0, self.columns - 1):
131+
self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x + 1]
132+
self._matrix[(y + 1) * self.columns - 1] = last_pixel
133+
self._update()
134+
135+
def _shift_up(self, rotate=False):
136+
"""
137+
Shift all pixels up
138+
"""
139+
for x in range(0, self.columns):
140+
last_pixel = self._matrix[(self.rows - 1) * self.columns + x] if rotate else 0
141+
for y in range(self.rows - 1, 0, -1):
142+
self._matrix[y * self.columns + x] = self._matrix[(y - 1) * self.columns + x]
143+
self._matrix[x] = last_pixel
144+
self._update()
145+
146+
def _shift_down(self, rotate=False):
147+
"""
148+
Shift all pixels down
149+
"""
150+
for x in range(0, self.columns):
151+
last_pixel = self._matrix[x] if rotate else 0
152+
for y in range(0, self.rows - 1):
153+
self._matrix[y * self.columns + x] = self._matrix[(y + 1) * self.columns + x]
154+
self._matrix[(self.rows - 1) * self.columns + x] = last_pixel
155+
self._update()
156+
157+
class DotStarFeatherWing(PixelMatrixFeatherWing):
158+
"""Class representing a `DotStar FeatherWing
159+
<https://www.adafruit.com/product/3449>`_.
160+
161+
The feather uses pins D13 and D11"""
162+
def __init__(self, clock=board.D13, data=board.D11, brightness=0.2):
163+
"""
164+
:param pin clock: The clock pin for the featherwing
165+
:param pin data: The data pin for the featherwing
166+
:param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
167+
"""
168+
super().__init__()
169+
self.rows = 6
170+
self.columns = 12
171+
self._matrix = dotstar.DotStar(clock, data, self.rows * self.columns,
172+
brightness=brightness, auto_write=False)
173+
98174
def fill(self, color=0):
99175
"""
100176
Fills all of the DotStars with a color or unlit if empty.
@@ -119,8 +195,7 @@ def fill(self, color=0):
119195
dotstar.fill() # Clear all lit DotStars
120196
121197
"""
122-
self._matrix.fill(color)
123-
self._update()
198+
super()._fill(color)
124199

125200
def show(self):
126201
"""
@@ -142,7 +217,7 @@ def show(self):
142217
dotstar.show() # Update the DotStars
143218
144219
"""
145-
self._matrix.show()
220+
super()._show()
146221

147222
def shift_right(self, rotate=False):
148223
"""
@@ -175,12 +250,7 @@ def shift_right(self, rotate=False):
175250
time.sleep(.1)
176251
177252
"""
178-
for y in range(0, self.rows):
179-
last_pixel = self._matrix[(y + 1) * self.columns - 1] if rotate else 0
180-
for x in range(self.columns - 1, 0, -1):
181-
self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x - 1]
182-
self._matrix[y * self.columns] = last_pixel
183-
self._update()
253+
super()._shift_right(rotate)
184254

185255
def shift_left(self, rotate=False):
186256
"""
@@ -213,12 +283,7 @@ def shift_left(self, rotate=False):
213283
time.sleep(.1)
214284
215285
"""
216-
for y in range(0, self.rows):
217-
last_pixel = self._matrix[y * self.columns] if rotate else 0
218-
for x in range(0, self.columns - 1):
219-
self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x + 1]
220-
self._matrix[(y + 1) * self.columns - 1] = last_pixel
221-
self._update()
286+
super()._shift_left(rotate)
222287

223288
def shift_up(self, rotate=False):
224289
"""
@@ -251,12 +316,7 @@ def shift_up(self, rotate=False):
251316
time.sleep(.1)
252317
253318
"""
254-
for x in range(0, self.columns):
255-
last_pixel = self._matrix[(self.rows - 1) * self.columns + x] if rotate else 0
256-
for y in range(self.rows - 1, 0, -1):
257-
self._matrix[y * self.columns + x] = self._matrix[(y - 1) * self.columns + x]
258-
self._matrix[x] = last_pixel
259-
self._update()
319+
super()._shift_up(rotate)
260320

261321
def shift_down(self, rotate=False):
262322
"""
@@ -289,19 +349,7 @@ def shift_down(self, rotate=False):
289349
time.sleep(.1)
290350
291351
"""
292-
for x in range(0, self.columns):
293-
last_pixel = self._matrix[x] if rotate else 0
294-
for y in range(0, self.rows - 1):
295-
self._matrix[y * self.columns + x] = self._matrix[(y + 1) * self.columns + x]
296-
self._matrix[(self.rows - 1) * self.columns + x] = last_pixel
297-
self._update()
298-
299-
def _update(self):
300-
"""
301-
Update the Display automatically if auto_write is set to True
302-
"""
303-
if self._auto_write:
304-
self._matrix.show()
352+
super()._shift_down(rotate)
305353

306354
@property
307355
def auto_write(self):

adafruit_featherwing/neopixel_featherwing.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,26 @@
3333

3434
import board
3535
import neopixel
36-
from adafruit_featherwing.dotstar_featherwing import DotStarFeatherWing
36+
from adafruit_featherwing.dotstar_featherwing import PixelMatrixFeatherWing
37+
#pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation
3738

38-
class NeoPixelFeatherWing(DotStarFeatherWing):
39+
class NeoPixelFeatherWing(PixelMatrixFeatherWing):
3940
"""Class representing a `NeoPixel FeatherWing
4041
<https://www.adafruit.com/product/2945>`_.
4142
4243
The feather uses pins D6 by default"""
43-
#pylint: disable-msg=super-init-not-called
4444
def __init__(self, pixel_pin=board.D6, brightness=0.1):
4545
"""
4646
:param pin pixel_pin: The pin for the featherwing
4747
:param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
4848
"""
49+
super().__init__()
4950
self.rows = 4
5051
self.columns = 8
51-
self._auto_write = True
52-
self._display = neopixel.NeoPixel(pixel_pin, self.rows * self.columns,
53-
brightness=brightness, auto_write=False,
54-
pixel_order=neopixel.GRB)
55-
#pylint: enable-msg=super-init-not-called
52+
self._matrix = neopixel.NeoPixel(pixel_pin, self.rows * self.columns,
53+
brightness=brightness, auto_write=False,
54+
pixel_order=neopixel.GRB)
5655

57-
#pylint: disable-msg=useless-super-delegation
5856
def fill(self, color=0):
5957
"""
6058
Fills all of the NeoPixels with a color or unlit if empty.
@@ -77,7 +75,7 @@ def fill(self, color=0):
7775
neopixel.fill() # Clear all lit NeoPixels
7876
7977
"""
80-
super().fill(color)
78+
super()._fill(color)
8179

8280
def show(self):
8381
"""
@@ -99,7 +97,7 @@ def show(self):
9997
neopixel.show() # Update the NeoPixels
10098
10199
"""
102-
super().show()
100+
super()._show()
103101

104102
def shift_right(self, rotate=False):
105103
"""
@@ -132,7 +130,7 @@ def shift_right(self, rotate=False):
132130
time.sleep(.1)
133131
134132
"""
135-
super().shift_right(rotate)
133+
super()._shift_right(rotate)
136134

137135
def shift_left(self, rotate=False):
138136
"""
@@ -165,8 +163,7 @@ def shift_left(self, rotate=False):
165163
time.sleep(.1)
166164
167165
"""
168-
super().shift_left(rotate)
169-
#pylint: enable-msg=useless-super-delegation
166+
super()._shift_left(rotate)
170167

171168
def shift_up(self, rotate=False):
172169
"""
@@ -199,7 +196,7 @@ def shift_up(self, rotate=False):
199196
time.sleep(.1)
200197
201198
"""
202-
super().shift_down(rotate) # Up and down are reversed
199+
super()._shift_down(rotate) # Up and down are reversed
203200

204201
def shift_down(self, rotate=False):
205202
"""
@@ -232,7 +229,7 @@ def shift_down(self, rotate=False):
232229
time.sleep(.1)
233230
234231
"""
235-
super().shift_up(rotate) # Up and down are reversed
232+
super()._shift_up(rotate) # Up and down are reversed
236233

237234
@property
238235
def auto_write(self):
@@ -258,7 +255,12 @@ def auto_write(self):
258255
neopixel.show() # Update the NeoPixels
259256
260257
"""
261-
return super().auto_write
258+
return self._auto_write
259+
260+
@auto_write.setter
261+
def auto_write(self, write):
262+
if isinstance(write, bool):
263+
self._auto_write = write
262264

263265
@property
264266
def brightness(self):
@@ -282,4 +284,9 @@ def brightness(self):
282284
neopixel.brightness = 0.3
283285
284286
"""
285-
return super().brightness
287+
return self._matrix.brightness
288+
289+
@brightness.setter
290+
def brightness(self, brightness):
291+
self._matrix.brightness = min(max(brightness, 0.0), 1.0)
292+
self._update()

0 commit comments

Comments
 (0)