Keyboard firmwares for Atmel AVR and Cortex-M
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.

USBHostMouse.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "USBHostMouse.h"
  17. #if USBHOST_MOUSE
  18. USBHostMouse::USBHostMouse() {
  19. host = USBHost::getHostInst();
  20. init();
  21. }
  22. void USBHostMouse::init() {
  23. dev = NULL;
  24. int_in = NULL;
  25. onUpdate = NULL;
  26. onButtonUpdate = NULL;
  27. onXUpdate = NULL;
  28. onYUpdate = NULL;
  29. onZUpdate = NULL;
  30. report_id = 0;
  31. dev_connected = false;
  32. mouse_device_found = false;
  33. mouse_intf = -1;
  34. buttons = 0;
  35. x = 0;
  36. y = 0;
  37. z = 0;
  38. }
  39. bool USBHostMouse::connected() {
  40. return dev_connected;
  41. }
  42. bool USBHostMouse::connect() {
  43. int len_listen;
  44. if (dev_connected) {
  45. return true;
  46. }
  47. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
  48. if ((dev = host->getDevice(i)) != NULL) {
  49. if(host->enumerate(dev, this))
  50. break;
  51. if (mouse_device_found) {
  52. int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
  53. if (!int_in)
  54. break;
  55. USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
  56. dev->setName("Mouse", mouse_intf);
  57. host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
  58. int_in->attach(this, &USBHostMouse::rxHandler);
  59. len_listen = int_in->getSize();
  60. if (len_listen > sizeof(report)) {
  61. len_listen = sizeof(report);
  62. }
  63. host->interruptRead(dev, int_in, report, len_listen, false);
  64. dev_connected = true;
  65. return true;
  66. }
  67. }
  68. }
  69. init();
  70. return false;
  71. }
  72. void USBHostMouse::rxHandler() {
  73. int len_listen = int_in->getSize();
  74. if (onUpdate) {
  75. (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
  76. }
  77. if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
  78. (*onButtonUpdate)(report[0] & 0x07);
  79. }
  80. if (onXUpdate && (x != report[1])) {
  81. (*onXUpdate)(report[1]);
  82. }
  83. if (onYUpdate && (y != report[2])) {
  84. (*onYUpdate)(report[2]);
  85. }
  86. if (onZUpdate && (z != report[3])) {
  87. (*onZUpdate)(report[3]);
  88. }
  89. // update mouse state
  90. buttons = report[0] & 0x07;
  91. x = report[1];
  92. y = report[2];
  93. z = report[3];
  94. if (len_listen > sizeof(report)) {
  95. len_listen = sizeof(report);
  96. }
  97. if (dev)
  98. host->interruptRead(dev, int_in, report, len_listen, false);
  99. }
  100. /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
  101. {
  102. // we don't check VID/PID for mouse driver
  103. }
  104. /*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
  105. {
  106. if ((mouse_intf == -1) &&
  107. (intf_class == HID_CLASS) &&
  108. (intf_subclass == 0x01) &&
  109. (intf_protocol == 0x02)) {
  110. mouse_intf = intf_nb;
  111. return true;
  112. }
  113. return false;
  114. }
  115. /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  116. {
  117. if (intf_nb == mouse_intf) {
  118. if (type == INTERRUPT_ENDPOINT && dir == IN) {
  119. mouse_device_found = true;
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. #endif