Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split flash_file out of flash_filer #24

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion filer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __FILER_H__
#define __FILER_H__

class filer: public serialio {
class filer: virtual public serialio {
public:
virtual const char *advance() =0;
virtual const char *rewind() =0;
Expand Down
40 changes: 21 additions & 19 deletions flash_filer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
#include "filer.h"
#include "flash_filer.h"

static File files[MAX_FILES];

#if defined(USE_SPIFFS)
static File file, dir;
static File dir;
#elif defined(USE_LITTLEFS)
static File file;
static Dir dir;
#endif

bool flash_filer::seek(uint32_t pos)
bool flash_file::seek(uint32_t pos)
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.seek(pos);
return files[_fd].seek(pos);
#else
return false;
#endif
Expand All @@ -44,43 +45,44 @@ bool flash_filer::start()
void flash_filer::stop()
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
file.close();
for (int i = 0; i < MAX_FILES; i++)
files[i].close();
#endif
}

bool flash_filer::more()
bool flash_file::more()
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.available() > 0;
return files[_fd].available() > 0;
#else
return false;
#endif
}

uint8_t flash_filer::read() {
uint8_t flash_file::read() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return file.read();
return files[_fd].read();
#else
return 0xff;
#endif
}

void flash_filer::write(uint8_t b) {
void flash_file::write(uint8_t b) {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
file.write(b);
file.flush();
files[_fd].write(b);
files[_fd].flush();
#endif
}

const char *flash_filer::advance() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
bool rewound = false;
file.close();
files[_current].close();
#if defined(USE_LITTLEFS)
static char buf[32];
while (true) {
if (dir.next()) {
file = dir.openFile("r+");
files[_current] = dir.openFile("r+");
break;
}
dir = LittleFS.openDir(_programs);
Expand All @@ -89,10 +91,10 @@ const char *flash_filer::advance() {
return buf;
#else
while (true) {
file = dir.openNextFile();
if (file) {
if (file.isDirectory())
file.close();
files[_current] = dir.openNextFile();
if (files[_current]) {
if (files[_current].isDirectory())
files[_current].close();
else
break;
} else if (!rewound) {
Expand All @@ -101,7 +103,7 @@ const char *flash_filer::advance() {
} else
return 0;
}
return file.name();
return files[_current].name();
#endif
#else
return 0;
Expand Down
26 changes: 20 additions & 6 deletions flash_filer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
#ifndef __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:
flash_filer(const char *programs): _programs(programs) {}

Expand All @@ -13,13 +29,11 @@ class flash_filer: public filer {

bool start();
void stop();
bool seek(uint32_t pos);

bool more();
uint8_t read();
void write(uint8_t);

void select(uint8_t f) { _current = f; }

private:
const char *_programs;
uint8_t _current;
};
#endif