My demo code...
// TEST VARS
// ==========================================
int delay_extra = 1000;
int use_touch_register = 1;
int use_touch_callback = 0;
// SD CARD
// ==========================================
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define SPI_SS_SD 5
void init_sd();
void listDir(fs::FS &fs, const char * dirname, uint8_t levels);
// DISPLAY & TOUCH
// ==========================================
#include <lvgl.h>
#include <TFT_eSPI.h> // Install the "TFT_eSPI" library by Bodmer
#include <XPT2046_Touchscreen.h> // Install the "XPT2046_Touchscreen" library by Paul Stoffregen
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
#define SCREEN_WIDTH 320 // TFT_HOR_RES, TFT_VER_RES
#define SCREEN_HEIGHT 240
int x, y, z; // Touchscreen coords: (x, y) and pressure (z)
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
void ui_create(void);
void log_print(lv_log_level_t level, const char * buf);
void touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data);
void ui_init(void);
void setup()
{
// INIT SERIAL
Serial.begin(115200);
delay(101);
Serial.println("INIT");
init_sd();
ui_init();
Serial.println("INIT END");
}
void loop() {
lv_task_handler(); // let the GUI do its work
lv_tick_inc(5); // tell LVGL how much time has passed + delay_extra?
//ldr_echo();
listDir(SD, "/", 1);
delay(delay_extra);
delay(5);
}
// SD CARD
// ==========================================
void init_sd()
{
//if(!SD.begin(5))
//if(!SD.begin())
if(!SD.begin(SPI_SS_SD))
//if(!SD.begin(SPI_SS_SD, spi, 80000000))
{
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
listDir(SD, "/", 1);
Serial.println("------");
listDir(SD, "/", 1);
}
void listDir(fs::FS &fs, const char * dirname, uint8_t levels)
{
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
root.close();
}
// DISPLAY & TOUCH
// ==========================================
void create_panel_home(void)
{
// BODY_MAIN: CREATE
lv_obj_t * c_body_home = lv_obj_create(lv_screen_active());
lv_obj_set_size(c_body_home, 320, 240);
lv_obj_set_flex_flow(c_body_home, LV_FLEX_FLOW_COLUMN);
// TITLE
lv_obj_t *label2 = lv_label_create(c_body_home);
lv_label_set_text_fmt(label2, "HELLO");
lv_obj_center(label2);
}
void ui_create(void)
{
//gen_styles();
//create_menu();
create_panel_home();
}
void log_print(lv_log_level_t level, const char * buf)
{
LV_UNUSED(level);
Serial.println(buf);
Serial.flush();
}
// Get the Touchscreen data
void touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data)
{
// Checks if Touchscreen was touched, and prints X, Y and Pressure (Z)
if(touchscreen.tirqTouched() && touchscreen.touched()) {
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
// Calibrate Touchscreen points with map function to the correct width and height
x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
z = p.z;
data->state = LV_INDEV_STATE_PRESSED;
// Set the coordinates
data->point.x = x;
data->point.y = y;
// Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor
//Serial.print("X = ");
//Serial.print(x);
//Serial.print(" | Y = ");
//Serial.print(y);
//Serial.print(" | Pressure = ");
//Serial.print(z);
//Serial.println();
}
else {
data->state = LV_INDEV_STATE_RELEASED;
}
}
void ui_init(void)
{
// BOOT INFO
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println(LVGL_Arduino);
// INIT LVGL
lv_init();
// Register print function for debugging
lv_log_register_print_cb(log_print);
if(use_touch_register){
// INIT TOUCHSCREEN
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 1: touchscreen.setRotation(1);
touchscreen.setRotation(3);
}
// Create a display object
lv_display_t * disp;
// Initialize the TFT display using the TFT_eSPI library
disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
// Initialize an LVGL input device object (Touchscreen)
lv_indev_t * indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
if(use_touch_register && use_touch_callback){
// Set the callback function to read Touchscreen input
lv_indev_set_read_cb(indev, touchscreen_read);
}
// CREATE UI
ui_create();
}