-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidjoystickrptparser.cpp
97 lines (82 loc) · 3.06 KB
/
hidjoystickrptparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "hidjoystickrptparser.h"
int ppm[6];
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(USBHID *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("X1: ");
//PrintHex<uint8_t > (evt->X, 0x80);
ppm[0] = map(evt->X, 0, 255, 1000, 2000);
Serial.print("CH1 (elevator): ");
Serial.print(ppm[0]);
//Serial.print("\tY1: ");
//PrintHex<uint8_t > (evt->Y, 0x80);
ppm[1] = map(evt->Y, 0, 255, 1000, 2000);
Serial.print(" | CH2 (rudder): ");
Serial.print(ppm[1]);
//Serial.print("\tX2: ");
//PrintHex<uint8_t > (evt->Z1, 0x80);
ppm[2] = map(evt->Z1, 0, 255, 1000, 2000);
Serial.print(" | CH3 (throttle): ");
Serial.print(ppm[2]);
Serial.println("");
//Serial.print("\tY2: ");
//PrintHex<uint8_t > (evt->Z2, 0x80);
//Serial.print("\tRz: ");
//PrintHex<uint8_t > (evt->Rz, 0x80);
//Serial.println("");
}
void JoystickEvents::OnHatSwitch(uint8_t hat) {
Serial.print("Hat Switch: ");
PrintHex<uint8_t > (hat, 0x80);
Serial.println("");
}
void JoystickEvents::OnButtonUp(uint8_t but_id) {
Serial.print("Up: ");
Serial.println(but_id, DEC);
}
void JoystickEvents::OnButtonDn(uint8_t but_id) {
Serial.print("Dn: ");
Serial.println(but_id, DEC);
}