]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / examples / HID / USBHIDJoystick / hidjoystickrptparser.cpp
1 #include "hidjoystickrptparser.h"
2
3 JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
4 joyEvents(evt),
5 oldHat(0xDE),
6 oldButtons(0) {
7 for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)
8 oldPad[i] = 0xD;
9 }
10
11 void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
12 bool match = true;
13
14 // Checking if there are changes in report since the method was last called
15 for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)
16 if (buf[i] != oldPad[i]) {
17 match = false;
18 break;
19 }
20
21 // Calling Game Pad event handler
22 if (!match && joyEvents) {
23 joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
24
25 for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
26 }
27
28 uint8_t hat = (buf[5] & 0xF);
29
30 // Calling Hat Switch event handler
31 if (hat != oldHat && joyEvents) {
32 joyEvents->OnHatSwitch(hat);
33 oldHat = hat;
34 }
35
36 uint16_t buttons = (0x0000 | buf[6]);
37 buttons <<= 4;
38 buttons |= (buf[5] >> 4);
39 uint16_t changes = (buttons ^ oldButtons);
40
41 // Calling Button Event Handler for every button changed
42 if (changes) {
43 for (uint8_t i = 0; i < 0x0C; i++) {
44 uint16_t mask = (0x0001 << i);
45
46 if (((mask & changes) > 0) && joyEvents)
47 if ((buttons & mask) > 0)
48 joyEvents->OnButtonDn(i + 1);
49 else
50 joyEvents->OnButtonUp(i + 1);
51 }
52 oldButtons = buttons;
53 }
54 }
55
56 void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
57 Serial.print("X1: ");
58 PrintHex<uint8_t > (evt->X, 0x80);
59 Serial.print("\tY1: ");
60 PrintHex<uint8_t > (evt->Y, 0x80);
61 Serial.print("\tX2: ");
62 PrintHex<uint8_t > (evt->Z1, 0x80);
63 Serial.print("\tY2: ");
64 PrintHex<uint8_t > (evt->Z2, 0x80);
65 Serial.print("\tRz: ");
66 PrintHex<uint8_t > (evt->Rz, 0x80);
67 Serial.println("");
68 }
69
70 void JoystickEvents::OnHatSwitch(uint8_t hat) {
71 Serial.print("Hat Switch: ");
72 PrintHex<uint8_t > (hat, 0x80);
73 Serial.println("");
74 }
75
76 void JoystickEvents::OnButtonUp(uint8_t but_id) {
77 Serial.print("Up: ");
78 Serial.println(but_id, DEC);
79 }
80
81 void JoystickEvents::OnButtonDn(uint8_t but_id) {
82 Serial.print("Dn: ");
83 Serial.println(but_id, DEC);
84 }
Imprint / Impressum