gc9a01_mpy MicroPython Driver

repository·main·Indexed 42 days ago

https://github.com/russhughes/gc9a01_mpy

Drivers and utilities for the GC9A01 display controller optimized for MicroPython. Features support for vector fonts via the Draw method, bitmapped fonts via the Text method, and pre-compiled firmware for ESP32, RP2040, and RP2350 devices. Includes utilities for custom font conversion using font_from_romfont and support for rendering JPG images, arcs, and sprites.

Tokens
10.7K
Snippets
50
Records
68
Agent score
52%

What's inside gc9a01_mpy

  1. Compare Bitmap and TrueType fonts

    main

    The driver supports two types of fonts: fixed-size Bitmap fonts and TrueType fonts. Choose between them based on your rendering needs and memory constraints.

    AttributeBitmap FontsTrueType Fonts
    SourcePC BIOS imagesTrue-Type fonts
    Conversion Tooltext_utilwrite_font_converter.py
    Character Sets128 and 256 PC character setsUser defined
    Pixel Sizes8x8, 8x16, 16x6, and 16x32width 256 pixels or less
    Rendering Methodtext() methodwrite() method
  2. Configure ESP32-S3-LCD-1.28

    main

    To use the Waveshare ESP32-S3-LCD-1.28 display, use the pre-defined configuration files located in tft_configs/ESP32-S3-LCD-1.28/.

    Hardware Details:

    Configuration Files:

    • tft_config.py: Contains the display driver initialization parameters (pins, SPI settings, etc.).
    • tft_buttons.py: Contains the button mapping/configuration for this specific board.
  3. Convert PC BIOS bitmap fonts using text_font_converter.py

    main

    Use the text_font_converter.py utility (found in the utils directory) to convert PC BIOS bitmap fonts from the romfont repository into Python font modules.

    Options:

    • -i, --input-directory: The directory containing the source .bin files.
    • -o, --output-directory: The directory where the converted Python font files will be written.
    • -f, --first-char: Limit the character set by specifying the first character.
    • -l, --last-char: Limit the character set by specifying the last character.
    text_font_converter.py -i font-bin -o fonts -f 32 -l 127
  4. Cross-compile and upload image modules to the board

    main

    Once you have generated a .py bitmap module, you should cross-compile it to a .mpy file for efficient use on the microcontroller, then upload it to the board using mpremote.

    Follow these steps:

    1. Cross-compile the module using mpy-cross.
    2. Upload the resulting .mpy file to the board's root directory using mpremote cp.
    # cross compile the python module
    mpy-cross cat_bitmap.py
    
    # upload the compiled module to the board
    mpremote cp cat_bitmap.mpy :
  5. Use bit mapped fonts with the Text method

    main

    The library supports bit mapped fonts for use with the Text method. These fonts are categorized into 128-character and 256-character sets.

    Frozen Fonts (Included in firmware): These fonts are pre-compiled and available directly from ROM, requiring no extra file management on your filesystem:

    • vga1_8x8.py
    • vga1_16x16.py
    • vga1_16x32.py
    • vga1_bold_16x16.py
    • vga1_bold_16x32.py

    Non-Frozen Fonts (External files): These fonts are not included in the firmware and must be provided on your filesystem:

    • vga2_8x8.py
    • vga2_8x16.py
    • vga2_16x16.py
    • vga2_16x32.py
    • vga2_bold_16x16.py
    • vga2_bold_16x32.py
  6. Identify compiled firmware for ESP32 devices

    main

    The repository provides pre-compiled .bin firmware files for various ESP32-based boards. When selecting a firmware, ensure you match your specific board model and memory configuration (e.g., Flash size like 4MiB, 8MiB, etc., or whether your board uses SPIRAM or is a UNICORE variant).

    Supported ESP32 board families include:

    • ARDUINO_NANO_ESP32
    • ESP32_GENERIC (supports various Flash sizes: 4MiB, 8MiB, 16MiB, 32MiB; and variants: SPIRAM, UNICORE)
    • ESP32_GENERIC_C3
    • ESP32_GENERIC_S2
    • ESP32_GENERIC_S3 (supports SPIRAM_OCT variants)
    • LILYGO_TTGO_LORA32
    • LOLIN_C3_MINI, LOLIN_S2_MINI, LOLIN_S2_PICO
    • M5STACK_ATOM
    • OLIMEX_ESP32_POE
    • SIL_WESP32
    • UM_FEATHERS series (FEATHERS2, FEATHERS2NEO, FEATHERS3, NANOS3, PROS3, TINYPICO, TINYS2, TINYS3, TINYWATCHS3)
  7. Optimize font memory usage

    main

    Fonts are stored as Python modules and imported using standard import statements. To reduce memory consumption on MicroPython devices:

    1. Pre-compile: Pre-compile the font Python modules into .mpy files.
    2. Freeze: For maximum savings, convert font files to frozen bytecode and compile them directly into the MicroPython firmware.
  8. Convert TrueType fonts to Python bitmap modules using write_font_converter.py

    main

    The write_font_converter.py utility converts characters from a TrueType font (.ttf) into a Python module containing bitmap data. This generated module can then be used with the bitmap or write methods in drivers like st7789 and ili9342.

    To use the generated module, import it into your project and pass it to the tft.write() method.

    # Example: Convert Chango-Regular.ttf to a python bitmap module 
    # with approximately 32 pixel high characters for the range 0x20-0x7f
    ./write_font_converter.py Chango-Regular.ttf 32 -c 0x20-0x7f > chango_32.py
  9. Initialize the GC9A01 display

    main

    Create a GC9A01 instance by providing the SPI bus and display dimensions. You must also provide the dc (data/command) pin. Other pins like reset, cs, and backlight are optional. You can also specify rotation (0-7) and buffer_size to manage memory usage.

    Memory Management Note: If you specify buffer_size, it must be large enough to hold the largest bitmap, font character, or JPG used (calculated as Rows x Columns x 2 bytes). If not specified, memory is allocated and freed dynamically, which may cause heap fragmentation; enabling garbage collection (GC) is recommended in this case.

    # Example initialization
    display = GC9A01(spi, width=240, height=240, dc=pin_dc, reset=pin_reset, cs=pin_cs)
    display.init()