load image from file and storage

from storage doesn't work yet, because of memory issues
This commit is contained in:
Peter Siegmund
2025-10-09 22:51:48 +02:00
parent 31845c0596
commit 4b2b8ed0e0
11 changed files with 16535 additions and 71 deletions

2
firmware/.clang-format Normal file
View File

@@ -0,0 +1,2 @@
---
BasedOnStyle: Microsoft

View File

@@ -0,0 +1,7 @@
idf_component_register(SRCS
"storage.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
fatfs
lwip
)

View File

@@ -0,0 +1,3 @@
dependencies:
lvgl/lvgl:
version: ^9.0.0

View File

@@ -0,0 +1,63 @@
#pragma once
#include <lvgl.h>
#ifdef __cplusplus
extern "C"
{
#endif
void fs_mount(void);
void fs_unmount(void);
/**
* Öffnet eine Datei.
* @param drv Zeiger auf den Treiber
* @param path Pfad zur Datei
* @param mode LV_FS_MODE_RD für Lesen, LV_FS_MODE_WR für Schreiben
* @return Zeiger auf ein Datei-Objekt im Erfolgsfall, sonst NULL
*/
void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode);
/**
* Schließt eine geöffnete Datei.
* @param drv Zeiger auf den Treiber
* @param file_p Zeiger auf das Datei-Objekt
* @return LV_FS_RES_OK im Erfolgsfall, sonst Fehlercode
*/
lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p);
/**
* Liest Daten aus einer Datei.
* @param drv Zeiger auf den Treiber
* @param file_p Zeiger auf das Datei-Objekt
* @param buf Puffer, in den die Daten gelesen werden
* @param btr Anzahl der zu lesenden Bytes (Bytes to Read)
* @param br Zeiger, in den die Anzahl der tatsächlich gelesenen Bytes geschrieben wird
* @return LV_FS_RES_OK im Erfolgsfall, sonst Fehlercode
*/
lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br);
/**
* Setzt den Lese-/Schreibzeiger in der Datei.
* @param drv Zeiger auf den Treiber
* @param file_p Zeiger auf das Datei-Objekt
* @param pos Position, zu der gesprungen werden soll
* @param whence Startpunkt: LV_FS_SEEK_SET (Anfang), LV_FS_SEEK_CUR (aktuell), LV_FS_SEEK_END (Ende)
* @return LV_FS_RES_OK im Erfolgsfall, sonst Fehlercode
*/
lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence);
/**
* Gibt die aktuelle Position des Lese-/Schreibzeigers zurück.
* @param drv Zeiger auf den Treiber
* @param file_p Zeiger auf das Datei-Objekt
* @param pos_p Zeiger, in den die aktuelle Position geschrieben wird
* @return LV_FS_RES_OK im Erfolgsfall, sonst Fehlercode
*/
lv_fs_res_t fs_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p);
/******* */
void setup_fatfs();
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -0,0 +1,254 @@
#include "storage.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_sntp.h"
#include "esp_vfs_fat.h"
#include "ff.h"
#include <lvgl.h>
#include <stdlib.h>
static const char *TAG = "storage";
static const char *base_path = "/storage";
static wl_handle_t wl_handle = WL_INVALID_HANDLE;
void fs_mount(void)
{
const esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 4,
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
.disk_status_check_enable = false,
.use_one_fat = false,
};
ESP_LOGI(TAG, "Mounting FAT filesystem in read/write mode");
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &wl_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
}
}
void fs_unmount(void)
{
if (wl_handle != WL_INVALID_HANDLE)
{
ESP_LOGI(TAG, "Unmounting FAT filesystem");
esp_err_t err = esp_vfs_fat_spiflash_unmount_rw_wl(base_path, wl_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to unmount FATFS (%s)", esp_err_to_name(err));
}
else
{
ESP_LOGI(TAG, "FAT filesystem unmounted");
wl_handle = WL_INVALID_HANDLE;
}
}
else
{
ESP_LOGW(TAG, "FAT filesystem is not mounted");
}
}
/**
* Opens a file.
* @param drv Pointer to the driver
* @param path Path to the file
* @param mode LV_FS_MODE_RD for reading, LV_FS_MODE_WR for writing
* @return Pointer to a file object in case of success, otherwise NULL
*/
void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode)
{
LV_UNUSED(drv);
ESP_LOGI(TAG, "fs_open called for path: %s, mode: %d", path, mode);
BYTE fatfs_mode;
if (mode == LV_FS_MODE_WR)
{
fatfs_mode = FA_WRITE | FA_CREATE_ALWAYS;
}
else if (mode == LV_FS_MODE_RD)
{
fatfs_mode = FA_READ;
}
else
{
ESP_LOGE(TAG, "fs_open: Unknown mode: %d", mode);
return NULL;
}
FIL *file_p = (FIL *)malloc(sizeof(FIL));
if (file_p == NULL)
{
ESP_LOGE(TAG, "fs_open: Failed to allocate memory for file object");
return NULL;
}
FRESULT res = f_open(file_p, path, fatfs_mode);
if (res != FR_OK)
{
ESP_LOGE(TAG, "fs_open: f_open failed with error code: %d", res);
free(file_p);
return NULL;
}
ESP_LOGI(TAG, "fs_open: Successfully opened file: %s", path);
return file_p;
}
/**
* Closes an opened file.
* @param drv Pointer to the driver
* @param file_p Pointer to the file object
* @return LV_FS_RES_OK on success, otherwise error code
*/
lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p)
{
LV_UNUSED(drv);
ESP_LOGI(TAG, "fs_close called.");
if (file_p == NULL)
{
ESP_LOGE(TAG, "fs_close: file pointer is NULL.");
return LV_FS_RES_INV_PARAM;
}
f_close((FIL *)file_p);
free(file_p);
return LV_FS_RES_OK;
}
/**
* Reads data from a file.
* @param drv Pointer to the driver
* @param file_p Pointer to the file object
* @param buf Buffer to read the data into
* @param btr Number of bytes to read (Bytes to Read)
* @param br Pointer to store the number of bytes actually read
* @return LV_FS_RES_OK on success, otherwise error code
*/
lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br)
{
LV_UNUSED(drv);
ESP_LOGI(TAG, "fs_read called, bytes to read: %" PRIu32, btr);
FRESULT res = f_read((FIL *)file_p, buf, btr, (UINT *)br);
if (res == FR_OK)
{
ESP_LOGI(TAG, "fs_read: Successfully read %" PRIu32 " bytes.", *br);
return LV_FS_RES_OK;
}
else
{
ESP_LOGE(TAG, "fs_read: f_read failed with error code: %d", res);
return LV_FS_RES_UNKNOWN;
}
}
/**
* Sets the read/write pointer in the file.
* @param drv Pointer to the driver
* @param file_p Pointer to the file object
* @param pos Position to seek to
* @param whence Starting point: LV_FS_SEEK_SET (beginning), LV_FS_SEEK_CUR (current), LV_FS_SEEK_END (end)
* @return LV_FS_RES_OK on success, otherwise error code
*/
lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence)
{
LV_UNUSED(drv);
ESP_LOGI(TAG, "fs_seek: pos=%" PRIu32 ", whence=%d", pos, whence);
// Map LVGL whence to FatFS f_lseek (f_lseek always starts from the beginning)
switch (whence)
{
case LV_FS_SEEK_SET:
f_lseek((FIL *)file_p, pos);
break;
case LV_FS_SEEK_CUR:
f_lseek((FIL *)file_p, f_tell((FIL *)file_p) + pos);
break;
case LV_FS_SEEK_END:
{
FSIZE_t fsize = f_size((FIL *)file_p);
ESP_LOGI(TAG, "fs_seek to end, file size: %lu", (unsigned long)fsize);
f_lseek((FIL *)file_p, fsize + pos);
}
break;
default:
break;
}
return LV_FS_RES_OK;
}
/**
* Returns the current position of the read/write pointer.
* @param drv Pointer to the driver
* @param file_p Pointer to the file object
* @param pos_p Pointer to store the current position
* @return LV_FS_RES_OK on success, otherwise error code
*/
lv_fs_res_t fs_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p)
{
LV_UNUSED(drv);
ESP_LOGI(TAG, "fs_tell called.");
*pos_p = f_tell((FIL *)file_p);
ESP_LOGI(TAG, "fs_tell: Current position: %" PRIu32, *pos_p);
return LV_FS_RES_OK;
}
/**************** */
void setup_fatfs()
{
const esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 4,
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
.disk_status_check_enable = false,
.use_one_fat = false,
};
ESP_LOGI(TAG, "Mounting FAT filesystem in read/write mode");
static wl_handle_t wl_handle = WL_INVALID_HANDLE;
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &wl_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
FILE *f;
ESP_LOGI(TAG, "Reading file");
const char *host_filename1 = "/storage/response.png";
struct stat info;
struct tm timeinfo;
char buffer[32];
if (stat(host_filename1, &info) < 0)
{
ESP_LOGE(TAG, "Failed to read file stats");
return;
}
localtime_r(&info.st_mtime, &timeinfo);
strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeinfo);
ESP_LOGI(TAG, "The file '%s' was modified at date: %s", host_filename1, buffer);
f = fopen(host_filename1, "rb");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
fclose(f);
ESP_LOGI(TAG, "Unmounting FAT filesystem");
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, wl_handle));
ESP_LOGI(TAG, "Done");
}

View File

@@ -37,6 +37,6 @@ direct_dependencies:
- idf - idf
- lovyan03/LovyanGFX - lovyan03/LovyanGFX
- lvgl/lvgl - lvgl/lvgl
manifest_hash: bddd2c10e17729ab5f356657e175fbb9b182bfaa34e4d46a270974778ae75636 manifest_hash: 180a2fcebb323954e96b7d1014d1ae4841a680c9b1f93853bd993bf23c9c9da1
target: esp32s3 target: esp32s3
version: 2.0.0 version: 2.0.0

View File

@@ -3,6 +3,7 @@ idf_component_register(SRCS
"main.cpp" "main.cpp"
PRIV_REQUIRES PRIV_REQUIRES
fatfs fatfs
storage
esp_wifi esp_wifi
wpa_supplicant wpa_supplicant
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

File diff suppressed because one or more lines are too long

View File

@@ -1,12 +1,12 @@
#include <lgfx.h> #include <lgfx.h>
#include <lvgl.h> #include <lvgl.h>
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "ff.h"
#include "image.h" #include "image.h"
#include "storage.h"
#define screenWidth (480) #define screenWidth (480)
#define screenHeight (320) #define screenHeight (320)
@@ -16,9 +16,9 @@ uint8_t lvBuffer1[lvBufferSize];
uint8_t lvBuffer2[lvBufferSize]; uint8_t lvBuffer2[lvBufferSize];
static const char *TAG = "main"; static const char *TAG = "main";
const char *base_path = "/storage";
LGFX tft; LGFX tft;
static FATFS fs;
void setup_tft(void) void setup_tft(void)
{ {
@@ -52,71 +52,25 @@ void my_touch_read(lv_indev_t *indev_driver, lv_indev_data_t *data)
} }
} }
void lv_handler() void fatfs_lvgl_init(void)
{ {
static uint64_t previousUpdate = 0; // 1. Das Dateisystem "mounten" (einmalig für FatFS)
static uint64_t interval = 0; f_mount(&fs, "S:", 1); // "S:" ist der Laufwerksbuchstabe
uint64_t eus = esp_timer_get_time(); // 2. Eine Treiber-Variable deklarieren
if (eus - previousUpdate > interval) static lv_fs_drv_t drv;
{ lv_fs_drv_init(&drv); // Treiberstruktur initialisieren
previousUpdate = eus;
interval = lv_timer_handler(); // Update the UI
lv_tick_inc(interval);
vTaskDelay(pdMS_TO_TICKS(interval - 1));
}
}
void setup_fatfs() // 3. Unsere Bridge-Funktionen zuweisen
{ drv.letter = 'S'; // WICHTIG: Muss mit dem Buchstaben in f_mount übereinstimmen
const esp_vfs_fat_mount_config_t mount_config = { drv.open_cb = fs_open;
.format_if_mount_failed = false, drv.close_cb = fs_close;
.max_files = 4, drv.read_cb = fs_read;
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE, drv.seek_cb = fs_seek;
.disk_status_check_enable = false, drv.tell_cb = fs_tell;
.use_one_fat = false,
};
ESP_LOGI(TAG, "Mounting FAT filesystem in read/write mode"); // 4. Den Treiber bei LVGL registrieren
static wl_handle_t wl_handle = WL_INVALID_HANDLE; lv_fs_drv_register(&drv);
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &wl_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
FILE *f;
ESP_LOGI(TAG, "Reading file");
const char *host_filename1 = "/storage/response.png";
struct stat info;
struct tm timeinfo;
char buffer[32];
if (stat(host_filename1, &info) < 0)
{
ESP_LOGE(TAG, "Failed to read file stats");
return;
}
localtime_r(&info.st_mtime, &timeinfo);
strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeinfo);
ESP_LOGI(TAG, "The file '%s' was modified at date: %s", host_filename1, buffer);
f = fopen(host_filename1, "rb");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
fclose(f);
ESP_LOGI(TAG, "Unmounting FAT filesystem");
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, wl_handle));
ESP_LOGI(TAG, "Done");
} }
void esp_lv_log_print(lv_log_level_t level, const char *buf) void esp_lv_log_print(lv_log_level_t level, const char *buf)
@@ -145,14 +99,15 @@ void esp_lv_log_print(lv_log_level_t level, const char *buf)
void setup() void setup()
{ {
setup_fatfs();
setup_tft(); setup_tft();
lv_init(); lv_init();
lv_log_register_print_cb(esp_lv_log_print); lv_log_register_print_cb(esp_lv_log_print);
fs_mount();
fatfs_lvgl_init();
static auto *display = lv_display_create(screenWidth, screenHeight); static auto *display = lv_display_create(screenWidth, screenHeight);
lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB565); lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB565);
lv_display_set_flush_cb(display, flush); lv_display_set_flush_cb(display, flush);
@@ -163,15 +118,23 @@ void setup()
lv_indev_set_read_cb(lvInput, my_touch_read); lv_indev_set_read_cb(lvInput, my_touch_read);
ESP_LOGI(TAG, "create image"); ESP_LOGI(TAG, "create image");
#ifndef LOCAL
static auto *image = lv_image_create(lv_scr_act()); static auto *image = lv_image_create(lv_scr_act());
lv_image_set_src(image, &image_data); lv_image_set_src(image, &image_data);
lv_obj_align(image, LV_ALIGN_TOP_LEFT, 0, 0); lv_obj_align(image, LV_ALIGN_TOP_LEFT, 0, 0);
#else
lv_obj_t *img = lv_image_create(lv_screen_active());
lv_image_set_src(img, "S:/response.png"); // Pfad beginnt mit "S:"
lv_obj_center(img);
#endif
ESP_LOGI(TAG, "image created"); ESP_LOGI(TAG, "image created");
} }
void loop() void loop()
{ {
lv_handler(); lv_tick_inc(10);
lv_timer_handler();
vTaskDelay(pdMS_TO_TICKS(10));
} }
void lvgl_task(void *pvParameter) void lvgl_task(void *pvParameter)
@@ -181,9 +144,10 @@ void lvgl_task(void *pvParameter)
{ {
loop(); loop();
} }
fs_unmount();
} }
extern "C" void app_main(void) extern "C" void app_main(void)
{ {
xTaskCreatePinnedToCore(lvgl_task, "lvgl_task", 4096, NULL, 5, NULL, 1); xTaskCreatePinnedToCore(lvgl_task, "lvgl_task", 8192, NULL, 5, NULL, 1);
} }

View File

@@ -17,6 +17,7 @@ CONFIG_LV_USE_MEM_MONITOR=y
CONFIG_LV_MEM_MONITOR_ALIGN_BOTTOM_LEFT=y CONFIG_LV_MEM_MONITOR_ALIGN_BOTTOM_LEFT=y
CONFIG_LV_USE_ST7796=y CONFIG_LV_USE_ST7796=y
CONFIG_LV_BUILD_EXAMPLES=n CONFIG_LV_BUILD_EXAMPLES=n
CONFIG_LV_BUILD_DEMOS=n
# partitions # partitions
CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM=y

Binary file not shown.

Before

Width:  |  Height:  |  Size: 0 B

After

Width:  |  Height:  |  Size: 142 KiB