How FabGL is organized
A FabGL application is built from a few independent blocks:
| Block | Main classes | Role |
| Display | fabgl::VGAController, fabgl::VGA16Controller, fabgl::VGATextController, fabgl::CVBS16Controller, ... | Generate the video signal and own the frame buffer |
| Drawing | fabgl::Canvas | Queue drawing primitives (lines, text, bitmaps...) to a display controller |
| Input | fabgl::PS2Controller, fabgl::Keyboard, fabgl::Mouse | Read PS/2 devices through the ULP coprocessor |
| User interface | fabgl::uiApp and the ui* controls, fabgl::InputBox | Windows, controls, dialogs and an event loop |
| Text | fabgl::Terminal | ANSI/VT terminal on top of a display controller |
| Audio | fabgl::SoundGenerator and the waveform generators | Mix several channels into a mono output |
| Games | fabgl::Scene, fabgl::Sprite, fabgl::CollisionDetector | Periodic updates, sprites and collision detection |
A display controller is always the starting point: you create one, call begin() to assign the GPIOs and setResolution() to start the video signal. All the other graphic blocks (Canvas, Terminal, uiApp) receive a pointer to it.
Video and audio signals are generated by DMA and interrupts, so the CPU is only involved in preparing the frame buffer and the audio samples. PS/2 communication runs on the ULP coprocessor, leaving both CPU cores free for the application.
Adding FabGL to a project
FabGL is an ESP-IDF component located in components/fabgl. Point EXTRA_COMPONENT_DIRS to it in the project CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
set(EXTRA_COMPONENT_DIRS "/path/to/FabGL/components/fabgl")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(myapp)
and require it from main/CMakeLists.txt:
idf_component_register(SRCS "main.cpp" REQUIRES fabgl)
The examples use the following sdkconfig.defaults, which is a good starting point for any application:
CONFIG_IDF_TARGET="esp32"
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_COMPILER_OPTIMIZATION_PERF=y
CONFIG_FREERTOS_HZ=1000
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n
CONFIG_ULP_COPROC_ENABLED=y
CONFIG_ULP_COPROC_TYPE_FSM=y
CONFIG_ULP_COPROC_RESERVE_MEM=4096
CONFIG_FREERTOS_HZ=1000 gives millisecond resolution to the timeouts used by FabGL, and running app_main() on CPU 1 keeps the application away from the core that generates the video signal. The ULP settings are required by the PS/2 controller (at least 2112 bytes of ULP memory).
Include the single header fabgl.h to access the whole library. All classes are in the fabgl namespace.
The first program
This program initializes a 16-color VGA output at 640x480 and draws some text:
extern "C" void app_main()
{
DisplayController.
begin();
cv.setBrushColor(Color::Blue);
cv.clear();
cv.setPenColor(Color::BrightYellow);
cv.selectFont(&fabgl::FONT_8x16);
cv.drawText(20, 20, "Hello from FabGL!");
}
Provides a set of drawing methods.
VGA bitmapped display controller with 16 colors.
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).
Single include file of the FabGL library.
Notes:
- Display controllers are usually declared as global objects: they live for the whole application and most of them are singletons (see instance()).
- app_main() can return: the video signal keeps running in the background.
- Colors can be given as named colors (fabgl::Color) or as RGB888 values; they are mapped to the nearest color available on the display.
Adding keyboard and mouse
PS2Controller.
begin(PS2Preset::KeyboardPort0_MousePort1);
auto keyboard = PS2Controller.
keyboard();
if (keyboard->getNextVirtualKey(&item) && item.
down && item.
ASCII)
printf(
"typed '%c'\n", item.
ASCII);
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.
Contains a virtual key, its state and the associated scancode.
See Keyboard and mouse for details.
Where to go next
- The examples in examples/ are complete ESP-IDF projects: build one with idf.py build flash monitor from its folder. Examples describes each of them and how to use it. VGA/HardwareTest is a good way to check a board.
- Choosing and configuring a display controller explains how to choose the display controller and the resolution.
- Graphical user interface shows how to build applications with windows and controls.