r65emu/flash_filer.cpp

166 lines
3.0 KiB
C++
Raw Normal View History

2018-11-10 09:32:33 +00:00
#include <stdint.h>
#include "hardware.h"
#if defined(USE_SPIFFS)
#include <SPIFFS.h>
#elif defined(USE_LITTLEFS)
2018-11-10 13:35:41 +00:00
#include <FS.h>
#include <LittleFS.h>
#endif
2019-02-11 18:28:38 +00:00
#include "serialio.h"
2019-02-10 13:58:52 +00:00
#include "filer.h"
#include "flash_filer.h"
2014-10-26 17:54:01 +00:00
2023-11-18 13:23:52 +00:00
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
static File files[MAX_FILES];
2023-11-18 13:23:52 +00:00
#endif
#if defined(USE_SPIFFS)
static File dir;
#elif defined(USE_LITTLEFS)
2018-11-10 13:35:41 +00:00
static Dir dir;
2018-11-10 09:32:33 +00:00
#endif
2014-10-26 17:54:01 +00:00
bool flash_file::seek(uint32_t pos)
2023-05-25 14:05:04 +00:00
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return files[_fd].seek(pos);
2023-09-30 11:51:19 +00:00
#else
2023-05-25 14:05:04 +00:00
return false;
2023-09-30 11:51:19 +00:00
#endif
2023-05-25 14:05:04 +00:00
}
bool flash_file::more()
2014-10-26 17:54:01 +00:00
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return files[_fd].available() > 0;
2023-09-30 11:51:19 +00:00
#else
return false;
#endif
}
uint8_t flash_file::read() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return files[_fd].read();
2023-09-30 11:51:19 +00:00
#else
return 0xff;
#endif
}
2023-10-20 12:01:15 +00:00
flash_file::operator bool() const {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
return files[_fd];
#else
return false;
#endif
}
void flash_file::write(uint8_t b) {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
files[_fd].write(b);
files[_fd].flush();
2018-11-16 12:35:07 +00:00
#endif
2014-10-26 17:54:01 +00:00
}
2023-10-20 12:01:15 +00:00
bool flash_filer::start()
{
#if defined(USE_LITTLEFS)
dir = LittleFS.openDir(_programs);
#elif defined(USE_SPIFFS)
dir = SPIFFS.open(_programs);
if (!dir)
return false;
#endif
return true;
}
void flash_filer::stop()
{
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
for (int i = 0; i < MAX_FILES; i++)
files[i].close();
#endif
}
2019-02-10 13:58:52 +00:00
const char *flash_filer::advance() {
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
files[_current].close();
#if defined(USE_LITTLEFS)
2018-11-10 13:35:41 +00:00
static char buf[32];
while (true) {
if (dir.next()) {
files[_current] = dir.openFile("r+");
2018-11-10 13:35:41 +00:00
break;
}
dir = LittleFS.openDir(_programs);
2018-11-10 13:35:41 +00:00
}
strncpy(buf, dir.fileName().c_str(), sizeof(buf));
return buf;
#else
2023-11-15 18:22:28 +00:00
bool rewound = false;
2014-10-26 17:54:01 +00:00
while (true) {
files[_current] = dir.openNextFile();
if (files[_current]) {
if (files[_current].isDirectory())
files[_current].close();
2014-10-26 17:54:01 +00:00
else
break;
} else if (!rewound) {
dir.rewindDirectory();
rewound = true;
} else
return 0;
}
return files[_current].name();
2018-11-10 13:35:41 +00:00
#endif
2018-11-10 09:32:33 +00:00
#else
return 0;
#endif
2014-10-26 17:54:01 +00:00
}
2019-02-10 13:58:52 +00:00
const char *flash_filer::rewind() {
#if defined(USE_SPIFFS)
2014-10-26 17:54:01 +00:00
dir.rewindDirectory();
2018-11-10 09:32:33 +00:00
#endif
2014-10-26 17:54:01 +00:00
return advance();
}
2019-02-12 18:17:46 +00:00
2019-02-24 11:50:10 +00:00
#if !defined(NO_CHECKPOINT)
2023-11-15 18:22:28 +00:00
#if defined(USE_SPIFFS)
2019-02-12 18:17:46 +00:00
static char buf[32];
static char chkpt[] = { "CHKPOINT" };
static int cpid = 0;
2023-11-15 18:22:28 +00:00
#endif
2019-02-12 18:17:46 +00:00
const char *flash_filer::checkpoint() {
#if defined(USE_SPIFFS)
2019-02-12 18:17:46 +00:00
stop();
2019-03-26 19:27:25 +00:00
snprintf(buf, sizeof(buf), "%s%s.%03d", _programs, chkpt, cpid++);
2019-02-12 18:17:46 +00:00
File file = SPIFFS.open(buf, FILE_WRITE);
2019-02-12 18:17:46 +00:00
hardware_checkpoint(file);
file.close();
2019-03-26 19:27:25 +00:00
start();
2019-02-12 18:17:46 +00:00
return buf;
2023-11-18 10:58:15 +00:00
#else
return "";
#endif
2019-02-12 18:17:46 +00:00
}
void flash_filer::restore(const char *filename) {
#if defined(USE_SPIFFS)
2019-02-12 18:17:46 +00:00
stop();
2019-03-26 19:27:25 +00:00
snprintf(buf, sizeof(buf), "%s%s", _programs, filename);
2019-02-12 18:17:46 +00:00
File file = SPIFFS.open(buf, FILE_READ);
2019-02-12 18:17:46 +00:00
hardware_restore(file);
file.close();
2019-03-26 19:27:25 +00:00
int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid);
2019-02-12 18:17:46 +00:00
cpid = (n == 1)? 0: cpid+1;
#endif
2019-03-26 19:27:25 +00:00
start();
2019-02-12 18:17:46 +00:00
}
2019-02-24 11:50:10 +00:00
#endif