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