Hello, for my project im trying to read the data from my USB joystick (buttonpresses) to power some relays. this is the first time i try something like this, some how i cant seem to use the Hexadecimal data from my structure in my IF statement, any tips?
(if i press a button on the joystick, i can see in the serial monitor on point a, 20 being printed. i want to use this hexadecimal number in an IF statement to power a pin)
#include <usbhid.h>
#include <hiduniversal.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
/////////////////////////////////////////////////////////////
struct GamePadEventData
{
uint8_t f; // https://playground.arduino.cc/Code/Struct/
uint8_t c;
uint8_t a;
uint8_t b;
uint8_t d;
uint8_t e;
}
;
////////////////////////////////////////////////////////////////////////////////////////////////////
__attribute__((packed));
class JoystickEvents
{
public:
virtual void OnGamePadChanged(const GamePadEventData *evt);
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
#define RPT_GAMEPAD_LEN sizeof(GamePadEventData)
class JoystickReportParser : public HIDReportParser
{
JoystickEvents *joyEvents;
uint8_t oldPad[RPT_GAMEPAD_LEN];
public:
JoystickReportParser(JoystickEvents *evt);
virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};
JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt)
{}
void JoystickReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
// Checking if there are changes in report since the method was last called
bool match = (sizeof(oldPad) == len) && (memcmp(oldPad, buf, len) == 0);
//Calling Game Pad event handler
if (!match && joyEvents) {
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
memcpy(oldPad, buf, len);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
Serial.print("A: ");
PrintHex<uint8_t>(evt->a, 0x80);
////////////////////////
Serial.print(" B: ");
PrintHex<uint8_t>(evt->b, 0x80);
/////////////////////////////
Serial.print(" C: ");
PrintHex<uint8_t>(evt->c, 0x80);
//////////////////////////
Serial.print(" D: ");
PrintHex<uint8_t>(evt->d, 0x80);
//////////////////////////////
Serial.print(" E: ");
PrintHex<uint8_t>(evt->e, 0x80);
////////////////////////////////////
Serial.print(" F: ");
PrintHex<uint8_t>(evt->f, 0x80);
//////////////////////////////////////
Serial.println();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
#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 );
pinMode(43, OUTPUT);
pinMode(45, OUTPUT);
pinMode(49, OUTPUT);
pinMode(47, OUTPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
Usb.Task();
// Here is where im trying to use the data from the struct (in the top):
//GamePadEventData
// uint8_t f;
// uint8_t c;
// uint8_t a;
// uint8_t b;
// uint8_t d;
// uint8_t e;
// to use in a if function to power some pins,
// somehow i cant just do:
// if (GamePadEventData.a) == 20)
// {
// digitalWrite(43, HIGH);
// }
// any ideas? or tips?
}