|
| 1 | +# SPDX-FileCopyrightText: 2021 Collin Cunningham for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +import usb_hid |
| 7 | +from adafruit_hid.keyboard import Keyboard |
| 8 | +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS |
| 9 | +from adafruit_hid.keycode import Keycode |
| 10 | +from digitalio import DigitalInOut, Direction, Pull |
| 11 | + |
| 12 | +# The pins connected to each switch/button |
| 13 | +buttonpins = [board.D3, board.D4, board.D5] |
| 14 | +# The pins connected to each LED |
| 15 | +ledpins = [board.D2, board.D6, board.D7] |
| 16 | + |
| 17 | +# our array of button & LED objects |
| 18 | +buttons = [] |
| 19 | +leds = [] |
| 20 | + |
| 21 | +# The keycode sent for each switch/button |
| 22 | +buttonkeys = [Keycode.B, Keycode.C, Keycode.A] |
| 23 | +buttonspressed = [False, False, False] |
| 24 | +buttonspressedlast = [False, False, False] |
| 25 | + |
| 26 | +# the keyboard object! |
| 27 | +kbd = Keyboard(usb_hid.devices) |
| 28 | +# we're americans :) |
| 29 | +layout = KeyboardLayoutUS(kbd) |
| 30 | + |
| 31 | +# make all button pin objects, make them inputs w/pullups |
| 32 | +for pin in buttonpins: |
| 33 | + button = DigitalInOut(pin) |
| 34 | + button.direction = Direction.INPUT |
| 35 | + button.pull = Pull.UP |
| 36 | + buttons.append(button) |
| 37 | + |
| 38 | +# make all LED objects, make them outputs |
| 39 | +for pin in ledpins: |
| 40 | + led = DigitalInOut(pin) |
| 41 | + led.direction = Direction.OUTPUT |
| 42 | + leds.append(led) |
| 43 | + |
| 44 | +# set up the status LED |
| 45 | +statusled = DigitalInOut(board.D13) |
| 46 | +statusled.direction = Direction.OUTPUT |
| 47 | + |
| 48 | +print("Waiting for button presses") |
| 49 | + |
| 50 | + |
| 51 | +def pressbutton(index): |
| 52 | + switch_led = leds[index] # find the switch LED |
| 53 | + k = buttonkeys[index] # get the corresp. keycode/str |
| 54 | + switch_led.value = True # turn on LED |
| 55 | + kbd.press(k) # send keycode |
| 56 | + |
| 57 | + |
| 58 | +def releasebutton(index): |
| 59 | + switch_led = leds[index] # find the switch LED |
| 60 | + k = buttonkeys[index] # get the corresp. keycode/str |
| 61 | + switch_led.value = False # turn on LED |
| 62 | + kbd.release(k) # send keycode |
| 63 | + |
| 64 | +while True: |
| 65 | + # check each button |
| 66 | + for button in buttons: |
| 67 | + i = buttons.index(button) |
| 68 | + if button.value is False: # button is pressed? |
| 69 | + buttonspressed[i] = True # save pressed button |
| 70 | + # was button not pressed last time? |
| 71 | + if buttonspressedlast[i] is False: |
| 72 | + print("Pressed #%d" % i) |
| 73 | + pressbutton(i) |
| 74 | + else: |
| 75 | + buttonspressed[i] = False # button was not pressed |
| 76 | + if buttonspressedlast[i] is True: # was button pressed last time? |
| 77 | + print("Released #%d" % i) |
| 78 | + releasebutton(i) |
| 79 | + #lightneopixels() |
| 80 | + # save pressed buttons as pressed last |
| 81 | + buttonspressedlast = list(buttonspressed) |
| 82 | + time.sleep(0.01) |
0 commit comments