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.

149 line
4.4KB

  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)
  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(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  115. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 4 0 0 0 0 0
  116. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  117. // So Len=9 passed in data starting at report ID=1...
  118. USBHDBGSerial.printf("MouseController::process_bluetooth_HID_data\n");
  119. if (length == 0) return false;
  120. if (data[0] != 1) return false;
  121. USBHDBGSerial.printf(" Mouse Data: ");
  122. const uint8_t *p = (const uint8_t *)data;
  123. do {
  124. if (*p < 16) USBHDBGSerial.print('0');
  125. USBHDBGSerial.print(*p++, HEX);
  126. USBHDBGSerial.print(' ');
  127. } while (--length);
  128. USBHDBGSerial.println();
  129. return true;
  130. }
  131. void MouseController::release_bluetooth()
  132. {
  133. //btdevice = nullptr;
  134. }