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 "hardware.h"
#if defined(USE_SD)
#include <SD.h>
#define DISK SD
#elif defined(USE_SPIFFS)
#if defined(USE_SPIFFS)
#include <SPIFFS.h>
#define DISK SPIFFS
#elif defined(USE_FS)
#elif defined(USE_LITTLEFS)
#include <FS.h>
#include <LittleFS.h>
#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);

View File

@ -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) {}

View File

@ -6,8 +6,9 @@
#include <SD.h>
#elif defined(USE_SPIFFS)
#include <SPIFFS.h>
#elif defined(USE_FS)
#elif defined(USE_LITTLEFS)
#include <FS.h>
#include <LittleFS.h>
#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)

View File

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

View File

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

View File

@ -22,5 +22,5 @@
// "tape" storage...
#undef USE_SD
#undef USE_FS
#undef USE_LITTLEFS
#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