sdtape abstraction

This commit is contained in:
Stephen Crane 2014-10-26 17:54:01 +00:00
parent 72f53947d2
commit cd9744757c
3 changed files with 74 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include "ps2drv.h"
#include "utftdisplay.h"
#include "keyboard.h"
#include "sdtape.h"
#include "hardware.h"
#endif

52
sdtape.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <SD.h>
#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();
}

21
sdtape.h Normal file
View File

@ -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