Mistake on this page?
Report an issue in GitHub or email us
USBMouse.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef USBMOUSE_H
19 #define USBMOUSE_H
20 
21 #include "USBHID.h"
22 #include "PlatformMutex.h"
23 
24 #define REPORT_ID_MOUSE 2
25 
26 /* Common usage */
27 
28 enum MOUSE_BUTTON {
29  MOUSE_LEFT = 1,
30  MOUSE_RIGHT = 2,
31  MOUSE_MIDDLE = 4,
32 };
33 
34 /* X and Y limits */
35 /* These values do not directly map to screen pixels */
36 /* Zero may be interpreted as meaning 'no movement' */
37 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
38 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
39 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
40 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
41 
42 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
43 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
44 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
45 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
46 
47 enum MOUSE_TYPE {
48  ABS_MOUSE,
49  REL_MOUSE,
50 };
51 
52 /**
53  * \defgroup drivers_USBMouse USBMouse class
54  * \ingroup drivers-public-api-usb
55  * @{
56  */
57 
58 /**
59  *
60  * USBMouse example
61  * @code
62  * #include "mbed.h"
63  * #include "USBMouse.h"
64  *
65  * USBMouse mouse;
66  *
67  * int main(void)
68  * {
69  * while (1)
70  * {
71  * mouse.move(20, 0);
72  * ThisThread::sleep_for(500);
73  * }
74  * }
75  *
76  * @endcode
77  *
78  *
79  * @code
80  * #include "mbed.h"
81  * #include "USBMouse.h"
82  * #include <math.h>
83  *
84  * USBMouse mouse(true, ABS_MOUSE);
85  *
86  * int main(void)
87  * {
88  * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
89  * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
90  * uint16_t x_screen = 0;
91  * uint16_t y_screen = 0;
92  *
93  * uint32_t x_origin = x_center;
94  * uint32_t y_origin = y_center;
95  * uint32_t radius = 5000;
96  * uint32_t angle = 0;
97  *
98  * while (1)
99  * {
100  * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
101  * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
102  *
103  * mouse.move(x_screen, y_screen);
104  * angle += 3;
105  * ThisThread::sleep_for(10);
106  * }
107  * }
108  *
109  * @endcode
110  *
111  * @note Synchronization level: Thread safe
112  */
113 class USBMouse: public USBHID {
114 public:
115 
116  /**
117  * Basic constructor
118  *
119  * Construct this object optionally connecting and blocking until it is ready.
120  *
121  * @note Do not use this constructor in derived classes.
122  *
123  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
124  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
125  * @param vendor_id Your vendor_id
126  * @param product_id Your product_id
127  * @param product_release Your product_release
128  */
129  USBMouse(bool connect_blocking = true, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001);
130 
131  /**
132  * Fully featured constructor
133  *
134  * Construct this object with the supplied USBPhy and parameters. The user
135  * this object is responsible for calling connect() or init().
136  *
137  * @note Derived classes must use this constructor and call init() or
138  * connect() themselves. Derived classes should also call deinit() in
139  * their destructor. This ensures that no interrupts can occur when the
140  * object is partially constructed or destroyed.
141  *
142  * @param phy USB phy to use
143  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
144  * @param vendor_id Your vendor_id
145  * @param product_id Your product_id
146  * @param product_release Your product_release
147  */
148  USBMouse(USBPhy *phy, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001);
149 
150  /**
151  * Destroy this object
152  *
153  * Any classes which inherit from this class must call deinit
154  * before this destructor runs.
155  */
156  virtual ~USBMouse();
157 
158  /**
159  * Write a state of the mouse
160  *
161  * @param x x-axis position
162  * @param y y-axis position
163  * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
164  * @param z wheel state (>0 to scroll down, <0 to scroll up)
165  * @returns true if there is no error, false otherwise
166  */
167  bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
168 
169  /**
170  * Move the cursor to (x, y)
171  *
172  * @param x x-axis position
173  * @param y y-axis position
174  * @returns true if there is no error, false otherwise
175  */
176  bool move(int16_t x, int16_t y);
177 
178  /**
179  * Press one or several buttons
180  *
181  * @param button button state (ex: press(MOUSE_LEFT))
182  * @returns true if there is no error, false otherwise
183  */
184  bool press(uint8_t button);
185 
186  /**
187  * Release one or several buttons
188  *
189  * @param button button state (ex: release(MOUSE_LEFT))
190  * @returns true if there is no error, false otherwise
191  */
192  bool release(uint8_t button);
193 
194  /**
195  * Double click (MOUSE_LEFT)
196  *
197  * @returns true if there is no error, false otherwise
198  */
199  bool double_click();
200 
201  /**
202  * Click
203  *
204  * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
205  * @returns true if there is no error, false otherwise
206  */
207  bool click(uint8_t button);
208 
209  /**
210  * Scrolling
211  *
212  * @param z value of the wheel (>0 to go down, <0 to go up)
213  * @returns true if there is no error, false otherwise
214  */
215  bool scroll(int8_t z);
216 
217  /*
218  * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
219  *
220  * @returns pointer to the report descriptor
221  */
222  virtual const uint8_t *report_desc();
223 
224 protected:
225  /*
226  * Get configuration descriptor
227  *
228  * @returns pointer to the configuration descriptor
229  */
230  virtual const uint8_t *configuration_desc(uint8_t index);
231 
232 private:
233  MOUSE_TYPE _mouse_type;
234  uint8_t _button;
235  uint8_t _configuration_descriptor[41];
236  PlatformMutex _mutex;
237 
238  bool mouse_send(int8_t x, int8_t y, uint8_t buttons, int8_t z);
239 };
240 
241 /** @}*/
242 
243 #endif
USBHID example.
Definition: USBHID.h:53
USBMouse(bool connect_blocking=true, MOUSE_TYPE mouse_type=REL_MOUSE, uint16_t vendor_id=0x1234, uint16_t product_id=0x0001, uint16_t product_release=0x0001)
Basic constructor.
bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z)
Write a state of the mouse.
bool move(int16_t x, int16_t y)
Move the cursor to (x, y)
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
bool click(uint8_t button)
Click.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
virtual ~USBMouse()
Destroy this object.
bool scroll(int8_t z)
Scrolling.
bool press(uint8_t button)
Press one or several buttons.
USBMouse example.
Definition: USBMouse.h:113
bool release(uint8_t button)
Release one or several buttons.
bool double_click()
Double click (MOUSE_LEFT)
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.