Files
muziekdoos/FW/leo_muziekdoos_esp32/src/storage.cpp
2021-12-10 17:15:22 +01:00

105 lines
2.2 KiB
C++

#include "storage.h"
#include <Arduino.h>
#include "FS.h"
#if defined ESP_ARDUINO_VERSION_VAL
#if (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0)
#include <LittleFS.h>
#define ESP_V2
#endif
#else
#include <LITTLEFS.h>
#endif
#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
#include <time.h>
#endif
#define FORMAT_LITTLEFS_IF_FAILED false
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
log_i("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if (!root)
{
log_e("- failed to open directory");
return;
}
if (!root.isDirectory())
{
log_e(" - not a directory");
return;
}
File file = root.openNextFile();
while (file)
{
if (file.isDirectory())
{
log_i(" DIR : ");
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
Serial.println(file.name());
#else
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
log_i("FILE: %s LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",file.name(), (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
#endif
if (levels)
{
listDir(fs, file.name(), levels - 1);
}
}
else
{
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
Serial.println(file.size());
#else
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
log_i(" FILE: %s, SIZE: %d LAST WRITE: %d-%02d-%02d %02d:%02d:%02d" ,file.name(), file.size(), (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
#endif
}
file = root.openNextFile();
}
}
void readFile(fs::FS &fs, const char *path)
{
log_i("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
log_e("- failed to open file for reading");
return;
}
log_i("- read from file:");
while (file.available())
{
Serial.write(file.read());
}
file.close();
}
void initStorage()
{
if (!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED))
{
log_e("LITTLEFS Mount Failed");
return;
}
listDir(LITTLEFS, "/", 0);
}
void handleStorage()
{
}