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/USBMouse.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 USBMOUSE_H |
DieterGraef | 0:0a2eaa300982 | 20 | #define USBMOUSE_H |
DieterGraef | 0:0a2eaa300982 | 21 | |
DieterGraef | 0:0a2eaa300982 | 22 | #include "USBHID.h" |
DieterGraef | 0:0a2eaa300982 | 23 | |
DieterGraef | 0:0a2eaa300982 | 24 | #define REPORT_ID_MOUSE 2 |
DieterGraef | 0:0a2eaa300982 | 25 | |
DieterGraef | 0:0a2eaa300982 | 26 | /* Common usage */ |
DieterGraef | 0:0a2eaa300982 | 27 | |
DieterGraef | 0:0a2eaa300982 | 28 | enum MOUSE_BUTTON |
DieterGraef | 0:0a2eaa300982 | 29 | { |
DieterGraef | 0:0a2eaa300982 | 30 | MOUSE_LEFT = 1, |
DieterGraef | 0:0a2eaa300982 | 31 | MOUSE_RIGHT = 2, |
DieterGraef | 0:0a2eaa300982 | 32 | MOUSE_MIDDLE = 4, |
DieterGraef | 0:0a2eaa300982 | 33 | }; |
DieterGraef | 0:0a2eaa300982 | 34 | |
DieterGraef | 0:0a2eaa300982 | 35 | /* X and Y limits */ |
DieterGraef | 0:0a2eaa300982 | 36 | /* These values do not directly map to screen pixels */ |
DieterGraef | 0:0a2eaa300982 | 37 | /* Zero may be interpreted as meaning 'no movement' */ |
DieterGraef | 0:0a2eaa300982 | 38 | #define X_MIN_ABS (1) /*!< Minimum value on x-axis */ |
DieterGraef | 0:0a2eaa300982 | 39 | #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */ |
DieterGraef | 0:0a2eaa300982 | 40 | #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */ |
DieterGraef | 0:0a2eaa300982 | 41 | #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */ |
DieterGraef | 0:0a2eaa300982 | 42 | |
DieterGraef | 0:0a2eaa300982 | 43 | #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */ |
DieterGraef | 0:0a2eaa300982 | 44 | #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */ |
DieterGraef | 0:0a2eaa300982 | 45 | #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */ |
DieterGraef | 0:0a2eaa300982 | 46 | #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */ |
DieterGraef | 0:0a2eaa300982 | 47 | |
DieterGraef | 0:0a2eaa300982 | 48 | enum MOUSE_TYPE |
DieterGraef | 0:0a2eaa300982 | 49 | { |
DieterGraef | 0:0a2eaa300982 | 50 | ABS_MOUSE, |
DieterGraef | 0:0a2eaa300982 | 51 | REL_MOUSE, |
DieterGraef | 0:0a2eaa300982 | 52 | }; |
DieterGraef | 0:0a2eaa300982 | 53 | |
DieterGraef | 0:0a2eaa300982 | 54 | /** |
DieterGraef | 0:0a2eaa300982 | 55 | * |
DieterGraef | 0:0a2eaa300982 | 56 | * USBMouse example |
DieterGraef | 0:0a2eaa300982 | 57 | * @code |
DieterGraef | 0:0a2eaa300982 | 58 | * #include "mbed.h" |
DieterGraef | 0:0a2eaa300982 | 59 | * #include "USBMouse.h" |
DieterGraef | 0:0a2eaa300982 | 60 | * |
DieterGraef | 0:0a2eaa300982 | 61 | * USBMouse mouse; |
DieterGraef | 0:0a2eaa300982 | 62 | * |
DieterGraef | 0:0a2eaa300982 | 63 | * int main(void) |
DieterGraef | 0:0a2eaa300982 | 64 | * { |
DieterGraef | 0:0a2eaa300982 | 65 | * while (1) |
DieterGraef | 0:0a2eaa300982 | 66 | * { |
DieterGraef | 0:0a2eaa300982 | 67 | * mouse.move(20, 0); |
DieterGraef | 0:0a2eaa300982 | 68 | * wait(0.5); |
DieterGraef | 0:0a2eaa300982 | 69 | * } |
DieterGraef | 0:0a2eaa300982 | 70 | * } |
DieterGraef | 0:0a2eaa300982 | 71 | * |
DieterGraef | 0:0a2eaa300982 | 72 | * @endcode |
DieterGraef | 0:0a2eaa300982 | 73 | * |
DieterGraef | 0:0a2eaa300982 | 74 | * |
DieterGraef | 0:0a2eaa300982 | 75 | * @code |
DieterGraef | 0:0a2eaa300982 | 76 | * #include "mbed.h" |
DieterGraef | 0:0a2eaa300982 | 77 | * #include "USBMouse.h" |
DieterGraef | 0:0a2eaa300982 | 78 | * #include <math.h> |
DieterGraef | 0:0a2eaa300982 | 79 | * |
DieterGraef | 0:0a2eaa300982 | 80 | * USBMouse mouse(ABS_MOUSE); |
DieterGraef | 0:0a2eaa300982 | 81 | * |
DieterGraef | 0:0a2eaa300982 | 82 | * int main(void) |
DieterGraef | 0:0a2eaa300982 | 83 | * { |
DieterGraef | 0:0a2eaa300982 | 84 | * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2; |
DieterGraef | 0:0a2eaa300982 | 85 | * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2; |
DieterGraef | 0:0a2eaa300982 | 86 | * uint16_t x_screen = 0; |
DieterGraef | 0:0a2eaa300982 | 87 | * uint16_t y_screen = 0; |
DieterGraef | 0:0a2eaa300982 | 88 | * |
DieterGraef | 0:0a2eaa300982 | 89 | * uint32_t x_origin = x_center; |
DieterGraef | 0:0a2eaa300982 | 90 | * uint32_t y_origin = y_center; |
DieterGraef | 0:0a2eaa300982 | 91 | * uint32_t radius = 5000; |
DieterGraef | 0:0a2eaa300982 | 92 | * uint32_t angle = 0; |
DieterGraef | 0:0a2eaa300982 | 93 | * |
DieterGraef | 0:0a2eaa300982 | 94 | * while (1) |
DieterGraef | 0:0a2eaa300982 | 95 | * { |
DieterGraef | 0:0a2eaa300982 | 96 | * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius; |
DieterGraef | 0:0a2eaa300982 | 97 | * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius; |
DieterGraef | 0:0a2eaa300982 | 98 | * |
DieterGraef | 0:0a2eaa300982 | 99 | * mouse.move(x_screen, y_screen); |
DieterGraef | 0:0a2eaa300982 | 100 | * angle += 3; |
DieterGraef | 0:0a2eaa300982 | 101 | * wait(0.01); |
DieterGraef | 0:0a2eaa300982 | 102 | * } |
DieterGraef | 0:0a2eaa300982 | 103 | * } |
DieterGraef | 0:0a2eaa300982 | 104 | * |
DieterGraef | 0:0a2eaa300982 | 105 | * @endcode |
DieterGraef | 0:0a2eaa300982 | 106 | */ |
DieterGraef | 0:0a2eaa300982 | 107 | class USBMouse: public USBHID |
DieterGraef | 0:0a2eaa300982 | 108 | { |
DieterGraef | 0:0a2eaa300982 | 109 | public: |
DieterGraef | 0:0a2eaa300982 | 110 | |
DieterGraef | 0:0a2eaa300982 | 111 | /** |
DieterGraef | 0:0a2eaa300982 | 112 | * Constructor |
DieterGraef | 0:0a2eaa300982 | 113 | * |
DieterGraef | 0:0a2eaa300982 | 114 | * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE) |
DieterGraef | 0:0a2eaa300982 | 115 | * @param vendor_id Your vendor_id (default: 0x1234) |
DieterGraef | 0:0a2eaa300982 | 116 | * @param product_id Your product_id (default: 0x0001) |
DieterGraef | 0:0a2eaa300982 | 117 | * @param product_release Your preoduct_release (default: 0x0001) |
DieterGraef | 0:0a2eaa300982 | 118 | * |
DieterGraef | 0:0a2eaa300982 | 119 | */ |
DieterGraef | 0:0a2eaa300982 | 120 | USBMouse(uint16_t HW_Interface,MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001): |
DieterGraef | 0:0a2eaa300982 | 121 | USBHID(HW_Interface,0, 0, vendor_id, product_id, product_release, false) |
DieterGraef | 0:0a2eaa300982 | 122 | { |
DieterGraef | 0:0a2eaa300982 | 123 | button = 0; |
DieterGraef | 0:0a2eaa300982 | 124 | this->mouse_type = mouse_type; |
DieterGraef | 0:0a2eaa300982 | 125 | connect(); |
DieterGraef | 0:0a2eaa300982 | 126 | }; |
DieterGraef | 0:0a2eaa300982 | 127 | |
DieterGraef | 0:0a2eaa300982 | 128 | /** |
DieterGraef | 0:0a2eaa300982 | 129 | * Write a state of the mouse |
DieterGraef | 0:0a2eaa300982 | 130 | * |
DieterGraef | 0:0a2eaa300982 | 131 | * @param x x-axis position |
DieterGraef | 0:0a2eaa300982 | 132 | * @param y y-axis position |
DieterGraef | 0:0a2eaa300982 | 133 | * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE) |
DieterGraef | 0:0a2eaa300982 | 134 | * @param z wheel state (>0 to scroll down, <0 to scroll up) |
DieterGraef | 0:0a2eaa300982 | 135 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 136 | */ |
DieterGraef | 0:0a2eaa300982 | 137 | bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z); |
DieterGraef | 0:0a2eaa300982 | 138 | |
DieterGraef | 0:0a2eaa300982 | 139 | |
DieterGraef | 0:0a2eaa300982 | 140 | /** |
DieterGraef | 0:0a2eaa300982 | 141 | * Move the cursor to (x, y) |
DieterGraef | 0:0a2eaa300982 | 142 | * |
DieterGraef | 0:0a2eaa300982 | 143 | * @param x-axis position |
DieterGraef | 0:0a2eaa300982 | 144 | * @param y-axis position |
DieterGraef | 0:0a2eaa300982 | 145 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 146 | */ |
DieterGraef | 0:0a2eaa300982 | 147 | bool move(int16_t x, int16_t y); |
DieterGraef | 0:0a2eaa300982 | 148 | |
DieterGraef | 0:0a2eaa300982 | 149 | /** |
DieterGraef | 0:0a2eaa300982 | 150 | * Press one or several buttons |
DieterGraef | 0:0a2eaa300982 | 151 | * |
DieterGraef | 0:0a2eaa300982 | 152 | * @param button button state (ex: press(MOUSE_LEFT)) |
DieterGraef | 0:0a2eaa300982 | 153 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 154 | */ |
DieterGraef | 0:0a2eaa300982 | 155 | bool press(uint8_t button); |
DieterGraef | 0:0a2eaa300982 | 156 | |
DieterGraef | 0:0a2eaa300982 | 157 | /** |
DieterGraef | 0:0a2eaa300982 | 158 | * Release one or several buttons |
DieterGraef | 0:0a2eaa300982 | 159 | * |
DieterGraef | 0:0a2eaa300982 | 160 | * @param button button state (ex: release(MOUSE_LEFT)) |
DieterGraef | 0:0a2eaa300982 | 161 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 162 | */ |
DieterGraef | 0:0a2eaa300982 | 163 | bool release(uint8_t button); |
DieterGraef | 0:0a2eaa300982 | 164 | |
DieterGraef | 0:0a2eaa300982 | 165 | /** |
DieterGraef | 0:0a2eaa300982 | 166 | * Double click (MOUSE_LEFT) |
DieterGraef | 0:0a2eaa300982 | 167 | * |
DieterGraef | 0:0a2eaa300982 | 168 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 169 | */ |
DieterGraef | 0:0a2eaa300982 | 170 | bool doubleClick(); |
DieterGraef | 0:0a2eaa300982 | 171 | |
DieterGraef | 0:0a2eaa300982 | 172 | /** |
DieterGraef | 0:0a2eaa300982 | 173 | * Click |
DieterGraef | 0:0a2eaa300982 | 174 | * |
DieterGraef | 0:0a2eaa300982 | 175 | * @param button state of the buttons ( ex: clic(MOUSE_LEFT)) |
DieterGraef | 0:0a2eaa300982 | 176 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 177 | */ |
DieterGraef | 0:0a2eaa300982 | 178 | bool click(uint8_t button); |
DieterGraef | 0:0a2eaa300982 | 179 | |
DieterGraef | 0:0a2eaa300982 | 180 | /** |
DieterGraef | 0:0a2eaa300982 | 181 | * Scrolling |
DieterGraef | 0:0a2eaa300982 | 182 | * |
DieterGraef | 0:0a2eaa300982 | 183 | * @param z value of the wheel (>0 to go down, <0 to go up) |
DieterGraef | 0:0a2eaa300982 | 184 | * @returns true if there is no error, false otherwise |
DieterGraef | 0:0a2eaa300982 | 185 | */ |
DieterGraef | 0:0a2eaa300982 | 186 | bool scroll(int8_t z); |
DieterGraef | 0:0a2eaa300982 | 187 | |
DieterGraef | 0:0a2eaa300982 | 188 | /* |
DieterGraef | 0:0a2eaa300982 | 189 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
DieterGraef | 0:0a2eaa300982 | 190 | * |
DieterGraef | 0:0a2eaa300982 | 191 | * @returns pointer to the report descriptor |
DieterGraef | 0:0a2eaa300982 | 192 | */ |
DieterGraef | 0:0a2eaa300982 | 193 | virtual uint8_t * reportDesc(); |
DieterGraef | 0:0a2eaa300982 | 194 | |
DieterGraef | 0:0a2eaa300982 | 195 | protected: |
DieterGraef | 0:0a2eaa300982 | 196 | /* |
DieterGraef | 0:0a2eaa300982 | 197 | * Get configuration descriptor |
DieterGraef | 0:0a2eaa300982 | 198 | * |
DieterGraef | 0:0a2eaa300982 | 199 | * @returns pointer to the configuration descriptor |
DieterGraef | 0:0a2eaa300982 | 200 | */ |
DieterGraef | 0:0a2eaa300982 | 201 | virtual uint8_t * configurationDesc(); |
DieterGraef | 0:0a2eaa300982 | 202 | |
DieterGraef | 0:0a2eaa300982 | 203 | private: |
DieterGraef | 0:0a2eaa300982 | 204 | MOUSE_TYPE mouse_type; |
DieterGraef | 0:0a2eaa300982 | 205 | uint8_t button; |
DieterGraef | 0:0a2eaa300982 | 206 | bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z); |
DieterGraef | 0:0a2eaa300982 | 207 | }; |
DieterGraef | 0:0a2eaa300982 | 208 | |
DieterGraef | 0:0a2eaa300982 | 209 | #endif |