Skip to content

Commit 0246396

Browse files
authored
Merge pull request #35 from sandyjmacdonald/master
Adding support for the 4x4 RGB LED matrix of Keybow 2040
2 parents 6c368a0 + 5292151 commit 0246396

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This driver supports the following hardware:
2222
* `Adafruit 16x8 CharliePlex LED Matrix Bonnets <https://www.adafruit.com/product/4127>`_
2323
* `Pimoroni 17x7 Scroll pHAT HD <https://www.adafruit.com/product/3473>`_
2424
* `Pimoroni 28x3 (r,g,b) Led Shim <https://www.adafruit.com/product/3831>`_
25+
* `Pimoroni Keybow 2040 with 4x4 matrix of RGB LEDs <https://shop.pimoroni.com/products/keybow-2040>`_
2526

2627

2728
Dependencies

adafruit_is31fl3731.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* `Adafruit 15x7 CharliePlex LED Matrix Display FeatherWings
2323
<https://www.adafruit.com/product/2965>`_
2424
25+
* Pimoroni LED SHIM
26+
<https://shop.pimoroni.com/products/led-shim>_
27+
28+
* Pimoroni Keybow 2040
29+
<https://shop.pimoroni.com/products/keybow-2040>_
30+
2531
**Software and Dependencies:**
2632
2733
* Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards:
@@ -476,3 +482,58 @@ def pixel_addr(x, y):
476482
if x < 27:
477483
return x + 83
478484
return 93
485+
486+
487+
class Keybow2040(Matrix):
488+
"""Supports the Pimoroni Keybow 2040 with 4x4 matrix of RGB LEDs """
489+
490+
width = 16
491+
height = 3
492+
493+
# pylint: disable=too-many-arguments
494+
495+
def pixelrgb(self, x, y, r, g, b, blink=None, frame=None):
496+
"""
497+
Blink or brightness for x, y-pixel
498+
499+
:param x: horizontal pixel position
500+
:param y: vertical pixel position
501+
:param r: red brightness value 0->255
502+
:param g: green brightness value 0->255
503+
:param b: blue brightness value 0->255
504+
:param blink: True to blink
505+
:param frame: the frame to set the pixel
506+
"""
507+
x = x + (4 * y)
508+
509+
super().pixel(x, 0, r, blink, frame)
510+
super().pixel(x, 1, g, blink, frame)
511+
super().pixel(x, 2, b, blink, frame)
512+
513+
# pylint: disable=inconsistent-return-statements
514+
# pylint: disable=too-many-return-statements
515+
# pylint: disable=too-many-branches
516+
517+
@staticmethod
518+
def pixel_addr(x, y):
519+
520+
lookup = [
521+
(120, 88, 104), # 0, 0
522+
(136, 40, 72), # 1, 0
523+
(112, 80, 96), # 2, 0
524+
(128, 32, 64), # 3, 0
525+
(121, 89, 105), # 0, 1
526+
(137, 41, 73), # 1, 1
527+
(113, 81, 97), # 2, 1
528+
(129, 33, 65), # 3, 1
529+
(122, 90, 106), # 0, 2
530+
(138, 25, 74), # 1, 2
531+
(114, 82, 98), # 2, 2
532+
(130, 17, 66), # 3, 2
533+
(123, 91, 107), # 0, 3
534+
(139, 26, 75), # 1, 3
535+
(115, 83, 99), # 2, 3
536+
(131, 18, 67), # 3, 3
537+
]
538+
539+
return lookup[x][y]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
Example to display a rainbow animation on the RGB LED keys of the
6+
Keybow 2040.
7+
8+
Usage:
9+
Rename this file code.py and pop it on your Keybow 2040's
10+
CIRCUITPY drive.
11+
12+
This example is for use on the Keybow 2040 only, due to the way
13+
that the LEDs are mapped out.
14+
15+
Author(s): Sandy Macdonald.
16+
"""
17+
18+
import time
19+
import math
20+
import board
21+
import busio
22+
23+
import adafruit_is31fl3731
24+
25+
# pylint: disable=inconsistent-return-statements
26+
# pylint: disable=too-many-return-statements
27+
# pylint: disable=invalid-name
28+
29+
30+
def hsv_to_rgb(hue, sat, val):
31+
"""
32+
Convert HSV colour to RGB
33+
34+
:param hue: hue; 0.0-1.0
35+
:param sat: saturation; 0.0-1.0
36+
:param val: value; 0.0-1.0
37+
"""
38+
39+
if sat == 0.0:
40+
return (val, val, val)
41+
42+
i = int(hue * 6.0)
43+
44+
p = val * (1.0 - sat)
45+
f = (hue * 6.0) - i
46+
q = val * (1.0 - sat * f)
47+
t = val * (1.0 - sat * (1.0 - f))
48+
49+
i %= 6
50+
51+
if i == 0:
52+
return (val, t, p)
53+
if i == 1:
54+
return (q, val, p)
55+
if i == 2:
56+
return (p, val, t)
57+
if i == 3:
58+
return (p, q, val)
59+
if i == 4:
60+
return (t, p, val)
61+
if i == 5:
62+
return (val, p, q)
63+
64+
65+
i2c = busio.I2C(board.GP5, board.GP4)
66+
67+
# Set up 4x4 RGB matrix of Keybow 2040
68+
display = adafruit_is31fl3731.Keybow2040(i2c)
69+
70+
step = 0
71+
72+
while True:
73+
step += 1
74+
for y in range(0, 4):
75+
for x in range(0, 4):
76+
pixel_hue = (x + y + (step / 20)) / 8
77+
pixel_hue = pixel_hue - int(pixel_hue)
78+
pixel_hue += 0
79+
pixel_hue = pixel_hue - math.floor(pixel_hue)
80+
81+
rgb = hsv_to_rgb(pixel_hue, 1, 1)
82+
83+
display.pixelrgb(
84+
x, y, int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
85+
)
86+
87+
time.sleep(0.01)

0 commit comments

Comments
 (0)