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