FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
LoopbackTerminal

A VT/ANSI terminal connected to itself: whatever you type on the PS/2 keyboard is sent back to the terminal, so you can try the terminal escape sequences without any host computer.

At startup the screen (VGA text mode, 80x25) shows the screen and terminal size, the keyboard status and the free memory.

What it demonstrates

  • fabgl::Terminal in local mode (Terminal.connectLocally(), then available() / read() / write())
  • fabgl::VGATextController: the text-only VGA mode with the smallest memory footprint
  • Handling of DEL (backspace + erase to end of line) and CR (CR + LF) in the loopback

Hardware

  • VGA output (default pins)
  • PS/2 keyboard on port 0 (CLK = GPIO33, DATA = GPIO32)

Usage

Just type. To send an escape sequence, press ESC and then type the rest of the sequence. Examples:

Type Effect
ESC [2J Clear the screen
ESC [1;1H Move the cursor to the top left corner
ESC [31m Red foreground (30...37 normal colors, 90...97 bright colors)
ESC [44m Blue background
ESC [0m Reset the attributes
ESC [5m Blinking text

The full list of supported sequences is in the Terminal documentation.

Build

See Building the examples.

Source code

Main file of the example, main/LoopbackTerminal.cpp. The complete project is in the examples/VGA/LoopbackTerminal folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 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
30
31
32#include "fabgl.h"
33
34
35
36
37fabgl::VGATextController DisplayController;
38fabgl::PS2Controller PS2Controller;
39fabgl::Terminal Terminal;
40
41
42void print_info()
43{
44 Terminal.write("\e[37m* * FabGL - Loopback VT/ANSI Terminal\r\n");
45 Terminal.write("\e[34m* * 2019-2026 by Fabrizio Di Vittorio - www.fabgl.com\e[32m\r\n\n");
46 Terminal.printf("\e[32mScreen Size :\e[33m %d x %d\r\n", DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight());
47 Terminal.printf("\e[32mTerminal Size :\e[33m %d x %d\r\n", Terminal.getColumns(), Terminal.getRows());
48 Terminal.printf("\e[32mKeyboard :\e[33m %s\r\n", PS2Controller.keyboard()->isKeyboardAvailable() ? "OK" : "Error");
49 Terminal.printf("\e[32mFree DMA Memory :\e[33m %d\r\n", heap_caps_get_free_size(MALLOC_CAP_DMA));
50 Terminal.printf("\e[32mFree 32 bit Memory :\e[33m %d\r\n\n", heap_caps_get_free_size(MALLOC_CAP_32BIT));
51 Terminal.write("\e[32mFree typing test - press ESC to introduce escape VT/ANSI codes\r\n\n");
52}
53
54
55void setup()
56{
57
58 PS2Controller.begin(PS2Preset::KeyboardPort0);
59
60 DisplayController.begin();
61 DisplayController.setResolution();
62
63 Terminal.begin(&DisplayController);
64 Terminal.connectLocally(); // to use Terminal.read(), available(), etc..
65
66 Terminal.setBackgroundColor(Color::Black);
67 Terminal.setForegroundColor(Color::BrightGreen);
68 Terminal.clear();
69
70 print_info();
71
72 Terminal.enableCursor(true);
73
74 /*
75 // do you want sound on key press? ;-)
76 Terminal.onVirtualKey = [&](VirtualKey * vk, bool keyDown) {
77 if (keyDown)
78 Terminal.soundGenerator()->playSound(VICNoiseGenerator(), (*vk == VirtualKey::VK_RETURN ? 90 : 120), 10);;
79 Terminal.soundGenerator()->playSound(SquareWaveformGenerator(), (*vk == VirtualKey::VK_RETURN ? 1000 : 2000), 4);;
80 };
81 */
82}
83
84
85void loop()
86{
87 if (Terminal.available()) {
88 char c = Terminal.read();
89 switch (c) {
90 case 0x7F: // DEL -> backspace + ESC[K
91 Terminal.write("\b\e[K");
92 break;
93 case 0x0D: // CR -> CR + LF
94 Terminal.write("\r\n");
95 break;
96 default:
97 Terminal.write(c);
98 break;
99 }
100 }
101}
102
103
104extern "C" void app_main()
105{
106 setup();
107 while (true)
108 loop();
109}
int getViewPortHeight()
Determines the height of the viewport.
int getViewPortWidth()
Determines the width of the viewport.
bool isKeyboardAvailable()
Checks whether the keyboard has been detected and correctly initialized.
Definition keyboard.h:168
static Keyboard * keyboard()
Returns the Keyboard object automatically created by PS2Controller.
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes the PS/2 device controller.
PS/2 device controller.
void connectLocally()
Allows the terminal to be used locally.
Definition terminal.cpp:439
int getRows()
Returns the number of lines.
Definition terminal.h:1075
void clear(bool moveCursor=true)
Clears the screen.
Definition terminal.cpp:797
int available()
Gets the number of codes available in the keyboard queue.
int getColumns()
Returns the number of columns.
Definition terminal.h:1068
size_t write(const uint8_t *buffer, size_t size)
Sends the specified number of codes to the display.
int read()
Reads codes from keyboard.
void enableCursor(bool value)
Enables or disables the cursor.
Definition terminal.cpp:902
void setBackgroundColor(Color color, bool setAsDefault=true)
Sets the background color.
Definition terminal.cpp:707
bool begin(BaseDisplayController *displayController, int maxColumns=-1, int maxRows=-1, Keyboard *keyboard=nullptr)
Initializes the terminal.
Definition terminal.cpp:307
void setForegroundColor(Color color, bool setAsDefault=true)
Sets the foreground color.
Definition terminal.cpp:727
An ANSI-VT100 compatible display terminal.
Definition terminal.h:883
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
void setResolution(char const *modeline=nullptr, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets the fixed resolution.
VGA text-only display controller.
Single include file of the FabGL library.