Description
What MCU/Processor/Board and compiler are you using? esp32
What LVGL version are you using? v8.3.8
What do you want to achieve? multiple displays with one screen each and update text on each one
What have you tried so far? setting one screen for each display but can’t update content on screen 0 and screen 1 only screen 2 works
Code to reproduce
The code block(s) should be formatted like:
#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <lvgl.h>
#include <ui/ui.h>
#define SPI_FREQUENCY 27000000
#define NUM_DISPLAYS 3
#define BUFFER_SIZE (240 * 240 / 13)
#define DISPLAY_CHECK_INTERVAL 100 // Interval for checking display status in ms
static const uint16_t screenWidth = 240;
static const uint16_t screenHeight = 240;
// Structures for each display
TFT_eSPI lcds[NUM_DISPLAYS] = {TFT_eSPI(), TFT_eSPI(), TFT_eSPI()};
static lv_color_t draw_bufs[NUM_DISPLAYS][BUFFER_SIZE];
static lv_color_t draw_bufs2[NUM_DISPLAYS][BUFFER_SIZE];
lv_disp_draw_buf_t lv_draw_bufs[NUM_DISPLAYS];
lv_disp_drv_t lv_disp_drivers[NUM_DISPLAYS];
lv_disp_t* displays[NUM_DISPLAYS];
int cs_pins[NUM_DISPLAYS] = {16, 4, 17}; // CS pins for 3 displays
static int display_indices[NUM_DISPLAYS] = {0, 1, 2}; // Indices for displays
static bool display_initialized[NUM_DISPLAYS] = {false, false, false};
void selectDisplay(int csPin) {
if (csPin < 0) {
Serial.println("CS Pin invalido");
return;
}
for (int i = 0; i < NUM_DISPLAYS; i++) {
if (cs_pins[i] >= 0) {
digitalWrite(cs_pins[i], HIGH);
}
}
digitalWrite(csPin, LOW);
Serial.println(csPin);
}
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
if (!disp || !disp->user_data || !area || !color_p) {
Serial.println("Invalid flush parameters");
return;
}
int index = *(int *)(disp->user_data);
if (index < 0 || index >= NUM_DISPLAYS) {
Serial.println("Invalid display index");
lv_disp_flush_ready(disp);
return;
}
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
if (w == 0 || h == 0 || w > screenWidth || h > screenHeight) {
Serial.println("Invalid area dimensions");
lv_disp_flush_ready(disp);
return;
}
Serial.println(index);
selectDisplay(cs_pins[index]);
lcds[index].startWrite();
lcds[index].setAddrWindow(area->x1, area->y1, w, h);
lcds[index].pushColors((uint16_t *)&color_p->full, w * h, true);
lcds[index].endWrite();
lv_disp_flush_ready(disp);
}
bool setup_display(int index) {
if (index < 0 || index >= NUM_DISPLAYS) {
Serial.println("Invalid display index in setup");
return false;
}
// pinMode(cs_pins[index], OUTPUT);
digitalWrite(cs_pins[index], LOW);
lcds[index].begin();
lcds[index].setRotation(0);
lcds[index].fillScreen(TFT_BLACK);
// Verify buffer allocation
if (!draw_bufs[index] || !draw_bufs2[index]) {
Serial.println("Buffer allocation failed");
return false;
}
lv_disp_draw_buf_init(&lv_draw_bufs[index], draw_bufs[index], draw_bufs2[index], BUFFER_SIZE);
lv_disp_drv_init(&lv_disp_drivers[index]);
lv_disp_drivers[index].hor_res = screenWidth;
lv_disp_drivers[index].ver_res = screenHeight;
lv_disp_drivers[index].flush_cb = my_disp_flush;
lv_disp_drivers[index].draw_buf = &lv_draw_bufs[index];
lv_disp_drivers[index].user_data = &display_indices[index];
// Register display driver and save display handle
displays[index] = lv_disp_drv_register(&lv_disp_drivers[index]);
if (!displays[index]) {
Serial.println("Display driver registration failed");
return false;
}
display_initialized[index] = true;
return true;
}
void setup() {
Serial.begin(115200);
while (!Serial) delay(10); // Wait until Serial is ready
pinMode(4, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
digitalWrite(4, HIGH);
digitalWrite(16, HIGH);
digitalWrite(17, HIGH);
lv_init();
// Initialize each display and register them
for (int i = 0; i < NUM_DISPLAYS; i++) {
if (!setup_display(i)) {
Serial.println("Display setup failed");
continue;
}
// Use lv_disp_set_default() with the correct display handle
lv_disp_set_default(displays[i]);
// Initialize UI screens here for each display
ui_init();
// Load different screens on each display
if (i == 0) {
lv_disp_load_scr(ui_Screen1); // Load Screen1 on Display 0
} else if (i == 1) {
lv_disp_load_scr(ui_Screen2); // Load Screen2 on Display 1
} else if (i == 2) {
lv_disp_load_scr(ui_Screen3); // Load Screen3 on Display 2
}
}
}
void loop() {
lv_disp_set_default(displays[0]);
lv_label_set_text(ui_rpm, (String(800)).c_str());
lv_timer_handler();
lv_disp_set_default(displays[1]);
lv_label_set_text(ui_rpm, (String(800)).c_str());
lv_timer_handler();
lv_disp_set_default(displays[2]);
lv_label_set_text(ui_rpm, (String(800)).c_str());
lv_timer_handler();
}