FabGL
ESP32 Display 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 "comdrivers/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 
160 class Keyboard : public PS2Device {
161 
162 public:
163 
164  Keyboard();
165 
185  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys = true, bool createVKQueue = true);
186 
206  void begin(bool generateVirtualKeys, bool createVKQueue, int PS2Port);
207 
215  void setUIApp(uiApp * app) { m_uiApp = app; }
216 
222  bool reset();
223 
231  bool isKeyboardAvailable() { return m_keyboardAvailable; }
232 
247  void setLayout(KeyboardLayout const * layout);
248 
254  KeyboardLayout const * getLayout() { return m_layout; }
255 
266  bool isVKDown(VirtualKey virtualKey);
267 
276  int virtualKeyAvailable();
277 
289  VirtualKey getNextVirtualKey(bool * keyDown = nullptr, int timeOutMS = -1);
290 
294  void emptyVirtualKeyQueue();
295 
309  int virtualKeyToASCII(VirtualKey virtualKey);
310 
320  int scancodeAvailable();
321 
334  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
335 
344  void suspendVirtualKeyGeneration(bool value);
345 
357  bool setLEDs(bool numLock, bool capsLock, bool scrollLock) { return send_cmdLEDs(numLock, capsLock, scrollLock); }
358 
368  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
369 
380  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
381 
382 #if FABGLIB_HAS_VirtualKeyO_STRING
383  static char const * virtualKeyToString(VirtualKey virtualKey);
384 #endif
385 
386 private:
387 
388  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = nullptr);
389  VirtualKey VKtoAlternateVK(VirtualKey in_vk, KeyboardLayout const * layout = nullptr);
390  void updateLEDs();
391  VirtualKey blockingGetVirtualKey(bool * keyDown);
392  static void SCodeToVKConverterTask(void * pvParameters);
393 
394 
395  bool m_keyboardAvailable; // self test passed and support for scancode set 2
396 
397  // these are valid after a call to generateVirtualKeys(true)
398  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
399  QueueHandle_t m_virtualKeyQueue;
400 
401  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
402 
403  KeyboardLayout const * m_layout;
404 
405  uiApp * m_uiApp;
406 
407  bool m_CTRL;
408  bool m_ALT;
409  bool m_SHIFT;
410  bool m_CAPSLOCK;
411  bool m_NUMLOCK;
412  bool m_SCROLLLOCK;
413 
414  // store status of the three LEDs
415  bool m_numLockLED;
416  bool m_capsLockLED;
417  bool m_scrollLockLED;
418 };
419 
420 
421 
422 
423 
424 } // end of namespace
425 
426 
427 
428 
429 
430 
431 
432 
433 
434 
uint8_t shift
Definition: keyboard.h:103
void emptyVirtualKeyQueue()
Empties the virtual keys queue.
Definition: keyboard.cpp:935
All in one structure to fully represent a keyboard layout.
Definition: keyboard.h:112
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:380
Represents the whole application base class.
Definition: fabui.h:2254
bool isVKDown(VirtualKey virtualKey)
Gets the virtual keys status.
Definition: keyboard.cpp:902
This file contains all classes related to FabGL Graphical User Interface.
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Sets keyboard LEDs status.
Definition: keyboard.h:357
int scancodeAvailable()
Gets the number of scancodes available in the queue.
Definition: keyboard.cpp:468
uint8_t capslock
Definition: keyboard.h:104
KeyboardLayout const * getLayout()
Gets current keyboard layout.
Definition: keyboard.h:254
int virtualKeyToASCII(VirtualKey virtualKey)
Converts virtual key to ASCII.
Definition: keyboard.cpp:525
uint8_t scancode
Definition: keyboard.h:90
void setLayout(KeyboardLayout const *layout)
Sets keyboard layout.
Definition: keyboard.cpp:496
uint8_t ctrl
Definition: keyboard.h:101
bool isKeyboardAvailable()
Checks if keyboard has been detected and correctly initialized.
Definition: keyboard.h:231
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Gets keyboard LEDs status.
Definition: keyboard.cpp:460
The PS2 Keyboard controller class.
Definition: keyboard.h:160
uint8_t numlock
Definition: keyboard.h:105
VirtualKey virtualKey
Definition: keyboard.h:91
VirtualKey reqVirtualKey
Definition: keyboard.h:99
void suspendVirtualKeyGeneration(bool value)
Suspends or resume the virtual key generation task.
Definition: keyboard.cpp:893
AltVirtualKeyDef alternateVK[64]
Definition: keyboard.h:117
uint8_t alt
Definition: keyboard.h:102
VirtualKeyDef scancodeToVK[92]
Definition: keyboard.h:115
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:534
int virtualKeyAvailable()
Gets the number of virtual keys available in the queue.
Definition: keyboard.cpp:929
Definition: canvas.cpp:31
void setUIApp(uiApp *app)
Sets current UI app.
Definition: keyboard.h:215
Base class for PS2 devices (like mouse or keyboard).
Definition: ps2device.h:66
Associates scancode to virtualkey.
Definition: keyboard.h:89
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:474
Associates a virtualkey and various shift states (ctrl, alt, etc..) to another virtualkey.
Definition: keyboard.h:98
VirtualKeyDef exScancodeToVK[32]
Definition: keyboard.h:116
VirtualKey getNextVirtualKey(bool *keyDown=nullptr, int timeOutMS=-1)
Gets a virtual key from the queue.
Definition: keyboard.cpp:914
VirtualKey virtualKey
Definition: keyboard.h:107
void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys=true, bool createVKQueue=true)
Initializes Keyboard specifying CLOCK and DATA GPIOs.
Definition: keyboard.cpp:431
This file contains fabgl::PS2Device definition.