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