1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-12-10 14:49:19 +00:00

configure filers via ctor

This commit is contained in:
Stephen Crane 2019-03-26 19:27:25 +00:00
parent e90fb2c6e7
commit d7c82c67a1
7 changed files with 34 additions and 27 deletions

View File

@ -22,17 +22,14 @@ static File file;
static Dir dir;
#endif
static const char *programs;
#define STORAGE defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
bool flash_filer::start(const char *programs)
bool flash_filer::start()
{
::programs = programs;
#if defined(USE_FS)
dir = SPIFFS.openDir(programs);
dir = SPIFFS.openDir(_programs);
#elif defined(DISK)
dir = DISK.open(programs);
dir = DISK.open(_programs);
if (!dir)
return false;
#endif
@ -72,7 +69,7 @@ const char *flash_filer::advance() {
file = dir.openFile("r");
break;
}
dir = SPIFFS.openDir(programs);
dir = SPIFFS.openDir(_programs);
}
strncpy(buf, dir.fileName().c_str(), sizeof(buf));
return buf;
@ -112,7 +109,7 @@ 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++);
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);
@ -123,7 +120,7 @@ const char *flash_filer::checkpoint() {
#endif
hardware_checkpoint(file);
file.close();
start(::programs);
start();
#endif
return buf;
}
@ -131,7 +128,7 @@ const char *flash_filer::checkpoint() {
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);
snprintf(buf, sizeof(buf), "%s%s", _programs, filename);
#if defined(USE_SD)
File file = SD.open(buf, O_READ);
@ -142,9 +139,9 @@ void flash_filer::restore(const char *filename) {
#endif
hardware_restore(file);
file.close();
int n = sscanf(buf + strlen(::programs), "%[A-Z0-9].%d", chkpt, &cpid);
int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid);
cpid = (n == 1)? 0: cpid+1;
#endif
start(::programs);
start();
}
#endif

View File

@ -9,7 +9,7 @@ public:
virtual const char *checkpoint() =0;
virtual void restore(const char *) = 0;
virtual bool start(const char *) =0;
virtual bool start() =0;
virtual void stop() =0;
};
@ -17,19 +17,22 @@ public:
// implement write to new file (like checkpoint)
class flash_filer: public filer {
public:
flash_filer(const char *programs): _programs(programs) {}
const char *advance();
const char *rewind();
const char *checkpoint();
void restore(const char *);
bool start(const char *);
bool start();
void stop();
uint8_t read() { return _buf[_pos++]; }
bool more();
private:
const char *_programs;
unsigned _pos, _len;
uint8_t _buf[128];
};

View File

@ -12,6 +12,7 @@
#include "serialio.h"
#include "filer.h"
#include "serial_filer.h"
#include "socket_filer.h"
#include "timed.h"
#include "hardware.h"
#include "sound_dac.h"

View File

@ -5,10 +5,6 @@
#include "filer.h"
#include "serial_filer.h"
bool serial_filer::start(const char *) {
return true;
}
const unsigned speeds[] = {
115200, 57600, 19200, 9600, 4800, 2400
};
@ -16,7 +12,7 @@ const unsigned speeds[] = {
const char *serial_filer::advance() {
static char buf[16];
unsigned s = speeds[_currsp];
Serial.begin(s);
_serial.begin(s);
_currsp++;
if (_currsp == sizeof(speeds)/sizeof(speeds[0]))
_currsp = 0;
@ -24,15 +20,15 @@ const char *serial_filer::advance() {
}
void serial_filer::write(uint8_t b) {
Serial.write(b);
_serial.write(b);
}
uint8_t serial_filer::read() {
return Serial.read();
return _serial.read();
}
bool serial_filer::more() {
return Serial.available() > 0;
return _serial.available() > 0;
}
#if !defined(NO_CHECKPOINT)

View File

@ -1,17 +1,21 @@
#ifndef __SERIAL_FILER_H__
#define __SERIAL_FILER_H__
class HardwareSerial;
// see https://playground.arduino.cc/Interfacing/LinuxTTY
// FIXME: do this in minicom config file
class serial_filer: public filer {
public:
serial_filer(HardwareSerial &serial): _serial(serial) {}
const char *advance();
const char *rewind() { _currsp = 0; return advance(); }
const char *checkpoint();
void restore(const char *);
bool start(const char *);
bool start() { return true; }
void stop() {}
uint8_t read();
@ -19,6 +23,7 @@ public:
void write(uint8_t);
private:
HardwareSerial &_serial;
unsigned _currsp;
};
#endif

View File

@ -7,7 +7,7 @@
#if !defined(ESP32)
#pragma message "no socket filer"
#else
#elif defined(WIFI_SSID)
#include <WiFi.h>
static WiFiServer server(23);
@ -20,11 +20,11 @@ static bool connected() {
return client.connected();
}
// FIXME: hostname
bool socket_filer::start(const char *) {
bool socket_filer::start() {
#if defined(WIFI_SSID)
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.setHostname(_hostname);
for (int i=0; i < 60 && WiFi.status() != WL_CONNECTED; i++)
delay(1000);

View File

@ -3,17 +3,22 @@
class socket_filer: public filer {
public:
socket_filer(const char *hostname): _hostname(hostname) {}
const char *advance() { return 0; }
const char *rewind() { return advance(); }
const char *checkpoint();
void restore(const char *);
bool start(const char *);
bool start();
void stop() {}
uint8_t read();
bool more();
void write(uint8_t);
private:
const char *_hostname;
};
#endif