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.
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 
140  void enableVirtualKeys(bool generateVirtualKeys, bool createVKQueue);
141 
149  void setUIApp(uiApp * app) { m_uiApp = app; }
150 
156  bool reset();
157 
165  bool isKeyboardAvailable() { return m_keyboardAvailable; }
166 
181  void setLayout(KeyboardLayout const * layout);
182 
188  KeyboardLayout const * getLayout() { return m_layout; }
189 
200  bool isVKDown(VirtualKey virtualKey);
201 
210  int virtualKeyAvailable();
211 
223  VirtualKey getNextVirtualKey(bool * keyDown = nullptr, int timeOutMS = -1);
224 
236  bool getNextVirtualKey(VirtualKeyItem * item, int timeOutMS = -1);
237 
245  void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert = false);
246 
253  void injectVirtualKey(VirtualKeyItem const & item, bool insert = false);
254 
258  void emptyVirtualKeyQueue();
259 
274  int virtualKeyToASCII(VirtualKey virtualKey);
275 
288  void setCodePage(CodePage const * codepage) { m_codepage = codepage; }
289 
298  int scancodeAvailable();
299 
311  int getNextScancode(int timeOutMS = -1, bool requestResendOnTimeOut = false);
312 
324  bool setLEDs(bool numLock, bool capsLock, bool scrollLock);
325 
335  void getLEDs(bool * numLock, bool * capsLock, bool * scrollLock);
336 
347  bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS) { return send_cmdTypematicRateAndDelay(repeatRateMS, repeatDelayMS); }
348 
362  bool setScancodeSet(int value);
363 
369  int scancodeSet() { return m_scancodeSet; }
370 
371  static uint8_t convScancodeSet2To1(uint8_t code);
372 
373 #if FABGLIB_HAS_VirtualKeyO_STRING
374  static char const * virtualKeyToString(VirtualKey virtualKey);
375 #endif
376 
377 
379 
386  Delegate<VirtualKey *, bool> onVirtualKey;
387 
388 
390 
391 
400 
401 
402 
403 private:
404 
405  VirtualKey scancodeToVK(uint8_t scancode, bool isExtended, KeyboardLayout const * layout = nullptr);
406  VirtualKey VKtoAlternateVK(VirtualKey in_vk, bool down, KeyboardLayout const * layout = nullptr);
407  VirtualKey manageCAPSLOCK(VirtualKey vk);
408  void updateLEDs();
409  bool blockingGetVirtualKey(VirtualKeyItem * item);
410  void convertScancode2to1(VirtualKeyItem * item);
411  void postVirtualKeyItem(VirtualKeyItem const & item);
412  static int convKeypadVKToNum(VirtualKey vk);
413 
414  static void SCodeToVKConverterTask(void * pvParameters);
415 
416 
417  bool m_keyboardAvailable; // self test passed and support for scancode set 2
418 
419  TaskHandle_t m_SCodeToVKConverterTask; // Task that converts scancodes to virtual key and populates m_virtualKeyQueue
420  QueueHandle_t m_virtualKeyQueue;
421 
422  uint8_t m_VKMap[(int)(VK_LAST + 7) / 8];
423 
424  // allowed values: 1, 2 or 3
425  // 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
426  // in order to decode virtual keys.
427  uint8_t m_scancodeSet;
428 
429  KeyboardLayout const * m_layout;
430 
431  uiApp * m_uiApp;
432 
433  bool m_CTRL;
434  bool m_LALT;
435  bool m_RALT;
436  bool m_SHIFT;
437  bool m_CAPSLOCK;
438  bool m_GUI;
439  bool m_NUMLOCK;
440  bool m_SCROLLLOCK;
441 
442  VirtualKey m_lastDeadKey;
443 
444  // store status of the three LEDs
445  bool m_numLockLED;
446  bool m_capsLockLED;
447  bool m_scrollLockLED;
448 
449  CodePage const * m_codepage;
450 
451 };
452 
453 
454 
455 
456 
457 } // end of namespace
458 
459 
460 
461 
462 
463 
464 
465 
466 
467 
void injectVirtualKey(VirtualKey virtualKey, bool keyDown, bool insert=false)
Adds or inserts a virtual key into the virtual keys queue.
Definition: keyboard.cpp:546
All in one structure to fully represent a keyboard layout.
Definition: kbdlayouts.h:82
void enableVirtualKeys(bool generateVirtualKeys, bool createVKQueue)
Dynamically enables or disables Virtual Keys generation.
Definition: keyboard.cpp:100
bool isKeyboardAvailable()
Checks if keyboard has been detected and correctly initialized.
Definition: keyboard.h:165
Represents the whole application base class.
Definition: fabui.h:3030
This file contains all classes related to FabGL Graphical User Interface.
int virtualKeyToASCII(VirtualKey virtualKey)
Converts virtual key to ASCII.
Definition: keyboard.cpp:257
void setUIApp(uiApp *app)
Sets current UI app.
Definition: keyboard.h:149
void emptyVirtualKeyQueue()
Empties the virtual keys queue.
Definition: keyboard.cpp:722
This file contains keyboard layouts.
Delegate< VirtualKey *, bool > onVirtualKey
Delegate called whenever a new virtual key is decoded from scancodes.
Definition: keyboard.h:386
void setCodePage(CodePage const *codepage)
Sets font codepage for virtual key to ASCII conversion.
Definition: keyboard.h:288
void getLEDs(bool *numLock, bool *capsLock, bool *scrollLock)
Gets keyboard LEDs status.
Definition: keyboard.cpp:184
bool isVKDown(VirtualKey virtualKey)
Gets the virtual keys status.
Definition: keyboard.cpp:683
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:1304
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:1036
bool setLEDs(bool numLock, bool capsLock, bool scrollLock)
Sets keyboard LEDs status.
Definition: keyboard.cpp:175
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:92
Definition: canvas.cpp:36
VirtualKey getNextVirtualKey(bool *keyDown=nullptr, int timeOutMS=-1)
Gets a virtual key from the queue.
Definition: keyboard.cpp:704
int virtualKeyAvailable()
Gets the number of virtual keys available in the queue.
Definition: keyboard.cpp:716
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:130
int getNextScancode(int timeOutMS=-1, bool requestResendOnTimeOut=false)
Gets a scancode from the queue.
Definition: keyboard.cpp:207
int scancodeAvailable()
Gets the number of scancodes available in the queue.
Definition: keyboard.cpp:201
static int scancodeToVirtualKeyTaskStackSize
Stack size of the task that converts scancodes to Virtual Keys Keyboard.
Definition: keyboard.h:399
bool setTypematicRateAndDelay(int repeatRateMS, int repeatDelayMS)
Sets typematic rate and delay.
Definition: keyboard.h:347
int scancodeSet()
Gets current scancode set.
Definition: keyboard.h:369
bool setScancodeSet(int value)
Sets the scancode set.
Definition: keyboard.cpp:156
void setLayout(KeyboardLayout const *layout)
Sets keyboard layout.
Definition: keyboard.cpp:224
KeyboardLayout const * getLayout()
Gets current keyboard layout.
Definition: keyboard.h:188
This file contains fabgl::PS2Device definition.