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 "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  * USBKeyboard example
79  * @code
80  *
81  * #include "mbed.h"
82  * #include "USBKeyboard.h"
83  *
84  * USBKeyboard key;
85  *
86  * int main(void)
87  * {
88  * while (1) {
89  * key.printf("Hello World\r\n");
90  * wait(1);
91  * }
92  * }
93  *
94  * @endcode
95  *
96  * @note Synchronization level: Thread safe
97  */
98 class USBKeyboard: public USBHID, public mbed::Stream {
99 public:
100 
101  /**
102  * Basic constructor
103  *
104  * Construct this object optionally connecting and blocking until it is ready.
105  *
106  * @note Do not use this constructor in derived classes.
107  *
108  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
109  * @param vendor_id Your vendor_id
110  * @param product_id Your product_id
111  * @param product_release Your product_release
112  */
113  USBKeyboard(bool connect_blocking = true, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
114 
115  /**
116  * Fully featured constructor
117  *
118  * Construct this object with the supplied USBPhy and parameters. The user
119  * this object is responsible for calling connect() or init().
120  *
121  * @note Derived classes must use this constructor and call init() or
122  * connect() themselves. Derived classes should also call deinit() in
123  * their destructor. This ensures that no interrupts can occur when the
124  * object is partially constructed or destroyed.
125  *
126  * @param phy USB phy to use
127  * @param vendor_id Your vendor_id
128  * @param product_id Your product_id
129  * @param product_release Your product_release
130  */
131  USBKeyboard(USBPhy *phy, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
132 
133  /**
134  * Destroy this object
135  *
136  * Any classes which inherit from this class must call deinit
137  * before this destructor runs.
138  */
139  virtual ~USBKeyboard();
140 
141  /**
142  * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
143  *
144  * @code
145  * //To send CTRL + s (save)
146  * keyboard.key_code('s', KEY_CTRL);
147  * @endcode
148  *
149  * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
150  * @param key character to send
151  * @returns true if there is no error, false otherwise
152  */
153  bool key_code(uint8_t key, uint8_t modifier = 0);
154 
155  /**
156  * Send a character
157  *
158  * @param c character to be sent
159  * @returns true if there is no error, false otherwise
160  */
161  virtual int _putc(int c);
162 
163  /**
164  * Control media keys
165  *
166  * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
167  * @returns true if there is no error, false otherwise
168  */
169  bool media_control(MEDIA_KEY key);
170 
171  /*
172  * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
173  *
174  * @returns pointer to the report descriptor
175  */
176  virtual const uint8_t *report_desc();
177 
178  /*
179  * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
180  */
181  virtual void report_rx();
182 
183  /**
184  * 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:
185  * - First bit: NUM_LOCK
186  * - Second bit: CAPS_LOCK
187  * - Third bit: SCROLL_LOCK
188  *
189  * @returns status of lock keys
190  */
191  uint8_t lock_status();
192 
193 protected:
194  /*
195  * Get configuration descriptor
196  *
197  * @returns pointer to the configuration descriptor
198  */
199  virtual const uint8_t *configuration_desc(uint8_t index);
200 
201 private:
202 
203  //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
204  virtual int _getc();
205 
206  uint8_t _configuration_descriptor[41];
207  uint8_t _lock_status;
208  PlatformMutex _mutex;
209 
210 };
211 
212 #endif
USBHID example.
Definition: USBHID.h:49
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:98
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.