24
24
# THE SOFTWARE.
25
25
26
26
"""
27
- `adafruit_dotstar` - DotStar strip driver (for CircuitPython 4 .0+ with _pixelbuf)
27
+ `adafruit_dotstar` - DotStar strip driver (for CircuitPython 5 .0+ with _pixelbuf)
28
28
=================================================================================
29
29
30
- * Author(s): Damien P. George, Limor Fried, Scott Shawcroft, Roy Hooper
30
+ * Author(s): Damien P. George, Limor Fried, Scott Shawcroft & Roy Hooper
31
31
"""
32
32
import busio
33
33
import digitalio
34
34
try :
35
- from _pixelbuf import PixelBuf , RGBD as RGB , RBGD as RBG , GRBD as GRB , GBRD as GBR , BRGD as BRG , BGRD as BGR
35
+ from _pixelbuf import PixelBuf
36
36
except ImportError :
37
- from pypixelbuf import PixelBuf , RGB , RBG , GRB , GBR , BRG , BGR , LRGB , LRBG , LGRB , LGBR , LBRG , LBGR
37
+ from pypixelbuf import PixelBuf
38
38
39
39
__version__ = "0.0.0-auto.0"
40
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git"
41
41
42
42
START_HEADER_SIZE = 4
43
43
44
+ RBG = 'PRBG'
45
+ RGB = 'PRGB'
46
+ GRB = 'PGRB'
47
+ GBR = 'PGBR'
48
+ BRG = 'PBRG'
49
+ BGR = 'PBGR'
50
+ BGR = 'PBGR'
44
51
45
- class DotStar :
52
+
53
+ class DotStar (PixelBuf ):
46
54
"""
47
55
A sequence of dotstars.
48
56
@@ -52,10 +60,13 @@ class DotStar:
52
60
:param float brightness: Brightness of the pixels between 0.0 and 1.0
53
61
:param bool auto_write: True if the dotstars should immediately change when
54
62
set. If False, `show` must be called explicitly.
55
- :param ByteOrder pixel_order: Set the pixel order on the strip - different
63
+ :param str pixel_order: Set the pixel order on the strip - different
56
64
strips implement this differently. If you send red, and it looks blue
57
- or green on the strip, modify this! It should be one of the values above
58
-
65
+ or green on the strip, modify this! It should be one of the values above.
66
+ :param int baudrate: Desired clock rate if using hardware SPI (ignored if
67
+ using 'soft' SPI). This is only a recommendation; the actual clock
68
+ rate may be slightly different depending on what the system hardware
69
+ can provide.
59
70
60
71
Example for Gemma M0:
61
72
@@ -88,6 +99,7 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True,
88
99
self .cpin .direction = digitalio .Direction .OUTPUT
89
100
self .cpin .value = False
90
101
self .n = n
102
+
91
103
# Supply one extra clock cycle for each two pixels in the strip.
92
104
end_header_size = n // 16
93
105
if n % 16 != 0 :
@@ -106,20 +118,28 @@ def __init__(self, clock, data, n, *, brightness=1.0, auto_write=True,
106
118
for i in range (end_header_index , bufsize ):
107
119
self ._rawbuf [i ] = 0xff
108
120
# Mark the beginnings of each pixel.
109
- # for i in range(START_HEADER_SIZE, end_header_index, 4):
110
- # self._rawbuf[i] = 0xff
121
+ for i in range (START_HEADER_SIZE , end_header_index , 4 ):
122
+ self ._rawbuf [i ] = 0xff
111
123
self ._buf [:] = self ._rawbuf [:]
112
124
113
- writer = self . _spi . write if self ._spi else self . _ds_writebytes
114
- def write_fn ( * _ ):
115
- self . _spi . write ( self . _buf )
125
+ super ( DotStar , self ). __init__ ( n , self ._buf , byteorder = pixel_order ,
126
+ rawbuf = self . _rawbuf , offset = START_HEADER_SIZE ,
127
+ brightness = brightness , auto_write = auto_write )
116
128
117
- self ._pb = PixelBuf (n , self ._buf , byteorder = pixel_order ,
118
- rawbuf = self ._rawbuf , write_function = write_fn , offset = START_HEADER_SIZE ,
119
- write_args = (), brightness = brightness , auto_write = auto_write )
129
+ def show (self ):
130
+ if self ._spi :
131
+ self ._spi .write (self ._buf )
132
+ else :
133
+ self .ds_writebytes ()
120
134
121
- if self .auto_write :
122
- self .show ()
135
+ def _ds_writebytes (self ):
136
+ for b in self .buf :
137
+ for _ in range (8 ):
138
+ self .dpin .value = (b & 0x80 )
139
+ self .cpin .value = True
140
+ self .cpin .value = False
141
+ b = b << 1
142
+ self .cpin .value = False
123
143
124
144
def deinit (self ):
125
145
"""Blank out the DotStars and release the resources."""
@@ -139,71 +159,3 @@ def __exit__(self, exception_type, exception_value, traceback):
139
159
140
160
def __repr__ (self ):
141
161
return "[" + ", " .join ([str (x ) for x in self ]) + "]"
142
-
143
- def _set_item (self , index , value ):
144
- if isinstance (index , slice ):
145
- start , stop , step = index .indices (self .n )
146
- self ._pb [start :stop :step ] = value
147
- self ._pb [index ] = value
148
-
149
- def __setitem__ (self , index , value ):
150
- """
151
- value can be one of three things:
152
- a (r,g,b) list/tuple
153
- a (r,g,b, brightness) list/tuple (if bpp=4, pixel_order=LBGR)
154
- a single, longer int that contains RGB values, like 0xFFFFFF
155
- brightness, if specified should be a float 0-1
156
-
157
- Set a pixel value. You can set per-pixel brightness here, if it's not passed it
158
- will use the max value for pixel brightness value, which is a good default.
159
-
160
- Important notes about the per-pixel brightness - it's accomplished by
161
- PWMing the entire output of the LED, and that PWM is at a much
162
- slower clock than the rest of the LEDs. This can cause problems in
163
- Persistence of Vision Applications
164
- """
165
- if isinstance (index , slice ):
166
- start , stop , step = index .indices (self .n )
167
- self ._pb [start :stop :step ] = value
168
- self ._pb [index ] = value
169
-
170
- def __getitem__ (self , index ):
171
- if isinstance (index , slice ):
172
- start , stop , step = index .indices (self .n )
173
- return self ._pb [start :stop :step ]
174
- return self ._pb [index ]
175
-
176
- @property
177
- def brightness (self ):
178
- return self ._pb .brightness
179
-
180
- @brightness .setter
181
- def brightness (self , brightness ):
182
- self ._pb .brightness = brightness
183
-
184
- @property
185
- def auto_write (self ):
186
- return self ._pb .auto_write
187
-
188
- @auto_write .setter
189
- def auto_write (self , auto_write ):
190
- self ._pb .auto_write = auto_write
191
-
192
- def show (self ):
193
- self ._pb .show ()
194
-
195
- def __len__ (self ):
196
- return self .n
197
-
198
- def fill (self , color ):
199
- """Colors all pixels the given ***color***."""
200
- self ._pb [0 :self .n ] = (color , ) * self .n
201
-
202
- def _ds_writebytes (self , buf ):
203
- for b in buf :
204
- for _ in range (8 ):
205
- self .dpin .value = (b & 0x80 )
206
- self .cpin .value = True
207
- self .cpin .value = False
208
- b = b << 1
209
- self .cpin .value = False
0 commit comments