So I have been researching for weeks on how to use a USB host shield to read data from an attached HID device as serial data that can be used by the SoftwareSerial or Serial libraries, allowing users to connect a USB gamepad or joystick without breaking it open and re-wiring it directly to Arduino pins.
I found this article: https://www.circuitsathome.com/mcu/hid-joystick-code-sample/
which seemed to have some good information, along with some other posts on this forum where people much more skilled than me seem to have thrown in the towel on this and broken their controller open. I know there is another way, and maybe with the infinite knowledge of some of the poeple on these forums we can figure it out!
here is a snippet of the code i'm using to control my project that works when using my HC-05 and SoftwareSerial libraries:
#define DIR_UP 1
#define DIR_DOWN 2
#define DIR_LEFT 3
#define DIR_RIGHT 4
/* *** Bluetooth controller button defines and input method *** */
#define BTN_NONE 0
#define BTN_UP 1
#define BTN_DOWN 2
#define BTN_LEFT 3
#define BTN_RIGHT 4
#define BTN_START 5
#define BTN_EXIT 6
uint8_t curControl = BTN_NONE;
SoftwareSerial bluetooth(10, 11);
void readInput(){
curControl = BTN_NONE;
if (bluetooth.available() > 0) {
// read the incoming byte:
uint8_t incomingByte = bluetooth.read();
Serial.println(incomingByte);
switch(incomingByte){
case 238:
curControl = BTN_LEFT;
break;
case 239:
curControl = BTN_RIGHT;
break;
case 236:
curControl = BTN_UP;
break;
case 237:
curControl = BTN_DOWN;
break;
case 224:
curControl = BTN_START;
break;
case 225:
curControl = BTN_EXIT;
break;
}
}
}
I also found this code in the Examples of the USBHostShield 2.0 library UsbhidJoystick to collect incoming signal data from my USB gamepad using the host shield:
#include <usbhid.h>
#include <hiduniversal.h>
#include <usbhub.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
#include "hidjoystickrptparser.h"
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
JoystickEvents JoyEvents;
JoystickReportParser Joy(&JoyEvents);
void setup() {
Serial.begin(115200);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("OSC did not start.");
delay(200);
if (!Hid.SetReportParser(0, &Joy))
ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);
}
void loop() {
Usb.Task();
}
When I connected the gamepad, i wrote down the commands that came in via Serial Monitor for each button pressed. Each button yielded 2 lines of coordinates in this format:
right X1: 01 Y1: 7F X2: 7F Y2: FF Rz: 7F
X1: 01 Y1: 7F X2: 7F Y2: 7F Rz: 7F
up X1: 01 Y1: 7F X2: 7F Y2: 7F Rz: 00
X1: 01 Y1: 7F X2: 7F Y2: 7F Rz: 7F
My question is, does anyone know how to convert this incoming data from the USB controller into the simple BTN_Down or DIR_down commands needed to control the program? I am trying to cannibalize the example code into my project as it seems to illustrate that the Host Shield is receiving consistent data from the controller, now I just need to program my project to read the USB data like the example code and convert the coordinate input data into the 7 simple BTN or DIR commands needed for the project. I am open to reposting this, but still fairly new to the forums and even newer to Arduino programming. Anyone have any ideas or should I throw in the towel like so many have before me?