diff options
author | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
commit | d311fd8d1a0af3b767d26b520d399bf0db22f058 (patch) | |
tree | b9de5704143ac205b4e26222f9d7551e9b161924 /tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp | |
parent | a766918d5c48204375f4c207b30bbbf1389df14f (diff) | |
parent | 6f3141965e88c4ee0ebf7c18a243e4c2d9c5021b (diff) |
Merge pull request #34 from jackhumbert/tmk-master
merging from tmk, new_project script and template
Diffstat (limited to 'tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp')
-rw-r--r-- | tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp new file mode 100644 index 0000000000..083b95cac5 --- /dev/null +++ b/tmk_core/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); +} |