Created
September 24, 2024 19:17
-
-
Save vwillcox/927169413a0c536ac7968c744cf3cc75 to your computer and use it in GitHub Desktop.
Some code to allow me to Control Farm Together 2 with the Keybow2040 from Pimoroni
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 | |
# 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 `pmk` folder | |
# into your `lib` folder on your `CIRCUITPY` drive. | |
# NOTE! Requires the adafruit_hid CircuitPython library also! | |
from pmk import PMK | |
from pmk.platform.keybow2040 import Keybow2040 as Hardware # for Keybow 2040 | |
# from pmk.platform.rgbkeypadbase import RGBKeypadBase as Hardware # for Pico RGB Keypad Base | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
import time | |
# Set up Keybow | |
keybow = PMK(Hardware()) | |
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 = [Keycode.SHIFT, #0 | |
Keycode.ONE, #1 | |
Keycode.TWO, #2 | |
Keycode.ESCAPE, #3 | |
Keycode.V, #4 | |
Keycode.V, #5 | |
Keycode.A, #6 | |
Keycode.M, #7 | |
Keycode.M, #8 | |
Keycode.NINE, #9 | |
Keycode.S, #10 | |
Keycode.W, #11 | |
Keycode.Q, #12 | |
Keycode.D, #13 | |
Keycode.D, #14 | |
Keycode.V] #15 | |
colors = [ | |
(255, 0, 0), # 0 | |
(0, 0, 0), # 1 | |
(0, 0, 0), # 2 | |
(255, 165, 0), # 3 | |
(0, 0, 255), # 4 | |
(0, 0, 0), # 5 | |
(0, 255, 0), # 6 | |
(255, 0, 255), # 7 | |
(255, 0, 255), # 8 | |
(0, 0, 0), # 9 | |
(0, 255, 0), # 10 | |
(0, 255, 0), # 11 | |
(0, 0, 255), # 12 | |
(0, 0, 0), # 13 | |
(0, 255, 0), # 14 | |
(0, 255, 255) # 15 | |
] | |
def set_key_colors(): | |
for i in range(16): | |
r, g, b = colors[i] | |
keys[i].set_led(r, g, b) | |
set_key_colors() | |
last_activity_time = time.time() # Initialize the last activity time | |
leds_off = False # Track if LEDs are off | |
def reset_activity_timer(): | |
global last_activity_time, leds_off | |
last_activity_time = time.time() | |
if leds_off: | |
set_key_colors() | |
leds_off = False | |
# 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): | |
reset_activity_timer() | |
keycode = keymap[key.number] | |
keyboard.press(keycode) | |
key.set_led(255, 0, 0) # Red when pressed | |
# A release handler that turns off the LED | |
@keybow.on_release(key) | |
def release_handler(key): | |
reset_activity_timer() | |
keycode = keymap[key.number] | |
r, g, b = colors[key.number] | |
key.set_led(r, g, b) | |
#key.led_off() | |
keyboard.release(keycode) | |
while True: | |
# Always remember to call keybow.update()! | |
keybow.update() | |
# Check for inactivity | |
if time.time() - last_activity_time > 60: # 300 seconds = 5 minutes | |
if not leds_off: | |
for key in keys: | |
key.led_off() # Turn off all LEDs | |
leds_off = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment