move checkpoint/restore onto filer

This commit is contained in:
Stephen Crane 2019-02-12 18:17:46 +00:00
parent d2377f4dbe
commit 2bdf7a4fe3
5 changed files with 53 additions and 69 deletions

View File

@ -1,58 +0,0 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "hardware.h"
#include "serialio.h"
#include "filer.h"
#include "checkpoint.h"
#if defined(USE_SD)
#include <SD.h>
#elif defined(USE_SPIFFS)
#include <SPIFFS.h>
#elif defined(ESP8266)
#include <FS.h>
#endif
static char buf[32];
static char chkpt[] = { "CHKPOINT" };
static int cpid = 0;
const char *checkpoint(filer &f, const char *dir) {
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
f.stop();
snprintf(buf, sizeof(buf), "%s%s.%03d", dir, chkpt, cpid++);
#if defined(USE_SD)
File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC);
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_WRITE);
#else
File file = SPIFFS.open(buf, "w");
#endif
hardware_checkpoint(file);
file.close();
f.start(dir);
#endif
return buf;
}
void restore(filer &f, const char *dir, const char *filename) {
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
f.stop();
snprintf(buf, sizeof(buf), "%s%s", dir, filename);
#if defined(USE_SD)
File file = SD.open(buf, O_READ);
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_READ);
#else
File file = SPIFFS.open(buf, "r");
#endif
hardware_restore(file);
file.close();
int n = sscanf(buf + strlen(dir), "%[A-Z0-9].%d", chkpt, &cpid);
cpid = (n == 1)? 0: cpid+1;
#endif
f.start(dir);
}

View File

@ -1,8 +0,0 @@
#ifndef __CHECKPOINT_H__
#define __CHECKPOINT_H__
// utility checkpoint functions
const char *checkpoint(filer &f, const char *dir);
void restore(filer &f, const char *dir, const char *filename);
#endif

View File

@ -1,3 +1,4 @@
#include <Arduino.h>
#include <stdint.h>
#include "hardware.h"
@ -19,15 +20,16 @@ static File file, dir;
#elif defined(USE_FS)
static File file;
static Dir dir;
static const char *programs;
#endif
static const char *programs;
#define STORAGE defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
bool flash_filer::start(const char *programs)
{
#if defined(USE_FS)
::programs = programs;
#if defined(USE_FS)
dir = SPIFFS.openDir(programs);
#elif defined(DISK)
dir = DISK.open(programs);
@ -101,3 +103,46 @@ const char *flash_filer::rewind() {
#endif
return advance();
}
static char buf[32];
static char chkpt[] = { "CHKPOINT" };
static int cpid = 0;
const char *flash_filer::checkpoint() {
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
stop();
snprintf(buf, sizeof(buf), "%s%s.%03d", ::programs, chkpt, cpid++);
#if defined(USE_SD)
File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC);
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_WRITE);
#else
File file = SPIFFS.open(buf, "w");
#endif
hardware_checkpoint(file);
file.close();
start(::programs);
#endif
return buf;
}
void flash_filer::restore(const char *filename) {
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
stop();
snprintf(buf, sizeof(buf), "%s%s", ::programs, filename);
#if defined(USE_SD)
File file = SD.open(buf, O_READ);
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_READ);
#else
File file = SPIFFS.open(buf, "r");
#endif
hardware_restore(file);
file.close();
int n = sscanf(buf + strlen(::programs), "%[A-Z0-9].%d", chkpt, &cpid);
cpid = (n == 1)? 0: cpid+1;
#endif
start(::programs);
}

View File

@ -6,6 +6,9 @@ public:
virtual const char *advance() =0;
virtual const char *rewind() =0;
virtual const char *checkpoint() =0;
virtual void restore(const char *) = 0;
virtual bool start(const char *) =0;
virtual void stop() =0;
};
@ -17,6 +20,9 @@ public:
const char *advance();
const char *rewind();
const char *checkpoint();
void restore(const char *);
bool start(const char *);
void stop();

View File

@ -13,7 +13,6 @@
#include "filer.h"
#include "timed.h"
#include "hardware.h"
#include "checkpoint.h"
#include "sound_dac.h"
#include "sound_pwm.h"