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 USBMOUSEKEYBOARD_H
wue 0:7b58cdacf811 20 #define USBMOUSEKEYBOARD_H
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 #define REPORT_ID_KEYBOARD 1
wue 0:7b58cdacf811 23 #define REPORT_ID_MOUSE 2
wue 0:7b58cdacf811 24 #define REPORT_ID_VOLUME 3
wue 0:7b58cdacf811 25
wue 0:7b58cdacf811 26 #include "USBMouse.h"
wue 0:7b58cdacf811 27 #include "USBKeyboard.h"
wue 0:7b58cdacf811 28 #include "Stream.h"
wue 0:7b58cdacf811 29 #include "USBHID.h"
wue 0:7b58cdacf811 30
wue 0:7b58cdacf811 31 /**
wue 0:7b58cdacf811 32 * USBMouseKeyboard example
wue 0:7b58cdacf811 33 * @code
wue 0:7b58cdacf811 34 *
wue 0:7b58cdacf811 35 * #include "mbed.h"
wue 0:7b58cdacf811 36 * #include "USBMouseKeyboard.h"
wue 0:7b58cdacf811 37 *
wue 0:7b58cdacf811 38 * USBMouseKeyboard key_mouse;
wue 0:7b58cdacf811 39 *
wue 0:7b58cdacf811 40 * int main(void)
wue 0:7b58cdacf811 41 * {
wue 0:7b58cdacf811 42 * while(1)
wue 0:7b58cdacf811 43 * {
wue 0:7b58cdacf811 44 * key_mouse.move(20, 0);
wue 0:7b58cdacf811 45 * key_mouse.printf("Hello From MBED\r\n");
wue 0:7b58cdacf811 46 * wait(1);
wue 0:7b58cdacf811 47 * }
wue 0:7b58cdacf811 48 * }
wue 0:7b58cdacf811 49 * @endcode
wue 0:7b58cdacf811 50 *
wue 0:7b58cdacf811 51 *
wue 0:7b58cdacf811 52 * @code
wue 0:7b58cdacf811 53 *
wue 0:7b58cdacf811 54 * #include "mbed.h"
wue 0:7b58cdacf811 55 * #include "USBMouseKeyboard.h"
wue 0:7b58cdacf811 56 *
wue 0:7b58cdacf811 57 * USBMouseKeyboard key_mouse(ABS_MOUSE);
wue 0:7b58cdacf811 58 *
wue 0:7b58cdacf811 59 * int main(void)
wue 0:7b58cdacf811 60 * {
wue 0:7b58cdacf811 61 * while(1)
wue 0:7b58cdacf811 62 * {
wue 0:7b58cdacf811 63 * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
wue 0:7b58cdacf811 64 * key_mouse.printf("Hello from MBED\r\n");
wue 0:7b58cdacf811 65 * wait(1);
wue 0:7b58cdacf811 66 * }
wue 0:7b58cdacf811 67 * }
wue 0:7b58cdacf811 68 * @endcode
wue 0:7b58cdacf811 69 */
wue 0:7b58cdacf811 70 class USBMouseKeyboard: public USBHID, public Stream
wue 0:7b58cdacf811 71 {
wue 0:7b58cdacf811 72 public:
wue 0:7b58cdacf811 73
wue 0:7b58cdacf811 74 /**
wue 0:7b58cdacf811 75 * Constructor
wue 0:7b58cdacf811 76 *
wue 0:7b58cdacf811 77 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
wue 0:7b58cdacf811 78 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
wue 0:7b58cdacf811 79 * @param vendor_id Your vendor_id (default: 0x1234)
wue 0:7b58cdacf811 80 * @param product_id Your product_id (default: 0x0001)
wue 0:7b58cdacf811 81 * @param product_release Your preoduct_release (default: 0x0001)
wue 0:7b58cdacf811 82 *
wue 0:7b58cdacf811 83 */
wue 0:7b58cdacf811 84 USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
wue 0:7b58cdacf811 85 USBHID(0, 0, vendor_id, product_id, product_release, false)
wue 0:7b58cdacf811 86 {
wue 0:7b58cdacf811 87 lock_status = 0;
wue 0:7b58cdacf811 88 button = 0;
wue 0:7b58cdacf811 89 this->mouse_type = mouse_type;
wue 0:7b58cdacf811 90 connect();
wue 0:7b58cdacf811 91 };
wue 0:7b58cdacf811 92
wue 0:7b58cdacf811 93 /**
wue 0:7b58cdacf811 94 * Write a state of the mouse
wue 0:7b58cdacf811 95 *
wue 0:7b58cdacf811 96 * @param x x-axis position
wue 0:7b58cdacf811 97 * @param y y-axis position
wue 0:7b58cdacf811 98 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
wue 0:7b58cdacf811 99 * @param z wheel state (>0 to scroll down, <0 to scroll up)
wue 0:7b58cdacf811 100 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 101 */
wue 0:7b58cdacf811 102 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
wue 0:7b58cdacf811 103
wue 0:7b58cdacf811 104
wue 0:7b58cdacf811 105 /**
wue 0:7b58cdacf811 106 * Move the cursor to (x, y)
wue 0:7b58cdacf811 107 *
wue 0:7b58cdacf811 108 * @param x x-axis position
wue 0:7b58cdacf811 109 * @param y y-axis position
wue 0:7b58cdacf811 110 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 111 */
wue 0:7b58cdacf811 112 bool move(int16_t x, int16_t y);
wue 0:7b58cdacf811 113
wue 0:7b58cdacf811 114 /**
wue 0:7b58cdacf811 115 * Press one or several buttons
wue 0:7b58cdacf811 116 *
wue 0:7b58cdacf811 117 * @param button button state (ex: press(MOUSE_LEFT))
wue 0:7b58cdacf811 118 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 119 */
wue 0:7b58cdacf811 120 bool press(uint8_t button);
wue 0:7b58cdacf811 121
wue 0:7b58cdacf811 122 /**
wue 0:7b58cdacf811 123 * Release one or several buttons
wue 0:7b58cdacf811 124 *
wue 0:7b58cdacf811 125 * @param button button state (ex: release(MOUSE_LEFT))
wue 0:7b58cdacf811 126 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 127 */
wue 0:7b58cdacf811 128 bool release(uint8_t button);
wue 0:7b58cdacf811 129
wue 0:7b58cdacf811 130 /**
wue 0:7b58cdacf811 131 * Double click (MOUSE_LEFT)
wue 0:7b58cdacf811 132 *
wue 0:7b58cdacf811 133 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 134 */
wue 0:7b58cdacf811 135 bool doubleClick();
wue 0:7b58cdacf811 136
wue 0:7b58cdacf811 137 /**
wue 0:7b58cdacf811 138 * Click
wue 0:7b58cdacf811 139 *
wue 0:7b58cdacf811 140 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
wue 0:7b58cdacf811 141 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 142 */
wue 0:7b58cdacf811 143 bool click(uint8_t button);
wue 0:7b58cdacf811 144
wue 0:7b58cdacf811 145 /**
wue 0:7b58cdacf811 146 * Scrolling
wue 0:7b58cdacf811 147 *
wue 0:7b58cdacf811 148 * @param z value of the wheel (>0 to go down, <0 to go up)
wue 0:7b58cdacf811 149 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 150 */
wue 0:7b58cdacf811 151 bool scroll(int8_t z);
wue 0:7b58cdacf811 152
wue 0:7b58cdacf811 153 /**
wue 0:7b58cdacf811 154 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
wue 0:7b58cdacf811 155 *
wue 0:7b58cdacf811 156 * @code
wue 0:7b58cdacf811 157 * //To send CTRL + s (save)
wue 0:7b58cdacf811 158 * keyboard.keyCode('s', KEY_CTRL);
wue 0:7b58cdacf811 159 * @endcode
wue 0:7b58cdacf811 160 *
wue 0:7b58cdacf811 161 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
wue 0:7b58cdacf811 162 * @param key character to send
wue 0:7b58cdacf811 163 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 164 */
wue 0:7b58cdacf811 165 bool keyCode(uint8_t key, uint8_t modifier = 0);
wue 0:7b58cdacf811 166
wue 0:7b58cdacf811 167 /**
wue 0:7b58cdacf811 168 * Send a character
wue 0:7b58cdacf811 169 *
wue 0:7b58cdacf811 170 * @param c character to be sent
wue 0:7b58cdacf811 171 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 172 */
wue 0:7b58cdacf811 173 virtual int _putc(int c);
wue 0:7b58cdacf811 174
wue 0:7b58cdacf811 175 /**
wue 0:7b58cdacf811 176 * Control media keys
wue 0:7b58cdacf811 177 *
wue 0:7b58cdacf811 178 * @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 179 * @returns true if there is no error, false otherwise
wue 0:7b58cdacf811 180 */
wue 0:7b58cdacf811 181 bool mediaControl(MEDIA_KEY key);
wue 0:7b58cdacf811 182
wue 0:7b58cdacf811 183 /**
wue 0:7b58cdacf811 184 * 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 185 * - First bit: NUM_LOCK
wue 0:7b58cdacf811 186 * - Second bit: CAPS_LOCK
wue 0:7b58cdacf811 187 * - Third bit: SCROLL_LOCK
wue 0:7b58cdacf811 188 *
wue 0:7b58cdacf811 189 * @returns status of lock keys
wue 0:7b58cdacf811 190 */
wue 0:7b58cdacf811 191 uint8_t lockStatus();
wue 0:7b58cdacf811 192
wue 0:7b58cdacf811 193 /*
wue 0:7b58cdacf811 194 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
wue 0:7b58cdacf811 195 *
wue 0:7b58cdacf811 196 * @returns pointer to the report descriptor
wue 0:7b58cdacf811 197 */
wue 0:7b58cdacf811 198 virtual uint8_t * reportDesc();
wue 0:7b58cdacf811 199
wue 0:7b58cdacf811 200 /*
wue 0:7b58cdacf811 201 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
wue 0:7b58cdacf811 202 *
wue 0:7b58cdacf811 203 * @returns if handle by subclass, return true
wue 0:7b58cdacf811 204 */
wue 0:7b58cdacf811 205 virtual bool EP1_OUT_callback();
wue 0:7b58cdacf811 206
wue 0:7b58cdacf811 207
wue 0:7b58cdacf811 208 private:
wue 0:7b58cdacf811 209 bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
wue 0:7b58cdacf811 210 MOUSE_TYPE mouse_type;
wue 0:7b58cdacf811 211 uint8_t button;
wue 0:7b58cdacf811 212 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
wue 0:7b58cdacf811 213
wue 0:7b58cdacf811 214 uint8_t lock_status;
wue 0:7b58cdacf811 215
wue 0:7b58cdacf811 216 //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
wue 0:7b58cdacf811 217 virtual int _getc() { return -1;}
wue 0:7b58cdacf811 218 };
wue 0:7b58cdacf811 219
wue 0:7b58cdacf811 220 #endif