Skip to content

Commit 2619924

Browse files
committed
Switch brightness to a property instead of function.
1 parent 04f6bb1 commit 2619924

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

dotstar.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
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)
33

44
import digitalio
55
import time
66

77
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+
831
ORDER = (1, 0, 2, 3)
932
def __init__(self, data, clock, n, bpp=3, brightness=1.0):
1033
self.dpin = digitalio.DigitalInOut(data)
@@ -37,13 +60,13 @@ def __getitem__(self, index):
3760
def __len__(self):
3861
return self.n
3962

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)
4770

4871
def fill(self, color):
4972
for i in range(self.n):
@@ -57,15 +80,18 @@ def ds_writebytes(self, bytes):
5780
self.cpin.value = False
5881
b = b << 1
5982

60-
def write(self):
83+
def show(self):
6184
# Tell strip we're ready with many 0x00's
6285
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]
6391
for i in range(self.n):
64-
# each pixel starts with 0xFF, then red/green/blue
65-
pixel = [0xFF, 0, 0, 0]
6692
# scale each pixel by the brightness
6793
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)
6995
# write this pixel
7096
self.ds_writebytes(pixel)
7197
# Tell strip we're done with many 0xFF's

0 commit comments

Comments
 (0)