Mistake on this page?
Report an issue in GitHub or email us
USBMouseKeyboard.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 USBMOUSEKEYBOARD_H
19 #define USBMOUSEKEYBOARD_H
20 
21 #define REPORT_ID_KEYBOARD 1
22 #define REPORT_ID_MOUSE 2
23 #define REPORT_ID_VOLUME 3
24 
25 #include "USBMouse.h"
26 #include "USBKeyboard.h"
27 #include "platform/Stream.h"
28 #include "USBHID.h"
29 #include "PlatformMutex.h"
30 
31 /**
32  * \defgroup drivers_USBMouseKeyboard USBMouseKeyboard class
33  * \ingroup drivers-public-api-usb
34  * @{
35  */
36 
37 /**
38  * USBMouseKeyboard example
39  * @code
40  *
41  * #include "mbed.h"
42  * #include "USBMouseKeyboard.h"
43  *
44  * USBMouseKeyboard key_mouse;
45  *
46  * int main(void)
47  * {
48  * while(1)
49  * {
50  * key_mouse.move(20, 0);
51  * key_mouse.printf("Hello From MBED\r\n");
52  * ThisThread::sleep_for(1000);
53  * }
54  * }
55  * @endcode
56  *
57  *
58  * @code
59  *
60  * #include "mbed.h"
61  * #include "USBMouseKeyboard.h"
62  *
63  * USBMouseKeyboard key_mouse(ABS_MOUSE);
64  *
65  * int main(void)
66  * {
67  * while(1)
68  * {
69  * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
70  * key_mouse.printf("Hello from MBED\r\n");
71  * ThisThread::sleep_for(1000);
72  * }
73  * }
74  * @endcode
75  *
76  * @note Synchronization level: Thread safe
77  */
78 class USBMouseKeyboard: public USBHID, public mbed::Stream {
79 public:
80 
81  /**
82  * Basic constructor
83  *
84  * Construct this object optionally connecting and blocking until it is ready.
85  *
86  * @note Do not use this constructor in derived classes.
87  *
88  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
89  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
90  * @param vendor_id Your vendor_id (default: 0x1234)
91  * @param product_id Your product_id (default: 0x0001)
92  * @param product_release Your preoduct_release (default: 0x0001)
93  *
94  */
95  USBMouseKeyboard(bool connect_blocking = true, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001);
96 
97  /**
98  * Fully featured constructor
99  *
100  * Construct this object with the supplied USBPhy and parameters. The user
101  * this object is responsible for calling connect() or init().
102  *
103  * @note Derived classes must use this constructor and call init() or
104  * connect() themselves. Derived classes should also call deinit() in
105  * their destructor. This ensures that no interrupts can occur when the
106  * object is partially constructed or destroyed.
107  *
108  * @param phy USB phy to use
109  * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
110  * @param vendor_id Your vendor_id (default: 0x1234)
111  * @param product_id Your product_id (default: 0x0001)
112  * @param product_release Your preoduct_release (default: 0x0001)
113  *
114  */
115  USBMouseKeyboard(USBPhy *phy, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001);
116 
117  /**
118  * Destroy this object
119  *
120  * Any classes which inherit from this class must call deinit
121  * before this destructor runs.
122  */
123  virtual ~USBMouseKeyboard();
124 
125  /**
126  * Write a state of the mouse
127  *
128  * @param x x-axis position
129  * @param y y-axis position
130  * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
131  * @param z wheel state (>0 to scroll down, <0 to scroll up)
132  * @returns true if there is no error, false otherwise
133  */
134  bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
135 
136 
137  /**
138  * Move the cursor to (x, y)
139  *
140  * @param x x-axis position
141  * @param y y-axis position
142  * @returns true if there is no error, false otherwise
143  */
144  bool move(int16_t x, int16_t y);
145 
146  /**
147  * Press one or several buttons
148  *
149  * @param button button state (ex: press(MOUSE_LEFT))
150  * @returns true if there is no error, false otherwise
151  */
152  bool press(uint8_t button);
153 
154  /**
155  * Release one or several buttons
156  *
157  * @param button button state (ex: release(MOUSE_LEFT))
158  * @returns true if there is no error, false otherwise
159  */
160  bool release(uint8_t button);
161 
162  /**
163  * Double click (MOUSE_LEFT)
164  *
165  * @returns true if there is no error, false otherwise
166  */
167  bool doubleClick();
168 
169  /**
170  * Click
171  *
172  * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
173  * @returns true if there is no error, false otherwise
174  */
175  bool click(uint8_t button);
176 
177  /**
178  * Scrolling
179  *
180  * @param z value of the wheel (>0 to go down, <0 to go up)
181  * @returns true if there is no error, false otherwise
182  */
183  bool scroll(int8_t z);
184 
185  /**
186  * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
187  *
188  * @code
189  * //To send CTRL + s (save)
190  * keyboard.keyCode('s', KEY_CTRL);
191  * @endcode
192  *
193  * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
194  * @param key character to send
195  * @returns true if there is no error, false otherwise
196  */
197  bool key_code(uint8_t key, uint8_t modifier = 0);
198 
199  /**
200  * Send a character
201  *
202  * @param c character to be sent
203  * @returns true if there is no error, false otherwise
204  */
205  virtual int _putc(int c);
206 
207  /**
208  * Control media keys
209  *
210  * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
211  * @returns true if there is no error, false otherwise
212  */
213  bool media_control(MEDIA_KEY key);
214 
215  /**
216  * Read status of lock keys. Useful to switch-on/off LEDs according to key pressed. Only the first three bits of the result is important:
217  * - First bit: NUM_LOCK
218  * - Second bit: CAPS_LOCK
219  * - Third bit: SCROLL_LOCK
220  *
221  * @returns status of lock keys
222  */
223  uint8_t lock_status();
224 
225  /*
226  * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
227  *
228  * @returns pointer to the report descriptor
229  */
230  virtual const uint8_t *report_desc();
231 
232  /*
233  * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
234  *
235  * @returns if handle by subclass, return true
236  */
237  virtual void report_rx();
238 
239 
240 private:
241  MOUSE_TYPE _mouse_type;
242  uint8_t _button;
243  uint8_t _lock_status;
244  PlatformMutex _mutex;
245 
246  bool _mouse_send(int8_t x, int8_t y, uint8_t buttons, int8_t z);
247 
248  //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
249  virtual int _getc();
250 };
251 
252 /** @}*/
253 
254 #endif
USBHID example.
Definition: USBHID.h:53
bool scroll(int8_t z)
Scrolling.
bool doubleClick()
Double click (MOUSE_LEFT)
File stream.
Definition: Stream.h:42
bool move(int16_t x, int16_t y)
Move the cursor to (x, y)
bool key_code(uint8_t key, uint8_t modifier=0)
To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key.
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
uint8_t lock_status()
Read status of lock keys.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
bool press(uint8_t button)
Press one or several buttons.
USBMouseKeyboard(bool connect_blocking=true, MOUSE_TYPE mouse_type=REL_MOUSE, uint16_t vendor_id=0x0021, uint16_t product_id=0x0011, uint16_t product_release=0x0001)
Basic constructor.
bool click(uint8_t button)
Click.
virtual int _putc(int c)
Send a character.
bool media_control(MEDIA_KEY key)
Control media keys.
virtual ~USBMouseKeyboard()
Destroy this object.
USBMouseKeyboard example.
bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z)
Write a state of the mouse.
bool release(uint8_t button)
Release one or several buttons.
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.