Skip to content

Instantly share code, notes, and snippets.

@inindev
Created October 10, 2022 11:35
Show Gist options
  • Save inindev/37e79b1d48a31821860c865cf8475e18 to your computer and use it in GitHub Desktop.
Save inindev/37e79b1d48a31821860c865cf8475e18 to your computer and use it in GitHub Desktop.
esp32c3 ili9488 tft proof of concept
#define LGFX_USE_V1
#include <LovyanGFX.hpp>
#include <SPI.h>
// xiao esp32c3
// 2 a0 +---uu---+ 5v
// 3 a1 | uu | gnd
// 4 a2 | | 3v3
// 5 a3 | | mosi 10
// 6 sda | | miso 9
// 7 scl | b r | sck 8
// 21 tx +--------+ cs 20
#define LCD_DC 3
#define LCD_RST 4
#define LCD_BL 21
#define LCD_MOSI 10
#define LCD_MISO 9
#define LCD_SCK 8
#define LCD_CS 20
class LGFX : public lgfx::LGFX_Device
{
// lgfx::Panel_ILI9341 _panel_instance;
lgfx::Panel_ILI9488 _panel_instance;
lgfx::Bus_SPI _bus_instance;
public:
// Create a constructor and set various settings here.
// If you change the class name, specify the same name for the constructor.
LGFX(void)
{
{ // Make bus control setting
auto cfg = _bus_instance.config(); // Get a structure for bus settings
// SPI bus settings
cfg.spi_host = SPI2_HOST; // Select SPI to use (VSPI_HOST or HSPI_HOST)
cfg.spi_mode = 0; // Set SPI communication mode (0 ~ 3)
cfg.freq_write = 20000000; // SPI clock for transmission (up to 80MHz, rounded to the value obtained by dividing 80MHz by an integer)
cfg.freq_read = 16000000; // SPI clock when receiving
cfg.spi_3wire = true; // Set to true if reception is done on the MOSI pin
cfg.use_lock = true; // Set true to use transaction lock
cfg.dma_channel = 1; // Set the DMA channel (1 or 2. 0=disable)
cfg.pin_sclk = LCD_SCK; // Set SPI SCLK pin number
cfg.pin_mosi = LCD_MOSI; // Set SPI MOSI pin number
// cfg.pin_miso = LCD_MISO; // Set SPI MISO pin number (-1 = disable)
cfg.pin_miso = -1; // Set SPI MISO pin number (-1 = disable)
cfg.pin_dc = LCD_DC; // Set SPI D/C pin number (-1 = disable)
// When using the same SPI bus as the SD card, be sure to set MISO without omitting it.
_bus_instance.config(cfg); // Applies the set value to the bus
_panel_instance.setBus(&_bus_instance); // Set the bus on the panel
}
{ // Set the display panel control
auto cfg = _panel_instance.config(); // Gets the structure for display panel settings
cfg.pin_cs = LCD_CS; // Pin number to which CS is connected (-1 = disable)
cfg.pin_rst = LCD_RST; // Pin number to which RST is connected (-1 = disable)
cfg.pin_busy = -1; // Pin number to which BUSY is connected (-1 = disable)
// The following setting values are general initial values for each panel, so please try commenting out any unknown items.
cfg.memory_width = 320; // Maximum width supported by the driver IC
cfg.memory_height = 480; // Maximum height supported by the driver IC
cfg.panel_width = 320; // actual displayable width
cfg.panel_height = 480; // actual visible height
cfg.offset_x = 0; // Panel offset amount in X direction
cfg.offset_y = 0; // Panel offset amount in Y direction
cfg.offset_rotation = 0; // Rotation direction value offset 0~7 (4~7 is upside down)
cfg.dummy_read_pixel = 8; // Number of bits for dummy read before pixel readout
cfg.dummy_read_bits = 1; // Number of bits for dummy read before non-pixel data read
cfg.readable = true; // Set to true if data can be read
cfg.invert = false; // Set to true if the light and dark of the panel is inverted
cfg.rgb_order = false; // Set to true if the panel's red and blue are swapped
cfg.dlen_16bit = false; // Set to true for panels that send data length in 16-bit units
cfg.bus_shared = true; // If the bus is shared with the SD card, set to true (bus control with drawJpgFile etc)
_panel_instance.config(cfg);
}
setPanel(&_panel_instance); // Sets the panel to use
}
};
LGFX tft;
uint8_t rot = 2;
uint16_t color_arr[] = {TFT_WHITE, TFT_RED, TFT_ORANGE, TFT_YELLOW, TFT_GREEN, TFT_BLUE, TFT_VIOLET, TFT_NAVY, TFT_BLACK};
uint8_t color_arr_len = sizeof(color_arr)/sizeof(uint16_t);
void testLines(uint16_t color)
{
int32_t x1, y1, x2, y2;
int32_t w = tft.width();
int32_t h = tft.height();
tft.fillScreen(TFT_BLACK);
x1 = y1 = 0;
y2 = h - 1;
for (x2 = 0; x2 < w; x2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
x2 = w - 1;
for (y2 = 0; y2 < h; y2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
tft.fillScreen(TFT_BLACK);
x1 = w - 1;
y1 = 0;
y2 = h - 1;
for (x2 = 0; x2 < w; x2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
x2 = 0;
for (y2 = 0; y2 < h; y2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
tft.fillScreen(TFT_BLACK);
x1 = 0;
y1 = h - 1;
y2 = 0;
for (x2 = 0; x2 < w; x2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
x2 = w - 1;
for (y2 = 0; y2 < h; y2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
tft.fillScreen(TFT_BLACK);
x1 = w - 1;
y1 = h - 1;
y2 = 0;
for (x2 = 0; x2 < w; x2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
x2 = 0;
for (y2 = 0; y2 < h; y2 += 6)
{
tft.drawLine(x1, y1, x2, y2, color);
}
}
void showMsg(uint8_t rot) {
tft.fillScreen(TFT_YELLOW);
tft.setRotation(rot);
tft.setTextColor(TFT_CYAN, TFT_NAVY);
tft.setTextSize(3); // 18x28
tft.fillRect(80, 160, 160, 160, TFT_NAVY);
tft.setCursor(117, 190);
tft.println("TOUCH");
tft.setCursor(144, 230);
tft.println("TO");
tft.setCursor(90, 270);
tft.println("CONTINUE");
}
void setup() {
Serial.begin(115200);
tft.init();
showMsg(rot);
}
void loop() {
while(Serial.available() < 1) {
delay(100);
}
int val = Serial.read();
switch(val) {
case 'a':
rot++;
if(rot > 3) rot = 0;
Serial.print("rotate screen: ");
Serial.println(rot);
showMsg(rot);
break;
case 'b':
Serial.println("fill screen blue");
tft.fillScreen(TFT_BLUE);
break;
case 'c':
Serial.println("fill screen cyan");
tft.fillScreen(TFT_CYAN);
break;
case 'f':
Serial.println("fill screen color array");
for(uint8_t i=0; i<color_arr_len; ++i) {
tft.fillScreen(color_arr[i]);
}
for(uint8_t i=color_arr_len; i>0; --i) {
tft.fillScreen(color_arr[i-1]);
}
break;
case 'g':
Serial.println("fill screen green");
tft.fillScreen(TFT_GREEN);
break;
case 'l':
Serial.println("lines test");
for(uint8_t i=1; i<color_arr_len-1; ++i) {
testLines(color_arr[i]);
}
for(uint8_t i=color_arr_len-2; i>0; --i) {
testLines(color_arr[i]);
}
break;
case 'm':
Serial.println("fill screen magenta");
tft.fillScreen(TFT_MAGENTA);
break;
case 'o':
Serial.println("fill screen orange");
tft.fillScreen(TFT_ORANGE);
break;
case 'p':
Serial.println("fill screen pink");
tft.fillScreen(TFT_PINK);
break;
case 'r':
Serial.println("fill screen red");
tft.fillScreen(TFT_RED);
break;
case 's':
Serial.println("fill screen sky blue");
tft.fillScreen(TFT_SKYBLUE);
break;
case 'v':
Serial.println("fill screen violet");
tft.fillScreen(TFT_VIOLET);
break;
case 'w':
Serial.println("fill screen white");
tft.fillScreen(TFT_WHITE);
break;
case 'x':
Serial.println("fill screen black");
tft.fillScreen(TFT_BLACK);
break;
case 'y':
Serial.println("fill screen yellow");
tft.fillScreen(TFT_YELLOW);
break;
case '\r':
case '\n':
break;
default:
Serial.print("unassigned option: ");
if(val > 32 && val < 127) {
Serial.println((char)val);
} else {
Serial.println(val);
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment