Skip to content

Instantly share code, notes, and snippets.

@mosch
Forked from mschwld/code.py
Created April 7, 2023 11:51
Show Gist options
  • Save mosch/b1a400c19a418578e219782310aa93e4 to your computer and use it in GitHub Desktop.
Save mosch/b1a400c19a418578e219782310aa93e4 to your computer and use it in GitHub Desktop.
from keybow_hardware.pim56x import PIM56X as Hardware
from keybow2040 import Keybow2040
import supervisor
import time
import usb_cdc
keybow = Keybow2040(Hardware())
keys = keybow.keys
uart = usb_cdc.serials[1]
incmd = []
for key in keys:
@keybow.on_press(key)
def press_handler(key):
keynum = str(key.number)
send_serial(keynum)
def startup_animation():
for i in range(0, 16):
keybow.set_led(i, 255, 255, 255)
time.sleep(0.01)
for i in range(0, 16):
keybow.set_led(i, 0, 0, 0)
time.sleep(0.04)
def send_serial(msg):
nmsg = msg + "\r\n"
s = bytearray(nmsg.encode())
uart.write(s)
time.sleep(0.05)
def read_serial():
global incmd
global uart
if uart.in_waiting > 0:
mybytes = uart.read(1)
if mybytes == b'\r' and len(incmd) > 1:
cmdstring = ""
for i in incmd:
cmdstring = cmdstring + i.decode("UTF-8")
if "," in cmdstring:
key = cmdstring.split(",")[0]
value = cmdstring.split(",")[1]
switch_key_rgb(key, value)
incmd = []
else:
incmd.append(mybytes)
def switch_key_rgb(key, value):
if value[0] == "0":
keybow.set_led(int(key), 0, 0, 0)
elif value[0] == "1":
keybow.set_led(int(key), 0, 255, 0)
elif value[0] == "2":
keybow.set_led(int(key), 0, 0, 255)
elif value[0] == "3":
keybow.set_led(int(key), 255, 0, 0)
elif value[0] == "4":
keybow.set_led(int(key), 255, 255, 0)
elif value[0] == "5":
keybow.set_led(int(key), 255, 255, 255)
startup_animation()
while True:
keybow.update()
read_serial()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment