1
- # DotStar driver for CircuitPython
2
- # MIT license; Copyright (c) 2017 Ladyada & Damein George (original Neopixel object)
1
+ # DotStar driver for CircuitPython
2
+ # MIT license; Copyright (c) 2017 Ladyada & Damien George (original Neopixel object)
3
3
4
4
import digitalio
5
5
import time
6
6
7
7
class DotStar :
8
+ """
9
+ A sequence of dotstars.
10
+
11
+ :param ~microcontroller.Pin data: The pin to output dotstar data on.
12
+ :param ~microcontroller.Pin clock: The pin to output dotstar clock on.
13
+ :param int n: The number of dotstars in the chain
14
+ :param int bpp: Bytes per pixel (usually 3 or 4)
15
+ :param float brightness: Brightness of the pixels between 0.0 and 1.0
16
+
17
+ Example for Gemma M0:
18
+
19
+ .. code-block:: python
20
+
21
+ import dotstar
22
+ from board import *
23
+
24
+ RED = 0x100000
25
+
26
+ with dotstar.DotStar(APA102_MOSI, APA102_SCK, 1) as pixels:
27
+ pixels[0] = RED
28
+ pixels.show()
29
+ """
30
+
8
31
ORDER = (1 , 0 , 2 , 3 )
9
32
def __init__ (self , data , clock , n , bpp = 3 , brightness = 1.0 ):
10
33
self .dpin = digitalio .DigitalInOut (data )
@@ -37,13 +60,13 @@ def __getitem__(self, index):
37
60
def __len__ (self ):
38
61
return self .n
39
62
40
- def set_brightness ( self , range ):
41
- if ( range > 1.0 ):
42
- self .brightness = 1.0
43
- elif ( range < 0 ):
44
- self . brightness = 0.0
45
- else :
46
- self .brightness = range
63
+ @ property
64
+ def brightness ( self ):
65
+ return self ._brightness
66
+
67
+ @ brightness . setter
68
+ def brightness ( self , brightness ) :
69
+ self ._brightness = min ( max ( brightness , 0.0 ), 1.0 )
47
70
48
71
def fill (self , color ):
49
72
for i in range (self .n ):
@@ -57,15 +80,18 @@ def ds_writebytes(self, bytes):
57
80
self .cpin .value = False
58
81
b = b << 1
59
82
60
- def write (self ):
83
+ def show (self ):
61
84
# Tell strip we're ready with many 0x00's
62
85
self .ds_writebytes ([0x00 , 0x00 , 0x00 , 0x00 ])
86
+ # Each pixel starts with 0xFF, then red/green/blue. Although the data
87
+ # sheet suggests using a global brightness in the first byte, we don't
88
+ # do that because it causes further issues with persistence of vision
89
+ # projects.
90
+ pixel = [0xFF , 0 , 0 , 0 ]
63
91
for i in range (self .n ):
64
- # each pixel starts with 0xFF, then red/green/blue
65
- pixel = [0xFF , 0 , 0 , 0 ]
66
92
# scale each pixel by the brightness
67
93
for x in range (3 ):
68
- pixel [x + 1 ] = int (self .buf [i * self .bpp + x ] * self .brightness )
94
+ pixel [x + 1 ] = int (self .buf [i * self .bpp + x ] * self ._brightness )
69
95
# write this pixel
70
96
self .ds_writebytes (pixel )
71
97
# Tell strip we're done with many 0xFF's
0 commit comments