您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

163 行
4.6KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. void MouseController::init()
  26. {
  27. USBHIDParser::driver_ready_for_hid_collection(this);
  28. BluetoothController::driver_ready_for_bluetooth(this);
  29. }
  30. hidclaim_t MouseController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  31. {
  32. // only claim Desktop/Mouse
  33. if (topusage != 0x10002) return CLAIM_NO;
  34. // only claim from one physical device
  35. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  36. mydevice = dev;
  37. collections_claimed++;
  38. return CLAIM_REPORT;
  39. }
  40. void MouseController::disconnect_collection(Device_t *dev)
  41. {
  42. if (--collections_claimed == 0) {
  43. mydevice = NULL;
  44. }
  45. }
  46. void MouseController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  47. {
  48. // TODO: check if absolute coordinates
  49. hid_input_begin_ = true;
  50. }
  51. void MouseController::hid_input_data(uint32_t usage, int32_t value)
  52. {
  53. //USBHDBGSerial.printf("Mouse: usage=%X, value=%d\n", usage, value);
  54. uint32_t usage_page = usage >> 16;
  55. usage &= 0xFFFF;
  56. if (usage_page == 9 && usage >= 1 && usage <= 8) {
  57. if (value == 0) {
  58. buttons &= ~(1 << (usage -1));
  59. } else {
  60. buttons |= (1 << (usage -1));
  61. }
  62. } else if (usage_page == 1) {
  63. switch (usage) {
  64. case 0x30:
  65. mouseX = value;
  66. break;
  67. case 0x31:
  68. mouseY = value;
  69. break;
  70. case 0x32: // Apple uses this for horizontal scroll
  71. wheelH = value;
  72. break;
  73. case 0x38:
  74. wheel = value;
  75. break;
  76. }
  77. } else if (usage_page == 12) {
  78. if (usage == 0x238) { // Microsoft uses this for horizontal scroll
  79. wheelH = value;
  80. }
  81. }
  82. }
  83. void MouseController::hid_input_end()
  84. {
  85. if (hid_input_begin_) {
  86. mouseEvent = true;
  87. hid_input_begin_ = false;
  88. }
  89. }
  90. void MouseController::mouseDataClear() {
  91. mouseEvent = false;
  92. buttons = 0;
  93. mouseX = 0;
  94. mouseY = 0;
  95. wheel = 0;
  96. wheelH = 0;
  97. }
  98. bool MouseController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  99. {
  100. // How to handle combo devices?
  101. USBHDBGSerial.printf("MouseController Controller::claim_bluetooth - Class %x\n", bluetooth_class);
  102. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && (bluetooth_class & 0x80)) {
  103. USBHDBGSerial.printf("MouseController::claim_bluetooth TRUE\n");
  104. btdevice = (Device_t*)driver; // remember this way
  105. return true;
  106. }
  107. return false;
  108. }
  109. bool MouseController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  110. {
  111. // Example DATA from bluetooth keyboard:
  112. // 0 1 2 3 4 5 6 7 8 910 1 2 3 4 5 6 7
  113. // LEN D
  114. //BT rx2_data(14): b 20 a 0 6 0 71 0 a1 2 0 9 fe 0
  115. //BT rx2_data(14): b 20 a 0 6 0 71 0 a1 2 0 8 fd 0
  116. // So Len=9 passed in data starting at report ID=1...
  117. if (length == 0) return false;
  118. #ifdef USBHOST_PRINT_DEBUG
  119. USBHDBGSerial.printf("MouseController::process_bluetooth_HID_data %d\n", length);
  120. USBHDBGSerial.printf(" Mouse Data: ");
  121. const uint8_t *p = (const uint8_t *)data;
  122. uint16_t len = length;
  123. do {
  124. if (*p < 16) USBHDBGSerial.print('0');
  125. USBHDBGSerial.print(*p++, HEX);
  126. USBHDBGSerial.print(' ');
  127. } while (--len);
  128. USBHDBGSerial.println();
  129. #endif
  130. // Looks like report 2 is for the mouse info.
  131. if (data[0] != 2) return false;
  132. buttons = data[1];
  133. mouseX = (int8_t)data[2];
  134. mouseY = (int8_t)data[3];
  135. if (length >= 5) {
  136. wheel = (int8_t)data[4];
  137. if (length >= 6) {
  138. wheelH = (int8_t)data[5];
  139. }
  140. }
  141. mouseEvent = true;
  142. return true;
  143. }
  144. void MouseController::release_bluetooth()
  145. {
  146. //btdevice = nullptr;
  147. }