r65emu/sdtape.cpp

103 lines
1.6 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
2014-10-26 17:54:01 +00:00
#include "sdtape.h"
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-16 12:37:38 +00:00
static const char *programs;
2018-11-10 09:32:33 +00:00
#endif
2014-10-26 17:54:01 +00:00
2018-11-16 12:30:09 +00:00
#define STORAGE defined(USE_SD) || defined(USE_SPIFFS) || defined(USE_FS)
2018-09-06 13:21:17 +00:00
bool sdtape::start(const char *programs)
2014-10-26 17:54:01 +00:00
{
2018-11-16 12:30:09 +00:00
#if defined(USE_FS)
2018-11-16 12:35:07 +00:00
::programs = programs;
dir = SPIFFS.openDir(programs);
2018-11-11 11:59:43 +00:00
#elif defined(DISK)
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
}
void sdtape::stop()
{
2018-11-18 12:20:05 +00:00
#if STORAGE
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
}
bool sdtape::more()
{
if (_pos >= _len) {
_pos = 0;
2018-11-18 12:20:05 +00:00
#if STORAGE
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;
}
const char *sdtape::advance() {
2018-11-18 12:20:05 +00:00
#if STORAGE
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;
}
2018-11-16 12:35:07 +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
}
const char *sdtape::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();
}