Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5415

Troubleshooting • Re: Microcontroller-based HID keyboard gives random inputs on Raspberry Pi 4

$
0
0
Quick update now that I'm home. The code that I posted in the most recent reply was basically spot on. Here is the exact code I was using:

Code:

# Write your code here :-)# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries# SPDX-License-Identifier: MIT# RaspberryPi Pico RP2040 Mechanical Keyboardimport timeimport boardfrom digitalio import DigitalInOut, Direction, Pullimport usb_hidfrom adafruit_hid.keyboard import Keyboardfrom adafruit_hid.keycode import Keycodefrom adafruit_hid.consumer_control import ConsumerControlfrom adafruit_hid.consumer_control_code import ConsumerControlCodeprint("---Pico Pad Keyboard---")led = DigitalInOut(board.LED)led.direction = Direction.OUTPUTled.value = Truekbd = Keyboard(usb_hid.devices)cc = ConsumerControl(usb_hid.devices)# list of pins to use (skipping GP15 on Pico because it's funky)pins = (    board.GP0,    board.GP1,    board.GP2,    board.GP3)MEDIA = 1KEY = 2keymap = {    (0): (KEY, [Keycode.LEFT_ARROW]),    (1): (KEY, [Keycode.RIGHT_ARROW]),    (2): (KEY, [Keycode.DOWN_ARROW]),    (3): (KEY, [Keycode.UP_ARROW])    }switches = []for i in range(len(pins)):    switch = DigitalInOut(pins[i])    switch.direction = Direction.INPUT    switch.pull = Pull.UP    switches.append(switch)switch_state = [0, 0, 0, 0]while True:    for button in range(4):        if switch_state[button] == 0:            if not switches[button].value:                try:                    if keymap[button][0] == KEY:                        kbd.press(*keymap[button][1])                    else:                        cc.send(keymap[button][1])                except ValueError:  # deals w six key limit                    pass                switch_state[button] = 1        if switch_state[button] == 1:            if switches[button].value:                try:                    if keymap[button][0] == KEY:                        kbd.release(*keymap[button][1])                except ValueError:                    pass                switch_state[button] = 0    time.sleep(0.1)  # debounce

Statistics: Posted by nm720bike — Mon Sep 02, 2024 8:30 pm



Viewing all articles
Browse latest Browse all 5415

Trending Articles