Skip to content
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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion micropython/drivers/led/neopixel/manifest.py
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)
17 changes: 16 additions & 1 deletion micropython/drivers/led/neopixel/neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class NeoPixel:
# G R B W
ORDER = (1, 0, 2, 3)

def __init__(self, pin, n, bpp=3, timing=1):
def __init__(self, pin, n, bpp=3, timing=1, brightness=None):
self.pin = pin
self.n = n
self.bpp = bpp
self.b = None if brightness is None else min(max(brightness, 0), 1)
self.buf = bytearray(n * bpp)
self.pin.init(pin.OUT)
# Timing arg can either be 1 for 800kHz or 0 for 400kHz,
Expand All @@ -22,11 +23,17 @@ def __init__(self, pin, n, bpp=3, timing=1):
else timing
)

def _b(self, v):
if self.b is not None:
return tuple(round(c * self.b) for c in v)
return v

def __len__(self):
return self.n

def __setitem__(self, i, v):
offset = i * self.bpp
v = self._b(v)
for i in range(self.bpp):
self.buf[offset + self.ORDER[i]] = v[i]

Expand All @@ -35,6 +42,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._b(v)
b = self.buf
l = len(self.buf)
bpp = self.bpp
Expand All @@ -45,6 +53,13 @@ def fill(self, v):
b[j] = c
j += bpp

def brightness(self, b=None):
if b is None:
return self.b
self.b = min(max(b, 0), 1)
for i in range(self.n * self.bpp):
self.buf[i] = round(self.buf[i] * self.b)

def write(self):
# BITSTREAM_TYPE_HIGH_LOW = 0
bitstream(self.pin, 0, self.timing, self.buf)
29 changes: 29 additions & 0 deletions micropython/drivers/led/neopixel/neopixel_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import machine
import neopixel


def neopixel_test():
np = neopixel.NeoPixel(machine.Pin(1), 3)
print("Fill with a color.")
np.fill((255, 128, 64))
print("Verify the bytes to be written")
expected = bytearray([255, 128, 64, 255, 128, 64, 255, 128, 64])
actual = np.buf
passed = "passed" if expected == actual else "failed"
print(f"Initial fill: {passed}.")
print()

print("Change brightness of all pixels.")
np.brightness(0.5)
expected = bytearray([127, 64, 32, 127, 64, 32, 127, 64, 32])
actual = np.buf
passed = "passed" if expected == actual else "failed"
print(f"Brightness change: {passed}.")
print()

print("Get current brightness.")
expected = 0.5
actual = np.brightness()
passed = "passed" if expected == actual else "failed"
print(f"Brightness get: {passed}.")
print()
Loading