Mistake on this page?
Report an issue in GitHub or email us
USBKeyboard.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 USBKEYBOARD_H
19 #define USBKEYBOARD_H
20 
21 #include "USBHID.h"
22 #include "platform/Stream.h"
23 #include "PlatformMutex.h"
24 
25 /* Modifiers, left keys then right keys. */
26 enum MODIFIER_KEY {
27  KEY_CTRL = 0x01,
28  KEY_SHIFT = 0x02,
29  KEY_ALT = 0x04,
30  KEY_LOGO = 0x08,
31  KEY_RCTRL = 0x10,
32  KEY_RSHIFT = 0x20,
33  KEY_RALT = 0x40,
34  KEY_RLOGO = 0x80,
35 };
36 
37 
38 enum MEDIA_KEY {
39  KEY_NEXT_TRACK, /*!< next Track Button */
40  KEY_PREVIOUS_TRACK, /*!< Previous track Button */
41  KEY_STOP, /*!< Stop Button */
42  KEY_PLAY_PAUSE, /*!< Play/Pause Button */
43  KEY_MUTE, /*!< Mute Button */
44  KEY_VOLUME_UP, /*!< Volume Up Button */
45  KEY_VOLUME_DOWN, /*!< Volume Down Button */
46 };
47 
48 enum FUNCTION_KEY {
49  KEY_F1 = 128, /* F1 key */
50  KEY_F2, /* F2 key */
51  KEY_F3, /* F3 key */
52  KEY_F4, /* F4 key */
53  KEY_F5, /* F5 key */
54  KEY_F6, /* F6 key */
55  KEY_F7, /* F7 key */
56  KEY_F8, /* F8 key */
57  KEY_F9, /* F9 key */
58  KEY_F10, /* F10 key */
59  KEY_F11, /* F11 key */
60  KEY_F12, /* F12 key */
61 
62  KEY_PRINT_SCREEN, /* Print Screen key */
63  KEY_SCROLL_LOCK, /* Scroll lock */
64  KEY_CAPS_LOCK, /* caps lock */
65  KEY_NUM_LOCK, /* num lock */
66  KEY_INSERT, /* Insert key */
67  KEY_HOME, /* Home key */
68  KEY_PAGE_UP, /* Page Up key */
69  KEY_PAGE_DOWN, /* Page Down key */
70 
71  RIGHT_ARROW, /* Right arrow */
72  LEFT_ARROW, /* Left arrow */
73  DOWN_ARROW, /* Down arrow */
74  UP_ARROW, /* Up arrow */
75 };
76 
77 /**
78  * \defgroup drivers_USBKeyboard USBKeyboard class
79  * \ingroup drivers-public-api-usb
80  * @{
81  */
82 
83 /**
84  * USBKeyboard example
85  * @code
86  *
87  * #include "mbed.h"
88  * #include "USBKeyboard.h"
89  *
90  * USBKeyboard key;
91  *
92  * int main(void)
93  * {
94  * while (1) {
95  * key.printf("Hello World\r\n");
96  * ThisThread::sleep_for(1000);
97  * }
98  * }
99  *
100  * @endcode
101  *
102  * @note Synchronization level: Thread safe
103  */
104 class USBKeyboard: public USBHID, public mbed::Stream {
105 public:
106 
107  /**
108  * Basic constructor
109  *
110  * Construct this object optionally connecting and blocking until it is ready.
111  *
112  * @note Do not use this constructor in derived classes.
113  *
114  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
115  * @param vendor_id Your vendor_id
116  * @param product_id Your product_id
117  * @param product_release Your product_release
118  */
119  USBKeyboard(bool connect_blocking = true, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
120 
121  /**
122  * Fully featured constructor
123  *
124  * Construct this object with the supplied USBPhy and parameters. The user
125  * this object is responsible for calling connect() or init().
126  *
127  * @note Derived classes must use this constructor and call init() or
128  * connect() themselves. Derived classes should also call deinit() in
129  * their destructor. This ensures that no interrupts can occur when the
130  * object is partially constructed or destroyed.
131  *
132  * @param phy USB phy to use
133  * @param vendor_id Your vendor_id
134  * @param product_id Your product_id
135  * @param product_release Your product_release
136  */
137  USBKeyboard(USBPhy *phy, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
138 
139  /**
140  * Destroy this object
141  *
142  * Any classes which inherit from this class must call deinit
143  * before this destructor runs.
144  */
145  virtual ~USBKeyboard();
146 
147  /**
148  * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
149  *
150  * @code
151  * //To send CTRL + s (save)
152  * keyboard.key_code('s', KEY_CTRL);
153  * @endcode
154  *
155  * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
156  * @param key character to send
157  * @returns true if there is no error, false otherwise
158  */
159  bool key_code(uint8_t key, uint8_t modifier = 0);
160 
161  /**
162  * Send a character
163  *
164  * @param c character to be sent
165  * @returns true if there is no error, false otherwise
166  */
167  virtual int _putc(int c);
168 
169  /**
170  * Control media keys
171  *
172  * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
173  * @returns true if there is no error, false otherwise
174  */
175  bool media_control(MEDIA_KEY key);
176 
177  /*
178  * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
179  *
180  * @returns pointer to the report descriptor
181  */
182  virtual const uint8_t *report_desc();
183 
184  /*
185  * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
186  */
187  virtual void report_rx();
188 
189  /**
190  * 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:
191  * - First bit: NUM_LOCK
192  * - Second bit: CAPS_LOCK
193  * - Third bit: SCROLL_LOCK
194  *
195  * @returns status of lock keys
196  */
197  uint8_t lock_status();
198 
199 protected:
200  /*
201  * Get configuration descriptor
202  *
203  * @returns pointer to the configuration descriptor
204  */
205  virtual const uint8_t *configuration_desc(uint8_t index);
206 
207 private:
208 
209  //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
210  virtual int _getc();
211 
212  uint8_t _configuration_descriptor[41];
213  uint8_t _lock_status;
214  PlatformMutex _mutex;
215 
216 };
217 
218 /** @}*/
219 
220 #endif
USBHID example.
Definition: USBHID.h:53
uint8_t lock_status()
Read status of lock keys.
virtual int _putc(int c)
Send a character.
bool media_control(MEDIA_KEY key)
Control media keys.
File stream.
Definition: Stream.h:42
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
USBKeyboard(bool connect_blocking=true, uint16_t vendor_id=0x1235, uint16_t product_id=0x0050, uint16_t product_release=0x0001)
Basic constructor.
bool key_code(uint8_t key, uint8_t modifier=0)
To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key.
virtual ~USBKeyboard()
Destroy this object.
USBKeyboard example.
Definition: USBKeyboard.h:104
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.