FabGL
ESP32 VGA Controller and Graphics Library
|
The PS2 Keyboard controller class. More...
#include <keyboard.h>
Public Member Functions | |
void | begin (gpio_num_t clkGPIO, gpio_num_t dataGPIO, bool generateVirtualKeys=true, bool createVKQueue=true) |
Initializes KeyboardClass specifying CLOCK and DATA GPIOs. More... | |
void | begin (bool generateVirtualKeys, bool createVKQueue, int PS2Port) |
Initializes KeyboardClass without initializing the PS/2 controller. More... | |
void | emptyVirtualKeyQueue () |
Empties the virtual keys queue. More... | |
KeyboardLayout const * | getLayout () |
Gets current keyboard layout. More... | |
void | getLEDs (bool *numLock, bool *capsLock, bool *scrollLock) |
Gets keyboard LEDs status. More... | |
int | getNextScancode (int timeOutMS=-1, bool requestResendOnTimeOut=false) |
Gets a scancode from the queue. More... | |
VirtualKey | getNextVirtualKey (bool *keyDown=nullptr, int timeOutMS=-1) |
Gets a virtual key from the queue. More... | |
PS2Device | identify () |
Identifies the device attached to the PS2 port. More... | |
bool | isKeyboardAvailable () |
Checks if keyboard has been detected and correctly initialized. More... | |
bool | isVKDown (VirtualKey virtualKey) |
Gets the virtual keys status. More... | |
bool | lock (int timeOutMS) |
Gets exclusive access to the device. More... | |
bool | reset () |
Sends a Reset command to the keyboard. More... | |
int | scancodeAvailable () |
Gets the number of scancodes available in the queue. More... | |
void | setLayout (KeyboardLayout const *layout) |
Sets keyboard layout. More... | |
bool | setLEDs (bool numLock, bool capsLock, bool scrollLock) |
Sets keyboard LEDs status. More... | |
bool | setTypematicRateAndDelay (int repeatRateMS, int repeatDelayMS) |
Sets typematic rate and delay. More... | |
void | setUIApp (uiApp *app) |
Sets current UI app. More... | |
void | suspendVirtualKeyGeneration (bool value) |
Suspends or resume the virtual key generation task. More... | |
void | unlock () |
Releases device from exclusive access. More... | |
int | virtualKeyAvailable () |
Gets the number of virtual keys available in the queue. More... | |
int | virtualKeyToASCII (VirtualKey virtualKey) |
Converts virtual key to ASCII. More... | |
The PS2 Keyboard controller class.
KeyboardClass connects to one port of the PS2 Controller class (fabgl::PS2ControllerClass) and provides the logic that converts scancodes to virtual keys or ASCII (and ANSI) codes.
It optionally creates a task that waits for scan codes from the PS2 device and puts virtual keys in a queue.
The PS2 controller uses ULP coprocessor and RTC slow memory to communicate with the PS2 device.
It is possible to specify an international keyboard layout. The default is US-layout.
There are three predefined kayboard layouts: US (USA), UK (United Kingdom), DE (German) and IT (Italian). Other layout can be added inheriting from US or from any other layout.
Applications do not need to create an instance of KeyboardClass because an instance named Keyboard is created automatically.
Example:
// Setup pins GPIO33 for CLK and GPIO32 for DATA Keyboard.begin(GPIO_NUM_33, GPIO_NUM_32); // clk, dat // Prints name of received virtual keys while (true) Serial.printf("VirtualKey = %s\n", Keyboard.virtualKeyToString(Keyboard.getNextVirtualKey()));
void fabgl::KeyboardClass::begin | ( | gpio_num_t | clkGPIO, |
gpio_num_t | dataGPIO, | ||
bool | generateVirtualKeys = true , |
||
bool | createVKQueue = true |
||
) |
Initializes KeyboardClass specifying CLOCK and DATA GPIOs.
A reset command (KeyboardClass.reset() method) is automatically sent to the keyboard.
This method also initializes the PS2ControllerClass to use port 0 only.
clkGPIO | The GPIO number of Clock line |
dataGPIO | The GPIO number of Data line |
generateVirtualKeys | If true creates a task which consumes scancodes to produce virtual keys, so you can call KeyboardClass.isVKDown(). |
createVKQueue | If true creates a task which consunes scancodes and produces virtual keys and put them in a queue, so you can call KeyboardClass.isVKDown(), KeyboardClass.virtualKeyAvailable() and KeyboardClass.getNextVirtualKey(). |
Example:
// Setup pins GPIO33 for CLK and GPIO32 for DATA Keyboard.begin(GPIO_NUM_33, GPIO_NUM_32); // clk, dat
void fabgl::KeyboardClass::begin | ( | bool | generateVirtualKeys, |
bool | createVKQueue, | ||
int | PS2Port | ||
) |
Initializes KeyboardClass without initializing the PS/2 controller.
A reset command (KeyboardClass.reset() method) is automatically sent to the keyboard.
This method does not initialize the PS2ControllerClass.
generateVirtualKeys | If true creates a task which consumes scancodes and produces virtual keys, so you can call KeyboardClass.isVKDown(). |
createVKQueue | If true creates a task which consunes scancodes to produce virtual keys and put them in a queue, so you can call KeyboardClass.isVKDown(), KeyboardClass.virtualKeyAvailable() and KeyboardClass.getNextVirtualKey(). |
PS2Port | The PS/2 port to use (0 or 1). |
Example:
// Setup pins GPIO33 for CLK and GPIO32 for DATA on port 0 PS2Controller.begin(GPIO_NUM_33, GPIO_NUM_32); // clk, dat Keyboard.begin(true, true, 0); // port 0
void fabgl::KeyboardClass::emptyVirtualKeyQueue | ( | ) |
Empties the virtual keys queue.
|
inline |
Gets current keyboard layout.
void fabgl::KeyboardClass::getLEDs | ( | bool * | numLock, |
bool * | capsLock, | ||
bool * | scrollLock | ||
) |
Gets keyboard LEDs status.
Use this method to know the current status of NUMLOCK, CAPSLOCK and SCROLLLOCK LEDs.
numLock | When true the NUMLOCK LED is switched on. |
capsLock | When true the CAPSLOCK LED is switched on. |
scrollLock | When true the SCROLLLOCK LED is switched on. |
int fabgl::KeyboardClass::getNextScancode | ( | int | timeOutMS = -1 , |
bool | requestResendOnTimeOut = false |
||
) |
Gets a scancode from the queue.
Scancodes are always generated but they can be consumed by the scancode-to-virtualkeys task. So, in order to use this method KeyboardClass.begin() method should be called with generateVirtualKeys = false and createVKQueue = false.
Alternatively it is also possible to suspend the conversion task calling KeyboardClass.suspendVirtualKeyGeneration() method.
timeOutMS | Timeout in milliseconds. -1 means no timeout (infinite time). |
requestResendOnTimeOut | If true and timeout has expired then asks the keyboard to resend the scancode. |
VirtualKey fabgl::KeyboardClass::getNextVirtualKey | ( | bool * | keyDown = nullptr , |
int | timeOutMS = -1 |
||
) |
Gets a virtual key from the queue.
Virtual keys are generated from scancodes only if generateVirtualKeys parameter is true (default) and createVKQueue parameter is true (default) of KeyboardClass.begin() method.
keyDown | A pointer to boolean variable which will contain if the virtual key is depressed (true) or released (false). |
timeOutMS | Timeout in milliseconds. -1 means no timeout (infinite time). |
|
inlineinherited |
Identifies the device attached to the PS2 port.
|
inline |
Checks if keyboard has been detected and correctly initialized.
isKeyboardAvailable() returns a valid value only after KeyboardClass.begin() or KeyboardClass.reset() has been called.
bool fabgl::KeyboardClass::isVKDown | ( | VirtualKey | virtualKey | ) |
Gets the virtual keys status.
This method allows to know the status of each virtual key (Down or Up).
Virtual keys are generated from scancodes only if generateVirtualKeys parameter of KeyboardClass.begin() method is true (default).
virtualKey | The Virtual Key to test. |
|
inherited |
Gets exclusive access to the device.
timeOutMS | Timeout in milliseconds to wait before fail. |
bool fabgl::KeyboardClass::reset | ( | ) |
Sends a Reset command to the keyboard.
int fabgl::KeyboardClass::scancodeAvailable | ( | ) |
Gets the number of scancodes available in the queue.
Scancodes are always generated but they can be consumed by the scancode-to-virtualkeys task. So, in order to use this method KeyboardClass.begin() method should be called with generateVirtualKeys = false and createVKQueue = false.
Alternatively it is also possible to suspend the conversion task calling KeyboardClass.suspendVirtualKeyGeneration() method.
void fabgl::KeyboardClass::setLayout | ( | KeyboardLayout const * | layout | ) |
Sets keyboard layout.
It is possible to specify an international keyboard layout. The default is US-layout.
There are three predefined kayboard layouts: US (USA), UK (United Kingdom), DE (German) and IT (Italian). Other layout can be added inheriting from US or from any other layout.
layout | A pointer to the layout structure. |
Example:
// Set German layout setLayout(&fabgl::GermanLayout);
|
inline |
Sets keyboard LEDs status.
Use this method to switch-on or off the NUMLOCK, CAPSLOCK and SCROLLLOCK LEDs.
numLock | When true the NUMLOCK LED is switched on. |
capsLock | When true the CAPSLOCK LED is switched on. |
scrollLock | When true the SCROLLLOCK LED is switched on. |
|
inline |
Sets typematic rate and delay.
If the key is kept pressed down, after repeatDelayMS keyboard starts periodically sending codes with frequency repeatRateMS.
repeatRateMS | Repeat rate in milliseconds (in range 33 ms ... 500 ms). |
repeatDelayMS | Repeat delay in milliseconds (in range 250 ms ... 1000 ms, steps of 250 ms). |
|
inline |
Sets current UI app.
To use this generateVirtualKeys must be true in begin().
app | The UI app where to send keyboard events |
void fabgl::KeyboardClass::suspendVirtualKeyGeneration | ( | bool | value | ) |
Suspends or resume the virtual key generation task.
Use this method to temporarily suspend the scancode to virtual key conversion task. This is useful when scancode are necessary for a limited time.
value | If true conversion task is suspended. If false conversion task is resumed. |
|
inherited |
Releases device from exclusive access.
int fabgl::KeyboardClass::virtualKeyAvailable | ( | ) |
Gets the number of virtual keys available in the queue.
Virtual keys are generated from scancodes only if generateVirtualKeys parameter is true (default) and createVKQueue parameter is true (default) of KeyboardClass.begin() method.
int fabgl::KeyboardClass::virtualKeyToASCII | ( | VirtualKey | virtualKey | ) |
Converts virtual key to ASCII.
This method converts the specified virtual key to ASCII, if possible.
For example VK_A is converted to 'A' (ASCII 0x41), CTRL + VK_SPACE produces ASCII NUL (0x00), CTRL + letter produces ASCII control codes from SOH (0x01) to SUB (0x1A), CTRL + VK_BACKSLASH produces ASCII FS (0x1C), CTRL + VK_QUESTION produces ASCII US (0x1F), CTRL + VK_LEFTBRACKET produces ASCII ESC (0x1B), CTRL + VK_RIGHTBRACKET produces ASCII GS (0x1D), CTRL + VK_TILDE produces ASCII RS (0x1E) and VK_SCROLLLOCK produces XON or XOFF.
virtualKey | The virtual key to convert. |