split sd_filer out of flash_filer; USE_FS -> USE_LITTLEFS (#23)

This commit is contained in:
Stephen Crane 2023-10-12 18:27:56 +01:00 committed by GitHub
parent fa8759415e
commit 7c8980cbfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 150 additions and 37 deletions

View File

@ -1,30 +1,27 @@
#include <stdint.h> #include <stdint.h>
#include "hardware.h" #include "hardware.h"
#if defined(USE_SD) #if defined(USE_SPIFFS)
#include <SD.h>
#define DISK SD
#elif defined(USE_SPIFFS)
#include <SPIFFS.h> #include <SPIFFS.h>
#define DISK SPIFFS #elif defined(USE_LITTLEFS)
#elif defined(USE_FS)
#include <FS.h> #include <FS.h>
#include <LittleFS.h>
#endif #endif
#include "serialio.h" #include "serialio.h"
#include "filer.h" #include "filer.h"
#include "flash_filer.h" #include "flash_filer.h"
#if defined(DISK) #if defined(USE_SPIFFS)
static File file, dir; static File file, dir;
#elif defined(USE_FS) #elif defined(USE_LITTLEFS)
static File file; static File file;
static Dir dir; static Dir dir;
#endif #endif
bool flash_filer::seek(uint32_t pos) 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); return file.seek(pos);
#else #else
return false; return false;
@ -33,10 +30,10 @@ bool flash_filer::seek(uint32_t pos)
bool flash_filer::start() bool flash_filer::start()
{ {
#if defined(USE_FS) #if defined(USE_LITTLEFS)
dir = SPIFFS.openDir(_programs); dir = LittleFS.openDir(_programs);
#elif defined(DISK) #elif defined(USE_SPIFFS)
dir = DISK.open(_programs); dir = SPIFFS.open(_programs);
if (!dir) if (!dir)
return false; return false;
#endif #endif
@ -46,14 +43,14 @@ bool flash_filer::start()
void flash_filer::stop() void flash_filer::stop()
{ {
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
file.close(); file.close();
#endif #endif
} }
bool flash_filer::more() 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; return file.available() > 0;
#else #else
return false; return false;
@ -61,7 +58,7 @@ bool flash_filer::more()
} }
uint8_t flash_filer::read() { 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(); return file.read();
#else #else
return 0xff; return 0xff;
@ -69,24 +66,24 @@ uint8_t flash_filer::read() {
} }
void flash_filer::write(uint8_t b) { 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.write(b);
file.flush(); file.flush();
#endif #endif
} }
const char *flash_filer::advance() { 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; bool rewound = false;
file.close(); file.close();
#if defined(USE_FS) #if defined(USE_LITTLEFS)
static char buf[32]; static char buf[32];
while (true) { while (true) {
if (dir.next()) { if (dir.next()) {
file = dir.openFile("r+"); file = dir.openFile("r+");
break; break;
} }
dir = SPIFFS.openDir(_programs); dir = LittleFS.openDir(_programs);
} }
strncpy(buf, dir.fileName().c_str(), sizeof(buf)); strncpy(buf, dir.fileName().c_str(), sizeof(buf));
return buf; return buf;
@ -112,7 +109,7 @@ const char *flash_filer::advance() {
} }
const char *flash_filer::rewind() { const char *flash_filer::rewind() {
#if defined(DISK) #if defined(USE_SPIFFS)
dir.rewindDirectory(); dir.rewindDirectory();
#endif #endif
return advance(); return advance();
@ -124,11 +121,11 @@ static char chkpt[] = { "CHKPOINT" };
static int cpid = 0; static int cpid = 0;
const char *flash_filer::checkpoint() { const char *flash_filer::checkpoint() {
#if defined(DISK) #if defined(USE_SPIFFS)
stop(); stop();
snprintf(buf, sizeof(buf), "%s%s.%03d", _programs, chkpt, cpid++); 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); hardware_checkpoint(file);
file.close(); file.close();
start(); start();
@ -137,11 +134,11 @@ const char *flash_filer::checkpoint() {
} }
void flash_filer::restore(const char *filename) { void flash_filer::restore(const char *filename) {
#if defined(DISK) #if defined(USE_SPIFFS)
stop(); stop();
snprintf(buf, sizeof(buf), "%s%s", _programs, filename); 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); hardware_restore(file);
file.close(); file.close();
int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid); int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid);

View File

@ -1,8 +1,6 @@
#ifndef __FLASH_FILER_H__ #ifndef __FLASH_FILER_H__
#define __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 { class flash_filer: public filer {
public: public:
flash_filer(const char *programs): _programs(programs) {} flash_filer(const char *programs): _programs(programs) {}

View File

@ -6,8 +6,9 @@
#include <SD.h> #include <SD.h>
#elif defined(USE_SPIFFS) #elif defined(USE_SPIFFS)
#include <SPIFFS.h> #include <SPIFFS.h>
#elif defined(USE_FS) #elif defined(USE_LITTLEFS)
#include <FS.h> #include <FS.h>
#include <LittleFS.h>
#endif #endif
#include "memory.h" #include "memory.h"
@ -54,8 +55,8 @@ bool hardware_reset() {
#elif defined(USE_SPIFFS) #elif defined(USE_SPIFFS)
success = SPIFFS.begin(true); success = SPIFFS.begin(true);
#elif defined(USE_FS) #elif defined(USE_LITTLEFS)
success = SPIFFS.begin(); success = LittleFS.begin();
#endif #endif
#if defined(TFT_BACKLIGHT) #if defined(TFT_BACKLIGHT)

View File

@ -31,10 +31,7 @@
#undef USE_SD #undef USE_SD
//#define SD_CS D0 //#define SD_CS D0
#undef USE_SPIFFS #undef USE_SPIFFS
#undef USE_FS #define USE_LITTLEFS
#undef USE_SPIFFS
#define USE_FS
// sound // sound
#define PWM_SOUND D2 #define PWM_SOUND D2

View File

@ -28,9 +28,9 @@
#define KBD_DATA 34 #define KBD_DATA 34
#define KBD_IRQ 35 #define KBD_IRQ 35
// "tape" storage... // storage
#undef USE_SD #undef USE_SD
#undef USE_FS #undef USE_LITTLEFS
#define USE_SPIFFS #define USE_SPIFFS
// sound: dac and pwm // sound: dac and pwm

View File

@ -22,5 +22,5 @@
// "tape" storage... // "tape" storage...
#undef USE_SD #undef USE_SD
#undef USE_FS #undef USE_LITTLEFS
#define USE_SPIFFS #define USE_SPIFFS

95
sd_filer.cpp Normal file
View File

@ -0,0 +1,95 @@
#if defined(USE_SD)
#include <stdint.h>
#include <SD.h>
#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

25
sd_filer.h Normal file
View File

@ -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