split flash_file out of flash_filer (#24)

This commit is contained in:
Stephen Crane 2023-10-19 15:04:28 +01:00 committed by GitHub
parent 7c8980cbfc
commit bfb6b379d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 26 deletions

View File

@ -1,7 +1,7 @@
#ifndef __FILER_H__ #ifndef __FILER_H__
#define __FILER_H__ #define __FILER_H__
class filer: public serialio { class filer: virtual public serialio {
public: public:
virtual const char *advance() =0; virtual const char *advance() =0;
virtual const char *rewind() =0; virtual const char *rewind() =0;

View File

@ -12,17 +12,18 @@
#include "filer.h" #include "filer.h"
#include "flash_filer.h" #include "flash_filer.h"
static File files[MAX_FILES];
#if defined(USE_SPIFFS) #if defined(USE_SPIFFS)
static File file, dir; static File dir;
#elif defined(USE_LITTLEFS) #elif defined(USE_LITTLEFS)
static File file;
static Dir dir; static Dir dir;
#endif #endif
bool flash_filer::seek(uint32_t pos) bool flash_file::seek(uint32_t pos)
{ {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.seek(pos); return files[_fd].seek(pos);
#else #else
return false; return false;
#endif #endif
@ -44,43 +45,44 @@ bool flash_filer::start()
void flash_filer::stop() void flash_filer::stop()
{ {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
file.close(); for (int i = 0; i < MAX_FILES; i++)
files[i].close();
#endif #endif
} }
bool flash_filer::more() bool flash_file::more()
{ {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.available() > 0; return files[_fd].available() > 0;
#else #else
return false; return false;
#endif #endif
} }
uint8_t flash_filer::read() { uint8_t flash_file::read() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.read(); return files[_fd].read();
#else #else
return 0xff; return 0xff;
#endif #endif
} }
void flash_filer::write(uint8_t b) { void flash_file::write(uint8_t b) {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
file.write(b); files[_fd].write(b);
file.flush(); files[_fd].flush();
#endif #endif
} }
const char *flash_filer::advance() { const char *flash_filer::advance() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS) #if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
bool rewound = false; bool rewound = false;
file.close(); files[_current].close();
#if defined(USE_LITTLEFS) #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+"); files[_current] = dir.openFile("r+");
break; break;
} }
dir = LittleFS.openDir(_programs); dir = LittleFS.openDir(_programs);
@ -89,10 +91,10 @@ const char *flash_filer::advance() {
return buf; return buf;
#else #else
while (true) { while (true) {
file = dir.openNextFile(); files[_current] = dir.openNextFile();
if (file) { if (files[_current]) {
if (file.isDirectory()) if (files[_current].isDirectory())
file.close(); files[_current].close();
else else
break; break;
} else if (!rewound) { } else if (!rewound) {
@ -101,7 +103,7 @@ const char *flash_filer::advance() {
} else } else
return 0; return 0;
} }
return file.name(); return files[_current].name();
#endif #endif
#else #else
return 0; return 0;

View File

@ -1,7 +1,23 @@
#ifndef __FLASH_FILER_H__ #ifndef __FLASH_FILER_H__
#define __FLASH_FILER_H__ #define __FLASH_FILER_H__
class flash_filer: public filer { #define MAX_FILES 3
class flash_file: virtual public serialio {
public:
flash_file(uint8_t fd = 0): _fd(fd) {}
virtual bool more();
virtual uint8_t read();
virtual void write(uint8_t);
bool seek(uint32_t pos);
private:
const uint8_t _fd;
};
class flash_filer: public filer, public flash_file {
public: public:
flash_filer(const char *programs): _programs(programs) {} flash_filer(const char *programs): _programs(programs) {}
@ -13,13 +29,11 @@ public:
bool start(); bool start();
void stop(); void stop();
bool seek(uint32_t pos);
void select(uint8_t f) { _current = f; }
bool more();
uint8_t read();
void write(uint8_t);
private: private:
const char *_programs; const char *_programs;
uint8_t _current;
}; };
#endif #endif