Trinket M0 - HID gamepad help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket M0 - HID gamepad help

by caleb_kraft on Wed Sep 22, 2021 4:45 pm

I've got a trinket M0 that I need to set up as an hid gamepad (not keyboard/mouse). It just needs to have a single thumbstick attached.

I'm running into issues getting it to run code though. Even the HID keyboard example fails (as does the default code that comes loaded on it). When I reboot the trinket the dotstar just flashes red.

I know the board is fine as I can run the cap touch example found here just fine.

The trinket is running the latest version of circuitpython (7)

I've found an example of gamepad code, but it mentions needing a boot.py and some other complexities. I'm trying to simply get the examples functional before trying to figure out anything more complex.

Posts: 5
Joined: Wed Sep 22, 2021 1:21 pm

Re: Trinket M0 - HID gamepad help

by mikeysklar on Thu Sep 23, 2021 8:10 pm

Hey Caleb,

Let's start with hid_simple_gamepad.py example below. Is this the one you have been using? I see it has been updated in the last month.

https://github.com/adafruit/Adafruit_Ci ... gamepad.py

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# You must add a gamepad HID device inside your boot.py file
# in order to use this example.
# See this Learn Guide for details:
# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9

import board
import digitalio
import analogio
import usb_hid

from hid_gamepad import Gamepad

gp = Gamepad(usb_hid.devices)

# Create some buttons. The physical buttons are connected
# to ground on one side and these and these pins on the other.
button_pins = (board.D2, board.D3, board.D4, board.D5)

# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons[i] will send that button number when buttons[i]
# is pushed.
gamepad_buttons = (1, 2, 8, 15)

buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP

# Connect an analog two-axis joystick to A4 and A5.
ax = analogio.AnalogIn(board.A4)
ay = analogio.AnalogIn(board.A5)

# Equivalent of Arduino's map() function.
def range_map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min

while True:
    # Buttons are grounded when pressed (.value = False).
    for i, button in enumerate(buttons):
        gamepad_button_num = gamepad_buttons[i]
        if button.value:
            print(" release", gamepad_button_num, end="")
            print(" press", gamepad_button_num, end="")

    # Convert range[0, 65535] to -127 to 127
        x=range_map(ax.value, 0, 65535, -127, 127),
        y=range_map(ay.value, 0, 65535, -127, 127),
    print(" x", ax.value, "y", ay.value)

Please make sure your libraries in /lib are current with our Bundle release 20210922.

https://github.com/adafruit/Adafruit_Ci ... 210922.zip

If you keep getting an error please paste in anything relevant you see on the REPL console and we can take it from there.

Posts: 5855
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.