r65emu/flash_filer.cpp

134 lines
2.3 KiB
C++
Raw Normal View History

2018-11-10 09:32:33 +00:00
#include <stdint.h>
#include "hardware.h"
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
2014-10-26 17:54:01 +00:00
#include <SD.h>
#define DISK SD
#elif defined(USE_SPIFFS)
#include <SPIFFS.h>
#define DISK SPIFFS
2018-11-16 12:30:09 +00:00
#elif defined(USE_FS)
2018-11-10 13:35:41 +00:00
#include <FS.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
2018-11-10 09:32:33 +00:00
#if defined(DISK)
2014-10-26 17:54:01 +00:00
static File file, dir;
2018-11-16 12:30:09 +00:00
#elif defined(USE_FS)
2018-11-10 13:35:41 +00:00
static File file;
static Dir dir;
2018-11-10 09:32:33 +00:00
#endif
2014-10-26 17:54:01 +00:00
2019-03-26 19:27:25 +00:00
bool flash_filer::start()
2014-10-26 17:54:01 +00:00
{
2019-02-12 18:17:46 +00:00
#if defined(USE_FS)
2019-03-26 19:27:25 +00:00
dir = SPIFFS.openDir(_programs);
2018-11-11 11:59:43 +00:00
#elif defined(DISK)
2019-03-26 19:27:25 +00:00
dir = DISK.open(_programs);
2018-09-06 13:21:17 +00:00
if (!dir)
return false;
2018-11-10 09:32:33 +00:00
#endif
2018-09-06 13:21:17 +00:00
2014-11-15 23:24:47 +00:00
_pos = _len = 0;
2018-09-06 13:21:17 +00:00
return true;
2014-10-26 17:54:01 +00:00
}
2019-02-10 13:58:52 +00:00
void flash_filer::stop()
2014-10-26 17:54:01 +00:00
{
2021-02-21 12:37:25 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
2014-10-26 17:54:01 +00:00
file.close();
2018-11-10 09:32:33 +00:00
#endif
2014-10-26 17:54:01 +00:00
}
2019-02-10 13:58:52 +00:00
bool flash_filer::more()
2014-10-26 17:54:01 +00:00
{
if (_pos >= _len) {
_pos = 0;
2021-02-21 12:37:25 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
2014-10-26 17:54:01 +00:00
_len = file.read(_buf, sizeof(_buf));
2018-11-16 12:35:07 +00:00
#endif
2014-11-02 17:34:19 +00:00
if (_len == 0) // eof
return false;
2014-10-26 17:54:01 +00:00
}
return true;
}
2019-02-10 13:58:52 +00:00
const char *flash_filer::advance() {
2021-02-21 12:37:25 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
2014-10-26 17:54:01 +00:00
bool rewound = false;
file.close();
2018-11-16 12:30:09 +00:00
#if defined(USE_FS)
2018-11-10 13:35:41 +00:00
static char buf[32];
while (true) {
if (dir.next()) {
file = dir.openFile("r");
break;
}
2019-03-26 19:27:25 +00:00
dir = SPIFFS.openDir(_programs);
2018-11-10 13:35:41 +00:00
}
strncpy(buf, dir.fileName().c_str(), sizeof(buf));
return buf;
#else
2014-10-26 17:54:01 +00:00
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();
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() {
2018-11-16 12:30:09 +00:00
#if defined(DISK)
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)
2019-02-12 18:17:46 +00:00
static char buf[32];
static char chkpt[] = { "CHKPOINT" };
static int cpid = 0;
const char *flash_filer::checkpoint() {
2021-02-21 12:37:25 +00:00
#if defined(DISK)
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
2021-02-21 12:37:25 +00:00
File file = DISK.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
#endif
return buf;
}
void flash_filer::restore(const char *filename) {
2021-02-21 12:37:25 +00:00
#if defined(DISK)
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
2021-02-21 12:37:25 +00:00
File file = DISK.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