-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers/led/neopixel: Add brightness control. #739
base: master
Are you sure you want to change the base?
Changes from 9 commits
2d9486c
94e3dab
8105f2d
69f6d61
47c5dbc
92967e0
6f3ebea
f683608
0014eac
866fd2f
9e80530
9b5be62
6ca933b
1f1f51d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
metadata(description="WS2812/NeoPixel driver.", version="0.1.0") | ||
metadata(description="WS2812/NeoPixel driver.", version="0.2.0") | ||
|
||
module("neopixel.py", opt=3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,16 @@ class NeoPixel: | |
# G R B W | ||
ORDER = (1, 0, 2, 3) | ||
|
||
def __init__(self, pin, n, bpp=3, timing=1): | ||
def _clamp(self, v, c_min, c_max): | ||
return min(max(v, c_min), c_max) | ||
|
||
def __init__(self, pin, n, bpp=3, timing=1, brightness=None): | ||
self.pin = pin | ||
self.n = n | ||
self.bpp = bpp | ||
self.brightness = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should rename this to It's also smaller to write
rather than
|
||
if brightness is not None: | ||
self.brightness = self._clamp(float(brightness), 0.0, 1.0) | ||
self.buf = bytearray(n * bpp) | ||
self.pin.init(pin.OUT) | ||
# Timing arg can either be 1 for 800kHz or 0 for 400kHz, | ||
|
@@ -22,11 +28,17 @@ def __init__(self, pin, n, bpp=3, timing=1): | |
else timing | ||
) | ||
|
||
def _apply_brightness(self, v): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
if self.brightness is not None: | ||
return tuple(round(c * self.brightness) for c in v) | ||
return v | ||
|
||
def __len__(self): | ||
return self.n | ||
|
||
def __setitem__(self, i, v): | ||
offset = i * self.bpp | ||
v = self._apply_brightness(v) | ||
for i in range(self.bpp): | ||
self.buf[offset + self.ORDER[i]] = v[i] | ||
|
||
|
@@ -35,6 +47,7 @@ def __getitem__(self, i): | |
return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp)) | ||
|
||
def fill(self, v): | ||
v = self._apply_brightness(v) | ||
b = self.buf | ||
l = len(self.buf) | ||
bpp = self.bpp | ||
|
@@ -45,6 +58,13 @@ def fill(self, v): | |
b[j] = c | ||
j += bpp | ||
|
||
def set_brightness(self, b: float): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to Also I think, similarly, it should probably return the current brightness too, e.g. def brightness(self, brightness):
if brightness is not None:
self.b = min(max(brightness, 0), 1)
# This may look odd but because __getitem__ and __setitem__ handle all the
# brightness logic already, we can offload the work to those methods.
for i in range(self.n):
self[i] = self[i]
return self.b which is +10 bytes, but I think useful for people to write e.g. |
||
self.brightness = self._clamp(b, 0.0, 1.0) | ||
# This may look odd but because __getitem__ and __setitem__ handle all the | ||
# brightness logic already, we can offload the work to those methods. | ||
for i in range(self.n): | ||
self[i] = self[i] | ||
|
||
def write(self): | ||
# BITSTREAM_TYPE_HIGH_LOW = 0 | ||
bitstream(self.pin, 0, self.timing, self.buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely stylistically better, but functions are expensive in terms of code size (and therefore memory usage).
In this case, it's actually cheaper to just use
min(max(brightness), 0, 1)
directly in both places. Note also changing0.0
to0
etc is also helpful for size (and doesn't change the behaviour).