r65emu/checkpoint.cpp

59 lines
1.3 KiB
C++
Raw Normal View History

#include <stdint.h>
2018-11-10 09:32:33 +00:00
#include <stdio.h>
#include <string.h>
#include "hardware.h"
2019-02-11 18:28:38 +00:00
#include "serialio.h"
2019-02-10 13:58:52 +00:00
#include "filer.h"
2014-11-14 18:27:54 +00:00
#include "checkpoint.h"
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
#include <SD.h>
#elif defined(USE_SPIFFS)
#include <SPIFFS.h>
2018-11-10 13:35:41 +00:00
#elif defined(ESP8266)
#include <FS.h>
#endif
2014-11-14 18:27:54 +00:00
static char buf[32];
static char chkpt[] = { "CHKPOINT" };
static int cpid = 0;
2019-02-10 13:58:52 +00:00
const char *checkpoint(filer &f, const char *dir) {
2018-11-27 07:58:22 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
2019-02-10 13:58:52 +00:00
f.stop();
2014-11-14 18:27:54 +00:00
snprintf(buf, sizeof(buf), "%s%s.%03d", dir, chkpt, cpid++);
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
2014-11-14 18:27:54 +00:00
File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC);
2018-11-10 13:35:41 +00:00
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_WRITE);
2018-11-10 13:35:41 +00:00
#else
File file = SPIFFS.open(buf, "w");
#endif
2014-11-14 18:27:54 +00:00
hardware_checkpoint(file);
file.close();
2019-02-10 13:58:52 +00:00
f.start(dir);
2018-11-27 07:58:22 +00:00
#endif
2014-11-14 18:27:54 +00:00
return buf;
}
2019-02-10 13:58:52 +00:00
void restore(filer &f, const char *dir, const char *filename) {
2018-11-27 07:58:22 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
2019-02-10 13:58:52 +00:00
f.stop();
2014-11-14 18:27:54 +00:00
snprintf(buf, sizeof(buf), "%s%s", dir, filename);
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
2014-11-14 18:27:54 +00:00
File file = SD.open(buf, O_READ);
2018-11-10 13:35:41 +00:00
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_READ);
2018-11-10 13:35:41 +00:00
#else
File file = SPIFFS.open(buf, "r");
#endif
2014-11-14 18:27:54 +00:00
hardware_restore(file);
file.close();
int n = sscanf(buf + strlen(dir), "%[A-Z0-9].%d", chkpt, &cpid);
cpid = (n == 1)? 0: cpid+1;
2018-11-27 07:58:22 +00:00
#endif
2019-02-10 13:58:52 +00:00
f.start(dir);
2014-11-14 18:27:54 +00:00
}