summaryrefslogtreecommitdiffstats
path: root/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
diff options
context:
space:
mode:
authortmk <hasu@tmk-kbd.com>2015-05-13 04:13:10 +0200
committertmk <hasu@tmk-kbd.com>2015-05-13 04:13:10 +0200
commitf6d56675f9f981c5464f0ca7a1fbb0162154e8c5 (patch)
tree57c9d4b3808a26116ae0ee7956fc00b84841aa2b /protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
parent4d116a04e94cf0d19317d5b44e4fa9f34a3e5594 (diff)
downloadqmk_firmware-f6d56675f9f981c5464f0ca7a1fbb0162154e8c5.tar.gz
qmk_firmware-f6d56675f9f981c5464f0ca7a1fbb0162154e8c5.tar.xz
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
dc0e46e Rename LUFA to LUFA-git 3bfa7fa Remove LUFA-120730 215b764 Merge commit 'afa0f22a9299686fd88f58ce09c5b521ac917e8f' as 'protocol/lufa/LUFA' afa0f22 Squashed 'protocol/lufa/LUFA/' content from commit def7fca c0c42fa Remove submodule of LUFA 30f897d Merge commit '87ced33feb74e79c3281dda36eb6d6d153399b41' as 'protocol/usb_hid/USB_Host_Shield_2.0' 87ced33 Squashed 'protocol/usb_hid/USB_Host_Shield_2.0/' content from commit aab4a69 14f6d49 Remove submodule of USB_Host_Shield_2.0 git-subtree-dir: tmk_core git-subtree-split: dc0e46eaa4367d4e218f8816e3c117895820f07c
Diffstat (limited to 'protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp')
-rw-r--r--protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp b/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
new file mode 100644
index 000000000..083b95cac
--- /dev/null
+++ b/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
@@ -0,0 +1,84 @@
+#include "hidjoystickrptparser.h"
+
+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("X1: ");
+ PrintHex<uint8_t > (evt->X, 0x80);
+ Serial.print("\tY1: ");
+ PrintHex<uint8_t > (evt->Y, 0x80);
+ Serial.print("\tX2: ");
+ PrintHex<uint8_t > (evt->Z1, 0x80);
+ 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);
+}