implement multiple pixelbuffer in rgb extension
This commit is contained in:
parent
fb5ecf7e38
commit
c7eaeaf90c
24
docs/rgb.md
24
docs/rgb.md
@ -155,9 +155,9 @@ installed LED's in total.
|
|||||||
Not all RGB LEDs are compatible with Neopixels. To support these, the RGB
|
Not all RGB LEDs are compatible with Neopixels. To support these, the RGB
|
||||||
extension accepts an instance of a `Pixelbuf`-compatible object as an optional
|
extension accepts an instance of a `Pixelbuf`-compatible object as an optional
|
||||||
parameter. If supplied, `pixel_pin` is ignored and the supplied Pixelbuf is
|
parameter. If supplied, `pixel_pin` is ignored and the supplied Pixelbuf is
|
||||||
used instead of creating a Neopixel object. Note that you still need to pass
|
used instead of creating a Neopixel object.
|
||||||
the LED string parameters to the RGB extension so that it can manage animations
|
The RGB extension will figure out LED count from the pixelbuffer length if not
|
||||||
correctly.
|
passed explicitly.
|
||||||
|
|
||||||
This works easily with APA102 ("DotStar") LEDs, but for most other RGB LED
|
This works easily with APA102 ("DotStar") LEDs, but for most other RGB LED
|
||||||
chipsets you will need to provide a wrapper to match the expected interface.
|
chipsets you will need to provide a wrapper to match the expected interface.
|
||||||
@ -171,6 +171,22 @@ from kb import rgb_pixel_pin # This can be imported or defined manually
|
|||||||
_LED_COUNT=12
|
_LED_COUNT=12
|
||||||
pixels = adafruit_dotstar.DotStar(board.SCK, board.MOSI, _LED_COUNT)
|
pixels = adafruit_dotstar.DotStar(board.SCK, board.MOSI, _LED_COUNT)
|
||||||
|
|
||||||
rgb_ext = RGB(pixel_pin=0, pixels=pixels, num_pixels=_LED_COUNT)
|
rgb_ext = RGB(pixel_pin=None, pixels=pixels)
|
||||||
|
keyboard.extensions.append(rgb_ext)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multiple PixelBuffer
|
||||||
|
Simlar to alternate drivers tha RGB module supports passing multiple `Pixelbuf`
|
||||||
|
objects as an iterable.
|
||||||
|
```python
|
||||||
|
from kmk.extensions.RGB import RGB
|
||||||
|
|
||||||
|
pixels = (
|
||||||
|
Neopixel(...),
|
||||||
|
DotStar(...),
|
||||||
|
CustomPixelBuf(...)
|
||||||
|
)
|
||||||
|
|
||||||
|
rgb_ext = RGB(pixel_pin=None, pixels=pixels)
|
||||||
keyboard.extensions.append(rgb_ext)
|
keyboard.extensions.append(rgb_ext)
|
||||||
```
|
```
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from adafruit_pixelbuf import PixelBuf
|
||||||
from math import e, exp, pi, sin
|
from math import e, exp, pi, sin
|
||||||
|
|
||||||
from kmk.extensions import Extension
|
from kmk.extensions import Extension
|
||||||
@ -109,18 +110,25 @@ class RGB(Extension):
|
|||||||
if pixels is None:
|
if pixels is None:
|
||||||
import neopixel
|
import neopixel
|
||||||
|
|
||||||
self.pixels = neopixel.NeoPixel(
|
pixels = neopixel.NeoPixel(
|
||||||
pixel_pin,
|
pixel_pin,
|
||||||
num_pixels,
|
num_pixels,
|
||||||
pixel_order=rgb_order,
|
pixel_order=rgb_order,
|
||||||
auto_write=not disable_auto_write,
|
auto_write=not disable_auto_write,
|
||||||
)
|
)
|
||||||
else:
|
self.pixels = pixels
|
||||||
self.pixels = pixels
|
self.num_pixels = num_pixels
|
||||||
|
|
||||||
|
# PixelBuffer are already iterable, can't do the usual `try: iter(...)`
|
||||||
|
if issubclass(self.pixels.__class__, PixelBuf):
|
||||||
|
self.pixels = (self.pixels,)
|
||||||
|
|
||||||
|
if self.num_pixels == 0:
|
||||||
|
for pixels in self.pixels:
|
||||||
|
self.num_pixels += len(pixels)
|
||||||
|
|
||||||
self.rgbw = bool(len(rgb_order) == 4)
|
self.rgbw = bool(len(rgb_order) == 4)
|
||||||
|
|
||||||
self.num_pixels = num_pixels
|
|
||||||
self.hue_step = hue_step
|
self.hue_step = hue_step
|
||||||
self.sat_step = sat_step
|
self.sat_step = sat_step
|
||||||
self.val_step = val_step
|
self.val_step = val_step
|
||||||
@ -241,11 +249,10 @@ class RGB(Extension):
|
|||||||
:param val:
|
:param val:
|
||||||
:param index: Index of LED/Pixel
|
:param index: Index of LED/Pixel
|
||||||
'''
|
'''
|
||||||
if self.pixels:
|
if self.rgbw:
|
||||||
if self.rgbw:
|
self.set_rgb(hsv_to_rgbw(hue, sat, val), index)
|
||||||
self.set_rgb(hsv_to_rgbw(hue, sat, val), index)
|
else:
|
||||||
else:
|
self.set_rgb(hsv_to_rgb(hue, sat, val), index)
|
||||||
self.set_rgb(hsv_to_rgb(hue, sat, val), index)
|
|
||||||
|
|
||||||
def set_hsv_fill(self, hue, sat, val):
|
def set_hsv_fill(self, hue, sat, val):
|
||||||
'''
|
'''
|
||||||
@ -254,11 +261,10 @@ class RGB(Extension):
|
|||||||
:param sat:
|
:param sat:
|
||||||
:param val:
|
:param val:
|
||||||
'''
|
'''
|
||||||
if self.pixels:
|
if self.rgbw:
|
||||||
if self.rgbw:
|
self.set_rgb_fill(hsv_to_rgbw(hue, sat, val))
|
||||||
self.set_rgb_fill(hsv_to_rgbw(hue, sat, val))
|
else:
|
||||||
else:
|
self.set_rgb_fill(hsv_to_rgb(hue, sat, val))
|
||||||
self.set_rgb_fill(hsv_to_rgb(hue, sat, val))
|
|
||||||
|
|
||||||
def set_rgb(self, rgb, index):
|
def set_rgb(self, rgb, index):
|
||||||
'''
|
'''
|
||||||
@ -266,20 +272,26 @@ class RGB(Extension):
|
|||||||
:param rgb: RGB or RGBW
|
:param rgb: RGB or RGBW
|
||||||
:param index: Index of LED/Pixel
|
:param index: Index of LED/Pixel
|
||||||
'''
|
'''
|
||||||
if self.pixels and 0 <= index <= self.num_pixels - 1:
|
if 0 <= index <= self.num_pixels - 1:
|
||||||
self.pixels[index] = rgb
|
for pixels in self.pixels:
|
||||||
|
if index <= (len(pixels) - 1):
|
||||||
|
break
|
||||||
|
index -= len(pixels)
|
||||||
|
|
||||||
|
pixels[index] = rgb
|
||||||
|
|
||||||
if not self.disable_auto_write:
|
if not self.disable_auto_write:
|
||||||
self.pixels.show()
|
pixels.show()
|
||||||
|
|
||||||
def set_rgb_fill(self, rgb):
|
def set_rgb_fill(self, rgb):
|
||||||
'''
|
'''
|
||||||
Takes an RGB or RGBW and displays it on all LEDs/Neopixels
|
Takes an RGB or RGBW and displays it on all LEDs/Neopixels
|
||||||
:param rgb: RGB or RGBW
|
:param rgb: RGB or RGBW
|
||||||
'''
|
'''
|
||||||
if self.pixels:
|
for pixels in self.pixels:
|
||||||
self.pixels.fill(rgb)
|
pixels.fill(rgb)
|
||||||
if not self.disable_auto_write:
|
if not self.disable_auto_write:
|
||||||
self.pixels.show()
|
pixels.show()
|
||||||
|
|
||||||
def increase_hue(self, step=None):
|
def increase_hue(self, step=None):
|
||||||
'''
|
'''
|
||||||
@ -386,15 +398,14 @@ class RGB(Extension):
|
|||||||
'''
|
'''
|
||||||
Turns off all LEDs/Neopixels without changing stored values
|
Turns off all LEDs/Neopixels without changing stored values
|
||||||
'''
|
'''
|
||||||
if self.pixels:
|
self.set_hsv_fill(0, 0, 0)
|
||||||
self.set_hsv_fill(0, 0, 0)
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
'''
|
'''
|
||||||
Turns on all LEDs/Neopixels without changing stored values
|
Turns on all LEDs/Neopixels without changing stored values
|
||||||
'''
|
'''
|
||||||
if self.pixels:
|
for pixels in self.pixels:
|
||||||
self.pixels.show()
|
pixels.show()
|
||||||
|
|
||||||
def animate(self):
|
def animate(self):
|
||||||
'''
|
'''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user