Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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