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