ESP32VGA
ESP32 VGA Controller and Graphics Library
VGAKeyboard.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com)
3  Copyright (c) 2018 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of ESP32VGA Library.
7 
8  ESP32VGA is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  ESP32VGA is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with ESP32VGA. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef _VGAKEYBOARD_H_INCLUDED
24 #define _VGAKEYBOARD_H_INCLUDED
25 
26 
34 #include "freertos/FreeRTOS.h"
35 
36 #include "VGAConf.h"
37 
38 
39 namespace ESP32VGA {
40 
41 
42 // ASCII control characters
43 #define ASCII_NUL 0x00 // Null
44 #define ASCII_SOH 0x01 // Start of Heading
45 #define ASCII_STX 0x02 // Start of Text
46 #define ASCII_ETX 0x03 // End Of Text
47 #define ASCII_EOT 0x04 // End Of Transmission
48 #define ASCII_ENQ 0x05 // Enquiry
49 #define ASCII_ACK 0x06 // Acknowledge
50 #define ASCII_BELL 0x07 // Bell
51 #define ASCII_BS 0x08 // Backspace
52 #define ASCII_HT 0x09 // Horizontal Tab
53 #define ASCII_LF 0x0A // Line Feed
54 #define ASCII_VT 0x0B // Vertical Tab
55 #define ASCII_FF 0x0C // Form Feed
56 #define ASCII_CR 0x0D // Carriage Return
57 #define ASCII_SO 0x0E // Shift Out
58 #define ASCII_SI 0x0F // Shift In
59 #define ASCII_DLE 0x10 // Data Link Escape
60 #define ASCII_DC1 0x11 // Device Control 1
61 #define ASCII_XON 0x11 // Transmission On
62 #define ASCII_DC2 0x12 // Device Control 2
63 #define ASCII_DC3 0x13 // Device Control 3
64 #define ASCII_XOFF 0x13 // Transmission Off
65 #define ASCII_DC4 0x14 // Device Control 4
66 #define ASCII_NAK 0x15 // Negative Acknowledge
67 #define ASCII_SYN 0x16 // Synchronous Idle
68 #define ASCII_ETB 0x17 // End-of-Transmission-Block
69 #define ASCII_CAN 0x18 // Cancel
70 #define ASCII_EM 0x19 // End of Medium
71 #define ASCII_SUB 0x1A // Substitute
72 #define ASCII_ESC 0x1B // Escape
73 #define ASCII_FS 0x1C // File Separator
74 #define ASCII_GS 0x1D // Group Separator
75 #define ASCII_RS 0x1E // Record Separator
76 #define ASCII_US 0x1F // Unit Separator
77 #define ASCII_SPC 0x20 // Space
78 #define ASCII_DEL 0x7F // Delete
79 
80 
81 
85 enum class PS2Device_t {
86  OldATKeyboard,
87  MouseStandard,
88  MouseWithScrollWheel,
89  Mouse5Buttons,
90  MF2KeyboardWithTranslation,
91  M2Keyboard,
92 };
93 
94 
99  VK_NONE, // no character
100  VK_SPACE,
101  VK_0,
102  VK_1,
103  VK_2,
104  VK_3,
105  VK_4,
106  VK_5,
107  VK_6,
108  VK_7,
109  VK_8,
110  VK_9,
111  VK_KP_0,
112  VK_KP_1,
113  VK_KP_2,
114  VK_KP_3,
115  VK_KP_4,
116  VK_KP_5,
117  VK_KP_6,
118  VK_KP_7,
119  VK_KP_8,
120  VK_KP_9,
121  VK_a,
122  VK_b,
123  VK_c,
124  VK_d,
125  VK_e,
126  VK_f,
127  VK_g,
128  VK_h,
129  VK_i,
130  VK_j,
131  VK_k,
132  VK_l,
133  VK_m,
134  VK_n,
135  VK_o,
136  VK_p,
137  VK_q,
138  VK_r,
139  VK_s,
140  VK_t,
141  VK_u,
142  VK_v,
143  VK_w,
144  VK_x,
145  VK_y,
146  VK_z,
147  VK_A,
148  VK_B,
149  VK_C,
150  VK_D,
151  VK_E,
152  VK_F,
153  VK_G,
154  VK_H,
155  VK_I,
156  VK_J,
157  VK_K,
158  VK_L,
159  VK_M,
160  VK_N,
161  VK_O,
162  VK_P,
163  VK_Q,
164  VK_R,
165  VK_S,
166  VK_T,
167  VK_U,
168  VK_V,
169  VK_W,
170  VK_X,
171  VK_Y,
172  VK_Z,
173  VK_GRAVEACCENT, // "`"
174  VK_ACUTEACCENT, // "´"
175  VK_QUOTE, // "'"
176  VK_QUOTEDBL, // """
177  VK_EQUALS, // "="
178  VK_MINUS, // "-"
179  VK_KP_MINUS, // "-"
180  VK_PLUS, // "+"
181  VK_KP_PLUS, // "+"
182  VK_KP_MULTIPLY, // "*"
183  VK_ASTERISK, // "*"
184  VK_BACKSLASH, // "\"
185  VK_KP_DIVIDE, // "/"
186  VK_SLASH, // "/"
187  VK_KP_PERIOD, // "."
188  VK_PERIOD, // "."
189  VK_COLON, // ":"
190  VK_COMMA, // ","
191  VK_SEMICOLON, // ";"
192  VK_AMPERSAND, // "&"
193  VK_VERTICALBAR, // "|"
194  VK_HASH, // "#"
195  VK_AT, // "@"
196  VK_CARET, // "^"
197  VK_DOLLAR, // "$"
198  VK_POUND, // "£"
199  VK_EURO, // "€"
200  VK_PERCENT, // "%"
201  VK_EXCLAIM, // "!"
202  VK_QUESTION, // "?"
203  VK_LEFTBRACE, // "{"
204  VK_RIGHTBRACE, // "}"
205  VK_LEFTBRACKET, // "["
206  VK_RIGHTBRACKET, // "]"
207  VK_LEFTPAREN, // "("
208  VK_RIGHTPAREN, // ")"
209  VK_LESS, // "<"
210  VK_GREATER, // ">"
211  VK_UNDERSCORE, // "_"
212  VK_DEGREE, // "°"
213  VK_SECTION, // "§"
214  VK_TILDE, // "~"
215  VK_NEGATION, // "¬"
216  VK_LSHIFT,
217  VK_RSHIFT,
218  VK_LALT,
219  VK_RALT,
220  VK_LCTRL,
221  VK_RCTRL,
222  VK_LGUI,
223  VK_RGUI,
224  VK_ESCAPE,
225  VK_PRINTSCREEN1,
226  VK_PRINTSCREEN2,
227  VK_SYSREQ,
228  VK_INSERT,
229  VK_KP_INSERT,
230  VK_DELETE,
231  VK_KP_DELETE,
232  VK_BACKSPACE,
233  VK_HOME,
234  VK_KP_HOME,
235  VK_END,
236  VK_KP_END,
237  VK_PAUSE,
238  VK_BREAK,
239  VK_SCROLLLOCK,
240  VK_NUMLOCK,
241  VK_CAPSLOCK,
242  VK_TAB,
243  VK_RETURN,
244  VK_KP_ENTER,
245  VK_APPLICATION,
246  VK_PAGEUP,
247  VK_KP_PAGEUP,
248  VK_PAGEDOWN,
249  VK_KP_PAGEDOWN,
250  VK_UP,
251  VK_KP_UP,
252  VK_DOWN,
253  VK_KP_DOWN,
254  VK_LEFT,
255  VK_KP_LEFT,
256  VK_RIGHT,
257  VK_KP_RIGHT,
258  VK_KP_CENTER,
259  VK_F1,
260  VK_F2,
261  VK_F3,
262  VK_F4,
263  VK_F5,
264  VK_F6,
265  VK_F7,
266  VK_F8,
267  VK_F9,
268  VK_F10,
269  VK_F11,
270  VK_F12,
271  VK_GRAVE_a, // "à"
272  VK_GRAVE_e, // "è"
273  VK_ACUTE_e, // "é"
274  VK_GRAVE_i, // "ì"
275  VK_GRAVE_o, // "ò"
276  VK_GRAVE_u, // "ù"
277  VK_CEDILLA_c, // "ç"
278  VK_ESZETT, // "ß"
279  VK_UMLAUT_u, // "ü"
280  VK_UMLAUT_o, // "ö"
281  VK_UMLAUT_a, // "ä"
282 };
283 
284 
289  uint8_t scancode;
291 };
292 
293 
299  struct {
300  uint8_t ctrl : 1;
301  uint8_t alt : 1;
302  uint8_t shift : 1;
303  uint8_t capslock : 1;
304  uint8_t numlock : 1;
305  };
307 };
308 
309 
312  const char * name;
317 };
318 
319 
321 extern const KeyboardLayout_t USLayout;
322 
323 extern const KeyboardLayout_t UKLayout;
324 extern const KeyboardLayout_t GermanLayout;
325 extern const KeyboardLayout_t ItalianLayout;
326 
327 
328 class VGAKeyboardClass {
329 
330 public:
331 
332  VGAKeyboardClass();
333 
334  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO);
335 
336  void reset();
337 
338  // isKeyboardAvailable() updated after begin() or reset()
339  bool isKeyboardAvailable() { return m_keyboardAvailable; }
340 
341  void setLayout(KeyboardLayout_t const * layout);
342  KeyboardLayout_t const * getLayout() { return m_layout; }
343 
344  uint8_t scancodeAvailable();
345  int getNextScancode(int32_t timeOutMS = -1);
346 
347  VirtualKey_t getNextVirtualKey(bool * keyDown = NULL, int32_t timeOutMS = -1);
348  int virtualKeyToASCII(VirtualKey_t virtualKey);
349 
350  bool CTRL() { return m_CTRL; }
351  bool ALT() { return m_ALT; }
352  bool SHIFT() { return m_SHIFT; }
353  bool CAPSLOCK() { return m_CAPSLOCK; }
354  bool NUMLOCK() { return m_NUMLOCK; }
355  bool SCROLLLOCK() { return m_SCROLLLOCK; }
356 
357  bool send_cmdLEDs(bool numlock, bool capsLock, bool scrollLock);
358  bool send_cmdEcho();
359  bool send_cmdGetScancodeSet(uint8_t * result);
360  bool send_cmdSetScancodeSet(uint8_t scancodeSet);
361  bool send_cmdIdentify(PS2Device_t * result);
362  bool send_cmdDisableScanning();
363  bool send_cmdEnableScanning();
364  bool send_cmdTypematicDateAndDelay(uint16_t repeatRateMS, uint16_t repeatDelayMS);
365  bool send_cmdSetDefaultParams();
366  bool send_cmdReset();
367 
368 #if VGAKB_HASVIRTUALKEYTOSTRING
369  static char const * virtualKeyToString(VirtualKey_t virtualKey);
370 #endif
371 
372 private:
373 
374  bool rawSend(uint8_t value);
375  bool send(uint8_t cmd, uint8_t expectedReply);
376  VirtualKey_t scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout_t const * layout = NULL);
377  VirtualKey_t VKtoAlternateVK(VirtualKey_t in_vk, KeyboardLayout_t const * layout = NULL);
378  void updateLEDs();
379 
380  static void IRAM_ATTR keyboardClockInterrupt();
381 
382  static void IRAM_ATTR activateErrorState();
383  void resumeFromErrorState();
384 
385  bool m_keyboardAvailable; // self test passed and support for scancode set 2
386 
387  volatile gpio_num_t m_dataGPIO;
388  volatile gpio_num_t m_clkGPIO;
389  volatile QueueHandle_t m_ScanCodeQueue;
390  volatile uint8_t m_sendingDataValue; // data to send
391  volatile TaskHandle_t m_TXWaitTask; // task that is waiting for TX ends
392  volatile bool m_RXError; // error occurred receiving data
393 
394  KeyboardLayout_t const * m_layout;
395 
396  bool m_CTRL;
397  bool m_ALT;
398  bool m_SHIFT;
399  bool m_CAPSLOCK;
400  bool m_NUMLOCK;
401  bool m_SCROLLLOCK;
402 };
403 
404 
405 
406 
407 
408 
409 
410 
411 } // end of namespace
412 
413 
414 
415 extern ESP32VGA::VGAKeyboardClass VGAKeyboard;
416 
417 
418 
419 #endif
420 
421 
VirtualKey_t virtualKey
Definition: VGAKeyboard.h:290
VirtualKeyDef_t exScancodeToVK[32]
Definition: VGAKeyboard.h:315
VirtualKey_t virtualKey
Definition: VGAKeyboard.h:306
KeyboardLayout_t const * inherited
Definition: VGAKeyboard.h:313
Associates scancode to virtualkey.
Definition: VGAKeyboard.h:288
uint8_t scancode
Definition: VGAKeyboard.h:289
This file contains ESP32VGA library configuration settings, like number of supported colors...
Definition: VGACanvas.cpp:29
All in one structure to fully represent a keyboard layout.
Definition: VGAKeyboard.h:311
uint8_t alt
Definition: VGAKeyboard.h:301
PS2Device_t
Represents the type of device attached to PS/2 port.
Definition: VGAKeyboard.h:85
uint8_t ctrl
Definition: VGAKeyboard.h:300
VirtualKey_t reqVirtualKey
Definition: VGAKeyboard.h:298
AltVirtualKeyDef_t alternateVK[64]
Definition: VGAKeyboard.h:316
uint8_t numlock
Definition: VGAKeyboard.h:304
const char * name
Definition: VGAKeyboard.h:312
VirtualKey_t
Represents each possible real or derived (SHIFT + real) key.
Definition: VGAKeyboard.h:98
uint8_t shift
Definition: VGAKeyboard.h:302
VirtualKeyDef_t scancodeToVK[92]
Definition: VGAKeyboard.h:314
uint8_t capslock
Definition: VGAKeyboard.h:303
Associates a virtualkey and various shift states (ctrl, alt, etc..) to another virtualkey.
Definition: VGAKeyboard.h:297