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