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