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  *
54  * USBMouse example
55  * @code
56  * #include "mbed.h"
57  * #include "USBMouse.h"
58  *
59  * USBMouse mouse;
60  *
61  * int main(void)
62  * {
63  * while (1)
64  * {
65  * mouse.move(20, 0);
66  * wait(0.5);
67  * }
68  * }
69  *
70  * @endcode
71  *
72  *
73  * @code
74  * #include "mbed.h"
75  * #include "USBMouse.h"
76  * #include <math.h>
77  *
78  * USBMouse mouse(true, ABS_MOUSE);
79  *
80  * int main(void)
81  * {
82  * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
83  * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
84  * uint16_t x_screen = 0;
85  * uint16_t y_screen = 0;
86  *
87  * uint32_t x_origin = x_center;
88  * uint32_t y_origin = y_center;
89  * uint32_t radius = 5000;
90  * uint32_t angle = 0;
91  *
92  * while (1)
93  * {
94  * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
95  * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
96  *
97  * mouse.move(x_screen, y_screen);
98  * angle += 3;
99  * wait(0.01);
100  * }
101  * }
102  *
103  * @endcode
104  *
105  * @note Synchronization level: Thread safe
106  */
107 class USBMouse: public USBHID {
108 public:
109 
110  /**
111  * Basic constructor
112  *
113  * Construct this object optionally connecting and blocking until it is ready.
114  *
115  * @note Do not use this constructor in derived classes.
116  *
117  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
118  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
119  * @param vendor_id Your vendor_id
120  * @param product_id Your product_id
121  * @param product_release Your product_release
122  */
123  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);
124 
125  /**
126  * Fully featured constructor
127  *
128  * Construct this object with the supplied USBPhy and parameters. The user
129  * this object is responsible for calling connect() or init().
130  *
131  * @note Derived classes must use this constructor and call init() or
132  * connect() themselves. Derived classes should also call deinit() in
133  * their destructor. This ensures that no interrupts can occur when the
134  * object is partially constructed or destroyed.
135  *
136  * @param phy USB phy to use
137  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
138  * @param vendor_id Your vendor_id
139  * @param product_id Your product_id
140  * @param product_release Your product_release
141  */
142  USBMouse(USBPhy *phy, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001);
143 
144  /**
145  * Destroy this object
146  *
147  * Any classes which inherit from this class must call deinit
148  * before this destructor runs.
149  */
150  virtual ~USBMouse();
151 
152  /**
153  * Write a state of the mouse
154  *
155  * @param x x-axis position
156  * @param y y-axis position
157  * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
158  * @param z wheel state (>0 to scroll down, <0 to scroll up)
159  * @returns true if there is no error, false otherwise
160  */
161  bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
162 
163  /**
164  * Move the cursor to (x, y)
165  *
166  * @param x x-axis position
167  * @param y y-axis position
168  * @returns true if there is no error, false otherwise
169  */
170  bool move(int16_t x, int16_t y);
171 
172  /**
173  * Press one or several buttons
174  *
175  * @param button button state (ex: press(MOUSE_LEFT))
176  * @returns true if there is no error, false otherwise
177  */
178  bool press(uint8_t button);
179 
180  /**
181  * Release one or several buttons
182  *
183  * @param button button state (ex: release(MOUSE_LEFT))
184  * @returns true if there is no error, false otherwise
185  */
186  bool release(uint8_t button);
187 
188  /**
189  * Double click (MOUSE_LEFT)
190  *
191  * @returns true if there is no error, false otherwise
192  */
193  bool double_click();
194 
195  /**
196  * Click
197  *
198  * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
199  * @returns true if there is no error, false otherwise
200  */
201  bool click(uint8_t button);
202 
203  /**
204  * Scrolling
205  *
206  * @param z value of the wheel (>0 to go down, <0 to go up)
207  * @returns true if there is no error, false otherwise
208  */
209  bool scroll(int8_t z);
210 
211  /*
212  * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
213  *
214  * @returns pointer to the report descriptor
215  */
216  virtual const uint8_t *report_desc();
217 
218 protected:
219  /*
220  * Get configuration descriptor
221  *
222  * @returns pointer to the configuration descriptor
223  */
224  virtual const uint8_t *configuration_desc(uint8_t index);
225 
226 private:
227  MOUSE_TYPE _mouse_type;
228  uint8_t _button;
229  uint8_t _configuration_descriptor[41];
230  PlatformMutex _mutex;
231 
232  bool mouse_send(int8_t x, int8_t y, uint8_t buttons, int8_t z);
233 };
234 
235 #endif
USBHID example.
Definition: USBHID.h:49
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:107
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.