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.

156 lines
5.2 KiB

  1. /* Gamepad.cpp
  2. *
  3. * Based on the advanced HID library for Arduino:
  4. * https://github.com/NicoHood/HID
  5. * Copyright (c) 2014-2015 NicoHood
  6. *
  7. * Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
  8. *
  9. * GNU GENERAL PUBLIC LICENSE
  10. * Version 3, 29 June 2007
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. *
  25. */
  26. #include "Gamepad.h"
  27. static const uint8_t _hidReportDescriptor[] PROGMEM = {
  28. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  29. 0x09, 0x04, // USAGE (Joystick)
  30. 0xa1, 0x01, // COLLECTION (Application)
  31. 0xa1, 0x00, // COLLECTION (Physical)
  32. 0x05, 0x09, // USAGE_PAGE (Button)
  33. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  34. 0x29, 0x0c, // USAGE_MAXIMUM (Button 12)
  35. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  36. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  37. 0x95, 0x0c, // REPORT_COUNT (12)
  38. 0x75, 0x01, // REPORT_SIZE (1)
  39. 0x81, 0x02, // INPUT (Data,Var,Abs)
  40. 0x95, 0x01, // REPORT_COUNT (1) ; pad out the bits into a number divisible by 8
  41. 0x75, 0x04, // REPORT_SIZE (4)
  42. 0x81, 0x03, // INPUT (Const,Var,Abs)
  43. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  44. 0x09, 0x01, // USAGE (pointer)
  45. 0xa1, 0x00, // COLLECTION (Physical)
  46. 0x09, 0x30, // USAGE (X)
  47. 0x09, 0x31, // USAGE (Y)
  48. 0x15, 0xff, // LOGICAL_MINIMUM (-1)
  49. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  50. 0x95, 0x02, // REPORT_COUNT (2)
  51. 0x75, 0x08, // REPORT_SIZE (8)
  52. 0x81, 0x02, // INPUT (Data,Var,Abs)
  53. 0xc0, // END_COLLECTION
  54. 0xc0, // END_COLLECTION
  55. 0xc0, // END_COLLECTION
  56. };
  57. Gamepad_::Gamepad_(void) : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1)
  58. {
  59. epType[0] = EP_TYPE_INTERRUPT_IN;
  60. PluggableUSB().plug(this);
  61. }
  62. int Gamepad_::getInterface(uint8_t* interfaceCount)
  63. {
  64. *interfaceCount += 1; // uses 1
  65. HIDDescriptor hidInterface = {
  66. D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
  67. D_HIDREPORT(sizeof(_hidReportDescriptor)),
  68. D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
  69. };
  70. return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
  71. }
  72. int Gamepad_::getDescriptor(USBSetup& setup)
  73. {
  74. // Check if this is a HID Class Descriptor request
  75. if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
  76. if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
  77. // In a HID Class Descriptor wIndex cointains the interface number
  78. if (setup.wIndex != pluggedInterface) { return 0; }
  79. // Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
  80. // due to the USB specs, but Windows and Linux just assumes its in report mode.
  81. protocol = HID_REPORT_PROTOCOL;
  82. return USB_SendControl(TRANSFER_PGM, _hidReportDescriptor, sizeof(_hidReportDescriptor));
  83. }
  84. bool Gamepad_::setup(USBSetup& setup)
  85. {
  86. if (pluggedInterface != setup.wIndex) {
  87. return false;
  88. }
  89. uint8_t request = setup.bRequest;
  90. uint8_t requestType = setup.bmRequestType;
  91. if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
  92. {
  93. if (request == HID_GET_REPORT) {
  94. // TODO: HID_GetReport();
  95. return true;
  96. }
  97. if (request == HID_GET_PROTOCOL) {
  98. // TODO: Send8(protocol);
  99. return true;
  100. }
  101. }
  102. if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
  103. {
  104. if (request == HID_SET_PROTOCOL) {
  105. protocol = setup.wValueL;
  106. return true;
  107. }
  108. if (request == HID_SET_IDLE) {
  109. idle = setup.wValueL;
  110. return true;
  111. }
  112. if (request == HID_SET_REPORT)
  113. {
  114. }
  115. }
  116. return false;
  117. }
  118. void Gamepad_::reset()
  119. {
  120. _GamepadReport.X = 0;
  121. _GamepadReport.Y = 0;
  122. _GamepadReport.buttons = 0;
  123. this->send();
  124. }
  125. void Gamepad_::send()
  126. {
  127. USB_Send(pluggedEndpoint | TRANSFER_RELEASE, &_GamepadReport, sizeof(GamepadReport));
  128. }
  129. uint8_t Gamepad_::getShortName(char *name)
  130. {
  131. if(!next)
  132. {
  133. strcpy(name, gp_serial);
  134. return strlen(name);
  135. }
  136. return 0;
  137. }