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 #pragma once
24 
25 
26 
34 #include "freertos/FreeRTOS.h"
35 
36 #include "fabglconf.h"
37 #include "ps2device.h"
38 #include "fabui.h"
39 
40 
41 namespace fabgl {
42 
43 
44 // ASCII control characters
45 #define ASCII_NUL 0x00 // Null
46 #define ASCII_SOH 0x01 // Start of Heading
47 #define ASCII_STX 0x02 // Start of Text
48 #define ASCII_ETX 0x03 // End Of Text
49 #define ASCII_EOT 0x04 // End Of Transmission
50 #define ASCII_ENQ 0x05 // Enquiry
51 #define ASCII_ACK 0x06 // Acknowledge
52 #define ASCII_BELL 0x07 // Bell
53 #define ASCII_BS 0x08 // Backspace
54 #define ASCII_HT 0x09 // Horizontal Tab
55 #define ASCII_LF 0x0A // Line Feed
56 #define ASCII_VT 0x0B // Vertical Tab
57 #define ASCII_FF 0x0C // Form Feed
58 #define ASCII_CR 0x0D // Carriage Return
59 #define ASCII_SO 0x0E // Shift Out
60 #define ASCII_SI 0x0F // Shift In
61 #define ASCII_DLE 0x10 // Data Link Escape
62 #define ASCII_DC1 0x11 // Device Control 1
63 #define ASCII_XON 0x11 // Transmission On
64 #define ASCII_DC2 0x12 // Device Control 2
65 #define ASCII_DC3 0x13 // Device Control 3
66 #define ASCII_XOFF 0x13 // Transmission Off
67 #define ASCII_DC4 0x14 // Device Control 4
68 #define ASCII_NAK 0x15 // Negative Acknowledge
69 #define ASCII_SYN 0x16 // Synchronous Idle
70 #define ASCII_ETB 0x17 // End-of-Transmission-Block
71 #define ASCII_CAN 0x18 // Cancel
72 #define ASCII_EM 0x19 // End of Medium
73 #define ASCII_SUB 0x1A // Substitute
74 #define ASCII_ESC 0x1B // Escape
75 #define ASCII_FS 0x1C // File Separator
76 #define ASCII_GS 0x1D // Group Separator
77 #define ASCII_RS 0x1E // Record Separator
78 #define ASCII_US 0x1F // Unit Separator
79 #define ASCII_SPC 0x20 // Space
80 #define ASCII_DEL 0x7F // Delete
81 
82 
83 
84 
85 
89 struct VirtualKeyDef {
90  uint8_t scancode;
92 };
93 
94 
100  struct {
101  uint8_t ctrl : 1;
102  uint8_t alt : 1;
103  uint8_t shift : 1;
104  uint8_t capslock : 1;
105  uint8_t numlock : 1;
106  };
108 };
109 
110 
113  const char * name;
118 };
119 
120 
122 extern const KeyboardLayout USLayout;
123 
125 extern const KeyboardLayout UKLayout;
126 
128 extern const KeyboardLayout GermanLayout;
129 
131 extern const KeyboardLayout ItalianLayout;
132 
133 
159 
160 public:
161 
162  KeyboardClass();
163 
183  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys = true, bool createVKQueue = true);
184 
204  void begin(bool generateVirtualKeys, bool createVKQueue, int PS2Port);
205 
213  void setUIApp(uiApp * app) { m_uiApp = app; }
214 
220  bool reset();
221 
229  bool isKeyboardAvailable() { return m_keyboardAvailable; }
230 
245  void setLayout(KeyboardLayout const * layout);
246 
252  KeyboardLayout const * getLayout() { return m_layout; }
253 
264  bool isVKDown(VirtualKey virtualKey);
265 
274  int virtualKeyAvailable();
275 
287  VirtualKey getNextVirtualKey(bool * keyDown = nullptr, int timeOutMS = -1);
288 
292  void emptyVirtualKeyQueue();
293 
307  int virtualKeyToASCII(VirtualKey virtualKey);
308 
318  int scancodeAvailable();
319 
332  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
333 
342  void suspendVirtualKeyGeneration(bool value);
343 
355  bool setLEDs(bool numLock, bool capsLock, bool scrollLock) { return send_cmdLEDs(numLock, capsLock, scrollLock); }
356 
366  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
367 
378  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
379 
380 #if FABGLIB_HAS_VirtualKeyO_STRING
381  static char const * virtualKeyToString(VirtualKey virtualKey);
382 #endif
383 
384 private:
385 
386  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = nullptr);
387  VirtualKey VKtoAlternateVK(VirtualKey in_vk, KeyboardLayout const * layout = nullptr);
388  void updateLEDs();
389  VirtualKey blockingGetVirtualKey(bool * keyDown);
390  static void SCodeToVKConverterTask(void * pvParameters);
391 
392 
393  bool m_keyboardAvailable; // self test passed and support for scancode set 2
394 
395  // these are valid after a call to generateVirtualKeys(true)
396  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
397  QueueHandle_t m_virtualKeyQueue;
398 
399  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
400 
401  KeyboardLayout const * m_layout;
402 
403  uiApp * m_uiApp;
404 
405  bool m_CTRL;
406  bool m_ALT;
407  bool m_SHIFT;
408  bool m_CAPSLOCK;
409  bool m_NUMLOCK;
410  bool m_SCROLLLOCK;
411 
412  // store status of the three LEDs
413  bool m_numLockLED;
414  bool m_capsLockLED;
415  bool m_scrollLockLED;
416 };
417 
418 
419 
420 
421 
422 } // end of namespace
423 
424 
425 
426 extern fabgl::KeyboardClass Keyboard;
427 
428 
429 
430 
431 
432 
uint8_t shift
Definition: keyboard.h:103
All in one structure to fully represent a keyboard layout.
Definition: keyboard.h:112
int scancodeAvailable()
Gets the number of scancodes available in the queue.
Definition: keyboard.cpp:466
void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys=true, bool createVKQueue=true)
Initializes KeyboardClass specifying CLOCK and DATA GPIOs.
Definition: keyboard.cpp:433
Represents the whole application base class.
Definition: fabui.h:2244
This file contains all classes related to FabGL Graphical User Interface.
void setLayout(KeyboardLayout const *layout)
Sets keyboard layout.
Definition: keyboard.cpp:494
int virtualKeyAvailable()
Gets the number of virtual keys available in the queue.
Definition: keyboard.cpp:926
KeyboardLayout const * getLayout()
Gets current keyboard layout.
Definition: keyboard.h:252
uint8_t capslock
Definition: keyboard.h:104
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Gets keyboard LEDs status.
Definition: keyboard.cpp:458
uint8_t scancode
Definition: keyboard.h:90
void suspendVirtualKeyGeneration(bool value)
Suspends or resume the virtual key generation task.
Definition: keyboard.cpp:890
bool isVKDown(VirtualKey virtualKey)
Gets the virtual keys status.
Definition: keyboard.cpp:899
uint8_t ctrl
Definition: keyboard.h:101
uint8_t numlock
Definition: keyboard.h:105
VirtualKey virtualKey
Definition: keyboard.h:91
VirtualKey reqVirtualKey
Definition: keyboard.h:99
AltVirtualKeyDef alternateVK[64]
Definition: keyboard.h:117
uint8_t alt
Definition: keyboard.h:102
void setUIApp(uiApp *app)
Sets current UI app.
Definition: keyboard.h:213
VirtualKeyDef scancodeToVK[92]
Definition: keyboard.h:115
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:502
Definition: canvas.cpp:47
VirtualKey getNextVirtualKey(bool *keyDown=nullptr, int timeOutMS=-1)
Gets a virtual key from the queue.
Definition: keyboard.cpp:911
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Sets keyboard LEDs status.
Definition: keyboard.h:355
void emptyVirtualKeyQueue()
Empties the virtual keys queue.
Definition: keyboard.cpp:932
Associates scancode to virtualkey.
Definition: keyboard.h:89
bool isKeyboardAvailable()
Checks if keyboard has been detected and correctly initialized.
Definition: keyboard.h:229
This file contains FabGL library configuration settings, like number of supported colors...
const char * name
Definition: keyboard.h:113
KeyboardLayout const * inherited
Definition: keyboard.h:114
int getNextScancode(int timeOutMS=-1, bool requestResendOnTimeOut=false)
Gets a scancode from the queue.
Definition: keyboard.cpp:472
Associates a virtualkey and various shift states (ctrl, alt, etc..) to another virtualkey.
Definition: keyboard.h:98
VirtualKeyDef exScancodeToVK[32]
Definition: keyboard.h:116
bool reset()
Sends a Reset command to the keyboard.
Definition: keyboard.cpp:441
bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS)
Sets typematic rate and delay.
Definition: keyboard.h:378
int virtualKeyToASCII(VirtualKey virtualKey)
Converts virtual key to ASCII.
Definition: keyboard.cpp:523
VirtualKey virtualKey
Definition: keyboard.h:107
Base class for PS2 devices (like mouse or keyboard).
Definition: ps2device.h:68
The PS2 Keyboard controller class.
Definition: keyboard.h:158
This file contains fabgl::PS2DeviceClass definition.