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.

преди 7 години
преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  28. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  29. driver_ready_for_device(this);
  30. }
  31. bool MouseController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  32. {
  33. println("MouseController claim this=", (uint32_t)this, HEX);
  34. // only claim at interface level
  35. if (type != 1) return false;
  36. if (len < 9+9+7) return false;
  37. uint32_t numendpoint = descriptors[4];
  38. if (numendpoint < 1) return false;
  39. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  40. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  41. if (descriptors[7] != 2) return false; // bInterfaceProtocol, 2 = Mouse
  42. if (descriptors[9] != 9) return false;
  43. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  44. if (descriptors[18] != 7) return false;
  45. if (descriptors[19] != 5) return false; // endpoint descriptor
  46. uint32_t endpoint = descriptors[20];
  47. println("ep(mouse) = ", endpoint, HEX);
  48. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  49. endpoint &= 0x0F;
  50. if (endpoint == 0) return false;
  51. if (descriptors[21] != 3) return false; // must be interrupt type
  52. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  53. println("descriptors[22] = ",descriptors[22]);
  54. println("descriptors[23] = ",descriptors[23]);
  55. println("packet size(mouse) = ", size);
  56. // packey size seems to be 20 for (wireless type 2) or 6 bytes for wired
  57. packetSize = size;
  58. if ((size != 20) && (size != 6)) return false;
  59. if(packetSize == 6) packetSize = 8; // Minimum packet size needed is 8
  60. uint32_t interval = descriptors[24];
  61. println("polling interval = ", interval);
  62. datapipe = new_Pipe(dev, 3, endpoint, 1, packetSize, interval);
  63. datapipe->callback_function = callback;
  64. queue_Data_Transfer(datapipe, report, packetSize, this);
  65. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  66. queue_Control_Transfer(dev, &setup, NULL, this);
  67. return true;
  68. }
  69. void MouseController::control(const Transfer_t *transfer)
  70. {
  71. }
  72. void MouseController::callback(const Transfer_t *transfer)
  73. {
  74. //println("MouseController Callback (static)");
  75. if (transfer->driver) {
  76. ((MouseController *)(transfer->driver))->new_data(transfer);
  77. }
  78. }
  79. void MouseController::disconnect()
  80. {
  81. // TODO: free resources
  82. }
  83. // Arduino defined this static weak symbol callback, and their
  84. // examples use it as the only way to detect new key presses,
  85. // so unfortunate as static weak callbacks are, it probably
  86. // needs to be supported for compatibility
  87. extern "C" {
  88. void __MouseControllerEmptyCallback() { }
  89. }
  90. //void mouseEvent() __attribute__ ((weak, alias("__mouseEventEmptyCallback")));
  91. void MouseController::new_data(const Transfer_t *transfer)
  92. {
  93. println("MouseController Callback (member)");
  94. print(" Mouse Data: ");
  95. print_hexbytes(transfer->buffer, 8);
  96. // The mouse button report byte is 1 for left button, 2 for right button, 4 for wheel
  97. // button (three button mouse).
  98. //
  99. // The mouse x and y report consists of two 12 bit signed values packed into three
  100. // bytes and are relative values. Shown below are the three x/y bytes of a wireless
  101. // mouse report packet.
  102. //
  103. // byte 3 is the lower 8 bits of the x coordinate.
  104. // byte 5 is the upper 8 bits of the y coordinate.
  105. // byte 4 lower 4 bits are the upper 4 bits of the x coordinate.
  106. // byte 4 upper 4 bits are the lower 4 bits of the y coordinate.
  107. //
  108. // Example of one increment in all four directions:
  109. // x x
  110. // ff 0f 00 = left -1 00 01 00 = right 1
  111. // y y
  112. // 00 f0 ff = up -1 00 10 00 = down 1
  113. //
  114. // The wheel report is a signed byte that is 1 for forward and -1 for backward movement.
  115. // It is cleared to zero with any x and/or y movement.
  116. //
  117. // Wireless Logitech mouse reports have byte 0 set to 0x02 indicating a type 2 report.
  118. // Not sure what this really means yet but all bytes of the report packet are shifted
  119. // ahead by one byte and there is a single byte that is always zero after the button
  120. // report byte.
  121. if(packetSize == 20) {
  122. buttons = report[1];
  123. mouseX = ((report[4] & 0x0f) << 8) | ((report[3] & 0xff));
  124. mouseY = ((report[5] & 0xff) << 4) | ((report[4] >> 4) & 0x0f);
  125. wheel = report[6];
  126. } else {
  127. buttons = report[0];
  128. mouseX = ((report[2] & 0x0f) << 8) | ((report[1] & 0xff));
  129. mouseY = ((report[3] & 0xff) << 4) | ((report[2] >> 4) & 0x0f);
  130. wheel = report[4];
  131. }
  132. mouseEvent = true;
  133. memcpy(prev_report, report, 20);
  134. queue_Data_Transfer(datapipe, report, 20, this);
  135. }
  136. void MouseController::mouseDataClear() {
  137. mouseEvent = false;
  138. buttons = 0;
  139. mouseX = 0;
  140. mouseY = 0;
  141. wheel = 0;
  142. }