No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

166 líneas
4.9KB

  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) && (topusage != 0x10001)) 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 we are already in use than don't grab another one. Likewise don't grab if it is used as USB or HID object
  103. if (btdevice && (btdevice != (Device_t*)driver)) return false;
  104. if (mydevice != NULL) return false;
  105. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && (bluetooth_class & 0x80)) {
  106. USBHDBGSerial.printf("MouseController::claim_bluetooth TRUE\n");
  107. btdevice = (Device_t*)driver; // remember this way
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool MouseController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  113. {
  114. // Example DATA from bluetooth keyboard:
  115. // 0 1 2 3 4 5 6 7 8 910 1 2 3 4 5 6 7
  116. // LEN D
  117. //BT rx2_data(14): b 20 a 0 6 0 71 0 a1 2 0 9 fe 0
  118. //BT rx2_data(14): b 20 a 0 6 0 71 0 a1 2 0 8 fd 0
  119. // So Len=9 passed in data starting at report ID=1...
  120. if (length == 0) return false;
  121. #ifdef USBHOST_PRINT_DEBUG
  122. USBHDBGSerial.printf("MouseController::process_bluetooth_HID_data %d\n", length);
  123. USBHDBGSerial.printf(" Mouse Data: ");
  124. const uint8_t *p = (const uint8_t *)data;
  125. uint16_t len = length;
  126. do {
  127. if (*p < 16) USBHDBGSerial.print('0');
  128. USBHDBGSerial.print(*p++, HEX);
  129. USBHDBGSerial.print(' ');
  130. } while (--len);
  131. USBHDBGSerial.println();
  132. #endif
  133. // Looks like report 2 is for the mouse info.
  134. if (data[0] != 2) return false;
  135. buttons = data[1];
  136. mouseX = (int8_t)data[2];
  137. mouseY = (int8_t)data[3];
  138. if (length >= 5) {
  139. wheel = (int8_t)data[4];
  140. if (length >= 6) {
  141. wheelH = (int8_t)data[5];
  142. }
  143. }
  144. mouseEvent = true;
  145. return true;
  146. }
  147. void MouseController::release_bluetooth()
  148. {
  149. btdevice = nullptr;
  150. }