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 USBKEYBOARD_H
wue 0:7b58cdacf811 20 #define USBKEYBOARD_H
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 #include "USBHID.h"
wue 0:7b58cdacf811 23 #include "Stream.h"
wue 0:7b58cdacf811 24
wue 0:7b58cdacf811 25 /* Modifiers */
wue 0:7b58cdacf811 26 enum MODIFIER_KEY {
wue 0:7b58cdacf811 27 KEY_CTRL = 1,
wue 0:7b58cdacf811 28 KEY_SHIFT = 2,
wue 0:7b58cdacf811 29 KEY_ALT = 4,
wue 0:7b58cdacf811 30 };
wue 0:7b58cdacf811 31
wue 0:7b58cdacf811 32
wue 0:7b58cdacf811 33 enum MEDIA_KEY {
wue 0:7b58cdacf811 34 KEY_NEXT_TRACK, /*!< next Track Button */
wue 0:7b58cdacf811 35 KEY_PREVIOUS_TRACK, /*!< Previous track Button */
wue 0:7b58cdacf811 36 KEY_STOP, /*!< Stop Button */
wue 0:7b58cdacf811 37 KEY_PLAY_PAUSE, /*!< Play/Pause Button */
wue 0:7b58cdacf811 38 KEY_MUTE, /*!< Mute Button */
wue 0:7b58cdacf811 39 KEY_VOLUME_UP, /*!< Volume Up Button */
wue 0:7b58cdacf811 40 KEY_VOLUME_DOWN, /*!< Volume Down Button */
wue 0:7b58cdacf811 41 };
wue 0:7b58cdacf811 42
wue 0:7b58cdacf811 43 enum FUNCTION_KEY {
wue 0:7b58cdacf811 44 KEY_F1 = 128, /* F1 key */
wue 0:7b58cdacf811 45 KEY_F2, /* F2 key */
wue 0:7b58cdacf811 46 KEY_F3, /* F3 key */
wue 0:7b58cdacf811 47 KEY_F4, /* F4 key */
wue 0:7b58cdacf811 48 KEY_F5, /* F5 key */
wue 0:7b58cdacf811 49 KEY_F6, /* F6 key */
wue 0:7b58cdacf811 50 KEY_F7, /* F7 key */
wue 0:7b58cdacf811 51 KEY_F8, /* F8 key */
wue 0:7b58cdacf811 52 KEY_F9, /* F9 key */
wue 0:7b58cdacf811 53 KEY_F10, /* F10 key */
wue 0:7b58cdacf811 54 KEY_F11, /* F11 key */
wue 0:7b58cdacf811 55 KEY_F12, /* F12 key */
wue 0:7b58cdacf811 56
wue 0:7b58cdacf811 57 KEY_PRINT_SCREEN, /* Print Screen key */
wue 0:7b58cdacf811 58 KEY_SCROLL_LOCK, /* Scroll lock */
wue 0:7b58cdacf811 59 KEY_CAPS_LOCK, /* caps lock */
wue 0:7b58cdacf811 60 KEY_NUM_LOCK, /* num lock */
wue 0:7b58cdacf811 61 KEY_INSERT, /* Insert key */
wue 0:7b58cdacf811 62 KEY_HOME, /* Home key */
wue 0:7b58cdacf811 63 KEY_PAGE_UP, /* Page Up key */
wue 0:7b58cdacf811 64 KEY_PAGE_DOWN, /* Page Down key */
wue 0:7b58cdacf811 65
wue 0:7b58cdacf811 66 RIGHT_ARROW, /* Right arrow */
wue 0:7b58cdacf811 67 LEFT_ARROW, /* Left arrow */
wue 0:7b58cdacf811 68 DOWN_ARROW, /* Down arrow */
wue 0:7b58cdacf811 69 UP_ARROW, /* Up arrow */
wue 0:7b58cdacf811 70 };
wue 0:7b58cdacf811 71
wue 0:7b58cdacf811 72 /**
wue 0:7b58cdacf811 73 * USBKeyboard example
wue 0:7b58cdacf811 74 * @code
wue 0:7b58cdacf811 75 *
wue 0:7b58cdacf811 76 * #include "mbed.h"
wue 0:7b58cdacf811 77 * #include "USBKeyboard.h"
wue 0:7b58cdacf811 78 *
wue 0:7b58cdacf811 79 * USBKeyboard key;
wue 0:7b58cdacf811 80 *
wue 0:7b58cdacf811 81 * int main(void)
wue 0:7b58cdacf811 82 * {
wue 0:7b58cdacf811 83 * while (1)
wue 0:7b58cdacf811 84 * {
wue 0:7b58cdacf811 85 * key.printf("Hello World\r\n");
wue 0:7b58cdacf811 86 * wait(1);
wue 0:7b58cdacf811 87 * }
wue 0:7b58cdacf811 88 * }
wue 0:7b58cdacf811 89 *
wue 0:7b58cdacf811 90 * @endcode
wue 0:7b58cdacf811 91 */
wue 0:7b58cdacf811 92 class USBKeyboard: public USBHID, public Stream {
wue 0:7b58cdacf811 93 public:
wue 0:7b58cdacf811 94
wue 0:7b58cdacf811 95 /**
wue 0:7b58cdacf811 96 * Constructor
wue 0:7b58cdacf811 97 *
wue 0:7b58cdacf811 98 *
wue 0:7b58cdacf811 99 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
wue 0:7b58cdacf811 100 * @param vendor_id Your vendor_id (default: 0x1235)
wue 0:7b58cdacf811 101 * @param product_id Your product_id (default: 0x0050)
wue 0:7b58cdacf811 102 * @param product_release Your preoduct_release (default: 0x0001)
wue 0:7b58cdacf811 103 *
wue 0:7b58cdacf811 104 */
wue 0:7b58cdacf811 105 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
wue 0:7b58cdacf811 106 USBHID(0, 0, vendor_id, product_id, product_release, false) {
wue 0:7b58cdacf811 107 lock_status = 0;
wue 0:7b58cdacf811 108 connect();
wue 0:7b58cdacf811 109 };
wue 0:7b58cdacf811 110
wue 0:7b58cdacf811 111 /**
wue 0:7b58cdacf811 112 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
wue 0:7b58cdacf811 113 *
wue 0:7b58cdacf811 114 * @code
wue 0:7b58cdacf811 115 * //To send CTRL + s (save)
wue 0:7b58cdacf811 116 * keyboard.keyCode('s', KEY_CTRL);
wue 0:7b58cdacf811 117 * @endcode
wue 0:7b58cdacf811 118 *
wue 0:7b58cdacf811 119 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
wue 0:7b58cdacf811 120 * @param key character to send
wue 0:7b58cdacf811 121 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 122 */
wue 0:7b58cdacf811 123 bool keyCode(uint8_t key, uint8_t modifier = 0);
wue 0:7b58cdacf811 124
wue 0:7b58cdacf811 125 /**
wue 0:7b58cdacf811 126 * Send a character
wue 0:7b58cdacf811 127 *
wue 0:7b58cdacf811 128 * @param c character to be sent
wue 0:7b58cdacf811 129 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 130 */
wue 0:7b58cdacf811 131 virtual int _putc(int c);
wue 0:7b58cdacf811 132
wue 0:7b58cdacf811 133 /**
wue 0:7b58cdacf811 134 * Control media keys
wue 0:7b58cdacf811 135 *
wue 0:7b58cdacf811 136 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
wue 0:7b58cdacf811 137 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 138 */
wue 0:7b58cdacf811 139 bool mediaControl(MEDIA_KEY key);
wue 0:7b58cdacf811 140
wue 0:7b58cdacf811 141 /*
wue 0:7b58cdacf811 142 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
wue 0:7b58cdacf811 143 *
wue 0:7b58cdacf811 144 * @returns pointer to the report descriptor
wue 0:7b58cdacf811 145 */
wue 0:7b58cdacf811 146 virtual uint8_t * reportDesc();
wue 0:7b58cdacf811 147
wue 0:7b58cdacf811 148 /*
wue 0:7b58cdacf811 149 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
wue 0:7b58cdacf811 150 *
wue 0:7b58cdacf811 151 * @returns if handle by subclass, return true
wue 0:7b58cdacf811 152 */
wue 0:7b58cdacf811 153 virtual bool EP1_OUT_callback();
wue 0:7b58cdacf811 154
wue 0:7b58cdacf811 155 /**
wue 0:7b58cdacf811 156 * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
wue 0:7b58cdacf811 157 * - First bit: NUM_LOCK
wue 0:7b58cdacf811 158 * - Second bit: CAPS_LOCK
wue 0:7b58cdacf811 159 * - Third bit: SCROLL_LOCK
wue 0:7b58cdacf811 160 *
wue 0:7b58cdacf811 161 * @returns status of lock keys
wue 0:7b58cdacf811 162 */
wue 0:7b58cdacf811 163 uint8_t lockStatus();
wue 0:7b58cdacf811 164
wue 0:7b58cdacf811 165 protected:
wue 0:7b58cdacf811 166 /*
wue 0:7b58cdacf811 167 * Get configuration descriptor
wue 0:7b58cdacf811 168 *
wue 0:7b58cdacf811 169 * @returns pointer to the configuration descriptor
wue 0:7b58cdacf811 170 */
wue 0:7b58cdacf811 171 virtual uint8_t * configurationDesc();
wue 0:7b58cdacf811 172
wue 0:7b58cdacf811 173 private:
wue 0:7b58cdacf811 174 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
wue 0:7b58cdacf811 175 virtual int _getc() {
wue 0:7b58cdacf811 176 return -1;
wue 0:7b58cdacf811 177 };
wue 0:7b58cdacf811 178
wue 0:7b58cdacf811 179 uint8_t lock_status;
wue 0:7b58cdacf811 180
wue 0:7b58cdacf811 181 };
wue 0:7b58cdacf811 182
wue 0:7b58cdacf811 183 #endif