This post is a follow up to the ESP as a USB host. The EspUsbHost library worked well with a keyboard and mouse and I wanted to take a look and see if I could run a game controller using the same library. I managed to get a SNES type controller working and these are the steps I took.
Video link on YouTube : https://youtu.be/0RI_DfXpfEA
You do not have to follow this if all you want is the library you can download the zip file. If you are interested you can open and examine the library files with notepad and follow the notes. If you see where things can be done better I would be glad to here it.
/1.
I made copies of the two files EspUsbHost.h and EspUsbHost.cpp. I wanted to preserve the original library so that I could still get updates without erasing the game controller code, also if mistakes were made I could easily start over.
/2.
Every occurrence of the name EspUsbHost in the two copies was changed to GamepadHost, only one thing was deleted from these two copies everything else was an addition.
/3.
Before each modification in the files I wrote // NOTE & a number, there are 7 "NOTES" in total.
/4.
GamepadHost.h // NOTE 1 this was the sole deletion, I deleted #include <class/hid/hid.h> and replaced it with #include <gamepadhid.h>
The hid.h library contains device structures and enumerators and there was not one to match my device so I copied hid.h and created gamepad.h
/5.
GamepadHost.h // NOTE 2 the line below NOTE 2 is a function prototype for the virtual function onGameButtons
/6.
GamepadHost.cpp // NOTE 3 there are 12 lines below NOTE 3 which is an else if and looks for the game controller interface protocol, which is 0 for my device, if the protocol matches the device data is parsed and placed in the fields of a gamepadhid.h structure. The structure is named hid_gamepad_report_t and the instance is named report, report is what we want to make available to the user.
/7.
GamepadHost.cpp // NOTE 4 is the onGameButtons method and the method to use in our programs.
/8.
GamepadHost.cpp // NOTE 5 and // NOTE 6 if the line below NOTE 5 and the line below NOTE 6 are commented out it enables a feature to print the REPORT DESCRIPTOR to the Arduino IDE serial monitor.
/9.
gamepadhid.h // NOTE 7 this is the structure of three uint8_t, one for the D-pad X and one for the D-pad Y and the last for the remaining eight buttons. Below that is the enumeration of each of the buttons.
The library will install with the Arduino IDE "Add .ZIP Library" or if you have trouble it can be installed to the libraries folder manually. Restart the IDE and make sure you have ESP32-S3-USB-OTG as your board, then under File/Examples scroll down to GamepadHost > gamepad for a quick example sketch.
Very cool! At the moment I have no need for Game Pad support (mine were for RetroPie), however, for robot control or something, I think this would be very useful.
I found out, this morning actually, that PicoMite BASIC has built-in support for the Game Pad controllers. It also has support for the Wii controllers (Classic and Numchuck.) I almost ordered a Wii Classic controller to play with, but realized that I have too many projects going right now anyway. :(
I have actually seen some game controllers in the recycling center that I frequent, but haven't seen any for a little while. If I see more, I may pick some up to play with.
I just posted a short video on YouTube if you are interested, I have updated the OP but here it is also
https://youtu.be/0RI_DfXpfEA
What I did not mention was that this set up gives the opportunity of wireless remote control as well as hardwired.