67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
# This example displays a rainbow animation on Keybow 2040's keys.
|
|
|
|
import time
|
|
import math
|
|
import board
|
|
from keybow2040 import Keybow2040, number_to_xy
|
|
|
|
|
|
def hsv_to_rgb(h, s, v):
|
|
# Convert an HSV (0.0-1.0) colour to RGB (0-255)
|
|
if s == 0.0:
|
|
rgb = [v, v, v]
|
|
|
|
i = int(h * 6.0)
|
|
|
|
f = (h*6.)-i; p,q,t = v*(1.-s), v*(1.-s*f), v*(1.-s*(1.-f)); i%=6
|
|
|
|
if i == 0:
|
|
rgb = [v, t, p]
|
|
if i == 1:
|
|
rgb = [q, v, p]
|
|
if i == 2:
|
|
rgb = [p, v, t]
|
|
if i == 3:
|
|
rgb = [p, q, v]
|
|
if i == 4:
|
|
rgb = [t, p, v]
|
|
if i == 5:
|
|
rgb = [v, p, q]
|
|
|
|
rgb = (int(c * 255) for c in rgb)
|
|
|
|
return rgb
|
|
|
|
# Set up Keybow
|
|
i2c = board.I2C()
|
|
keybow = Keybow2040(i2c)
|
|
keys = keybow.keys
|
|
|
|
# Increment step to shift animation across keys.
|
|
step = 0
|
|
|
|
while True:
|
|
# Always remember to call keybow.update() on every iteration of your loop!
|
|
keybow.update()
|
|
|
|
step += 1
|
|
|
|
for i in range(16):
|
|
# Convert the key number to an x/y coordinate to calculate the hue
|
|
# in a matrix style-y.
|
|
x, y = number_to_xy(i)
|
|
|
|
# Calculate the hue.
|
|
hue = (x + y + (step / 20)) / 8
|
|
hue = hue - int(hue)
|
|
hue = hue - math.floor(hue)
|
|
|
|
# Convert the hue to RGB values.
|
|
r, g, b = hsv_to_rgb(hue, 1, 1)
|
|
|
|
# Display it on the key!
|
|
keys[i].set_led(r, g, b)
|