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