Created
April 30, 2021 13:08
-
-
Save gwthompson/cb3e9e99656cf719cb6341212610f6ea to your computer and use it in GitHub Desktop.
Keybow RP2040 Moode player control deck with rainbow lights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2021 Sandy Macdonald | |
# | |
# SPDX-License-Identifier: MIT | |
# Adaptation from Sandy MacDonald Examples | |
# A simple example of how to set up a keymap and HID keyboard on Keybow 2040. | |
# You'll need to connect Keybow 2040 to a computer, as you would with a regular | |
# USB keyboard. | |
# Drop the keybow2040.py file into your `lib` folder on your `CIRCUITPY` drive. | |
# NOTE! Requires the adafruit_hid CircuitPython library also! | |
import board | |
import math | |
from keybow2040 import Keybow2040, number_to_xy, hsv_to_rgb | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
# Set up Keybow | |
i2c = board.I2C() | |
keybow = Keybow2040(i2c) | |
keys = keybow.keys | |
# Set up the keyboard and layout | |
keyboard = Keyboard(usb_hid.devices) | |
layout = KeyboardLayoutUS(keyboard) | |
# A map of keycodes that will be mapped sequentially to each of the keys, 0-15 | |
keymap = ["rad1", | |
"rad1", | |
"mpc toggle", | |
"toggle_disp1", | |
"rad2", | |
"rad2", | |
"mpc stop", | |
"volm", | |
"rad3", | |
"rad3", | |
"mpc prev", | |
"vold", | |
"rad4", | |
"rad4", | |
"mpc next", | |
"volu"] | |
# The colour to set the keys when pressed, yellow. | |
rgb = (255, 255, 255) | |
# Increment step to shift animation across keys. | |
step = 0 | |
# Attach handler functions to all of the keys | |
for key in keys: | |
# A press handler that sends the keycode and turns on the LED | |
@keybow.on_press(key) | |
def press_handler(key): | |
keycode = keymap[key.number] | |
layout.write(keycode) | |
keyboard.send(Keycode.ENTER) | |
key.set_led(*rgb) | |
# A release handler that turns off the LED | |
# @keybow.on_release(key) | |
# def release_handler(key): | |
# key.led_off() | |
# Throw a CTRL +C when key is held | |
@keybow.on_hold(key) | |
def hold_handler(key): | |
keyboard.send(Keycode.CONTROL, Keycode.C) | |
while True: | |
# Always remember to call keybow.update()! | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment