|
| 1 | +# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# The Fisher-Price Kick and Play Piano Gym has five buttons that are |
| 5 | +# active high. Pressed = 1, Released = 0. This code turns that into |
| 6 | +# keyboard key press, key combos, and/or key press/combo macros. |
| 7 | + |
| 8 | +import time |
| 9 | +import board |
| 10 | +import usb_hid |
| 11 | +from adafruit_hid.keyboard import Keyboard |
| 12 | +from adafruit_hid.keycode import Keycode |
| 13 | +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS |
| 14 | +from digitalio import DigitalInOut, Direction, Pull |
| 15 | + |
| 16 | +# Set up a keyboard device. |
| 17 | +kbd = Keyboard(usb_hid.devices) |
| 18 | +layout = KeyboardLayoutUS(kbd) |
| 19 | + |
| 20 | +# Setup the buttons with internal pull-down resistors |
| 21 | +buttons = [] |
| 22 | +for pin in [board.A0, board.A2, board.CLK, board.D2, board.D3]: # kb2040 pins |
| 23 | + button = DigitalInOut(pin) |
| 24 | + button.direction = Direction.INPUT |
| 25 | + button.pull = Pull.DOWN |
| 26 | + buttons.append(button) |
| 27 | + |
| 28 | +# Each button corresponds to a key or key combination or a sequence of keys |
| 29 | +keys = [ |
| 30 | + Keycode.A, |
| 31 | + (Keycode.COMMAND, Keycode.TAB), |
| 32 | + [ |
| 33 | + Keycode.UP_ARROW, |
| 34 | + Keycode.ENTER |
| 35 | + ], |
| 36 | + [ |
| 37 | + Keycode.END, |
| 38 | + (Keycode.SHIFT, Keycode.HOME), |
| 39 | + (Keycode.COMMAND, Keycode.C), |
| 40 | + ], |
| 41 | + [ |
| 42 | + (Keycode.CONTROL, Keycode.A), |
| 43 | + 'Hello World', |
| 44 | + Keycode.PERIOD |
| 45 | + ] |
| 46 | +] |
| 47 | + |
| 48 | +while True: |
| 49 | + # check each button |
| 50 | + for button, key in zip(buttons, keys): |
| 51 | + if button.value: # button is pressed |
| 52 | + if isinstance(key, tuple): |
| 53 | + kbd.press(*key) |
| 54 | + kbd.release_all() |
| 55 | + elif isinstance(key, list): |
| 56 | + for macro_key in key: |
| 57 | + if isinstance(macro_key, str): # print a string |
| 58 | + layout.write(macro_key) |
| 59 | + elif isinstance(macro_key, tuple): # press combo keys |
| 60 | + kbd.press(*macro_key) |
| 61 | + kbd.release_all() |
| 62 | + else: # press a single key |
| 63 | + kbd.press(macro_key) |
| 64 | + kbd.release_all() |
| 65 | + time.sleep(0.1) # delay between keys |
| 66 | + else: # press a single key |
| 67 | + kbd.press(key) |
| 68 | + kbd.release_all() |
| 69 | + time.sleep(0.1) # debounce delay |
| 70 | + |
| 71 | + time.sleep(0.1) |
0 commit comments