Hi guys,
new day, new problem, new question.
Im sending multiple Values over 2 XBEE modules. The values are coming from the USB HOST 2 library/USB Joystick. I can use the Datas when im using one Arduino. But now i though its more easier to handle the Datas when they are send over XBEE's.
Thats what i send via the library
Buf: 0817EB350010
In the code you can see where it comes from. They pity is: i dont know where the "BUF: 08...." is coming from....so i dont know the file and line. I thought i could easily take the code from the time when i only had the USB Joystick and the Arduino...but i didnt get it.
I tried to separate the Datas like it did before but i got all the time error messages.
Im reading in the second Arduino the Datas via WirelessData = Serial.read()
. In the old example without XBEE's i had the "evt" marker and i could get the values out of the BUF value with the marker. My idea was to replace in the lines
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
with
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t WirelessData);
unfortunately it wasnt that easy. Can anyone help me?
Code of the Project:
hid joystickparser.h
#if !defined(__HIDJOYSTICKRPTPARSER_H__)
#define __HIDJOYSTICKRPTPARSER_H__
#include <inttypes.h>
#include <avr/pgmspace.h>
#include "avrpins.h"
#include "max3421e.h"
#include "usbhost.h"
#include "usb_ch9.h"
#include "Usb.h"
#if defined(ARDUINO) && ARDUINO >=100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif
#include "printhex.h"
#include "hexdump.h"
#include "message.h"
#include "confdescparser.h"
#include "hid.h"
struct GamePadEventData
{
uint8_t X, Y, Z1, Z2, Rz;
};
class JoystickEvents
{
public:
virtual void OnGamePadChanged(const GamePadEventData *evt);
virtual void OnHatSwitch(uint8_t hat);
virtual void OnButtonUp(uint8_t but_id);
virtual void OnButtonDn(uint8_t but_id);
};
#define RPT_GEMEPAD_LEN 5
class JoystickReportParser : public HIDReportParser
{
JoystickEvents *joyEvents;
uint8_t oldPad[RPT_GEMEPAD_LEN];
uint8_t oldHat;
uint16_t oldButtons;
public:
JoystickReportParser(JoystickEvents *evt);
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};
#endif // __HIDJOYSTICKRPTPARSER_H__
hidjoystickrtparser.cpp
#include "hidjoystickrptparser.h"
int JoyVarBtn;
int JoyVarX;
int JoyVarY;
int JoyVarZ;
int JoyVarBtnBase;
int JoyVarHAT;
int JoyVarBtnDn;
int JoyVarBtnUp;
JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt),
oldHat(0xDE),
oldButtons(0)
{
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
oldPad[i] = 0xD;
}
void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
bool match = true;
// Checking if there are changes in report since the method was last called
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
if (buf[i] != oldPad[i])
{
match = false;
break;
}
// Calling Game Pad event handler
if (!match && joyEvents)
{
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
}
uint8_t hat = (buf[5] & 0xF);
// Calling Hat Switch event handler
if (hat != oldHat && joyEvents)
{
joyEvents->OnHatSwitch(hat);
oldHat = hat;
}
uint16_t buttons = (0x0000 | buf[6]);
buttons <<= 4;
buttons |= (buf[5] >> 4);
uint16_t changes = (buttons ^ oldButtons);
// Calling Button Event Handler for every button changed
if (changes)
{
for (uint8_t i=0; i<0x0C; i++)
{
uint16_t mask = (0x0001 << i);
if (((mask & changes) > 0) && joyEvents)
if ((buttons & mask) > 0)
joyEvents->OnButtonDn(i+1);
else
joyEvents->OnButtonUp(i+1);
}
oldButtons = buttons;
}
}
void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
/* Serial.print("X: ");
PrintHex<uint8_t>(evt->X);
*/
JoyVarBtn = (evt->X);
JoyVarX = (evt->Y);
JoyVarY = (evt->Z1);
JoyVarZ = (evt->Z2);
JoyVarBtnBase = (evt->Rz);
}
void JoystickEvents::OnHatSwitch(uint8_t hat)
{
Serial.print("Hat Switch: ");
PrintHex<uint8_t>(hat);
Serial.println("");
JoyVarHAT = (hat);
}
void JoystickEvents::OnButtonUp(uint8_t but_id)
{
Serial.print("Up: ");
Serial.println(but_id, DEC);
JoyVarBtnUp = (but_id);
}
void JoystickEvents::OnButtonDn(uint8_t but_id)
{
Serial.print("Dn: ");
JoyVarBtnDn = (but_id);
}