From 67540df29fda162cf237cc705eac09462f507a9f Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Sat, 10 Nov 2018 09:32:33 +0000 Subject: [PATCH] cleanup disk --- checkpoint.cpp | 16 +++++++++++----- hardware.cpp | 6 +++--- hw/esp32-espi-dac.h | 2 +- hw/esp32-utft-dac.h | 2 +- hw/esp8266-pwm.h | 2 +- hw/lm4f-utft-sd.h | 1 + sdtape.cpp | 17 ++++++++++++++++- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/checkpoint.cpp b/checkpoint.cpp index 889a0a9..ca04022 100644 --- a/checkpoint.cpp +++ b/checkpoint.cpp @@ -1,9 +1,11 @@ #include +#include +#include #include "hardware.h" #include "sdtape.h" #include "checkpoint.h" -#if defined(SD_CS) +#if defined(USE_SD) #include #elif defined(USE_SPIFFS) #include @@ -17,13 +19,15 @@ const char *checkpoint(sdtape &tape, const char *dir) { tape.stop(); snprintf(buf, sizeof(buf), "%s%s.%03d", dir, chkpt, cpid++); -#if defined(SD_CS) +#if defined(USE_SD) || defined(USE_SPIFFS) +#if defined(USE_SD) File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC); -#elif defined(USE_SPIFFS) +#else File file = SPIFFS.open(buf, FILE_WRITE); #endif hardware_checkpoint(file); file.close(); +#endif tape.start(dir); return buf; } @@ -32,13 +36,15 @@ void restore(sdtape &tape, const char *dir, const char *filename) { tape.stop(); snprintf(buf, sizeof(buf), "%s%s", dir, filename); -#if defined(SD_CS) +#if defined(USE_SD) || defined(USE_SPIFFS) +#if defined(USE_SD) File file = SD.open(buf, O_READ); -#elif defined(USE_SPIFFS) +#else File file = SPIFFS.open(buf, FILE_READ); #endif hardware_restore(file); file.close(); +#endif int n = sscanf(buf + strlen(dir), "%[A-Z0-9].%d", chkpt, &cpid); cpid = (n == 1)? 0: cpid+1; tape.start(dir); diff --git a/hardware.cpp b/hardware.cpp index 33942ab..be740b3 100644 --- a/hardware.cpp +++ b/hardware.cpp @@ -2,7 +2,7 @@ #include -#if defined(SD_CS) +#if defined(USE_SD) #include #endif @@ -36,7 +36,7 @@ bool hardware_reset() { SPIRAM_DEV.setDataMode(SPI_MODE0); #endif -#if defined(SD_CS) +#if defined(USE_SD) success = SD.begin(SD_CS, 2, SD_SPI); pinMode(SPI_CS, OUTPUT); // without this, the SPI-RAM isn't seen #endif @@ -62,7 +62,7 @@ void hardware_init(CPU &cpu) { pinMode(TFT_BACKLIGHT, OUTPUT); #endif -#if defined(SD_CS) +#if defined(USE_SD) pinMode(SD_CS, OUTPUT); digitalWrite(SD_CS, HIGH); #endif diff --git a/hw/esp32-espi-dac.h b/hw/esp32-espi-dac.h index 40884f4..4a23868 100644 --- a/hw/esp32-espi-dac.h +++ b/hw/esp32-espi-dac.h @@ -19,7 +19,7 @@ #undef SPIRAM_CS // "tape" storage... -#undef SD_CS +#undef USE_SD #define USE_SPIFFS // sound: dac and pwm diff --git a/hw/esp32-utft-dac.h b/hw/esp32-utft-dac.h index 2c6ea4f..407411b 100644 --- a/hw/esp32-utft-dac.h +++ b/hw/esp32-utft-dac.h @@ -15,7 +15,7 @@ #undef SPIRAM_CS // "tape" storage... -#undef SD_CS +#undef USE_SD #define USE_SPIFFS // sound: dac and pwm diff --git a/hw/esp8266-pwm.h b/hw/esp8266-pwm.h index 651906c..1d758c2 100644 --- a/hw/esp8266-pwm.h +++ b/hw/esp8266-pwm.h @@ -19,7 +19,7 @@ #undef SPIRAM_CS // "tape" storage... -#undef SD_CS +#undef USE_SD #undef USE_SPIFFS // sound diff --git a/hw/lm4f-utft-sd.h b/hw/lm4f-utft-sd.h index da34925..c8f4cc5 100644 --- a/hw/lm4f-utft-sd.h +++ b/hw/lm4f-utft-sd.h @@ -19,6 +19,7 @@ #define SPIRAM_SIZE 65536 // "tape" storage... +#define USE_SD #define SD_CS PF_3 #define SD_SPI 1 diff --git a/sdtape.cpp b/sdtape.cpp index 08e727f..4fd9ea5 100644 --- a/sdtape.cpp +++ b/sdtape.cpp @@ -1,6 +1,7 @@ +#include #include "hardware.h" -#if defined(SD_CS) +#if defined(USE_SD) #include #define DISK SD #elif defined(USE_SPIFFS) @@ -10,13 +11,17 @@ #include "sdtape.h" +#if defined(DISK) static File file, dir; +#endif bool sdtape::start(const char *programs) { +#if defined(DISK) dir = DISK.open(programs); if (!dir) return false; +#endif _pos = _len = 0; return true; @@ -24,11 +29,14 @@ bool sdtape::start(const char *programs) void sdtape::stop() { +#if defined(DISK) file.close(); +#endif } bool sdtape::more() { +#if defined(DISK) if (_pos >= _len) { _pos = 0; _len = file.read(_buf, sizeof(_buf)); @@ -36,10 +44,12 @@ bool sdtape::more() if (_len == 0) // eof return false; } +#endif return true; } const char *sdtape::advance() { +#if defined(DISK) bool rewound = false; file.close(); while (true) { @@ -56,9 +66,14 @@ const char *sdtape::advance() { return 0; } return file.name(); +#else + return 0; +#endif } const char *sdtape::rewind() { +#if defined(DISK) dir.rewindDirectory(); +#endif return advance(); }