Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

HID Gamepad on CPX
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

HID Gamepad on CPX

by lerxst151 on Mon Aug 31, 2020 11:50 am

I'm trying to get a test setup using a CPX with the HID gamepad library.
I am on circuitpython 5.3.1 with the 5.x adafruit_hid library.
I am trying to use the hid_simple_gamepad.py example
I loaded this in Mu and saved it to the CPX. That part seems to work.
I did comment out the line at the bottom "print(" x", ax.value, "y", ay.value)" so I would not have that in the serial outpout. I am only concerned with the buttons right now.
When I try to open the Serial window i see it displayed "release 1 release 2 release 1 release 2 release 1 release 2 release 1 release 2" even though I have not pushed any of the buttons on the CPX and then Mu freezes up crashes.
When this happens it then corrupts the file system on the CPX and I have to clear that up and reload Circuitpython 5.3.1 on it again.

I did the hid_keyboard_shortcuts.py example and that works with out any problem.

If I do not open the Serial windows how can I tell if the buttons are working?
I do not see the CPX listed under Game Controllers or any where else that I can find in Device Manager that I can open an check the buttons or calibrate the joystick if I was using that part.

Here is the code from the gamepad example.


Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import analogio
import usb_hid

from adafruit_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:
            gp.release_buttons(gamepad_button_num)
            print(" release", gamepad_button_num, end="")
        else:
            gp.press_buttons(gamepad_button_num)
            print(" press", gamepad_button_num, end="")

    # Convert range[0, 65535] to -127 to 127
    gp.move_joysticks(
        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)

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by tannewt on Mon Aug 31, 2020 4:19 pm

Could you try with the latest 6.0.0 pre-release? There may have been a bug that has been fixed.

tannewt
 
Posts: 1784
Joined: Thu Oct 06, 2016 8:48 pm

Re: HID Gamepad on CPX

by lerxst151 on Mon Aug 31, 2020 8:44 pm

Just tried 6.0 alpha 3, same issue.
I works just fine with the keyboard example but not with the game pad and after rebooting the CPX the code.py is unable to be opened again.
I have to wipe the file system and start over.

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by danhalbert on Tue Sep 01, 2020 4:51 pm

Hi, the way the code is written, it will print out the button state over and over. Try commenting out that print statement too, and see if things are better.

danhalbert
 
Posts: 2118
Joined: Tue Aug 08, 2017 12:37 pm

Re: HID Gamepad on CPX

by lerxst151 on Thu Sep 03, 2020 8:32 pm

Changed the code so only the parts for the buttons on in it and commented out the print statements.
It does not crash Mu anymore but I can't tell if it is working. I get no output in the Serial window and it still does not work in my game as far as I can tell.
I still don't see anywhere in Device Manager that it shows up where I can check the button/calibration.

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by danhalbert on Fri Sep 04, 2020 2:32 pm

Below is a simpler program that sends events more slowly, so that you can see what's going on.

I put this program Gemma M0 with CircuitPython 5.3.1, and connected it to Windows 10. I started the program. Then I searched in the Taskbar search box for "Game Controller", and saw "Set up USB Game Controllers." Start that, choose "CircuitPython HID", and then click "Properties". You should see windows like the below. You should see the button 1 circle turning on and off, and the "+" in the square gradually moving.

If this works for you then you're well on your way. But whatever game you're using has to be willing to use this kind of gamepad.

game-controller.png
game-controller.png (24.79 KiB) Viewed 40 times


Here's the simpler program:
Code: Select all | TOGGLE FULL SIZE
import usb_hid
import time

from adafruit_hid.gamepad import Gamepad

gp = Gamepad(usb_hid.devices)

press = True
x = 0
y = 0

while True:
    if press:
        gp.press_buttons(1)
        print("press 1")
    else:
        gp.release_buttons(1)
        print("release 1")
    press = not press

    gp.move_joysticks(x=x-127, y=y-127)
    print("move", x-127, y-127)
    x = (x + 2) % 256
    y = (y + 2) % 256
    time.sleep(1)

danhalbert
 
Posts: 2118
Joined: Tue Aug 08, 2017 12:37 pm

Re: HID Gamepad on CPX

by lerxst151 on Sat Sep 05, 2020 10:11 am

danhalbert

Just tried your code on my CPX and a Trinket M0 and I still don't get anything showing up under Game Controller in Device Manager.
The Game Controller section only shows up if I have my Xbox One controller plugged in.

I just tried all of this also on a brand now Windows 10 laptop...same thing.

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by danhalbert on Sat Sep 05, 2020 10:25 am

It does not show up under Game Controllers; it shows up under HID devices:
hid.png
hid.png (44.63 KiB) Viewed 36 times

To get to the test windows I showed above, do this search in the taskbar:
usb-game.png
usb-game.png (83.96 KiB) Viewed 36 times

danhalbert
 
Posts: 2118
Joined: Tue Aug 08, 2017 12:37 pm

Re: HID Gamepad on CPX

by lerxst151 on Mon Sep 07, 2020 4:16 pm

danhalbert,
Thank you!... that was the part that I was missing.
Everything I read just said it should show up under Game Controllers. I took that to mean I would find it in Device manager where my Xbox controller shows up and not the only way to get to it was by searching for "USB Game Controller"

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by danhalbert on Mon Sep 07, 2020 4:30 pm

Glad you get it working. Yes, it's really confusing. I spent a long time on this when I was developing the software. Many game controllers are "sort of" USB HID controllers, but they have proprietary additions, and need a special driver (which might or might not be built into Windows). So they show up in the Game Controller category. The Xbox controller is an example.

danhalbert
 
Posts: 2118
Joined: Tue Aug 08, 2017 12:37 pm

Re: HID Gamepad on CPX

by lerxst151 on Mon Sep 07, 2020 6:31 pm

I'm guessing you can only have the 16 buttons.... is it possible to have more than just the 1 X and Y?

lerxst151
 
Posts: 24
Joined: Mon Apr 02, 2018 3:19 pm

Re: HID Gamepad on CPX

by danhalbert on Mon Sep 07, 2020 10:27 pm

We chose what kind of Gamepad to support to have it work on multiple operating systems. It might be possible to change, but right now it requires modifying the CircuitPython build. In the future we plan to add user-definable HID devices, including variants on Gamepad: https://github.com/adafruit/circuitpython/issues/1015.

danhalbert
 
Posts: 2118
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.