選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

hidjoystickrptparser.cpp 2.6KB

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