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-2020 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 
45 
49 struct VirtualKeyDef {
50  uint8_t scancode;
52 };
53 
54 
60  struct {
61  uint8_t ctrl : 1;
62  uint8_t alt : 1;
63  uint8_t shift : 1;
64  uint8_t capslock : 1;
65  uint8_t numlock : 1;
66  };
68 };
69 
70 
71 struct DeadKeyVirtualKeyDef {
72  VirtualKey deadKey;
73  VirtualKey reqVirtualKey;
74  VirtualKey virtualKey;
75 };
76 
77 
80  const char * name;
81  const char * desc;
88  DeadKeyVirtualKeyDef deadkeysToVK[64];
89 };
90 
91 
93 extern const KeyboardLayout USLayout;
94 
96 extern const KeyboardLayout UKLayout;
97 
99 extern const KeyboardLayout GermanLayout;
100 
102 extern const KeyboardLayout ItalianLayout;
103 
105 extern const KeyboardLayout SpanishLayout;
106 
107 
108 struct SupportedLayouts {
109  static int count() { return 5; }
110  static char const * * names() {
111  static char const * NAMES[] = {
112  GermanLayout.desc,
113  ItalianLayout.desc,
114  UKLayout.desc,
115  USLayout.desc,
116  SpanishLayout.desc };
117  return NAMES;
118  }
119  static char const * * shortNames() {
120  static char const * SNAMES[] = {
121  GermanLayout.name,
122  ItalianLayout.name,
123  UKLayout.name,
124  USLayout.name,
125  SpanishLayout.name };
126  return SNAMES;
127  }
128  static const KeyboardLayout * * layouts() {
129  static KeyboardLayout const * LAYOUTS[] = {
130  &GermanLayout,
131  &ItalianLayout,
132  &UKLayout,
133  &USLayout,
134  &SpanishLayout };
135  return LAYOUTS;
136  }
137 };
138 
139 
166 class Keyboard : public PS2Device {
167 
168 public:
169 
170  Keyboard();
171 
194  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys = true, bool createVKQueue = true);
195 
215  void begin(bool generateVirtualKeys, bool createVKQueue, int PS2Port);
216 
224  void setUIApp(uiApp * app) { m_uiApp = app; }
225 
231  bool reset();
232 
240  bool isKeyboardAvailable() { return m_keyboardAvailable; }
241 
256  void setLayout(KeyboardLayout const * layout);
257 
263  KeyboardLayout const * getLayout() { return m_layout; }
264 
275  bool isVKDown(VirtualKey virtualKey);
276 
285  int virtualKeyAvailable();
286 
298  VirtualKey getNextVirtualKey(bool * keyDown = nullptr, int timeOutMS = -1);
299 
307  void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert = false);
308 
312  void emptyVirtualKeyQueue();
313 
327  int virtualKeyToASCII(VirtualKey virtualKey);
328 
338  int scancodeAvailable();
339 
352  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
353 
362  void suspendVirtualKeyGeneration(bool value);
363 
375  bool setLEDs(bool numLock, bool capsLock, bool scrollLock) { return send_cmdLEDs(numLock, capsLock, scrollLock); }
376 
386  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
387 
398  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
399 
400 #if FABGLIB_HAS_VirtualKeyO_STRING
401  static char const * virtualKeyToString(VirtualKey virtualKey);
402 #endif
403 
404 
406 
413  Delegate<VirtualKey *, bool> onVirtualKey;
414 
415 
417 
418 
427 
428 
429 
430 private:
431 
432  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = nullptr);
433  VirtualKey VKtoAlternateVK(VirtualKey in_vk, KeyboardLayout const * layout = nullptr);
434  void updateLEDs();
435  VirtualKey blockingGetVirtualKey(bool * keyDown);
436  static void SCodeToVKConverterTask(void * pvParameters);
437 
438 
439  bool m_keyboardAvailable; // self test passed and support for scancode set 2
440 
441  // these are valid after a call to generateVirtualKeys(true)
442  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
443  QueueHandle_t m_virtualKeyQueue;
444 
445  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
446 
447  KeyboardLayout const * m_layout;
448 
449  uiApp * m_uiApp;
450 
451  bool m_CTRL;
452  bool m_ALT;
453  bool m_SHIFT;
454  bool m_CAPSLOCK;
455  bool m_NUMLOCK;
456  bool m_SCROLLLOCK;
457 
458  VirtualKey m_lastDeadKey;
459 
460  // store status of the three LEDs
461  bool m_numLockLED;
462  bool m_capsLockLED;
463  bool m_scrollLockLED;
464 };
465 
466 
467 
468 
469 
470 } // end of namespace
471 
472 
473 
474 
475 
476 
477 
478 
479 
480 
void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert=false)
Adds or inserts a virtual key into the virtual keys queue.
Definition: keyboard.cpp:1203
All in one structure to fully represent a keyboard layout.
Definition: keyboard.h:79
bool isKeyboardAvailable()
Checks if keyboard has been detected and correctly initialized.
Definition: keyboard.h:240
Represents the whole application base class.
Definition: fabui.h:2561
DeadKeyVirtualKeyDef deadkeysToVK[64]
Definition: keyboard.h:88
This file contains all classes related to FabGL Graphical User Interface.
VirtualKey deadKeysVK[8]
Definition: keyboard.h:87
VirtualKeyDef exScancodeToVK[32]
Definition: keyboard.h:84
VirtualKey virtualKey
Definition: keyboard.h:51
VirtualKey virtualKey
Definition: keyboard.h:67
int virtualKeyToASCII(VirtualKey virtualKey)
Converts virtual key to ASCII.
Definition: keyboard.cpp:738
void setUIApp(uiApp *app)
Sets current UI app.
Definition: keyboard.h:224
void emptyVirtualKeyQueue()
Empties the virtual keys queue.
Definition: keyboard.cpp:1296
Delegate< VirtualKey *, bool > onVirtualKey
Delegate called whenever a new virtual key is decoded from scancodes.
Definition: keyboard.h:413
AltVirtualKeyDef alternateVK[64]
Definition: keyboard.h:85
const char * desc
Definition: keyboard.h:81
const char * name
Definition: keyboard.h:80
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Gets keyboard LEDs status.
Definition: keyboard.cpp:670
bool isVKDown(VirtualKey virtualKey)
Gets the virtual keys status.
Definition: keyboard.cpp:1263
The PS2 Keyboard controller class.
Definition: keyboard.h:166
VirtualKeyDef scancodeToVK[92]
Definition: keyboard.h:83
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:951
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Sets keyboard LEDs status.
Definition: keyboard.h:375
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:641
Definition: canvas.cpp:31
VirtualKey getNextVirtualKey(bool *keyDown=nullptr, int timeOutMS=-1)
Gets a virtual key from the queue.
Definition: keyboard.cpp:1275
VirtualKey reqVirtualKey
Definition: keyboard.h:59
int virtualKeyAvailable()
Gets the number of virtual keys available in the queue.
Definition: keyboard.cpp:1290
Base class for PS2 devices (like mouse or keyboard).
Definition: ps2device.h:66
Associates scancode to virtualkey.
Definition: keyboard.h:49
This file contains FabGL library configuration settings, like number of supported colors...
bool reset()
Sends a Reset command to the keyboard.
Definition: keyboard.cpp:651
Associates a virtualkey and various shift states (ctrl, alt, etc..) to another virtualkey.
Definition: keyboard.h:58
int getNextScancode(int timeOutMS=-1, bool requestResendOnTimeOut=false)
Gets a scancode from the queue.
Definition: keyboard.cpp:684
int scancodeAvailable()
Gets the number of scancodes available in the queue.
Definition: keyboard.cpp:678
static int scancodeToVirtualKeyTaskStackSize
Stack size of the task that converts scancodes to Virtual Keys Keyboard.
Definition: keyboard.h:426
bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS)
Sets typematic rate and delay.
Definition: keyboard.h:398
void setLayout(KeyboardLayout const *layout)
Sets keyboard layout.
Definition: keyboard.cpp:706
KeyboardLayout const * inherited
Definition: keyboard.h:82
void suspendVirtualKeyGeneration(bool value)
Suspends or resume the virtual key generation task.
Definition: keyboard.cpp:1254
KeyboardLayout const * getLayout()
Gets current keyboard layout.
Definition: keyboard.h:263
This file contains fabgl::PS2Device definition.