usb device
USBHID/USBMouse.h@0:c1e89c49eae5, 2022-05-14 (annotated)
- Committer:
- ppo
- Date:
- Sat May 14 17:24:10 2022 +0000
- Revision:
- 0:c1e89c49eae5
commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ppo | 0:c1e89c49eae5 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
ppo | 0:c1e89c49eae5 | 2 | * |
ppo | 0:c1e89c49eae5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ppo | 0:c1e89c49eae5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
ppo | 0:c1e89c49eae5 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
ppo | 0:c1e89c49eae5 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
ppo | 0:c1e89c49eae5 | 7 | * Software is furnished to do so, subject to the following conditions: |
ppo | 0:c1e89c49eae5 | 8 | * |
ppo | 0:c1e89c49eae5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ppo | 0:c1e89c49eae5 | 10 | * substantial portions of the Software. |
ppo | 0:c1e89c49eae5 | 11 | * |
ppo | 0:c1e89c49eae5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ppo | 0:c1e89c49eae5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ppo | 0:c1e89c49eae5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ppo | 0:c1e89c49eae5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ppo | 0:c1e89c49eae5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ppo | 0:c1e89c49eae5 | 17 | */ |
ppo | 0:c1e89c49eae5 | 18 | |
ppo | 0:c1e89c49eae5 | 19 | #ifndef USBMOUSE_H |
ppo | 0:c1e89c49eae5 | 20 | #define USBMOUSE_H |
ppo | 0:c1e89c49eae5 | 21 | |
ppo | 0:c1e89c49eae5 | 22 | #include "USBHID.h" |
ppo | 0:c1e89c49eae5 | 23 | |
ppo | 0:c1e89c49eae5 | 24 | #define REPORT_ID_MOUSE 2 |
ppo | 0:c1e89c49eae5 | 25 | |
ppo | 0:c1e89c49eae5 | 26 | /* Common usage */ |
ppo | 0:c1e89c49eae5 | 27 | |
ppo | 0:c1e89c49eae5 | 28 | enum MOUSE_BUTTON |
ppo | 0:c1e89c49eae5 | 29 | { |
ppo | 0:c1e89c49eae5 | 30 | MOUSE_LEFT = 1, |
ppo | 0:c1e89c49eae5 | 31 | MOUSE_RIGHT = 2, |
ppo | 0:c1e89c49eae5 | 32 | MOUSE_MIDDLE = 4, |
ppo | 0:c1e89c49eae5 | 33 | }; |
ppo | 0:c1e89c49eae5 | 34 | |
ppo | 0:c1e89c49eae5 | 35 | /* X and Y limits */ |
ppo | 0:c1e89c49eae5 | 36 | /* These values do not directly map to screen pixels */ |
ppo | 0:c1e89c49eae5 | 37 | /* Zero may be interpreted as meaning 'no movement' */ |
ppo | 0:c1e89c49eae5 | 38 | #define X_MIN_ABS (1) /*!< Minimum value on x-axis */ |
ppo | 0:c1e89c49eae5 | 39 | #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */ |
ppo | 0:c1e89c49eae5 | 40 | #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */ |
ppo | 0:c1e89c49eae5 | 41 | #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */ |
ppo | 0:c1e89c49eae5 | 42 | |
ppo | 0:c1e89c49eae5 | 43 | #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */ |
ppo | 0:c1e89c49eae5 | 44 | #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */ |
ppo | 0:c1e89c49eae5 | 45 | #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */ |
ppo | 0:c1e89c49eae5 | 46 | #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */ |
ppo | 0:c1e89c49eae5 | 47 | |
ppo | 0:c1e89c49eae5 | 48 | enum MOUSE_TYPE |
ppo | 0:c1e89c49eae5 | 49 | { |
ppo | 0:c1e89c49eae5 | 50 | ABS_MOUSE, |
ppo | 0:c1e89c49eae5 | 51 | REL_MOUSE, |
ppo | 0:c1e89c49eae5 | 52 | }; |
ppo | 0:c1e89c49eae5 | 53 | |
ppo | 0:c1e89c49eae5 | 54 | /** |
ppo | 0:c1e89c49eae5 | 55 | * |
ppo | 0:c1e89c49eae5 | 56 | * USBMouse example |
ppo | 0:c1e89c49eae5 | 57 | * @code |
ppo | 0:c1e89c49eae5 | 58 | * #include "mbed.h" |
ppo | 0:c1e89c49eae5 | 59 | * #include "USBMouse.h" |
ppo | 0:c1e89c49eae5 | 60 | * |
ppo | 0:c1e89c49eae5 | 61 | * USBMouse mouse; |
ppo | 0:c1e89c49eae5 | 62 | * |
ppo | 0:c1e89c49eae5 | 63 | * int main(void) |
ppo | 0:c1e89c49eae5 | 64 | * { |
ppo | 0:c1e89c49eae5 | 65 | * while (1) |
ppo | 0:c1e89c49eae5 | 66 | * { |
ppo | 0:c1e89c49eae5 | 67 | * mouse.move(20, 0); |
ppo | 0:c1e89c49eae5 | 68 | * wait(0.5); |
ppo | 0:c1e89c49eae5 | 69 | * } |
ppo | 0:c1e89c49eae5 | 70 | * } |
ppo | 0:c1e89c49eae5 | 71 | * |
ppo | 0:c1e89c49eae5 | 72 | * @endcode |
ppo | 0:c1e89c49eae5 | 73 | * |
ppo | 0:c1e89c49eae5 | 74 | * |
ppo | 0:c1e89c49eae5 | 75 | * @code |
ppo | 0:c1e89c49eae5 | 76 | * #include "mbed.h" |
ppo | 0:c1e89c49eae5 | 77 | * #include "USBMouse.h" |
ppo | 0:c1e89c49eae5 | 78 | * #include <math.h> |
ppo | 0:c1e89c49eae5 | 79 | * |
ppo | 0:c1e89c49eae5 | 80 | * USBMouse mouse(ABS_MOUSE); |
ppo | 0:c1e89c49eae5 | 81 | * |
ppo | 0:c1e89c49eae5 | 82 | * int main(void) |
ppo | 0:c1e89c49eae5 | 83 | * { |
ppo | 0:c1e89c49eae5 | 84 | * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2; |
ppo | 0:c1e89c49eae5 | 85 | * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2; |
ppo | 0:c1e89c49eae5 | 86 | * uint16_t x_screen = 0; |
ppo | 0:c1e89c49eae5 | 87 | * uint16_t y_screen = 0; |
ppo | 0:c1e89c49eae5 | 88 | * |
ppo | 0:c1e89c49eae5 | 89 | * uint32_t x_origin = x_center; |
ppo | 0:c1e89c49eae5 | 90 | * uint32_t y_origin = y_center; |
ppo | 0:c1e89c49eae5 | 91 | * uint32_t radius = 5000; |
ppo | 0:c1e89c49eae5 | 92 | * uint32_t angle = 0; |
ppo | 0:c1e89c49eae5 | 93 | * |
ppo | 0:c1e89c49eae5 | 94 | * while (1) |
ppo | 0:c1e89c49eae5 | 95 | * { |
ppo | 0:c1e89c49eae5 | 96 | * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius; |
ppo | 0:c1e89c49eae5 | 97 | * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius; |
ppo | 0:c1e89c49eae5 | 98 | * |
ppo | 0:c1e89c49eae5 | 99 | * mouse.move(x_screen, y_screen); |
ppo | 0:c1e89c49eae5 | 100 | * angle += 3; |
ppo | 0:c1e89c49eae5 | 101 | * wait(0.01); |
ppo | 0:c1e89c49eae5 | 102 | * } |
ppo | 0:c1e89c49eae5 | 103 | * } |
ppo | 0:c1e89c49eae5 | 104 | * |
ppo | 0:c1e89c49eae5 | 105 | * @endcode |
ppo | 0:c1e89c49eae5 | 106 | */ |
ppo | 0:c1e89c49eae5 | 107 | class USBMouse: public USBHID |
ppo | 0:c1e89c49eae5 | 108 | { |
ppo | 0:c1e89c49eae5 | 109 | public: |
ppo | 0:c1e89c49eae5 | 110 | |
ppo | 0:c1e89c49eae5 | 111 | /** |
ppo | 0:c1e89c49eae5 | 112 | * Constructor |
ppo | 0:c1e89c49eae5 | 113 | * |
ppo | 0:c1e89c49eae5 | 114 | * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE) |
ppo | 0:c1e89c49eae5 | 115 | * @param vendor_id Your vendor_id (default: 0x1234) |
ppo | 0:c1e89c49eae5 | 116 | * @param product_id Your product_id (default: 0x0001) |
ppo | 0:c1e89c49eae5 | 117 | * @param product_release Your preoduct_release (default: 0x0001) |
ppo | 0:c1e89c49eae5 | 118 | * |
ppo | 0:c1e89c49eae5 | 119 | */ |
ppo | 0:c1e89c49eae5 | 120 | USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001): |
ppo | 0:c1e89c49eae5 | 121 | USBHID(0, 0, vendor_id, product_id, product_release, false) |
ppo | 0:c1e89c49eae5 | 122 | { |
ppo | 0:c1e89c49eae5 | 123 | button = 0; |
ppo | 0:c1e89c49eae5 | 124 | this->mouse_type = mouse_type; |
ppo | 0:c1e89c49eae5 | 125 | connect(); |
ppo | 0:c1e89c49eae5 | 126 | }; |
ppo | 0:c1e89c49eae5 | 127 | |
ppo | 0:c1e89c49eae5 | 128 | /** |
ppo | 0:c1e89c49eae5 | 129 | * Write a state of the mouse |
ppo | 0:c1e89c49eae5 | 130 | * |
ppo | 0:c1e89c49eae5 | 131 | * @param x x-axis position |
ppo | 0:c1e89c49eae5 | 132 | * @param y y-axis position |
ppo | 0:c1e89c49eae5 | 133 | * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE) |
ppo | 0:c1e89c49eae5 | 134 | * @param z wheel state (>0 to scroll down, <0 to scroll up) |
ppo | 0:c1e89c49eae5 | 135 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 136 | */ |
ppo | 0:c1e89c49eae5 | 137 | bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z); |
ppo | 0:c1e89c49eae5 | 138 | |
ppo | 0:c1e89c49eae5 | 139 | |
ppo | 0:c1e89c49eae5 | 140 | /** |
ppo | 0:c1e89c49eae5 | 141 | * Move the cursor to (x, y) |
ppo | 0:c1e89c49eae5 | 142 | * |
ppo | 0:c1e89c49eae5 | 143 | * @param x-axis position |
ppo | 0:c1e89c49eae5 | 144 | * @param y-axis position |
ppo | 0:c1e89c49eae5 | 145 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 146 | */ |
ppo | 0:c1e89c49eae5 | 147 | bool move(int16_t x, int16_t y); |
ppo | 0:c1e89c49eae5 | 148 | |
ppo | 0:c1e89c49eae5 | 149 | /** |
ppo | 0:c1e89c49eae5 | 150 | * Press one or several buttons |
ppo | 0:c1e89c49eae5 | 151 | * |
ppo | 0:c1e89c49eae5 | 152 | * @param button button state (ex: press(MOUSE_LEFT)) |
ppo | 0:c1e89c49eae5 | 153 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 154 | */ |
ppo | 0:c1e89c49eae5 | 155 | bool press(uint8_t button); |
ppo | 0:c1e89c49eae5 | 156 | |
ppo | 0:c1e89c49eae5 | 157 | /** |
ppo | 0:c1e89c49eae5 | 158 | * Release one or several buttons |
ppo | 0:c1e89c49eae5 | 159 | * |
ppo | 0:c1e89c49eae5 | 160 | * @param button button state (ex: release(MOUSE_LEFT)) |
ppo | 0:c1e89c49eae5 | 161 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 162 | */ |
ppo | 0:c1e89c49eae5 | 163 | bool release(uint8_t button); |
ppo | 0:c1e89c49eae5 | 164 | |
ppo | 0:c1e89c49eae5 | 165 | /** |
ppo | 0:c1e89c49eae5 | 166 | * Double click (MOUSE_LEFT) |
ppo | 0:c1e89c49eae5 | 167 | * |
ppo | 0:c1e89c49eae5 | 168 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 169 | */ |
ppo | 0:c1e89c49eae5 | 170 | bool doubleClick(); |
ppo | 0:c1e89c49eae5 | 171 | |
ppo | 0:c1e89c49eae5 | 172 | /** |
ppo | 0:c1e89c49eae5 | 173 | * Click |
ppo | 0:c1e89c49eae5 | 174 | * |
ppo | 0:c1e89c49eae5 | 175 | * @param button state of the buttons ( ex: clic(MOUSE_LEFT)) |
ppo | 0:c1e89c49eae5 | 176 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 177 | */ |
ppo | 0:c1e89c49eae5 | 178 | bool click(uint8_t button); |
ppo | 0:c1e89c49eae5 | 179 | |
ppo | 0:c1e89c49eae5 | 180 | /** |
ppo | 0:c1e89c49eae5 | 181 | * Scrolling |
ppo | 0:c1e89c49eae5 | 182 | * |
ppo | 0:c1e89c49eae5 | 183 | * @param z value of the wheel (>0 to go down, <0 to go up) |
ppo | 0:c1e89c49eae5 | 184 | * @returns true if there is no error, false otherwise |
ppo | 0:c1e89c49eae5 | 185 | */ |
ppo | 0:c1e89c49eae5 | 186 | bool scroll(int8_t z); |
ppo | 0:c1e89c49eae5 | 187 | |
ppo | 0:c1e89c49eae5 | 188 | /* |
ppo | 0:c1e89c49eae5 | 189 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
ppo | 0:c1e89c49eae5 | 190 | * |
ppo | 0:c1e89c49eae5 | 191 | * @returns pointer to the report descriptor |
ppo | 0:c1e89c49eae5 | 192 | */ |
ppo | 0:c1e89c49eae5 | 193 | virtual uint8_t * reportDesc(); |
ppo | 0:c1e89c49eae5 | 194 | |
ppo | 0:c1e89c49eae5 | 195 | protected: |
ppo | 0:c1e89c49eae5 | 196 | /* |
ppo | 0:c1e89c49eae5 | 197 | * Get configuration descriptor |
ppo | 0:c1e89c49eae5 | 198 | * |
ppo | 0:c1e89c49eae5 | 199 | * @returns pointer to the configuration descriptor |
ppo | 0:c1e89c49eae5 | 200 | */ |
ppo | 0:c1e89c49eae5 | 201 | virtual uint8_t * configurationDesc(); |
ppo | 0:c1e89c49eae5 | 202 | |
ppo | 0:c1e89c49eae5 | 203 | private: |
ppo | 0:c1e89c49eae5 | 204 | MOUSE_TYPE mouse_type; |
ppo | 0:c1e89c49eae5 | 205 | uint8_t button; |
ppo | 0:c1e89c49eae5 | 206 | bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z); |
ppo | 0:c1e89c49eae5 | 207 | }; |
ppo | 0:c1e89c49eae5 | 208 | |
ppo | 0:c1e89c49eae5 | 209 | #endif |