You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PS4Parser.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. e-mail : [email protected]
  13. */
  14. #include "PS4Parser.h"
  15. // To enable serial debugging see "settings.h"
  16. //#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller
  17. bool PS4Parser::checkDpad(ButtonEnum b) {
  18. switch (b) {
  19. case UP:
  20. return ps4Data.btn.dpad == DPAD_LEFT_UP || ps4Data.btn.dpad == DPAD_UP || ps4Data.btn.dpad == DPAD_UP_RIGHT;
  21. case RIGHT:
  22. return ps4Data.btn.dpad == DPAD_UP_RIGHT || ps4Data.btn.dpad == DPAD_RIGHT || ps4Data.btn.dpad == DPAD_RIGHT_DOWN;
  23. case DOWN:
  24. return ps4Data.btn.dpad == DPAD_RIGHT_DOWN || ps4Data.btn.dpad == DPAD_DOWN || ps4Data.btn.dpad == DPAD_DOWN_LEFT;
  25. case LEFT:
  26. return ps4Data.btn.dpad == DPAD_DOWN_LEFT || ps4Data.btn.dpad == DPAD_LEFT || ps4Data.btn.dpad == DPAD_LEFT_UP;
  27. default:
  28. return false;
  29. }
  30. }
  31. bool PS4Parser::getButtonPress(ButtonEnum b) {
  32. if (b <= LEFT) // Dpad
  33. return checkDpad(b);
  34. else
  35. return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]));
  36. }
  37. bool PS4Parser::getButtonClick(ButtonEnum b) {
  38. uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
  39. bool click = buttonClickState.val & mask;
  40. buttonClickState.val &= ~mask; // Clear "click" event
  41. return click;
  42. }
  43. uint8_t PS4Parser::getAnalogButton(ButtonEnum b) {
  44. if (b == L2) // These are the only analog buttons on the controller
  45. return ps4Data.trigger[0];
  46. else if (b == R2)
  47. return ps4Data.trigger[1];
  48. return 0;
  49. }
  50. uint8_t PS4Parser::getAnalogHat(AnalogHatEnum a) {
  51. return ps4Data.hatValue[(uint8_t)a];
  52. }
  53. void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
  54. if (len > 1 && buf) {
  55. #ifdef PRINTREPORT
  56. Notify(PSTR("\r\n"), 0x80);
  57. for (uint8_t i = 0; i < len; i++) {
  58. D_PrintHex<uint8_t > (buf[i], 0x80);
  59. Notify(PSTR(" "), 0x80);
  60. }
  61. #endif
  62. if (buf[0] == 0x01) // Check report ID
  63. memcpy(&ps4Data, buf + 1, min((uint8_t)(len - 1), sizeof(ps4Data)));
  64. else if (buf[0] == 0x11) { // This report is send via Bluetooth, it has an offset of 2 compared to the USB data
  65. if (len < 4) {
  66. #ifdef DEBUG_USB_HOST
  67. Notify(PSTR("\r\nReport is too short: "), 0x80);
  68. D_PrintHex<uint8_t > (len, 0x80);
  69. #endif
  70. return;
  71. }
  72. memcpy(&ps4Data, buf + 3, min((uint8_t)(len - 3), sizeof(ps4Data)));
  73. } else {
  74. #ifdef DEBUG_USB_HOST
  75. Notify(PSTR("\r\nUnknown report id: "), 0x80);
  76. D_PrintHex<uint8_t > (buf[0], 0x80);
  77. #endif
  78. return;
  79. }
  80. if (ps4Data.btn.val != oldButtonState.val) { // Check if anything has changed
  81. buttonClickState.val = ps4Data.btn.val & ~oldButtonState.val; // Update click state variable
  82. oldButtonState.val = ps4Data.btn.val;
  83. // The DPAD buttons does not set the different bits, but set a value corresponding to the buttons pressed, we will simply set the bits ourself
  84. uint8_t newDpad = 0;
  85. if (checkDpad(UP))
  86. newDpad |= 1 << UP;
  87. if (checkDpad(RIGHT))
  88. newDpad |= 1 << RIGHT;
  89. if (checkDpad(DOWN))
  90. newDpad |= 1 << DOWN;
  91. if (checkDpad(LEFT))
  92. newDpad |= 1 << LEFT;
  93. if (newDpad != oldDpad) {
  94. buttonClickState.dpad = newDpad & ~oldDpad; // Override values
  95. oldDpad = newDpad;
  96. }
  97. }
  98. }
  99. if (ps4Output.reportChanged)
  100. sendOutputReport(&ps4Output); // Send output report
  101. }