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