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