Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #include "stdint.h"
  19. #include "USBMouse.h"
  20. bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
  21. switch (mouse_type) {
  22. case REL_MOUSE:
  23. while (x > 127) {
  24. if (!mouseSend(127, 0, button, z)) return false;
  25. x = x - 127;
  26. }
  27. while (x < -128) {
  28. if (!mouseSend(-128, 0, button, z)) return false;
  29. x = x + 128;
  30. }
  31. while (y > 127) {
  32. if (!mouseSend(0, 127, button, z)) return false;
  33. y = y - 127;
  34. }
  35. while (y < -128) {
  36. if (!mouseSend(0, -128, button, z)) return false;
  37. y = y + 128;
  38. }
  39. return mouseSend(x, y, button, z);
  40. case ABS_MOUSE:
  41. HID_REPORT report;
  42. report.data[0] = x & 0xff;
  43. report.data[1] = (x >> 8) & 0xff;
  44. report.data[2] = y & 0xff;
  45. report.data[3] = (y >> 8) & 0xff;
  46. report.data[4] = -z;
  47. report.data[5] = button & 0x07;
  48. report.length = 6;
  49. return send(&report);
  50. default:
  51. return false;
  52. }
  53. }
  54. bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
  55. HID_REPORT report;
  56. report.data[0] = buttons & 0x07;
  57. report.data[1] = x;
  58. report.data[2] = y;
  59. report.data[3] = -z; // >0 to scroll down, <0 to scroll up
  60. report.length = 4;
  61. return send(&report);
  62. }
  63. bool USBMouse::move(int16_t x, int16_t y) {
  64. return update(x, y, button, 0);
  65. }
  66. bool USBMouse::scroll(int8_t z) {
  67. return update(0, 0, button, z);
  68. }
  69. bool USBMouse::doubleClick() {
  70. if (!click(MOUSE_LEFT))
  71. return false;
  72. wait(0.1);
  73. return click(MOUSE_LEFT);
  74. }
  75. bool USBMouse::click(uint8_t button) {
  76. if (!update(0, 0, button, 0))
  77. return false;
  78. wait(0.01);
  79. return update(0, 0, 0, 0);
  80. }
  81. bool USBMouse::press(uint8_t button_) {
  82. button = button_ & 0x07;
  83. return update(0, 0, button, 0);
  84. }
  85. bool USBMouse::release(uint8_t button_) {
  86. button = (button & (~button_)) & 0x07;
  87. return update(0, 0, button, 0);
  88. }
  89. uint8_t * USBMouse::reportDesc() {
  90. if (mouse_type == REL_MOUSE) {
  91. static uint8_t reportDescriptor[] = {
  92. USAGE_PAGE(1), 0x01, // Genric Desktop
  93. USAGE(1), 0x02, // Mouse
  94. COLLECTION(1), 0x01, // Application
  95. USAGE(1), 0x01, // Pointer
  96. COLLECTION(1), 0x00, // Physical
  97. REPORT_COUNT(1), 0x03,
  98. REPORT_SIZE(1), 0x01,
  99. USAGE_PAGE(1), 0x09, // Buttons
  100. USAGE_MINIMUM(1), 0x1,
  101. USAGE_MAXIMUM(1), 0x3,
  102. LOGICAL_MINIMUM(1), 0x00,
  103. LOGICAL_MAXIMUM(1), 0x01,
  104. INPUT(1), 0x02,
  105. REPORT_COUNT(1), 0x01,
  106. REPORT_SIZE(1), 0x05,
  107. INPUT(1), 0x01,
  108. REPORT_COUNT(1), 0x03,
  109. REPORT_SIZE(1), 0x08,
  110. USAGE_PAGE(1), 0x01,
  111. USAGE(1), 0x30, // X
  112. USAGE(1), 0x31, // Y
  113. USAGE(1), 0x38, // scroll
  114. LOGICAL_MINIMUM(1), 0x81,
  115. LOGICAL_MAXIMUM(1), 0x7f,
  116. INPUT(1), 0x06, // Relative data
  117. END_COLLECTION(0),
  118. END_COLLECTION(0),
  119. };
  120. reportLength = sizeof(reportDescriptor);
  121. return reportDescriptor;
  122. } else if (mouse_type == ABS_MOUSE) {
  123. static uint8_t reportDescriptor[] = {
  124. USAGE_PAGE(1), 0x01, // Generic Desktop
  125. USAGE(1), 0x02, // Mouse
  126. COLLECTION(1), 0x01, // Application
  127. USAGE(1), 0x01, // Pointer
  128. COLLECTION(1), 0x00, // Physical
  129. USAGE_PAGE(1), 0x01, // Generic Desktop
  130. USAGE(1), 0x30, // X
  131. USAGE(1), 0x31, // Y
  132. LOGICAL_MINIMUM(1), 0x00, // 0
  133. LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
  134. REPORT_SIZE(1), 0x10,
  135. REPORT_COUNT(1), 0x02,
  136. INPUT(1), 0x02, // Data, Variable, Absolute
  137. USAGE_PAGE(1), 0x01, // Generic Desktop
  138. USAGE(1), 0x38, // scroll
  139. LOGICAL_MINIMUM(1), 0x81, // -127
  140. LOGICAL_MAXIMUM(1), 0x7f, // 127
  141. REPORT_SIZE(1), 0x08,
  142. REPORT_COUNT(1), 0x01,
  143. INPUT(1), 0x06, // Data, Variable, Relative
  144. USAGE_PAGE(1), 0x09, // Buttons
  145. USAGE_MINIMUM(1), 0x01,
  146. USAGE_MAXIMUM(1), 0x03,
  147. LOGICAL_MINIMUM(1), 0x00, // 0
  148. LOGICAL_MAXIMUM(1), 0x01, // 1
  149. REPORT_COUNT(1), 0x03,
  150. REPORT_SIZE(1), 0x01,
  151. INPUT(1), 0x02, // Data, Variable, Absolute
  152. REPORT_COUNT(1), 0x01,
  153. REPORT_SIZE(1), 0x05,
  154. INPUT(1), 0x01, // Constant
  155. END_COLLECTION(0),
  156. END_COLLECTION(0)
  157. };
  158. reportLength = sizeof(reportDescriptor);
  159. return reportDescriptor;
  160. }
  161. return NULL;
  162. }
  163. #define DEFAULT_CONFIGURATION (1)
  164. #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
  165. + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
  166. + (1 * HID_DESCRIPTOR_LENGTH) \
  167. + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
  168. uint8_t * USBMouse::configurationDesc() {
  169. static uint8_t configurationDescriptor[] = {
  170. CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
  171. CONFIGURATION_DESCRIPTOR, // bDescriptorType
  172. LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
  173. MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
  174. 0x01, // bNumInterfaces
  175. DEFAULT_CONFIGURATION, // bConfigurationValue
  176. 0x00, // iConfiguration
  177. C_RESERVED | C_SELF_POWERED, // bmAttributes
  178. C_POWER(0), // bMaxPowerHello World from Mbed
  179. INTERFACE_DESCRIPTOR_LENGTH, // bLength
  180. INTERFACE_DESCRIPTOR, // bDescriptorType
  181. 0x00, // bInterfaceNumber
  182. 0x00, // bAlternateSetting
  183. 0x02, // bNumEndpoints
  184. HID_CLASS, // bInterfaceClass
  185. 1, // bInterfaceSubClass
  186. 2, // bInterfaceProtocol (mouse)
  187. 0x00, // iInterface
  188. HID_DESCRIPTOR_LENGTH, // bLength
  189. HID_DESCRIPTOR, // bDescriptorType
  190. LSB(HID_VERSION_1_11), // bcdHID (LSB)
  191. MSB(HID_VERSION_1_11), // bcdHID (MSB)
  192. 0x00, // bCountryCode
  193. 0x01, // bNumDescriptors
  194. REPORT_DESCRIPTOR, // bDescriptorType
  195. (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
  196. (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
  197. ENDPOINT_DESCRIPTOR_LENGTH, // bLength
  198. ENDPOINT_DESCRIPTOR, // bDescriptorType
  199. PHY_TO_DESC(EPINT_IN), // bEndpointAddress
  200. E_INTERRUPT, // bmAttributes
  201. LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
  202. MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
  203. 1, // bInterval (milliseconds)
  204. ENDPOINT_DESCRIPTOR_LENGTH, // bLength
  205. ENDPOINT_DESCRIPTOR, // bDescriptorType
  206. PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
  207. E_INTERRUPT, // bmAttributes
  208. LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
  209. MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
  210. 1, // bInterval (milliseconds)
  211. };
  212. return configurationDescriptor;
  213. }