TFT_eSPI Graphics Library
repository·master·Indexed 26 days ago
https://github.com/bodmer/tft_espiA high-performance, Arduino-compatible graphics and fonts library optimized for 32-bit processors including ESP32, RP2040, ESP8266, and STM32. It supports SPI and parallel (8/16-bit) interfaces, DMA, smooth anti-aliased fonts, and a Sprite class for flicker-free rendering. The library supports a wide range of display controllers such as ILI9341, ST7789, and GC9A01, and includes support for the XPT2046 touch screen controller.
What's inside TFT_eSPI
- TFT_eSPI is a stand-alone library providing both graphics functions and TFT chip driver support. It is highly optimized for high-performance processors including ESP8266, ESP32, STM32, and RP2040. While it supports other Arduino IDE compatible boards, these will use generic functions that result in slower performance. Note that the library uses 32-bit variables extensively, which may impact performance on 8-bit and 16-bit processors.
Configure Fonts and Library Settings
masterLibrary configuration (font selection, pins, and features) is managed via header files. You can enable or disable specific fonts and features by commenting out lines in the configuration files.
Configuration Methods:
- Edit the
User_Setup.hfile directly in the library folder. - Select a pre-defined configuration by editing the
User_Setup_Select.hfile.
- Edit the
Prepare BMP files for bmp2array4bit in GIMP
masterTo ensure compatibility with
bmp2array4bit.py, you must prepare your image in GIMP (or a similar tool) using these specific settings:- Remove Alpha Channel:
Layer->Transparency->Remove Alpha Channel. - Set Indexed Mode:
Image->Mode->Indexed.... - Palette Selection: Select
Generate optimum palette with 16 colors (max). - Export Settings: Export as a
.bmpfile.
CRITICAL: Do NOT enable the following options during export:
- Run-Length Encoded (RLE)
- Compatibility Options: "Do not write color space information"
Note: Other tools may work as long as they avoid run-length encoding and other advanced compression features.
- Remove Alpha Channel:
Enable ESP8266 SPI overlap mode
masterOn the ESP8266, you can use SPI overlap mode to allow the TFT screen to share the MOSI, MISO, and SCLK pins with the program FLASH. This frees up GPIO pins for other peripherals.
Constraints:
- Only one SPI device can be connected to the FLASH pins.
- The chip select (CS) for the TFT must be on pin D3 (GPIO0).
Configure Touch Controller support
masterThe library supports the XPT2046 touch screen controller for SPI-based displays.
Setup Requirements:
- The touch controller must share the SPI bus with the TFT.
- An additional chip select line is required.
Note: This support is subject to future deprecation when a suitable touch screen library becomes available.
Configure TFT_eSPI for your display and processor
masterTFT_eSPI requires the screen controller, interface pins, and library configuration settings to be defined within the library itself, rather than in your Arduino sketch.
To configure your hardware:
- Open the
User_Setup_Select.hfile within the library folder. - Select the appropriate setup file for your specific display and processor combination.
PlatformIO Users: You can define these settings on a per-project basis within your
platformio.inifile instead of modifying the library files directly.- Open the
Use Anti-aliased (Smooth) Fonts
masterThe library supports smooth, anti-aliased fonts in
.vlwformat. These fonts support 16-bit Unicode characters (e.g., Greek, Japanese) and UTF-8 strings.Implementation Options:
- File System: Upload
.vlwfiles to the processor's FLASH filing system (SPIFFS, LittleFS, or SD card). - C Arrays: Convert
.vlwfiles to C arrays (using theSmooth Font -> FLASH_Arrayexamples) and store them directly in FLASH. This is recommended for processors like STM32 that may not support a FLASH-based filing system.
Important Limitations:
- Anti-aliased fonts cannot be scaled using
setTextSize(). You must create a separate font file for every size you intend to use.
- File System: Upload
Use 8-bit Parallel Displays (Mcufriend shields)
masterThe library supports common 8-bit parallel "Mcufriend" shields for STM Nucleo (64/144) and ESP32 UNO-style boards, as well as STM32 "Blue/Black Pill" boards.
ESP32 UNO Style Hardware Note: Typical UNO/mcufriend boards map
LCD_RD,LCD_CS, andLCD_RSTto ESP32 analog pins (35, 34, 36) which are input-only. To fix this, you may need to wire the spare pins as follows:IO15$\rightarrow$IO35IO33$\rightarrow$IO34IO32$\rightarrow$IO36
An example setup file for this configuration can be found in the repository.
Use Sprites for off-screen graphics
masterSprites are invisible graphics buffers stored in RAM. You can draw graphics to a Sprite and then plot it onto the screen at any position. This is useful for flicker-free animations or creating frame buffers.
Key Characteristics:
- Bit Depth: Supports 16-bit (default), 8-bit (256 colors), or 1-bit (2 colors) to save RAM.
- RAM Usage:
- 16-bit:
(2 * width * height)bytes. - 8-bit:
(width * height)bytes.
- 16-bit:
- ESP32 PSRAM: If your ESP32 has SPIRAM, Sprites will use it, allowing for large full-screen buffers.
- Transparency: Sprites can be plotted to the TFT with one color specified as "transparent".
Refer to the
examples/Spritefolder for usage examples.Manage custom User_Setups during library updates
masterTo prevent your custom configurations from being overwritten when updating the
TFT_eSPIlibrary, store your setup files in a separate directory outside the library folder.Recommended Workflow:
- Create a folder named
TFT_eSPI_Setupsin your Arduino library folder. - Place your custom
.hfiles there. - In the library's
User_Setup_Select.h, point to your custom file using a relative path:
#include <../TFT_eSPI_Setups/my_custom_setup.h>Alternatively, you can create your own
my_setup_select.hfile and pointUser_Setup_Select.hto it to manage multiple configurations without touching the library files directly.- Create a folder named
Convert BMP images to 4-bit sprite arrays using bmp2array4bit.py
masterThe
bmp2array4bit.pyscript converts a BMP file into C or C++ code containing two arrays: one for the image palette and one for the image data itself. This is used for adding images to four-bit sprites in the TFT_eSPI library.Requirements:
- Python 3.6 or higher.
Usage: Run the script from your terminal using the following syntax:
python bmp2array4bit.py [-v] star.bmp [-o myfile.c]-v: Verbose mode (optional).star.bmp: Your input BMP file.-o myfile.c: The name of the output C file.
python bmp2array4bit.py [-v] star.bmp [-o myfile.c]Use SPI Display and SD Card simultaneously
masterIf you are using an SD card on the same SPI bus as your TFT display, you must explicitly define the SPI pins in your
User_setup.hfile, even if you are using the default pins for your board.Required Keys:
_TFT_MISO__TFT_MOSI__TFT_SCLK_
Failure to declare these pins explicitly may result in the SD card becoming unusable after the display is initialized.