-1

I have a reed switch (normally closed) attached to a Pico that when open send two key presses to the PC. I need a third key press to be sent when the switch is returned to the closed position.

My question then is whether all of this can be included in one script somehow, or do I need two scripts which will work concurrently or together in some way?

Below is the code for the keypresses when the switch is opened -

import board
from digitalio import DigitalInOut, Pull
from adafruit_debouncer import Debouncer
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kpd = Keyboard(usb_hid.devices)

# define buttons
NUM_KEYS = 2
PINS = (
    board.GP0,
    board.GP1,
)

KEYMAP = (
    ("Answer", [Keycode.CONTROL, Keycode.SHIFT, Keycode.P]),
    ("Headset", [Keycode.CONTROL, Keycode.SHIFT, Keycode.A]),
)

keys = []
for pin in PINS:
    dio = DigitalInOut(pin)
    dio.pull = Pull.UP
    keys.append(Debouncer(dio))

print("\nWelcome to keypad")
print("keymap:")
for k in range(NUM_KEYS):
    print("\t", (KEYMAP[k][0]))

while True:
    for i in range(NUM_KEYS):
        keys[i].update()
        if keys[i].rose:
            print(KEYMAP[i][0])
            kpd.send(*KEYMAP[i][1])

Any help/guidance much appreciated.

1 Answer 1

0

I have managed to solve this now with the following, much simpler script -

import board
import digitalio
import usb_hid
from adafruit_debouncer import Debouncer
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)

pin = digitalio.DigitalInOut(board.GP0)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
switch = Debouncer(pin)

while True:
    switch.update()
    if switch.rose:
        print("Open")
        kbd.send(Keycode.SHIFT, Keycode.P,)
        kbd.send(Keycode.SHIFT, Keycode.A,)
    if switch.fell:
        print("Closed")
        kbd.send(Keycode.SHIFT, Keycode.S,)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.