Skip to content

Commit 4912a0b

Browse files
author
Melissa LeBlanc-Williams
committed
Added DocBlock Examples. Fixed Shifting Bug.
1 parent a1a2ba2 commit 4912a0b

File tree

2 files changed

+169
-19
lines changed

2 files changed

+169
-19
lines changed

adafruit_featherwing/dotstar_featherwing.py

Lines changed: 167 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ def __init__(self, clock=board.D13, data=board.D11, brightness=0.2):
5454

5555
def __setitem__(self, indices, value):
5656
"""
57-
indices can be one of two things:
57+
indices can be one of three things:
5858
x and y ints that are calculated to the DotStar index
59+
a slice of DotStar indexes with a set of values that match the slice
5960
a single int that specifies the DotStar index
6061
value can be one of three things:
6162
a (r,g,b) list/tuple
@@ -67,9 +68,18 @@ def __setitem__(self, indices, value):
6768
self._update()
6869

6970
def __getitem__(self, indices):
71+
"""
72+
indices can be one of three things:
73+
x and y ints that are calculated to the DotStar index
74+
a slice of DotStar indexes to retrieve
75+
a single int that specifies the DotStar index
76+
"""
7077
return self._dotstar[self._get_index(indices)]
7178

7279
def _get_index(self, indices):
80+
"""
81+
Figure out which DotStar to address based on what was passed in
82+
"""
7383
if isinstance(indices, int):
7484
if not 0 <= indices < self.rows * self.columns:
7585
raise ValueError('The index of {} is out of range'.format(indices))
@@ -139,65 +149,185 @@ def shift_right(self, rotate=False):
139149
"""
140150
Shift all pixels right
141151
142-
:param rotate: (Optional) Rotate the shifted pixel to the beginning (default=False)
152+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
143153
144-
This example shifts pixels
154+
This example shifts 2 pixels to the right
145155
146156
.. code-block:: python
147157
148158
import time
149159
from adafruit_featherwing import dotstar_featherwing
150160
151161
dotstar = dotstar_featherwing.DotStarFeatherWing()
152-
dotstar.fill() # Clear any lit Dotstars
153-
dotstar.auto_write = False
154-
dotstar[0, 0] = (255, 255, 255) # Set White
162+
163+
# Draw Red and Green Pixels
164+
dotstar[5, 3] = (255, 0, 0)
165+
dotstar[6, 3] = (0, 255, 0)
166+
167+
# Rotate it off the screen
168+
for i in range(0, 11):
169+
dotstar.shift_right(True)
170+
time.sleep(.1)
171+
155172
time.sleep(1)
156-
dotstar.show() # Update the DotStars
173+
# Shift it off the screen
174+
for i in range(0, 11):
175+
dotstar.shift_right()
176+
time.sleep(.1)
157177
158178
"""
159179
for y in range(0, self.rows):
180+
last_pixel = self._dotstar[(y + 1) * self.columns - 1] if rotate else 0
160181
for x in range(self.columns - 1, 0, -1):
161182
self._dotstar[y * self.columns + x] = self._dotstar[y * self.columns + x - 1]
162-
last_pixel = self._dotstar[(y + 1) * self.columns - 1] if rotate else 0
163183
self._dotstar[y * self.columns] = last_pixel
164184
self._update()
165185

166186
def shift_left(self, rotate=False):
167-
"""Shift all pixels left"""
187+
"""
188+
Shift all pixels left
189+
190+
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
191+
192+
This example shifts 2 pixels to the left
193+
194+
.. code-block:: python
195+
196+
import time
197+
from adafruit_featherwing import dotstar_featherwing
198+
199+
dotstar = dotstar_featherwing.DotStarFeatherWing()
200+
201+
# Draw Red and Green Pixels
202+
dotstar[5, 3] = (255, 0, 0)
203+
dotstar[6, 3] = (0, 255, 0)
204+
205+
# Rotate it off the screen
206+
for i in range(0, 11):
207+
dotstar.shift_left(True)
208+
time.sleep(.1)
209+
210+
time.sleep(1)
211+
# Shift it off the screen
212+
for i in range(0, 11):
213+
dotstar.shift_left()
214+
time.sleep(.1)
215+
216+
"""
168217
for y in range(0, self.rows):
218+
last_pixel = self._dotstar[y * self.columns] if rotate else 0
169219
for x in range(0, self.columns - 1):
170220
self._dotstar[y * self.columns + x] = self._dotstar[y * self.columns + x + 1]
171-
last_pixel = self._dotstar[y * self.columns] if rotate else 0
172221
self._dotstar[(y + 1) * self.columns - 1] = last_pixel
173222
self._update()
174223

175224
def shift_up(self, rotate=False):
176-
"""Shift all pixels up"""
225+
"""
226+
Shift all pixels up
227+
228+
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
229+
230+
This example shifts 2 pixels up
231+
232+
.. code-block:: python
233+
234+
import time
235+
from adafruit_featherwing import dotstar_featherwing
236+
237+
dotstar = dotstar_featherwing.DotStarFeatherWing()
238+
239+
# Draw Red and Green Pixels
240+
dotstar[5, 3] = (255, 0, 0)
241+
dotstar[6, 3] = (0, 255, 0)
242+
243+
# Rotate it off the screen
244+
for i in range(0, 5):
245+
dotstar.shift_up(True)
246+
time.sleep(.1)
247+
248+
time.sleep(1)
249+
# Shift it off the screen
250+
for i in range(0, 5):
251+
dotstar.shift_up()
252+
time.sleep(.1)
253+
254+
"""
177255
for x in range(0, self.columns):
256+
last_pixel = self._dotstar[(self.rows - 1) * self.columns + x] if rotate else 0
178257
for y in range(self.rows - 1, 0, -1):
179258
self._dotstar[y * self.columns + x] = self._dotstar[(y - 1) * self.columns + x]
180-
last_pixel = self._dotstar[(self.rows - 1) * self.columns + x] if rotate else 0
181259
self._dotstar[x] = last_pixel
182260
self._update()
183261

184262
def shift_down(self, rotate=False):
185-
"""Shift all pixels down"""
263+
"""
264+
Shift all pixels down
265+
266+
:param rotate: (Optional) Rotate the shifted pixels to top (default=False)
267+
268+
This example shifts 2 pixels down
269+
270+
.. code-block:: python
271+
272+
import time
273+
from adafruit_featherwing import dotstar_featherwing
274+
275+
dotstar = dotstar_featherwing.DotStarFeatherWing()
276+
277+
# Draw Red and Green Pixels
278+
dotstar[5, 3] = (255, 0, 0)
279+
dotstar[6, 3] = (0, 255, 0)
280+
281+
# Rotate it off the screen
282+
for i in range(0, 5):
283+
dotstar.shift_down(True)
284+
time.sleep(.1)
285+
286+
time.sleep(1)
287+
# Shift it off the screen
288+
for i in range(0, 5):
289+
dotstar.shift_down()
290+
time.sleep(.1)
291+
292+
"""
186293
for x in range(0, self.columns):
294+
last_pixel = self._dotstar[x] if rotate else 0
187295
for y in range(0, self.rows - 1):
188296
self._dotstar[y * self.columns + x] = self._dotstar[(y + 1) * self.columns + x]
189-
last_pixel = self._dotstar[x] if rotate else 0
190297
self._dotstar[(self.rows - 1) * self.columns + x] = last_pixel
191298
self._update()
192299

193300
def _update(self):
301+
"""
302+
Update the Display automatically if auto_write is set to True
303+
"""
194304
if self._auto_write:
195305
self._dotstar.show()
196306

197307
@property
198308
def auto_write(self):
199-
"""Whether or not we are automatically updating
200-
If set to false, be sure to call show() to update"""
309+
"""
310+
Whether or not we are automatically updating
311+
If set to false, be sure to call show() to update
312+
313+
This lights DotStars with and without auto_write
314+
315+
.. code-block:: python
316+
317+
import time
318+
from adafruit_featherwing import dotstar_featherwing
319+
320+
dotstar = dotstar_featherwing.DotStarFeatherWing()
321+
dotstar.fill() # Clear any lit Dotstars
322+
dotstar[0, 0] = (255, 255, 255) # Set White
323+
time.sleep(1)
324+
325+
dotstar.auto_write = False
326+
dotstar[1, 0] = (255, 255, 255) # Set White
327+
time.sleep(1)
328+
dotstar.show() # Update the DotStars
329+
330+
"""
201331
return self._auto_write
202332

203333
@auto_write.setter
@@ -207,9 +337,29 @@ def auto_write(self, write):
207337

208338
@property
209339
def brightness(self):
210-
"""Overall brightness of the display"""
340+
"""
341+
Overall brightness of the display
342+
343+
This example changes the brightness
344+
345+
.. code-block:: python
346+
347+
import time
348+
from adafruit_featherwing import dotstar_featherwing
349+
350+
dotstar = dotstar_featherwing.DotStarFeatherWing()
351+
dotstar.brightness = 0
352+
dotstar.fill(0xFFFFFF)
353+
for i in range(0, 6):
354+
dotstar.brightness = (i / 10)
355+
time.sleep(.2)
356+
357+
dotstar.brightness = 0.3
358+
359+
"""
211360
return self._dotstar.brightness
212361

213362
@brightness.setter
214363
def brightness(self, brightness):
215364
self._dotstar.brightness = min(max(brightness, 0.0), 1.0)
365+
self._update()

examples/featherwing_dotstar_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def random_color():
4747
dotstar.show()
4848
dotstar.auto_write = True
4949

50-
# Shift without rotating in bits for an animated screen wipe
50+
# Shift pixels without rotating for an animated screen wipe
5151
for i in range(0, 6):
52-
dotstar.shift_up()
52+
dotstar.shift_down()
5353

5454
# Show pixels in random locations of random color
5555
# Bottom left corner is (0,0)

0 commit comments

Comments
 (0)