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

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:

#include "fabgl.h"
fabgl::VGA16Controller DisplayController;
extern "C" void app_main()
{
// default pinout: GPIO 22/21 red, 19/18 green, 5/4 blue, 23 HSync, 15 VSync
DisplayController.begin();
DisplayController.setResolution(VGA_640x480_60Hz);
fabgl::Canvas cv(&DisplayController);
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.
Definition canvas.h:73
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.
#define VGA_640x480_60Hz
Definition fabglconf.h:246

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

fabgl::PS2Controller PS2Controller;
// in app_main(), before using keyboard or 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.
PS/2 device controller.
uint8_t ASCII
Definition fabutils.h:1664
Contains a virtual key, its state and the associated scancode.
Definition fabutils.h:1660

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.