custom-keyboardraspberry-pi-picogamepadadafruit-circuitpythonusb-hid

Footpedal with pi pico and circut python


Im building my own footpedal for my pc and got it working more or less. I can send the he code so it acts as a keyboard but I want it to display as a FOOTPEDAL1 .. 2 .. 3 .. 4. I downloaded this code which has 16 buttons which are calles GAMEPAD1 .. 2...

First problem: The code just outputs every second a GAMEPAD1 then counting up to 16 and starting again. But I want to set FOOTPEDAL1 with the GP0 pin.

Second problem: The code names it GAMEPAD1... but I want it FOOTPEDAL1...

This is proof that it outputs a GAMEPAD key but a random

Here is the boot code:

import usb_hid

# This is only one example of a gamepad descriptor, and may not suit your needs.
GAMEPAD_REPORT_DESCRIPTOR = bytes((
    0x05, 0x01,  # Usage Page (Generic Desktop Ctrls)
    0x09, 0x05,  # Usage (Game Pad)
    0xA1, 0x01,  # Collection (Application)
    0x85, 0x04,  #   Report ID (4)
    0x05, 0x09,  #   Usage Page (Button)
    0x19, 0x01,  #   Usage Minimum (Button 1)
    0x29, 0x10,  #   Usage Maximum (Button 16)
    0x15, 0x00,  #   Logical Minimum (0)
    0x25, 0x01,  #   Logical Maximum (1)
    0x75, 0x01,  #   Report Size (1)
    0x95, 0x10,  #   Report Count (16)  ## 2 bytes
    0x81, 0x02,  #   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
    0xC0,        # End Collection      ## total 2 bytes
))

gamepad = usb_hid.Device(
    report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
    usage_page=0x01,           # Generic Desktop Control       ## Must match Usage Page above!
    usage=0x05,                # Gamepad                       ## Must match Usage above!
    report_ids=(4,),           # Descriptor uses report ID 4.  ## Must match Report ID above!
    in_report_lengths=(2,),    # This gamepad sends 2 bytes in its report. ## Must match number of bytes above!
    out_report_lengths=(0,),   # It does not receive any reports.
)

usb_hid.enable(
    (usb_hid.Device.KEYBOARD,
     usb_hid.Device.MOUSE,
     usb_hid.Device.CONSUMER_CONTROL,
     gamepad)
)

And here is the main code:

import time
import struct
import usb_hid
import adafruit_hid

gamepad_device = adafruit_hid.find_device(usb_hid.devices, usage_page=0x1, usage=0x05)

print("gamepad_device:",gamepad_device)

def send_gamepad_report(button_state):
    report = bytearray(2)  ## must be same size as specified in HID Report Descriptor in boot.py
    #report_id = 4  ## must match what's in HID Report Descriptor in boot.py
    struct.pack_into(
        "<H",      # little endian, one 2-byte value
        report,
        0,
        button_state
    )
    print(["%02x" % x for x in report])
    gamepad_device.send_report(report)
    
# utility to help us set & clear bits in the button_state
def set_bit(v,i):
    v |= (1 << i)
    return v

def clr_bit(v,i):
    v &= ~(1 << i)
    return v

button_state = 0x00  # holds the state of our buttons
button_num = 0 # which button are we current toggling

# while True:
     print("pushing button ", (button_num+1))  # human buttons start at one not zero
    
    # push button
     button_state = set_bit(button_state, button_num)
    
     send_gamepad_report(button_state)
     time.sleep(1)
    
     print("releasing button ", (button_num+1))  # human buttons start at one not zero

    # release button
     button_state = clr_bit(button_state, button_num)
     send_gamepad_report(button_state)
     time.sleep(1)

    # go to next button
     button_num = (button_num+1) % 16

I would be very happy to get a response!

It gives out all GAMEPAD from 1 to 16. It's counting 1 up every second. As soon as I connect a 3.3V pin to any GP pin, it outputs the current GAMEPAD(number).


Solution

  • Well, the code works as expected. The main loop is just for testing the functionality of the boot code together with the send_gamepad_report() function.

    There is no code that checks a real button - yet. That's what you have to provide.

    Your new main loop should look like this:

    HTH