34
34
import board
35
35
import adafruit_dotstar as dotstar
36
36
37
+ # Library Function Ideas
38
+ # Gradient (probably just for example)
39
+ # 20 40 60 80 100
40
+
37
41
class DotStarFeatherWing :
38
42
"""Class representing a `DotStar FeatherWing
39
43
<https://www.adafruit.com/product/3449>`_.
@@ -48,21 +52,168 @@ def __init__(self, clock=board.D13, data=board.D11, brightness=0.2):
48
52
self .rows = 6
49
53
self .columns = 12
50
54
self ._brightness = brightness
55
+ self ._auto_write = True
51
56
self ._dotstar = dotstar .DotStar (clock , data , self .rows * self .columns ,
52
- brightness = self ._brightness )
57
+ brightness = self ._brightness , auto_write = False )
53
58
54
59
def __setitem__ (self , indices , value ):
55
- x , y = indices
56
- self ._dotstar [y * self .columns + x ] = value
60
+ """
61
+ indices can be one of two things:
62
+ x and y ints that are calculated to the DotStar index
63
+ a single int that specifies the DotStar index
64
+ value can be one of three things:
65
+ a (r,g,b) list/tuple
66
+ a (r,g,b, brightness) list/tuple
67
+ a single, longer int that contains RGB values, like 0xFFFFFF
68
+ brightness, if specified should be a float 0-1
69
+ """
70
+ self ._dotstar [self ._get_index (indices )] = value
71
+ self ._update ()
57
72
58
73
def __getitem__ (self , indices ):
59
- x , y = indices
60
- return self ._dotstar [y * self .columns + x ]
74
+ return self ._dotstar [self ._get_index (indices )]
75
+
76
+ def _get_index (self , indices ):
77
+ if isinstance (indices , int ):
78
+ if not 0 <= indices < self .rows * self .columns :
79
+ raise ValueError ('The index of {} is out of range' .format (indices ))
80
+ return indices
81
+ elif isinstance (indices , slice ):
82
+ return indices
83
+ elif len (indices ) == 2 :
84
+ x , y = indices
85
+ if not 0 <= x < self .columns :
86
+ raise ValueError ('The X value of {} is out of range' .format (x ))
87
+ if not 0 <= y < self .rows :
88
+ raise ValueError ('The Y value of {} is out of range' .format (y ))
89
+ return y * self .columns + x
90
+ else :
91
+ raise ValueError ('Index must be 1 or 2 number' )
92
+
93
+ def fill (self , color = 0 ):
94
+ """
95
+ Fills all of the DotStars with a color or unlit if empty.
96
+
97
+ :param color: (Optional) The text or number to display (default=0)
98
+ :type color: list/tuple or int
99
+
100
+ This example shows various ways of using the fill() function
101
+
102
+ .. code-block:: python
61
103
62
- def fill (self , color ):
63
- """Fills the wing with a color.
64
- Does NOT update the LEDs.
104
+ import time
105
+ from adafruit_featherwing import dotstar_featherwing
106
+
107
+ dotstar = dotstar_featherwing.DotStarFeatherWing()
108
+ dotstar.fill((255, 255, 255)) # Fill White
109
+ time.sleep(1)
110
+ dotstar.fill((255, 255, 255, 0.5)) # Fill White Half Brightness
111
+ time.sleep(1)
112
+ dotstar.fill(0xFF0000) # Fill Red
113
+ time.sleep(1)
114
+ dotstar.fill() # Clear all lit DotStars
65
115
66
- :param (int, int, int) color: the color to fill with
67
116
"""
68
117
self ._dotstar .fill (color )
118
+ self ._update ()
119
+
120
+ def show (self ):
121
+ """
122
+ Update the DotStars. This is only needed if auto_write is set to False
123
+ This can be very useful for more advanced graphics effects.
124
+
125
+ This example changes the blink rate and prints out the current setting
126
+
127
+ .. code-block:: python
128
+
129
+ import time
130
+ from adafruit_featherwing import dotstar_featherwing
131
+
132
+ dotstar = dotstar_featherwing.DotStarFeatherWing()
133
+ dotstar.fill() # Clear any lit Dotstars
134
+ dotstar.auto_write = False
135
+ dotstar[0, 0] = (255, 255, 255) # Set White
136
+ time.sleep(1)
137
+ dotstar.show() # Update the DotStars
138
+
139
+ """
140
+ self ._dotstar .show ()
141
+
142
+ def shift_right (self , rotate = False ):
143
+ """
144
+ Shift all pixels right
145
+
146
+ :param rotate: (Optional) Rotate the shifted pixel to the beginning (default=False)
147
+
148
+ This example shifts pixels
149
+
150
+ .. code-block:: python
151
+
152
+ import time
153
+ from adafruit_featherwing import dotstar_featherwing
154
+
155
+ dotstar = dotstar_featherwing.DotStarFeatherWing()
156
+ dotstar.fill() # Clear any lit Dotstars
157
+ dotstar.auto_write = False
158
+ dotstar[0, 0] = (255, 255, 255) # Set White
159
+ time.sleep(1)
160
+ dotstar.show() # Update the DotStars
161
+
162
+ """
163
+ for y in range (0 , self .rows ):
164
+ for x in range (self .columns - 1 , 0 , - 1 ):
165
+ self ._dotstar [y * self .columns + x ] = self ._dotstar [y * self .columns + x - 1 ]
166
+ lastPixel = self ._dotstar [(y + 1 ) * self .columns - 1 ] if rotate else 0
167
+ self ._dotstar [y * self .columns ] = lastPixel
168
+ self ._update ()
169
+
170
+ def shift_left (self , rotate = False ):
171
+ """Shift all pixels left"""
172
+ for y in range (0 , self .rows ):
173
+ for x in range (0 , self .columns - 1 ):
174
+ self ._dotstar [y * self .columns + x ] = self ._dotstar [y * self .columns + x + 1 ]
175
+ lastPixel = self ._dotstar [y * self .columns ] if rotate else 0
176
+ self ._dotstar [(y + 1 ) * self .columns - 1 ] = lastPixel
177
+ self ._update ()
178
+
179
+ def shift_up (self , rotate = False ):
180
+ """Shift all pixels up"""
181
+ for x in range (0 , self .columns ):
182
+ for y in range (self .rows - 1 , 0 , - 1 ):
183
+ self ._dotstar [y * self .columns + x ] = self ._dotstar [(y - 1 ) * self .columns + x ]
184
+ lastPixel = self ._dotstar [(self .rows - 1 ) * self .columns + x ] if rotate else 0
185
+ self ._dotstar [x ] = lastPixel
186
+ self ._update ()
187
+
188
+ def shift_down (self , rotate = False ):
189
+ """Shift all pixels down"""
190
+ for x in range (0 , self .columns ):
191
+ for y in range (0 , self .rows - 1 ):
192
+ self ._dotstar [y * self .columns + x ] = self ._dotstar [(y + 1 ) * self .columns + x ]
193
+ lastPixel = self ._dotstar [x ] if rotate else 0
194
+ self ._dotstar [(self .rows - 1 ) * self .columns + x ] = lastPixel
195
+ self ._update ()
196
+
197
+ def _update (self ):
198
+ if self ._auto_write :
199
+ self ._dotstar .show ()
200
+
201
+ @property
202
+ def auto_write (self ):
203
+ """Whether or not we are automatically updating
204
+ If set to false, be sure to call show() to update"""
205
+ return self ._auto_write
206
+
207
+ @auto_write .setter
208
+ def auto_write (self , write ):
209
+ if isinstance (write , bool ):
210
+ self ._auto_write = write
211
+
212
+ @property
213
+ def brightness (self ):
214
+ """Overall brightness of the display"""
215
+ return self ._dotstar .brightness
216
+
217
+ @brightness .setter
218
+ def brightness (self , brightness ):
219
+ self ._dotstar .brightness = min (max (brightness , 0.0 ), 1.0 )
0 commit comments