From cd9744757cd1d1d0ea376758173820c79e03c4b0 Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Sun, 26 Oct 2014 17:54:01 +0000 Subject: [PATCH] sdtape abstraction --- r65emu.h | 1 + sdtape.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sdtape.h | 21 +++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 sdtape.cpp create mode 100644 sdtape.h diff --git a/r65emu.h b/r65emu.h index 47b7166..0e41628 100644 --- a/r65emu.h +++ b/r65emu.h @@ -10,6 +10,7 @@ #include "ps2drv.h" #include "utftdisplay.h" #include "keyboard.h" +#include "sdtape.h" #include "hardware.h" #endif diff --git a/sdtape.cpp b/sdtape.cpp new file mode 100644 index 0000000..6faec88 --- /dev/null +++ b/sdtape.cpp @@ -0,0 +1,52 @@ +#include +#include "sdtape.h" + +static File file, dir; + +void sdtape::start(const char *programs) +{ + dir = SD.open(programs); +} + +void sdtape::stop() +{ + file.close(); +} + +bool sdtape::more() +{ + if (_pos >= _len) { + _pos = 0; + _len = file.read(_buf, sizeof(_buf)); + + if (_len == 0) { + file.close(); + return false; // eof + } + } + return true; +} + +const char *sdtape::advance() { + bool rewound = false; + file.close(); + 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(); +} + +const char *sdtape::rewind() { + dir.rewindDirectory(); + return advance(); +} diff --git a/sdtape.h b/sdtape.h new file mode 100644 index 0000000..6d8146b --- /dev/null +++ b/sdtape.h @@ -0,0 +1,21 @@ +#ifndef _SDTAPE_H +#define _SDTAPE_H + +class sdtape { +public: + const char *advance(); + const char *rewind(); + + void start(const char *); + void stop(); + + sdtape(): _pos(0), _len(0) {} + + byte read() { return _buf[_pos++]; } + bool more(); + +private: + unsigned int _pos, _len; + byte _buf[128]; +}; +#endif