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-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3. Feel free to use FabGL in free software and hardware:
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 
30 
38 #include "freertos/FreeRTOS.h"
39 
40 #include "fabglconf.h"
41 #include "comdrivers/ps2device.h"
42 #include "fabui.h"
43 #include "kbdlayouts.h"
44 #include "codepages.h"
45 
46 
47 namespace fabgl {
48 
49 
50 
77 class Keyboard : public PS2Device {
78 
79 public:
80 
81  Keyboard();
82  ~Keyboard();
83 
106  void begin(gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys = true, bool createVKQueue = true);
107 
127  void begin(bool generateVirtualKeys, bool createVKQueue, int PS2Port);
128 
136  void setUIApp(uiApp * app) { m_uiApp = app; }
137 
143  bool reset();
144 
152  bool isKeyboardAvailable() { return m_keyboardAvailable; }
153 
168  void setLayout(KeyboardLayout const * layout);
169 
175  KeyboardLayout const * getLayout() { return m_layout; }
176 
187  bool isVKDown(VirtualKey virtualKey);
188 
197  int virtualKeyAvailable();
198 
210  VirtualKey getNextVirtualKey(bool * keyDown = nullptr, int timeOutMS = -1);
211 
223  bool getNextVirtualKey(VirtualKeyItem * item, int timeOutMS = -1);
224 
232  void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert = false);
233 
240  void injectVirtualKey(VirtualKeyItem const & item, bool insert = false);
241 
245  void emptyVirtualKeyQueue();
246 
261  int virtualKeyToASCII(VirtualKey virtualKey);
262 
275  void setCodePage(CodePage const * codepage) { m_codepage = codepage; }
276 
285  int scancodeAvailable();
286 
298  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
299 
311  bool setLEDs(bool numLock, bool capsLock, bool scrollLock);
312 
322  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
323 
334  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
335 
349  bool setScancodeSet(int value);
350 
356  int scancodeSet() { return m_scancodeSet; }
357 
358  static uint8_t convScancodeSet2To1(uint8_t code);
359 
360 #if FABGLIB_HAS_VirtualKeyO_STRING
361  static char const * virtualKeyToString(VirtualKey virtualKey);
362 #endif
363 
364 
366 
373  Delegate<VirtualKey *, bool> onVirtualKey;
374 
375 
377 
378 
387 
388 
389 
390 private:
391 
392  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = nullptr);
393  VirtualKey VKtoAlternateVK(VirtualKey in_vk, bool down, KeyboardLayout const * layout = nullptr);
394  VirtualKey manageCAPSLOCK(VirtualKey vk);
395  void updateLEDs();
396  bool blockingGetVirtualKey(VirtualKeyItem * item);
397  void convertScancode2to1(VirtualKeyItem * item);
398  void postVirtualKeyItem(VirtualKeyItem const & item);
399  static int convKeypadVKToNum(VirtualKey vk);
400 
401  static void SCodeToVKConverterTask(void * pvParameters);
402 
403 
404  bool m_keyboardAvailable; // self test passed and support for scancode set 2
405 
406  // these are valid after a call to generateVirtualKeys(true)
407  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
408  QueueHandle_t m_virtualKeyQueue;
409 
410  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
411 
412  // allowed values: 1, 2 or 3
413  // If virtual keys are enabled only 1 and 2 are possible. In case of scancode set 1 it is converted from scan code set 2, which is necessary
414  // in order to decode virtual keys.
415  uint8_t m_scancodeSet;
416 
417  KeyboardLayout const * m_layout;
418 
419  uiApp * m_uiApp;
420 
421  bool m_CTRL;
422  bool m_LALT;
423  bool m_RALT;
424  bool m_SHIFT;
425  bool m_CAPSLOCK;
426  bool m_GUI;
427  bool m_NUMLOCK;
428  bool m_SCROLLLOCK;
429 
430  VirtualKey m_lastDeadKey;
431 
432  // store status of the three LEDs
433  bool m_numLockLED;
434  bool m_capsLockLED;
435  bool m_scrollLockLED;
436 
437  CodePage const * m_codepage;
438 
439 };
440 
441 
442 
443 
444 
445 } // end of namespace
446 
447 
448 
449 
450 
451 
452 
453 
454 
455 
void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert=false)
Adds or inserts a virtual key into the virtual keys queue.
Definition: keyboard.cpp:526
All in one structure to fully represent a keyboard layout.
Definition: kbdlayouts.h:82
bool isKeyboardAvailable()
Checks if keyboard has been detected and correctly initialized.
Definition: keyboard.h:152
Represents the whole application base class.
Definition: fabui.h:2703
This file contains all classes related to FabGL Graphical User Interface.
int virtualKeyToASCII(VirtualKey virtualKey)
Converts virtual key to ASCII.
Definition: keyboard.cpp:237
void setUIApp(uiApp *app)
Sets current UI app.
Definition: keyboard.h:136
void emptyVirtualKeyQueue()
Empties the virtual keys queue.
Definition: keyboard.cpp:702
This file contains keyboard layouts.
Delegate< VirtualKey *, bool > onVirtualKey
Delegate called whenever a new virtual key is decoded from scancodes.
Definition: keyboard.h:373
void setCodePage(CodePage const *codepage)
Sets font codepage for virtual key to ASCII conversion.
Definition: keyboard.h:275
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Gets keyboard LEDs status.
Definition: keyboard.cpp:164
bool isVKDown(VirtualKey virtualKey)
Gets the virtual keys status.
Definition: keyboard.cpp:663
The PS2 Keyboard controller class.
Definition: keyboard.h:77
A struct which contains a virtual key, key state and associated scan code.
Definition: fabutils.h:1284
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:1016
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Sets keyboard LEDs status.
Definition: keyboard.cpp:155
This file contains codepages declarations.
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:101
Definition: canvas.cpp:36
VirtualKey getNextVirtualKey(bool *keyDown=nullptr, int timeOutMS=-1)
Gets a virtual key from the queue.
Definition: keyboard.cpp:684
int virtualKeyAvailable()
Gets the number of virtual keys available in the queue.
Definition: keyboard.cpp:696
Base class for PS2 devices (like mouse or keyboard).
Definition: ps2device.h:70
This file contains FabGL library configuration settings, like number of supported colors...
bool reset()
Sends a Reset command to the keyboard.
Definition: keyboard.cpp:110
int getNextScancode(int timeOutMS=-1, bool requestResendOnTimeOut=false)
Gets a scancode from the queue.
Definition: keyboard.cpp:187
int scancodeAvailable()
Gets the number of scancodes available in the queue.
Definition: keyboard.cpp:181
static int scancodeToVirtualKeyTaskStackSize
Stack size of the task that converts scancodes to Virtual Keys Keyboard.
Definition: keyboard.h:386
bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS)
Sets typematic rate and delay.
Definition: keyboard.h:334
int scancodeSet()
Gets current scancode set.
Definition: keyboard.h:356
bool setScancodeSet(int value)
Sets the scancode set.
Definition: keyboard.cpp:136
void setLayout(KeyboardLayout const *layout)
Sets keyboard layout.
Definition: keyboard.cpp:204
KeyboardLayout const * getLayout()
Gets current keyboard layout.
Definition: keyboard.h:175
This file contains fabgl::PS2Device definition.