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.

USBHID.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USB_HID_H
  19. #define USB_HID_H
  20. /* These headers are included for child class. */
  21. #include "USBEndpoints.h"
  22. #include "USBDescriptor.h"
  23. #include "USBDevice_Types.h"
  24. #include "USBHID_Types.h"
  25. #include "USBDevice.h"
  26. /**
  27. * USBHID example
  28. * @code
  29. * #include "mbed.h"
  30. * #include "USBHID.h"
  31. *
  32. * USBHID hid;
  33. * HID_REPORT recv;
  34. * BusOut leds(LED1,LED2,LED3,LED4);
  35. *
  36. * int main(void) {
  37. * while (1) {
  38. * hid.read(&recv);
  39. * leds = recv.data[0];
  40. * }
  41. * }
  42. * @endcode
  43. */
  44. class USBHID: public USBDevice {
  45. public:
  46. /**
  47. * Constructor
  48. *
  49. * @param output_report_length Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
  50. * @param input_report_length Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
  51. * @param vendor_id Your vendor_id
  52. * @param product_id Your product_id
  53. * @param product_release Your preoduct_release
  54. * @param connect Connect the device
  55. */
  56. USBHID(uint8_t output_report_length = 64, uint8_t input_report_length = 64, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001, bool connect = true);
  57. /**
  58. * Send a Report. warning: blocking
  59. *
  60. * @param report Report which will be sent (a report is defined by all data and the length)
  61. * @returns true if successful
  62. */
  63. bool send(HID_REPORT *report);
  64. /**
  65. * Send a Report. warning: non blocking
  66. *
  67. * @param report Report which will be sent (a report is defined by all data and the length)
  68. * @returns true if successful
  69. */
  70. bool sendNB(HID_REPORT *report);
  71. /**
  72. * Read a report: blocking
  73. *
  74. * @param report pointer to the report to fill
  75. * @returns true if successful
  76. */
  77. bool read(HID_REPORT * report);
  78. /**
  79. * Read a report: non blocking
  80. *
  81. * @param report pointer to the report to fill
  82. * @returns true if successful
  83. */
  84. bool readNB(HID_REPORT * report);
  85. protected:
  86. uint16_t reportLength;
  87. /*
  88. * Get the Report descriptor
  89. *
  90. * @returns pointer to the report descriptor
  91. */
  92. virtual uint8_t * reportDesc();
  93. /*
  94. * Get the length of the report descriptor
  95. *
  96. * @returns the length of the report descriptor
  97. */
  98. virtual uint16_t reportDescLength();
  99. /*
  100. * Get string product descriptor
  101. *
  102. * @returns pointer to the string product descriptor
  103. */
  104. virtual uint8_t * stringIproductDesc();
  105. /*
  106. * Get string interface descriptor
  107. *
  108. * @returns pointer to the string interface descriptor
  109. */
  110. virtual uint8_t * stringIinterfaceDesc();
  111. /*
  112. * Get configuration descriptor
  113. *
  114. * @returns pointer to the configuration descriptor
  115. */
  116. virtual uint8_t * configurationDesc();
  117. /*
  118. * HID Report received by SET_REPORT request. Warning: Called in ISR context
  119. * First byte of data will be the report ID
  120. *
  121. * @param report Data and length received
  122. */
  123. virtual void HID_callbackSetReport(HID_REPORT *report){};
  124. /*
  125. * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
  126. * This is used to handle extensions to standard requests
  127. * and class specific requests
  128. *
  129. * @returns true if class handles this request
  130. */
  131. virtual bool USBCallback_request();
  132. /*
  133. * Called by USBDevice layer. Set configuration of the device.
  134. * For instance, you can add all endpoints that you need on this function.
  135. *
  136. * @param configuration Number of the configuration
  137. * @returns true if class handles this request
  138. */
  139. virtual bool USBCallback_setConfiguration(uint8_t configuration);
  140. private:
  141. HID_REPORT outputReport;
  142. uint8_t output_length;
  143. uint8_t input_length;
  144. };
  145. #endif