Skip to content

Instantly share code, notes, and snippets.

@adrenalinehit
Created September 12, 2024 21:10
Show Gist options
  • Save adrenalinehit/c8e6efa1211cc51d552125c8a5a67bf1 to your computer and use it in GitHub Desktop.
Save adrenalinehit/c8e6efa1211cc51d552125c8a5a67bf1 to your computer and use it in GitHub Desktop.
/**
* @file lv_images.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2024-01-22
* @note Place the images file in the data directory into the SD card
*/
#include <LilyGo_RGBPanel.h>
#include <LV_Helper.h>
#if !LV_USE_PNG || !LV_USE_BMP || !LV_USE_SJPG
#error "lvgl png , bmp , sjpg decoder library is not enable!"
#endif
LilyGo_RGBPanel panel;
const char *filename[] = {}; //<-- i hope that initialisation works...
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;
}
int fileCount=0;
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
listDir(fs, file.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
//add the file that we've found on the sdcard into the array
Append(filename /*the pointer to the array up top*/, fileCount /*what number file we're up to*/, file.name /*the file name hopefully with path?*/)
fileCount++;
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
/*https://stackoverflow.com/questions/26208788/can-someone-explain-how-to-append-an-element-to-an-array-in-c-programming*/
void Append(int arr[],int n,String ele){
Serial.println("adding: "); //debug line
Serial.println(ele); //debug line
int size = n+1; // increasing the size
int arrnew[size]; // Creating the new array:
for(int i = 0; i<size;i++){
arrnew[i] = arr[i]; // copy the element old array to new array:
}
arrnew[n] = ele; // Appending the element:
}
void setup()
{
Serial.begin(115200);
// Initialize T-RGB, if the initialization fails, false will be returned.
if (!panel.begin()) {
while (1) {
Serial.println("Error, failed to initialize T-RGB"); delay(1000);
}
}
// Initialize SD Card
if (!panel.installSD()) {
Serial.println("Can't install SD Card!");
}
// Call lvgl initialization
beginLvglHelper(panel);
listDir(SD_MMC, "/", 0); //starts at the root of the sd_card and walks the directories, adding all the files it finds to the array
lv_obj_t *img = lv_img_create(lv_scr_act());
lv_obj_center(img);
lv_timer_create([](lv_timer_t *t) {
static int i = 0; //start from filename[0] and increment....
if (!SD_MMC.exists(filename[i])) {
Serial.println("File not find image..."); return ;
}
// String path = LV_FS_POSIX_LETTER + String(":/image_") + filename[i];
String path = lvgl_helper_get_fs_filename(filename[i]);
Serial.print("open : ");
Serial.println(path);
lv_img_set_src((lv_obj_t *)t->user_data, path.c_str());
i++;
i %= sizeof(filename) / sizeof(filename[0]); //this might reset to beginning, not sure....but you're saying this works, and i've not touched it
}, 5000, img);
panel.setBrightness(16);
}
void loop()
{
lv_timer_handler();
delay(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment