diff --git a/README.rst b/README.rst index 61fa057..912d2ba 100644 --- a/README.rst +++ b/README.rst @@ -22,6 +22,7 @@ This driver supports the following hardware: * `Adafruit 16x8 CharliePlex LED Matrix Bonnets `_ * `Pimoroni 17x7 Scroll pHAT HD `_ * `Pimoroni 28x3 (r,g,b) Led Shim `_ +* `Pimoroni Keybow 2040 with 4x4 matrix of RGB LEDs `_ Dependencies diff --git a/adafruit_is31fl3731.py b/adafruit_is31fl3731.py index 0e18b7d..bc1a1c3 100644 --- a/adafruit_is31fl3731.py +++ b/adafruit_is31fl3731.py @@ -22,6 +22,12 @@ * `Adafruit 15x7 CharliePlex LED Matrix Display FeatherWings `_ +* Pimoroni LED SHIM + _ + +* Pimoroni Keybow 2040 + _ + **Software and Dependencies:** * Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards: @@ -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] diff --git a/examples/is31fl3731_keybow_2040_rainbow.py b/examples/is31fl3731_keybow_2040_rainbow.py new file mode 100644 index 0000000..5042797 --- /dev/null +++ b/examples/is31fl3731_keybow_2040_rainbow.py @@ -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)