FabGL
ESP32 VGA Controller and Graphics Library
keyboard.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL 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  FabGL 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 FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef _KEYBOARD_H_INCLUDED
24 #define _KEYBOARD_H_INCLUDED
25 
26 
34 #include "freertos/FreeRTOS.h"
35 
36 #include "fabglconf.h"
37 #include "ps2device.h"
38 
39 
40 namespace fabgl {
41 
42 
43 // ASCII control characters
44 #define ASCII_NUL 0x00 // Null
45 #define ASCII_SOH 0x01 // Start of Heading
46 #define ASCII_STX 0x02 // Start of Text
47 #define ASCII_ETX 0x03 // End Of Text
48 #define ASCII_EOT 0x04 // End Of Transmission
49 #define ASCII_ENQ 0x05 // Enquiry
50 #define ASCII_ACK 0x06 // Acknowledge
51 #define ASCII_BELL 0x07 // Bell
52 #define ASCII_BS 0x08 // Backspace
53 #define ASCII_HT 0x09 // Horizontal Tab
54 #define ASCII_LF 0x0A // Line Feed
55 #define ASCII_VT 0x0B // Vertical Tab
56 #define ASCII_FF 0x0C // Form Feed
57 #define ASCII_CR 0x0D // Carriage Return
58 #define ASCII_SO 0x0E // Shift Out
59 #define ASCII_SI 0x0F // Shift In
60 #define ASCII_DLE 0x10 // Data Link Escape
61 #define ASCII_DC1 0x11 // Device Control 1
62 #define ASCII_XON 0x11 // Transmission On
63 #define ASCII_DC2 0x12 // Device Control 2
64 #define ASCII_DC3 0x13 // Device Control 3
65 #define ASCII_XOFF 0x13 // Transmission Off
66 #define ASCII_DC4 0x14 // Device Control 4
67 #define ASCII_NAK 0x15 // Negative Acknowledge
68 #define ASCII_SYN 0x16 // Synchronous Idle
69 #define ASCII_ETB 0x17 // End-of-Transmission-Block
70 #define ASCII_CAN 0x18 // Cancel
71 #define ASCII_EM 0x19 // End of Medium
72 #define ASCII_SUB 0x1A // Substitute
73 #define ASCII_ESC 0x1B // Escape
74 #define ASCII_FS 0x1C // File Separator
75 #define ASCII_GS 0x1D // Group Separator
76 #define ASCII_RS 0x1E // Record Separator
77 #define ASCII_US 0x1F // Unit Separator
78 #define ASCII_SPC 0x20 // Space
79 #define ASCII_DEL 0x7F // Delete
80 
81 
82 
86 enum VirtualKey {
90  VK_0,
91  VK_1,
92  VK_2,
93  VK_3,
94  VK_4,
95  VK_5,
96  VK_6,
97  VK_7,
98  VK_8,
99  VK_9,
272  VK_LAST, // marks the last virtual key
273 };
274 
275 
280  uint8_t scancode;
282 };
283 
284 
290  struct {
291  uint8_t ctrl : 1;
292  uint8_t alt : 1;
293  uint8_t shift : 1;
294  uint8_t capslock : 1;
295  uint8_t numlock : 1;
296  };
298 };
299 
300 
303  const char * name;
308 };
309 
310 
312 extern const KeyboardLayout USLayout;
313 
315 extern const KeyboardLayout UKLayout;
316 
318 extern const KeyboardLayout GermanLayout;
319 
321 extern const KeyboardLayout ItalianLayout;
322 
323 
349 
350 public:
351 
352  KeyboardClass();
353 
373  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys = true, bool createVKQueue = true);
374 
394  void begin(bool generateVirtualKeys, bool createVKQueue, int PS2Port);
395 
401  bool reset();
402 
410  bool isKeyboardAvailable() { return m_keyboardAvailable; }
411 
426  void setLayout(KeyboardLayout const * layout);
427 
433  KeyboardLayout const * getLayout() { return m_layout; }
434 
445  bool isVKDown(VirtualKey virtualKey);
446 
455  int virtualKeyAvailable();
456 
468  VirtualKey getNextVirtualKey(bool * keyDown = NULL, int timeOutMS = -1);
469 
483  int virtualKeyToASCII(VirtualKey virtualKey);
484 
494  int scancodeAvailable();
495 
508  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
509 
518  void suspendVirtualKeyGeneration(bool value);
519 
531  bool setLEDs(bool numLock, bool capsLock, bool scrollLock) { return send_cmdLEDs(numLock, capsLock, scrollLock); }
532 
542  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
543 
554  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
555 
556 #if FABGLIB_HAS_VirtualKeyO_STRING
557  static char const * virtualKeyToString(VirtualKey virtualKey);
558 #endif
559 
560 private:
561 
562  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = NULL);
563  VirtualKey VKtoAlternateVK(VirtualKey in_vk, KeyboardLayout const * layout = NULL);
564  void updateLEDs();
565  VirtualKey blockingGetVirtualKey(bool * keyDown);
566  static void SCodeToVKConverterTask(void * pvParameters);
567 
568 
569  bool m_keyboardAvailable; // self test passed and support for scancode set 2
570 
571  // these are valid after a call to generateVirtualKeys(true)
572  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
573  QueueHandle_t m_virtualKeyQueue;
574 
575  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
576 
577  KeyboardLayout const * m_layout;
578 
579  bool m_CTRL;
580  bool m_ALT;
581  bool m_SHIFT;
582  bool m_CAPSLOCK;
583  bool m_NUMLOCK;
584  bool m_SCROLLLOCK;
585 
586  // store status of the three LEDs
587  bool m_numLockLED;
588  bool m_capsLockLED;
589  bool m_scrollLockLED;
590 };
591 
592 
593 
594 
595 
596 
597 
598 
599 } // end of namespace
600 
601 
602 
603 extern fabgl::KeyboardClass Keyboard;
604 
605 
606 
607 #endif
608 
609 
Definition: keyboard.h:240
Definition: keyboard.h:113
Definition: keyboard.h:169
Definition: keyboard.h:102
Definition: keyboard.h:87
Definition: keyboard.h:190
Definition: keyboard.h:117
uint8_t shift
Definition: keyboard.h:293
Definition: keyboard.h:259
Definition: keyboard.h:132
Definition: keyboard.h:216
Definition: keyboard.h:203
Definition: keyboard.h:244
All in one structure to fully represent a keyboard layout.
Definition: keyboard.h:302
Definition: keyboard.h:151
int scancodeAvailable()
Get the number of scancodes available in the queue.
Definition: keyboard.cpp:464
Definition: keyboard.h:188
Definition: keyboard.h:110
Definition: keyboard.h:154
Definition: keyboard.h:231
Definition: keyboard.h:138
void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys=true, bool createVKQueue=true)
Initialize KeyboardClass specifying CLOCK and DATA GPIOs.
Definition: keyboard.cpp:431
Definition: keyboard.h:241
Definition: keyboard.h:252
Definition: keyboard.h:245
Definition: keyboard.h:206
void setLayout(KeyboardLayout const *layout)
Set keyboard layout.
Definition: keyboard.cpp:492
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: keyboard.h:86
Definition: keyboard.h:213
Definition: keyboard.h:124
Definition: keyboard.h:182
int virtualKeyAvailable()
Get the number of virtual keys available in the queue.
Definition: keyboard.cpp:901
Definition: keyboard.h:100
Definition: keyboard.h:171
KeyboardLayout const * getLayout()
Get current keyboard layout.
Definition: keyboard.h:433
Definition: keyboard.h:257
uint8_t capslock
Definition: keyboard.h:294
Definition: keyboard.h:158
Definition: keyboard.h:97
Definition: keyboard.h:101
Definition: keyboard.h:128
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Get keyboard LEDs status.
Definition: keyboard.cpp:456
Definition: keyboard.h:221
Definition: keyboard.h:197
Definition: keyboard.h:109
Definition: keyboard.h:189
Definition: keyboard.h:179
Definition: keyboard.h:266
Definition: keyboard.h:144
Definition: keyboard.h:242
Definition: keyboard.h:142
Definition: keyboard.h:170
uint8_t scancode
Definition: keyboard.h:280
Definition: keyboard.h:208
void suspendVirtualKeyGeneration(bool value)
Suspend or resume the virtual key generation task.
Definition: keyboard.cpp:871
Definition: keyboard.h:96
Definition: keyboard.h:106
Definition: keyboard.h:159
Definition: keyboard.h:163
bool isVKDown(VirtualKey virtualKey)
Get the virtual keys status.
Definition: keyboard.cpp:880
Definition: keyboard.h:141
Definition: keyboard.h:166
Definition: keyboard.h:105
Definition: keyboard.h:123
Definition: keyboard.h:177
Definition: keyboard.h:92
Definition: keyboard.h:116
Definition: keyboard.h:204
Definition: keyboard.h:156
uint8_t ctrl
Definition: keyboard.h:291
Definition: keyboard.h:137
Definition: keyboard.h:260
Definition: keyboard.h:152
Definition: keyboard.h:95
Definition: keyboard.h:126
Definition: keyboard.h:211
Definition: keyboard.h:246
Definition: keyboard.h:115
Definition: keyboard.h:143
Definition: keyboard.h:217
Definition: keyboard.h:207
Definition: keyboard.h:234
uint8_t numlock
Definition: keyboard.h:295
Definition: keyboard.h:111
Definition: keyboard.h:149
VirtualKey virtualKey
Definition: keyboard.h:281
Definition: keyboard.h:226
Definition: keyboard.h:161
VirtualKey reqVirtualKey
Definition: keyboard.h:289
Definition: keyboard.h:184
Definition: keyboard.h:139
Definition: keyboard.h:136
Definition: keyboard.h:130
AltVirtualKeyDef alternateVK[64]
Definition: keyboard.h:307
Definition: keyboard.h:183
uint8_t alt
Definition: keyboard.h:292
Definition: keyboard.h:222
Definition: keyboard.h:93
Definition: keyboard.h:251
Definition: keyboard.h:269
Definition: keyboard.h:258
Definition: keyboard.h:220
Definition: keyboard.h:155
Definition: keyboard.h:238
VirtualKeyDef scancodeToVK[92]
Definition: keyboard.h:305
Definition: keyboard.h:255
Definition: keyboard.h:174
Definition: keyboard.h:196
Definition: keyboard.h:195
Definition: keyboard.h:205
Definition: keyboard.h:162
Definition: keyboard.h:253
Definition: keyboard.h:229
Definition: canvas.cpp:37
Definition: keyboard.h:129
Definition: keyboard.h:127
Definition: keyboard.h:180
Definition: keyboard.h:104
Definition: keyboard.h:268
Definition: keyboard.h:209
Definition: keyboard.h:107
Definition: keyboard.h:230
Definition: keyboard.h:89
Definition: keyboard.h:227
Definition: keyboard.h:199
Definition: keyboard.h:254
Definition: keyboard.h:202
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Set keyboard LEDs status.
Definition: keyboard.h:531
Definition: keyboard.h:133
Definition: keyboard.h:232
Definition: keyboard.h:148
Associates scancode to virtualkey.
Definition: keyboard.h:279
Definition: keyboard.h:134
Definition: keyboard.h:235
Definition: keyboard.h:94
Definition: keyboard.h:237
Definition: keyboard.h:164
Definition: keyboard.h:215
Definition: keyboard.h:198
Definition: keyboard.h:91
Definition: keyboard.h:236
Definition: keyboard.h:135
Definition: keyboard.h:247
Definition: keyboard.h:212
bool isKeyboardAvailable()
Check if keyboard has been detected and correctly initialized.
Definition: keyboard.h:410
Definition: keyboard.h:167
Definition: keyboard.h:267
This file contains FabGL library configuration settings, like number of supported colors...
Definition: keyboard.h:120
const char * name
Definition: keyboard.h:303
Definition: keyboard.h:265
Definition: keyboard.h:233
KeyboardLayout const * inherited
Definition: keyboard.h:304
Definition: keyboard.h:175
Definition: keyboard.h:218
Definition: keyboard.h:173
Definition: keyboard.h:131
Definition: keyboard.h:200
int getNextScancode(int timeOutMS=-1, bool requestResendOnTimeOut=false)
Get a scancode from the queue.
Definition: keyboard.cpp:470
Associates a virtualkey and various shift states (ctrl, alt, etc..) to another virtualkey.
Definition: keyboard.h:288
Definition: keyboard.h:112
Definition: keyboard.h:223
Definition: keyboard.h:219
Definition: keyboard.h:172
Definition: keyboard.h:185
VirtualKeyDef exScancodeToVK[32]
Definition: keyboard.h:306
Definition: keyboard.h:98
bool reset()
Send a Reset command to the keyboard.
Definition: keyboard.cpp:439
bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS)
Set typematic rate and delay.
Definition: keyboard.h:554
Definition: keyboard.h:150
Definition: keyboard.h:194
Definition: keyboard.h:239
VirtualKey getNextVirtualKey(bool *keyDown=NULL, int timeOutMS=-1)
Get a virtual key from the queue.
Definition: keyboard.cpp:886
Definition: keyboard.h:256
Definition: keyboard.h:157
int virtualKeyToASCII(VirtualKey virtualKey)
Convert virtual key to ASCII.
Definition: keyboard.cpp:521
Definition: keyboard.h:210
Definition: keyboard.h:118
Definition: keyboard.h:90
Definition: keyboard.h:250
Definition: keyboard.h:181
Definition: keyboard.h:263
Definition: keyboard.h:103
Definition: keyboard.h:121
Definition: keyboard.h:108
Definition: keyboard.h:264
Definition: keyboard.h:224
Definition: keyboard.h:99
Definition: keyboard.h:125
Definition: keyboard.h:249
Definition: keyboard.h:147
Definition: keyboard.h:186
Definition: keyboard.h:228
VirtualKey virtualKey
Definition: keyboard.h:297
Base class for PS2 devices (like mouse or keyboard).
Definition: ps2device.h:69
Definition: keyboard.h:262
Definition: keyboard.h:191
Definition: keyboard.h:153
Definition: keyboard.h:160
Definition: keyboard.h:140
Definition: keyboard.h:122
Definition: keyboard.h:145
Definition: keyboard.h:114
Definition: keyboard.h:119
Definition: keyboard.h:178
Definition: keyboard.h:270
Definition: keyboard.h:248
Definition: keyboard.h:225
Definition: keyboard.h:214
Definition: keyboard.h:165
Definition: keyboard.h:243
The PS2 Keyboard controller class.
Definition: keyboard.h:348
Definition: keyboard.h:261
Definition: keyboard.h:146
Definition: keyboard.h:193
Definition: keyboard.h:176
Definition: keyboard.h:192
Definition: keyboard.h:187
Definition: keyboard.h:168
Definition: keyboard.h:201
This file contains fabgl::PS2DeviceClass definition.