![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control the wondrous spinning-frog game of Zuma's Revenge with a rotating chair and an Airzooka. Maps compass rotation, flex sensor and push button input to USB actions to control Zuma's Revenge (http://www.popcap.com/games/zumas-revenge/online)
Dependencies: LSM303DLHC mbed
Note that content for USB HID and USB Device is actually from the USBDevice mbed library. However, we made a couple of small changes to this library (allowing USB clicks at a particular location) that required us to break it off from the main project if we wanted to publish without pushing upstream.
USBHID/USBMouseKeyboard.h@0:4df415dde990, 2014-09-29 (annotated)
- Committer:
- andrewhead
- Date:
- Mon Sep 29 01:12:20 2014 +0000
- Revision:
- 0:4df415dde990
Initial Commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewhead | 0:4df415dde990 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
andrewhead | 0:4df415dde990 | 2 | * |
andrewhead | 0:4df415dde990 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
andrewhead | 0:4df415dde990 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
andrewhead | 0:4df415dde990 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
andrewhead | 0:4df415dde990 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
andrewhead | 0:4df415dde990 | 7 | * Software is furnished to do so, subject to the following conditions: |
andrewhead | 0:4df415dde990 | 8 | * |
andrewhead | 0:4df415dde990 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
andrewhead | 0:4df415dde990 | 10 | * substantial portions of the Software. |
andrewhead | 0:4df415dde990 | 11 | * |
andrewhead | 0:4df415dde990 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
andrewhead | 0:4df415dde990 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
andrewhead | 0:4df415dde990 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
andrewhead | 0:4df415dde990 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
andrewhead | 0:4df415dde990 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
andrewhead | 0:4df415dde990 | 17 | */ |
andrewhead | 0:4df415dde990 | 18 | |
andrewhead | 0:4df415dde990 | 19 | #ifndef USBMOUSEKEYBOARD_H |
andrewhead | 0:4df415dde990 | 20 | #define USBMOUSEKEYBOARD_H |
andrewhead | 0:4df415dde990 | 21 | |
andrewhead | 0:4df415dde990 | 22 | #define REPORT_ID_KEYBOARD 1 |
andrewhead | 0:4df415dde990 | 23 | #define REPORT_ID_MOUSE 2 |
andrewhead | 0:4df415dde990 | 24 | #define REPORT_ID_VOLUME 3 |
andrewhead | 0:4df415dde990 | 25 | |
andrewhead | 0:4df415dde990 | 26 | #include "USBMouse.h" |
andrewhead | 0:4df415dde990 | 27 | #include "USBKeyboard.h" |
andrewhead | 0:4df415dde990 | 28 | #include "Stream.h" |
andrewhead | 0:4df415dde990 | 29 | #include "USBHID.h" |
andrewhead | 0:4df415dde990 | 30 | |
andrewhead | 0:4df415dde990 | 31 | /** |
andrewhead | 0:4df415dde990 | 32 | * USBMouseKeyboard example |
andrewhead | 0:4df415dde990 | 33 | * @code |
andrewhead | 0:4df415dde990 | 34 | * |
andrewhead | 0:4df415dde990 | 35 | * #include "mbed.h" |
andrewhead | 0:4df415dde990 | 36 | * #include "USBMouseKeyboard.h" |
andrewhead | 0:4df415dde990 | 37 | * |
andrewhead | 0:4df415dde990 | 38 | * USBMouseKeyboard key_mouse; |
andrewhead | 0:4df415dde990 | 39 | * |
andrewhead | 0:4df415dde990 | 40 | * int main(void) |
andrewhead | 0:4df415dde990 | 41 | * { |
andrewhead | 0:4df415dde990 | 42 | * while(1) |
andrewhead | 0:4df415dde990 | 43 | * { |
andrewhead | 0:4df415dde990 | 44 | * key_mouse.move(20, 0); |
andrewhead | 0:4df415dde990 | 45 | * key_mouse.printf("Hello From MBED\r\n"); |
andrewhead | 0:4df415dde990 | 46 | * wait(1); |
andrewhead | 0:4df415dde990 | 47 | * } |
andrewhead | 0:4df415dde990 | 48 | * } |
andrewhead | 0:4df415dde990 | 49 | * @endcode |
andrewhead | 0:4df415dde990 | 50 | * |
andrewhead | 0:4df415dde990 | 51 | * |
andrewhead | 0:4df415dde990 | 52 | * @code |
andrewhead | 0:4df415dde990 | 53 | * |
andrewhead | 0:4df415dde990 | 54 | * #include "mbed.h" |
andrewhead | 0:4df415dde990 | 55 | * #include "USBMouseKeyboard.h" |
andrewhead | 0:4df415dde990 | 56 | * |
andrewhead | 0:4df415dde990 | 57 | * USBMouseKeyboard key_mouse(ABS_MOUSE); |
andrewhead | 0:4df415dde990 | 58 | * |
andrewhead | 0:4df415dde990 | 59 | * int main(void) |
andrewhead | 0:4df415dde990 | 60 | * { |
andrewhead | 0:4df415dde990 | 61 | * while(1) |
andrewhead | 0:4df415dde990 | 62 | * { |
andrewhead | 0:4df415dde990 | 63 | * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2); |
andrewhead | 0:4df415dde990 | 64 | * key_mouse.printf("Hello from MBED\r\n"); |
andrewhead | 0:4df415dde990 | 65 | * wait(1); |
andrewhead | 0:4df415dde990 | 66 | * } |
andrewhead | 0:4df415dde990 | 67 | * } |
andrewhead | 0:4df415dde990 | 68 | * @endcode |
andrewhead | 0:4df415dde990 | 69 | */ |
andrewhead | 0:4df415dde990 | 70 | class USBMouseKeyboard: public USBHID, public Stream |
andrewhead | 0:4df415dde990 | 71 | { |
andrewhead | 0:4df415dde990 | 72 | public: |
andrewhead | 0:4df415dde990 | 73 | |
andrewhead | 0:4df415dde990 | 74 | /** |
andrewhead | 0:4df415dde990 | 75 | * Constructor |
andrewhead | 0:4df415dde990 | 76 | * |
andrewhead | 0:4df415dde990 | 77 | * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE) |
andrewhead | 0:4df415dde990 | 78 | * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK |
andrewhead | 0:4df415dde990 | 79 | * @param vendor_id Your vendor_id (default: 0x1234) |
andrewhead | 0:4df415dde990 | 80 | * @param product_id Your product_id (default: 0x0001) |
andrewhead | 0:4df415dde990 | 81 | * @param product_release Your preoduct_release (default: 0x0001) |
andrewhead | 0:4df415dde990 | 82 | * |
andrewhead | 0:4df415dde990 | 83 | */ |
andrewhead | 0:4df415dde990 | 84 | USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001): |
andrewhead | 0:4df415dde990 | 85 | USBHID(0, 0, vendor_id, product_id, product_release, false) |
andrewhead | 0:4df415dde990 | 86 | { |
andrewhead | 0:4df415dde990 | 87 | lock_status = 0; |
andrewhead | 0:4df415dde990 | 88 | button = 0; |
andrewhead | 0:4df415dde990 | 89 | this->mouse_type = mouse_type; |
andrewhead | 0:4df415dde990 | 90 | connect(); |
andrewhead | 0:4df415dde990 | 91 | }; |
andrewhead | 0:4df415dde990 | 92 | |
andrewhead | 0:4df415dde990 | 93 | /** |
andrewhead | 0:4df415dde990 | 94 | * Write a state of the mouse |
andrewhead | 0:4df415dde990 | 95 | * |
andrewhead | 0:4df415dde990 | 96 | * @param x x-axis position |
andrewhead | 0:4df415dde990 | 97 | * @param y y-axis position |
andrewhead | 0:4df415dde990 | 98 | * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE) |
andrewhead | 0:4df415dde990 | 99 | * @param z wheel state (>0 to scroll down, <0 to scroll up) |
andrewhead | 0:4df415dde990 | 100 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 101 | */ |
andrewhead | 0:4df415dde990 | 102 | bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z); |
andrewhead | 0:4df415dde990 | 103 | |
andrewhead | 0:4df415dde990 | 104 | |
andrewhead | 0:4df415dde990 | 105 | /** |
andrewhead | 0:4df415dde990 | 106 | * Move the cursor to (x, y) |
andrewhead | 0:4df415dde990 | 107 | * |
andrewhead | 0:4df415dde990 | 108 | * @param x x-axis position |
andrewhead | 0:4df415dde990 | 109 | * @param y y-axis position |
andrewhead | 0:4df415dde990 | 110 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 111 | */ |
andrewhead | 0:4df415dde990 | 112 | bool move(int16_t x, int16_t y); |
andrewhead | 0:4df415dde990 | 113 | |
andrewhead | 0:4df415dde990 | 114 | /** |
andrewhead | 0:4df415dde990 | 115 | * Press one or several buttons |
andrewhead | 0:4df415dde990 | 116 | * |
andrewhead | 0:4df415dde990 | 117 | * @param button button state (ex: press(MOUSE_LEFT)) |
andrewhead | 0:4df415dde990 | 118 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 119 | */ |
andrewhead | 0:4df415dde990 | 120 | bool press(uint8_t button); |
andrewhead | 0:4df415dde990 | 121 | |
andrewhead | 0:4df415dde990 | 122 | /** |
andrewhead | 0:4df415dde990 | 123 | * Release one or several buttons |
andrewhead | 0:4df415dde990 | 124 | * |
andrewhead | 0:4df415dde990 | 125 | * @param button button state (ex: release(MOUSE_LEFT)) |
andrewhead | 0:4df415dde990 | 126 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 127 | */ |
andrewhead | 0:4df415dde990 | 128 | bool release(uint8_t button); |
andrewhead | 0:4df415dde990 | 129 | |
andrewhead | 0:4df415dde990 | 130 | /** |
andrewhead | 0:4df415dde990 | 131 | * Double click (MOUSE_LEFT) |
andrewhead | 0:4df415dde990 | 132 | * |
andrewhead | 0:4df415dde990 | 133 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 134 | */ |
andrewhead | 0:4df415dde990 | 135 | bool doubleClick(); |
andrewhead | 0:4df415dde990 | 136 | |
andrewhead | 0:4df415dde990 | 137 | /** |
andrewhead | 0:4df415dde990 | 138 | * Click |
andrewhead | 0:4df415dde990 | 139 | * |
andrewhead | 0:4df415dde990 | 140 | * @param button state of the buttons ( ex: clic(MOUSE_LEFT)) |
andrewhead | 0:4df415dde990 | 141 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 142 | */ |
andrewhead | 0:4df415dde990 | 143 | bool click(uint16_t x, uint16_t y, uint8_t button); |
andrewhead | 0:4df415dde990 | 144 | |
andrewhead | 0:4df415dde990 | 145 | /** |
andrewhead | 0:4df415dde990 | 146 | * Scrolling |
andrewhead | 0:4df415dde990 | 147 | * |
andrewhead | 0:4df415dde990 | 148 | * @param z value of the wheel (>0 to go down, <0 to go up) |
andrewhead | 0:4df415dde990 | 149 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 150 | */ |
andrewhead | 0:4df415dde990 | 151 | bool scroll(int8_t z); |
andrewhead | 0:4df415dde990 | 152 | |
andrewhead | 0:4df415dde990 | 153 | /** |
andrewhead | 0:4df415dde990 | 154 | * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key |
andrewhead | 0:4df415dde990 | 155 | * |
andrewhead | 0:4df415dde990 | 156 | * @code |
andrewhead | 0:4df415dde990 | 157 | * //To send CTRL + s (save) |
andrewhead | 0:4df415dde990 | 158 | * keyboard.keyCode('s', KEY_CTRL); |
andrewhead | 0:4df415dde990 | 159 | * @endcode |
andrewhead | 0:4df415dde990 | 160 | * |
andrewhead | 0:4df415dde990 | 161 | * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0) |
andrewhead | 0:4df415dde990 | 162 | * @param key character to send |
andrewhead | 0:4df415dde990 | 163 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 164 | */ |
andrewhead | 0:4df415dde990 | 165 | bool keyCode(uint8_t key, uint8_t modifier = 0); |
andrewhead | 0:4df415dde990 | 166 | |
andrewhead | 0:4df415dde990 | 167 | /** |
andrewhead | 0:4df415dde990 | 168 | * Send a character |
andrewhead | 0:4df415dde990 | 169 | * |
andrewhead | 0:4df415dde990 | 170 | * @param c character to be sent |
andrewhead | 0:4df415dde990 | 171 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 172 | */ |
andrewhead | 0:4df415dde990 | 173 | virtual int _putc(int c); |
andrewhead | 0:4df415dde990 | 174 | |
andrewhead | 0:4df415dde990 | 175 | /** |
andrewhead | 0:4df415dde990 | 176 | * Control media keys |
andrewhead | 0:4df415dde990 | 177 | * |
andrewhead | 0:4df415dde990 | 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) |
andrewhead | 0:4df415dde990 | 179 | * @returns true if there is no error, false otherwise |
andrewhead | 0:4df415dde990 | 180 | */ |
andrewhead | 0:4df415dde990 | 181 | bool mediaControl(MEDIA_KEY key); |
andrewhead | 0:4df415dde990 | 182 | |
andrewhead | 0:4df415dde990 | 183 | /** |
andrewhead | 0:4df415dde990 | 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: |
andrewhead | 0:4df415dde990 | 185 | * - First bit: NUM_LOCK |
andrewhead | 0:4df415dde990 | 186 | * - Second bit: CAPS_LOCK |
andrewhead | 0:4df415dde990 | 187 | * - Third bit: SCROLL_LOCK |
andrewhead | 0:4df415dde990 | 188 | * |
andrewhead | 0:4df415dde990 | 189 | * @returns status of lock keys |
andrewhead | 0:4df415dde990 | 190 | */ |
andrewhead | 0:4df415dde990 | 191 | uint8_t lockStatus(); |
andrewhead | 0:4df415dde990 | 192 | |
andrewhead | 0:4df415dde990 | 193 | /* |
andrewhead | 0:4df415dde990 | 194 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
andrewhead | 0:4df415dde990 | 195 | * |
andrewhead | 0:4df415dde990 | 196 | * @returns pointer to the report descriptor |
andrewhead | 0:4df415dde990 | 197 | */ |
andrewhead | 0:4df415dde990 | 198 | virtual uint8_t * reportDesc(); |
andrewhead | 0:4df415dde990 | 199 | |
andrewhead | 0:4df415dde990 | 200 | /* |
andrewhead | 0:4df415dde990 | 201 | * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys |
andrewhead | 0:4df415dde990 | 202 | * |
andrewhead | 0:4df415dde990 | 203 | * @returns if handle by subclass, return true |
andrewhead | 0:4df415dde990 | 204 | */ |
andrewhead | 0:4df415dde990 | 205 | virtual bool EP1_OUT_callback(); |
andrewhead | 0:4df415dde990 | 206 | |
andrewhead | 0:4df415dde990 | 207 | |
andrewhead | 0:4df415dde990 | 208 | private: |
andrewhead | 0:4df415dde990 | 209 | bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z); |
andrewhead | 0:4df415dde990 | 210 | MOUSE_TYPE mouse_type; |
andrewhead | 0:4df415dde990 | 211 | uint8_t button; |
andrewhead | 0:4df415dde990 | 212 | bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z); |
andrewhead | 0:4df415dde990 | 213 | |
andrewhead | 0:4df415dde990 | 214 | uint8_t lock_status; |
andrewhead | 0:4df415dde990 | 215 | |
andrewhead | 0:4df415dde990 | 216 | //dummy otherwise it doesn't compile (we must define all methods of an abstract class) |
andrewhead | 0:4df415dde990 | 217 | virtual int _getc() { return -1;} |
andrewhead | 0:4df415dde990 | 218 | }; |
andrewhead | 0:4df415dde990 | 219 | |
andrewhead | 0:4df415dde990 | 220 | #endif |