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