From 7c8980cbfc7a6b5c750fc25c7c28ec38c75d2c03 Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Thu, 12 Oct 2023 18:27:56 +0100 Subject: [PATCH] split sd_filer out of flash_filer; USE_FS -> USE_LITTLEFS (#23) --- flash_filer.cpp | 47 ++++++++++---------- flash_filer.h | 2 - hardware.cpp | 7 +-- hw/esp8bit.h | 5 +-- hw/node32s-example.h | 4 +- hw/ttgo-t7-v14-mini32.h | 2 +- sd_filer.cpp | 95 +++++++++++++++++++++++++++++++++++++++++ sd_filer.h | 25 +++++++++++ 8 files changed, 150 insertions(+), 37 deletions(-) create mode 100644 sd_filer.cpp create mode 100644 sd_filer.h diff --git a/flash_filer.cpp b/flash_filer.cpp index 0e759bd..aafb140 100644 --- a/flash_filer.cpp +++ b/flash_filer.cpp @@ -1,30 +1,27 @@ #include #include "hardware.h" -#if defined(USE_SD) -#include -#define DISK SD -#elif defined(USE_SPIFFS) +#if defined(USE_SPIFFS) #include -#define DISK SPIFFS -#elif defined(USE_FS) +#elif defined(USE_LITTLEFS) #include +#include #endif #include "serialio.h" #include "filer.h" #include "flash_filer.h" -#if defined(DISK) +#if defined(USE_SPIFFS) static File file, dir; -#elif defined(USE_FS) +#elif defined(USE_LITTLEFS) static File file; static Dir dir; #endif bool flash_filer::seek(uint32_t pos) { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) return file.seek(pos); #else return false; @@ -33,10 +30,10 @@ bool flash_filer::seek(uint32_t pos) bool flash_filer::start() { -#if defined(USE_FS) - dir = SPIFFS.openDir(_programs); -#elif defined(DISK) - dir = DISK.open(_programs); +#if defined(USE_LITTLEFS) + dir = LittleFS.openDir(_programs); +#elif defined(USE_SPIFFS) + dir = SPIFFS.open(_programs); if (!dir) return false; #endif @@ -46,14 +43,14 @@ bool flash_filer::start() void flash_filer::stop() { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) file.close(); #endif } bool flash_filer::more() { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) return file.available() > 0; #else return false; @@ -61,7 +58,7 @@ bool flash_filer::more() } uint8_t flash_filer::read() { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) return file.read(); #else return 0xff; @@ -69,24 +66,24 @@ uint8_t flash_filer::read() { } void flash_filer::write(uint8_t b) { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) file.write(b); file.flush(); #endif } const char *flash_filer::advance() { -#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) +#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) bool rewound = false; file.close(); -#if defined(USE_FS) +#if defined(USE_LITTLEFS) static char buf[32]; while (true) { if (dir.next()) { file = dir.openFile("r+"); break; } - dir = SPIFFS.openDir(_programs); + dir = LittleFS.openDir(_programs); } strncpy(buf, dir.fileName().c_str(), sizeof(buf)); return buf; @@ -112,7 +109,7 @@ const char *flash_filer::advance() { } const char *flash_filer::rewind() { -#if defined(DISK) +#if defined(USE_SPIFFS) dir.rewindDirectory(); #endif return advance(); @@ -124,11 +121,11 @@ static char chkpt[] = { "CHKPOINT" }; static int cpid = 0; const char *flash_filer::checkpoint() { -#if defined(DISK) +#if defined(USE_SPIFFS) stop(); snprintf(buf, sizeof(buf), "%s%s.%03d", _programs, chkpt, cpid++); - File file = DISK.open(buf, FILE_WRITE); + File file = SPIFFS.open(buf, FILE_WRITE); hardware_checkpoint(file); file.close(); start(); @@ -137,11 +134,11 @@ const char *flash_filer::checkpoint() { } void flash_filer::restore(const char *filename) { -#if defined(DISK) +#if defined(USE_SPIFFS) stop(); snprintf(buf, sizeof(buf), "%s%s", _programs, filename); - File file = DISK.open(buf, FILE_READ); + File file = SPIFFS.open(buf, FILE_READ); hardware_restore(file); file.close(); int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid); diff --git a/flash_filer.h b/flash_filer.h index f56f2e9..956a9d1 100644 --- a/flash_filer.h +++ b/flash_filer.h @@ -1,8 +1,6 @@ #ifndef __FLASH_FILER_H__ #define __FLASH_FILER_H__ -// split into sd_filer and fs_filer -// implement write to new file (like checkpoint) class flash_filer: public filer { public: flash_filer(const char *programs): _programs(programs) {} diff --git a/hardware.cpp b/hardware.cpp index 7492975..cad907f 100644 --- a/hardware.cpp +++ b/hardware.cpp @@ -6,8 +6,9 @@ #include #elif defined(USE_SPIFFS) #include -#elif defined(USE_FS) +#elif defined(USE_LITTLEFS) #include +#include #endif #include "memory.h" @@ -54,8 +55,8 @@ bool hardware_reset() { #elif defined(USE_SPIFFS) success = SPIFFS.begin(true); -#elif defined(USE_FS) - success = SPIFFS.begin(); +#elif defined(USE_LITTLEFS) + success = LittleFS.begin(); #endif #if defined(TFT_BACKLIGHT) diff --git a/hw/esp8bit.h b/hw/esp8bit.h index ef40306..4487489 100644 --- a/hw/esp8bit.h +++ b/hw/esp8bit.h @@ -31,10 +31,7 @@ #undef USE_SD //#define SD_CS D0 #undef USE_SPIFFS -#undef USE_FS - -#undef USE_SPIFFS -#define USE_FS +#define USE_LITTLEFS // sound #define PWM_SOUND D2 diff --git a/hw/node32s-example.h b/hw/node32s-example.h index 69cf719..cc6b0ce 100644 --- a/hw/node32s-example.h +++ b/hw/node32s-example.h @@ -28,9 +28,9 @@ #define KBD_DATA 34 #define KBD_IRQ 35 -// "tape" storage... +// storage #undef USE_SD -#undef USE_FS +#undef USE_LITTLEFS #define USE_SPIFFS // sound: dac and pwm diff --git a/hw/ttgo-t7-v14-mini32.h b/hw/ttgo-t7-v14-mini32.h index 7293abf..6a6968f 100644 --- a/hw/ttgo-t7-v14-mini32.h +++ b/hw/ttgo-t7-v14-mini32.h @@ -22,5 +22,5 @@ // "tape" storage... #undef USE_SD -#undef USE_FS +#undef USE_LITTLEFS #define USE_SPIFFS diff --git a/sd_filer.cpp b/sd_filer.cpp new file mode 100644 index 0000000..2569131 --- /dev/null +++ b/sd_filer.cpp @@ -0,0 +1,95 @@ +#if defined(USE_SD) + +#include +#include + +#include "hardware.h" +#include "serialio.h" +#include "filer.h" +#include "sd_filer.h" + +static File file, dir; + +bool sd_filer::seek(uint32_t pos) +{ + return file.seek(pos); +} + +bool sd_filer::start() +{ + dir = SD.open(_programs); + return (bool)dir; +} + +void sd_filer::stop() +{ + file.close(); +} + +bool sd_filer::more() +{ + return file.available() > 0; +} + +uint8_t sd_filer::read() { + return file.read(); +} + +void sd_filer::write(uint8_t b) { + file.write(b); + file.flush(); +} + +const char *sd_filer::advance() { + bool rewound = false; + file.close(); + while (true) { + file = dir.openNextFile(); + if (file) { + if (file.isDirectory()) + file.close(); + else + break; + } else if (!rewound) { + dir.rewindDirectory(); + rewound = true; + } else + return 0; + } + return file.name(); +} + +const char *sd_filer::rewind() { + dir.rewindDirectory(); +} + +#if !defined(NO_CHECKPOINT) +static char buf[32]; +static char chkpt[] = { "CHKPOINT" }; +static int cpid = 0; + +const char *sd_filer::checkpoint() { + stop(); + snprintf(buf, sizeof(buf), "%s%s.%03d", _programs, chkpt, cpid++); + + File file = SD.open(buf, FILE_WRITE); + hardware_checkpoint(file); + file.close(); + start(); + return buf; +} + +void sd_filer::restore(const char *filename) { + stop(); + snprintf(buf, sizeof(buf), "%s%s", _programs, filename); + + File file = SD.open(buf, FILE_READ); + hardware_restore(file); + file.close(); + int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid); + cpid = (n == 1)? 0: cpid+1; + start(); +} +#endif + +#endif diff --git a/sd_filer.h b/sd_filer.h new file mode 100644 index 0000000..ee885a2 --- /dev/null +++ b/sd_filer.h @@ -0,0 +1,25 @@ +#ifndef __SD_FILER_H__ +#define __SD_FILER_H__ + +class sd_filer: public filer { +public: + sd_filer(const char *programs): _programs(programs) {} + + const char *advance(); + const char *rewind(); + + const char *checkpoint(); + void restore(const char *); + + bool start(); + void stop(); + bool seek(uint32_t pos); + + bool more(); + uint8_t read(); + void write(uint8_t); + +private: + const char *_programs; +}; +#endif