Skip to content

Adding support for the 4x4 RGB LED matrix of Keybow 2040 #35

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

Merged
merged 5 commits into from
Feb 15, 2021
Merged
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This driver supports the following hardware:
* `Adafruit 16x8 CharliePlex LED Matrix Bonnets <https://www.adafruit.com/product/4127>`_
* `Pimoroni 17x7 Scroll pHAT HD <https://www.adafruit.com/product/3473>`_
* `Pimoroni 28x3 (r,g,b) Led Shim <https://www.adafruit.com/product/3831>`_
* `Pimoroni Keybow 2040 with 4x4 matrix of RGB LEDs <https://shop.pimoroni.com/products/keybow-2040>`_


Dependencies
Expand Down
61 changes: 61 additions & 0 deletions adafruit_is31fl3731.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* `Adafruit 15x7 CharliePlex LED Matrix Display FeatherWings
<https://www.adafruit.com/product/2965>`_

* Pimoroni LED SHIM
<https://shop.pimoroni.com/products/led-shim>_

* Pimoroni Keybow 2040
<https://shop.pimoroni.com/products/keybow-2040>_

**Software and Dependencies:**

* Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards:
Expand Down Expand Up @@ -476,3 +482,58 @@ def pixel_addr(x, y):
if x < 27:
return x + 83
return 93


class Keybow2040(Matrix):
"""Supports the Pimoroni Keybow 2040 with 4x4 matrix of RGB LEDs """

width = 16
height = 3

# pylint: disable=too-many-arguments

def pixelrgb(self, x, y, r, g, b, blink=None, frame=None):
"""
Blink or brightness for x, y-pixel

:param x: horizontal pixel position
:param y: vertical pixel position
:param r: red brightness value 0->255
:param g: green brightness value 0->255
:param b: blue brightness value 0->255
:param blink: True to blink
:param frame: the frame to set the pixel
"""
x = x + (4 * y)

super().pixel(x, 0, r, blink, frame)
super().pixel(x, 1, g, blink, frame)
super().pixel(x, 2, b, blink, frame)

# pylint: disable=inconsistent-return-statements
# pylint: disable=too-many-return-statements
# pylint: disable=too-many-branches

@staticmethod
def pixel_addr(x, y):

lookup = [
(120, 88, 104), # 0, 0
(136, 40, 72), # 1, 0
(112, 80, 96), # 2, 0
(128, 32, 64), # 3, 0
(121, 89, 105), # 0, 1
(137, 41, 73), # 1, 1
(113, 81, 97), # 2, 1
(129, 33, 65), # 3, 1
(122, 90, 106), # 0, 2
(138, 25, 74), # 1, 2
(114, 82, 98), # 2, 2
(130, 17, 66), # 3, 2
(123, 91, 107), # 0, 3
(139, 26, 75), # 1, 3
(115, 83, 99), # 2, 3
(131, 18, 67), # 3, 3
]

return lookup[x][y]
87 changes: 87 additions & 0 deletions examples/is31fl3731_keybow_2040_rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
# SPDX-License-Identifier: MIT

"""
Example to display a rainbow animation on the RGB LED keys of the
Keybow 2040.

Usage:
Rename this file code.py and pop it on your Keybow 2040's
CIRCUITPY drive.

This example is for use on the Keybow 2040 only, due to the way
that the LEDs are mapped out.

Author(s): Sandy Macdonald.
"""

import time
import math
import board
import busio

import adafruit_is31fl3731

# pylint: disable=inconsistent-return-statements
# pylint: disable=too-many-return-statements
# pylint: disable=invalid-name


def hsv_to_rgb(hue, sat, val):
"""
Convert HSV colour to RGB

:param hue: hue; 0.0-1.0
:param sat: saturation; 0.0-1.0
:param val: value; 0.0-1.0
"""

if sat == 0.0:
return (val, val, val)

i = int(hue * 6.0)

p = val * (1.0 - sat)
f = (hue * 6.0) - i
q = val * (1.0 - sat * f)
t = val * (1.0 - sat * (1.0 - f))

i %= 6

if i == 0:
return (val, t, p)
if i == 1:
return (q, val, p)
if i == 2:
return (p, val, t)
if i == 3:
return (p, q, val)
if i == 4:
return (t, p, val)
if i == 5:
return (val, p, q)


i2c = busio.I2C(board.GP5, board.GP4)

# Set up 4x4 RGB matrix of Keybow 2040
display = adafruit_is31fl3731.Keybow2040(i2c)

step = 0

while True:
step += 1
for y in range(0, 4):
for x in range(0, 4):
pixel_hue = (x + y + (step / 20)) / 8
pixel_hue = pixel_hue - int(pixel_hue)
pixel_hue += 0
pixel_hue = pixel_hue - math.floor(pixel_hue)

rgb = hsv_to_rgb(pixel_hue, 1, 1)

display.pixelrgb(
x, y, int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
)

time.sleep(0.01)