LovyanGFX

repository·master·Indexed 23 days ago

https://github.com/lovyan03/lovyangfx

A high-performance, feature-rich graphics library for microcontrollers including ESP32, ESP8266, and SAMD51. It supports various display controllers and interfaces (SPI, I2C, Parallel) and provides API compatibility with AdafruitGFX and TFT_eSPI. Key features include DMA transfer support, off-screen buffers (sprites) with fast rotation and scaling, 16-bit and 24-bit color modes, and PC simulation support via SDL2 or OpenCV. The library also offers a WebAssembly port for browser-based execution.

Tokens
11.8K
Snippets
15
Records
72
Agent score
83%

What's inside LovyanGFX

  1. Overview of LovyanGFX

    master

    LovyanGFX is a high-performance graphics library for displays (LCD, OLED, EPD) compatible with ESP32, ESP8266, and ATSAMD51. It aims to provide higher functional coverage and better performance than AdafruitGFX and TFT_eSPI while maintaining API compatibility with them.

    Key features include:

    • Support for ArduinoESP32 and ESP-IDF.
    • 16-bit and 24-bit color modes.
    • DMA transfer support for background processing during communication.
    • Fast rotation and scaling of off-screen buffers (sprites).
    • Support for multiple simultaneous displays.
    • Automatic color reduction for monochrome displays.
    • PC support via OpenCV or SDL2.
    • Composite video (NTSC, PAL) output on ESP32.
  2. Optimize SPI communication with startWrite and endWrite

    master

    By default, LovyanGFX handles SPI bus acquisition and release automatically for every drawing function. For high-performance applications, you can manually manage the SPI bus to reduce overhead.

    • lcd.startWrite(): Manually acquires the SPI bus. It uses a counter; multiple calls increment the counter, and only the first call actually acquires the bus.
    • lcd.endWrite(): Releases the SPI bus (decrements the counter). Only the last call actually releases the bus.
    • lcd.endTransaction() / lcd.beginTransaction(): Use these to force a bus release/acquisition regardless of the startWrite counter. This is useful if you need to switch to another SPI device (like an SD card) mid-operation.

    Warning: Functions starting with write (e.g., writePixel, writeFastVLine, writeFillRect) do not check the SPI bus state and require an explicit startWrite() call before use.

    // High-speed drawing block
    lcd.startWrite();
    lcd.drawLine(38, 0, 0, 38, 0xFFFF00U);
    lcd.drawLine(39, 1, 1, 39, 0xFF00FFU);
    lcd.endWrite();
    
    // Using high-speed 'write' functions
    lcd.startWrite();
    for (uint32_t x = 0; x < 128; ++x) {
      for (uint32_t y = 0; y < 128; ++y) {
        lcd.writePixel(x, y, lcd.color888(x*2, x + y, y*2));
      }
    }
    lcd.endWrite();
  3. Use Sprites (Off-screen buffers) for advanced graphics

    master

    A Sprite is an off-screen buffer that allows you to draw complex graphics and then push them to the display all at once. This prevents flickering and enables fast rotation/scaling.

    Workflow:

    1. Create a LGFX_Sprite instance, passing the display instance as an argument.
    2. Set the color depth using sprite.setColorDepth(n) (e.g., 8, 16, or 24).
    3. Allocate memory with sprite.createSprite(width, height).
    4. Draw to the sprite using standard drawing functions.
    5. Push the sprite to the display using sprite.pushSprite(x, y) or sprite.pushRotateZoom(...).
    6. Free memory with sprite.deleteSprite() when finished.

    Palette Mode (for 1, 2, 4, or 8-bit depth): In palette mode, drawing functions use a palette index instead of a color. You can define colors for these indices using sprite.setPaletteColor(index, color).

    Rotation and Zoom: sprite.pushRotateZoom(x, y, angle, scale_x, scale_y, pivot_x, pivot_y) allows you to render the sprite with rotation and scaling. Use sprite.setPivot(x, y) to define the center of rotation.

  4. Understand the motivation behind LovyanGFX

    master
    LovyanGFX was developed as an alternative to TFT_eSPI. While TFT_eSPI is a capable library, its multi-architecture support makes its structure complex, making it difficult to add new features like ESP-IDF support or 18-bit color support. LovyanGFX aims to provide these features while optimizing performance.
  5. Configure CVBS signal types and resolutions

    master

    The lgfx::Panel_CVBS supports several signal standards. Choosing the right one affects the maximum supported resolution and black level characteristics.

    Supported Signal Types

    • NTSC (Black level: 7.5IRE)
    • NTSC-J (Black level: 0IRE). Use this if NTSC blacks appear slightly washed out/white.
    • PAL
    • PAL-M
    • PAL-N

    Maximum Resolutions

    Signal TypeMax WidthMax Height
    PAL-M, NTSC, NTSC-J720480
    PAL-N864576
    PAL864576

    Note: You can set any resolution below the maximum. It is recommended to use resolutions that are integer divisions of the maximum (e.g., 720/2, 720/3) to avoid pixel aspect ratio distortion.

  6. Set up PlatformIO for native builds

    master

    To use LovyanGFX in a PC environment (SDL2), you must first configure PlatformIO to support platform = native builds. This requires installing build tools specific to your operating system.

    Linux

    Install build-essential using apt:

    sudo apt update
    sudo apt install build-essential

    macOS

    Install xcode-select via the terminal:

    xcode-select --install

    Windows

    1. Download and install MSYS2 from msys2.org.
    2. Add the following three paths to your Windows System Environment Variables PATH:
    • C:\msys64\mingw32\bin
    • C:\msys64\ucrt64\bin
    • C:\msys64\usr\bin
  7. Build and Run LovyanGFX WebAssembly port

    master

    To build and run the WebAssembly port of LovyanGFX, you need to use the Emscripten toolchain. Follow these steps to compile the project and serve it locally:

    1. Create a build directory: mkdir build
    2. Enter the directory: cd build
    3. Configure the project using Emscripten's CMake wrapper: emcmake cmake ..
    4. Compile the project: emmake make
    5. Serve the files using a local web server (e.g., http-server) to avoid CORS/file protocol issues.
    6. Open index.html in your browser.
    mkdir build
    cd build
    emcmake cmake ..
    emmake make
    http-server
  8. Set up PlatformIO native builds for LovyanGFX on PC

    master

    To use LovyanGFX in a Visual Studio Code + PlatformIO + SDL2 environment, you must first install the necessary compilation tools for the platform = native build type. Follow the instructions for your specific operating system:

    Linux

    Install basic compilation tools using apt:

    sudo apt update
    sudo apt install build-essential

    macOS

    Install Xcode compilation tools using xcode-select:

    xcode-select --install

    Windows

    1. Install MSYS2 from https://www.msys2.org/.
    2. Open the MSYS command window and install the compiler:
    pacman -S mingw-w64-ucrt-x86_64-gcc
    1. Add the following paths to your Windows System Path environment variable:
    • C:\msys64\mingw32\bin
    • C:\msys64\ucrt64\bin
    • C:\msys64\usr\bin
  9. Configure LovyanGFX for supported hardware

    master

    To use LovyanGFX with a supported board, you can either define the specific board macro before including the header or use LGFX_AUTODETECT to automatically identify the board at runtime.

    Using Automatic Detection: Define LGFX_AUTODETECT before including LovyanGFX.hpp. Note that some boards like D-duino-32 XS, WT32-SC01, and PyBadge do not support panel ID reading and are excluded from autodetect.

    Using Specific Board Definitions: If autodetect is not used, define the appropriate macro for your hardware (e.g., LGFX_M5STACK, LGFX_M5STICK_C, LGFX_WIO_TERMINAL, etc.).

    Note for non-ArduinoIDE environments: If you are not using the Arduino IDE or your board is not in the Board Manager, you must define the LGFX_... macro before the #include <LovyanGFX.hpp> line.

    // Define the board before including the header
    #define LGFX_AUTODETECT
    
    #include <LovyanGFX.hpp>
    #include <LGFX_AUTODETECT.hpp>
    
    static LGFX lcd;
    static LGFX_Sprite sprite(&lcd);
  10. Coexist LovyanGFX with M5Stack.h or M5StickC.h

    master

    If you need to use both M5Stack.h (or M5StickC.h) and LovyanGFX.hpp in the same project, use one of the following two methods:

    Method 1: Manual Include Order

    Include <LovyanGFX.hpp> after <M5Stack.h>. Note that you should not use M5.Lcd. Instead, create and use a separate instance of LovyanGFX.

    Method 2: Using ESP32-Chimera-Core

    Use the ESP32-Chimera-Core library. This will automatically make M5.Lcd an instance of LovyanGFX, allowing for seamless integration.

  11. Convert BDF to C using bdfconv

    master

    To convert BDF files into C source files compatible with the u8g2 format used in LovyanGFX, use bdfconv.exe. This process utilizes a mapping file (specifically a modified ja.map based on japanese3.map) to ensure correct character encoding.

    Command Flags:

    • -v: Verbose mode.
    • -b 0: Bit depth/format setting.
    • -f 1: Font format setting.
    • -M "[map_file]": Specifies the character map file (e.g., japanese3.map).
    • -o [output_file].c: Specifies the output C file path.
    • -n [font_name]: Specifies the internal font name used in the C file.

    After conversion, the resulting .c files are typically collected into a single lgfx_font_japan.c file, often requiring a regex replacement of /U8G2_FONT_SECTION\(".*"\) // to match the project's expected format.

    bdfconv.exe -v -b 0 -f 1 -M "japanese3.map" ..\bdf\lgfx_font_japan_mincho_16.bdf -o ..\output\lgfx_font_japan_mincho_16.c -n lgfx_font_japan_mincho_16