#include <LovyanGFX.hpp>
// Configuration example for using LovyanGFX with custom settings on ESP32
/// Create a class derived from LGFX_Device to configure custom settings.
class LGFX : public lgfx::LGFX_Device
{
/*
You can change the class name from "LGFX" to another name if you wish.
If using with AUTODETECT, change the name to something other than "LGFX" as "LGFX" is already in use.
Also, when using multiple panels simultaneously, give each a different name.
Note: If you change the class name, you also need to change the constructor name to the same name.
You can freely decide how to name it, but considering cases where settings might increase,
for example, if you've configured SPI connection with ILI9341 on ESP32 DevKit-C,
LGFX_DevKitC_SPI_ILI9341
naming it like this and matching the file name with the class name can make it less confusing when using.
//*/
// Prepare an instance that matches the type of panel you're connecting.
lgfx::Panel_GC9A01 _panel_instance;
//lgfx::Panel_GDEW0154M09 _panel_instance;
//lgfx::Panel_HX8357B _panel_instance;
//lgfx::Panel_HX8357D _panel_instance;
//lgfx::Panel_ILI9163 _panel_instance;
// lgfx::Panel_ILI9341 _panel_instance;
//lgfx::Panel_ILI9342 _panel_instance;
//lgfx::Panel_ILI9481 _panel_instance;
//lgfx::Panel_ILI9486 _panel_instance;
//lgfx::Panel_ILI9488 _panel_instance;
//lgfx::Panel_IT8951 _panel_instance;
//lgfx::Panel_RA8875 _panel_instance;
//lgfx::Panel_SH110x _panel_instance; // SH1106, SH1107
//lgfx::Panel_SSD1306 _panel_instance;
//lgfx::Panel_SSD1327 _panel_instance;
//lgfx::Panel_SSD1331 _panel_instance;
//lgfx::Panel_SSD1351 _panel_instance; // SSD1351, SSD1357
//lgfx::Panel_SSD1963 _panel_instance;
//lgfx::Panel_ST7735 _panel_instance;
//lgfx::Panel_ST7735S _panel_instance;
//lgfx::Panel_ST7789 _panel_instance;
//lgfx::Panel_ST7796 _panel_instance;
// Prepare an instance that matches the type of bus for connecting the panel.
lgfx::Bus_SPI _bus_instance; // SPI bus instance
//lgfx::Bus_I2C _bus_instance; // I2C bus instance
//lgfx::Bus_Parallel8 _bus_instance; // 8-bit parallel bus instance
// Prepare an instance for backlight control if possible. (Delete if not needed)
// lgfx::Light_PWM _light_instance;
// Prepare an instance that matches the type of touch screen. (Delete if not needed)
//lgfx::Touch_CST816S _touch_instance;
// lgfx::Touch_FT5x06 _touch_instance; // FT5206, FT5306, FT5406, FT6206, FT6236, FT6336, FT6436
//lgfx::Touch_GSL1680E_800x480 _touch_instance; // GSL_1680E, 1688E, 2681B, 2682B
//lgfx::Touch_GSL1680F_800x480 _touch_instance;
//lgfx::Touch_GSL1680F_480x272 _touch_instance;
//lgfx::Touch_GSLx680_320x320 _touch_instance;
//lgfx::Touch_GT911 _touch_instance;
//lgfx::Touch_STMPE610 _touch_instance;
//lgfx::Touch_TT21xxx _touch_instance; // TT21100
//lgfx::Touch_XPT2046 _touch_instance;
public:
// Create a constructor and configure various settings here.
// If you changed the class name, specify the same name for the constructor as well.
LGFX(void)
{
{ // Configure bus control.
auto cfg = _bus_instance.config(); // Get the structure for bus configuration.
// SPI bus settings
cfg.spi_host = VSPI_HOST; // Select the SPI to use ESP32-S2,C3 : SPI2_HOST or SPI3_HOST / ESP32 : VSPI_HOST or HSPI_HOST
// Note: Due to ESP-IDF version updates, the use of VSPI_HOST and HSPI_HOST is deprecated. If you get an error, use SPI2_HOST or SPI3_HOST instead.
cfg.spi_mode = 0; // Set SPI communication mode (0 ~ 3)
cfg.freq_write = 40000000; // SPI clock when sending (max 80MHz, rounded to an integer division of 80MHz)
cfg.freq_read = 16000000; // SPI clock when receiving
cfg.spi_3wire = true; // Set true if receiving on the MOSI pin
cfg.use_lock = true; // Set true if using transaction lock
cfg.dma_channel = SPI_DMA_CH_AUTO; // Set DMA channel to use (0=DMA not used / 1=1ch / 2=ch / SPI_DMA_CH_AUTO=automatic setting)
// Note: Due to ESP-IDF version updates, SPI_DMA_CH_AUTO (automatic setting) is recommended for DMA channels. Specifying 1ch or 2ch is deprecated.
cfg.pin_sclk = 18; // Set the pin number for SPI's SCLK
cfg.pin_mosi = 23; // Set the pin number for SPI's MOSI
// cfg.pin_miso = 19; // Set the pin number for SPI's MISO (-1 = disable)
cfg.pin_dc = 27; // Set the pin number for SPI's D/C (-1 = disable)
// When using a shared SPI bus with an SD card, be sure to set MISO and don't omit it.
//*/
/*
// I2C bus settings
cfg.i2c_port = 0; // Select the I2C port to use (0 or 1)
cfg.freq_write = 400000; // Clock when sending
cfg.freq_read = 400000; // Clock when receiving
cfg.pin_sda = 21; // Pin number connected to SDA
cfg.pin_scl = 22; // Pin number connected to SCL
cfg.i2c_addr = 0x3C; // I2C device address
//*/
/*
// 8-bit parallel bus settings
cfg.i2s_port = I2S_NUM_0; // Select the I2S port to use (I2S_NUM_0 or I2S_NUM_1) (Uses ESP32's I2S LCD mode)
cfg.freq_write = 20000000; // Transmission clock (max 20MHz, rounded to an integer division of 80MHz)
cfg.pin_wr = 4; // Pin number connected to WR
cfg.pin_rd = 2; // Pin number connected to RD
cfg.pin_rs = 15; // Pin number connected to RS(D/C)
cfg.pin_d0 = 12; // Pin number connected to D0
cfg.pin_d1 = 13; // Pin number connected to D1
cfg.pin_d2 = 26; // Pin number connected to D2
cfg.pin_d3 = 25; // Pin number connected to D3
cfg.pin_d4 = 17; // Pin number connected to D4
cfg.pin_d5 = 16; // Pin number connected to D5
cfg.pin_d6 = 27; // Pin number connected to D6
cfg.pin_d7 = 14; // Pin number connected to D7
//*/
_bus_instance.config(cfg); // Reflect the settings to the bus.
_panel_instance.setBus(&_bus_instance); // Set the bus to the panel.
}
{ // Configure display panel control.
auto cfg = _panel_instance.config(); // Get the structure for display panel configuration.
cfg.pin_cs = 15; // Pin number connected to CS (-1 = disable)
cfg.pin_rst = 22; // Pin number connected to RST (-1 = disable)
cfg.pin_busy = -1; // Pin number connected to BUSY (-1 = disable)
// Note: The following setting values have typical initial values set for each panel, so comment out and try unknown items.
cfg.panel_width = 240; // Actually displayable width
cfg.panel_height = 240; // Actually displayable height
cfg.offset_x = 0; // Panel X direction offset amount
cfg.offset_y = 0; // Panel Y direction offset amount
cfg.offset_rotation = 0; // Offset of rotation direction value 0~7 (4~7 is upside down)
cfg.dummy_read_pixel = 8; // Number of dummy read bits before reading pixel data
cfg.dummy_read_bits = 1; // Number of dummy read bits before reading data other than pixel data
cfg.readable = false; // Set to true if data can be read
cfg.invert = false; // Set to true if panel brightness is inverted
cfg.rgb_order = false; // Set to true if panel red and blue are swapped
cfg.dlen_16bit = false; // Set to true for panels that send data in 16-bit units in 16-bit parallel or SPI
cfg.bus_shared = false; // Set to true if bus is shared with SD card (bus control will be done in drawJpgFile etc.)
// Set the following only for drivers with variable pixel count like ST7735 or ILI9163 when the display is misaligned.
// cfg.memory_width = 240; // Maximum width supported by the driver IC
// cfg.memory_height = 320; // Maximum height supported by the driver IC
_panel_instance.config(cfg);
}
//*
//*/
//*
//*/
setPanel(&_panel_instance); // Set the panel to use.
}
};
// Create an instance of the prepared class.
LGFX display;
void setup(void)
{
// Initialize the SPI bus and panel to make it usable.
display.init();
display.setTextSize((std::max(display.width(), display.height()) + 255) >> 8);
// Perform calibration if touch is available. (Can be omitted)
display.fillScreen(TFT_BLACK);
}
uint32_t count = ~0;
void loop(void)
{
display.startWrite();
display.setRotation(++count & 7);
display.setColorDepth((count & 8) ? 16 : 24);
display.setTextColor(TFT_WHITE);
display.drawNumber(display.getRotation(), 16, 0);
/*
display.setTextColor(0xFF0000U);
display.drawString("R", 30, 16);
display.setTextColor(0x00FF00U);
display.drawString("G", 40, 16);
display.setTextColor(0x0000FFU);
display.drawString("B", 50, 16);
display.drawRect(30,30,display.width()-60,display.height()-60,count*7);
display.drawFastHLine(0, 0, 10);
*/
display.endWrite();
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1