Bridge: Added File I/O

This commit is contained in:
Cristian Maglie 2013-06-07 17:39:03 +02:00
parent 514b18bb05
commit 842a235ec4
4 changed files with 140 additions and 58 deletions

View File

@ -101,6 +101,53 @@ void BridgeClass::writeCommandInput(uint8_t handle,
transfer(cmd, 2, buff, size, NULL, 0); transfer(cmd, 2, buff, size, NULL, 0);
} }
uint8_t BridgeClass::fileOpen(String &file, uint8_t mode, uint8_t &err) {
uint8_t cmd[] = {'F', mode};
uint8_t res[2];
transfer(cmd, 2, (uint8_t*)file.c_str(), file.length(), res, 2);
err = res[0];
return res[1];
}
void BridgeClass::fileClose(uint8_t handle) {
uint8_t cmd[] = {'f', handle};
transfer(cmd, 2);
}
unsigned int BridgeClass::fileRead(uint8_t handle, uint8_t *buff, unsigned int size, uint8_t &err) {
uint8_t s = size > 255 ? 255 : size-1;
uint8_t cmd[] = {'G', handle, s};
uint8_t l = transfer(cmd, 3, buff, size) - 1;
err = buff[0]; // First byte is error code
if (l>0) {
// Shift the reminder of buffer
for (uint8_t i=0; i<l; i++)
buff[i] = buff[i+1];
}
return l;
}
void BridgeClass::fileWrite(uint8_t handle, const uint8_t *buff, unsigned int size, uint8_t &err) {
uint8_t cmd[] = {'g', handle};
uint8_t res[1];
transfer(cmd, 2, buff, size, res, 1);
err = res[0];
}
void BridgeClass::fileSeek(uint8_t handle, uint32_t position, uint8_t &err) {
uint8_t cmd[] = {
's',
handle,
(position >> 24) & 0xFF,
(position >> 16) & 0xFF,
(position >> 8) & 0xFF,
position & 0xFF
};
uint8_t res[1];
transfer(cmd, 6, res, 1);
err = res[0];
}
unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) { unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) {
uint8_t tmp[] = { 'm' }; uint8_t tmp[] = { 'm' };
return transfer(tmp, 1, buff, size); return transfer(tmp, 1, buff, size);

View File

@ -42,6 +42,20 @@ public:
void writeCommandInput(uint8_t handle, const char *buff, unsigned int size) void writeCommandInput(uint8_t handle, const char *buff, unsigned int size)
{ writeCommandInput(handle, reinterpret_cast<const uint8_t *>(buff), size); } { writeCommandInput(handle, reinterpret_cast<const uint8_t *>(buff), size); }
// Methods to handle files
uint8_t fileOpen(String &file, uint8_t mode, uint8_t &err);
void fileClose(uint8_t handle);
unsigned int fileRead(uint8_t handle, uint8_t *buff, unsigned int size, uint8_t &err);
unsigned int fileRead(uint8_t handle, char *buff, unsigned int size, uint8_t &err)
{ return fileRead(handle, reinterpret_cast<uint8_t *>(buff), size, err); }
void fileWrite(uint8_t handle, const uint8_t *buff, unsigned int size, uint8_t &err);
void fileWrite(uint8_t handle, const char *buff, unsigned int size, uint8_t &err)
{ fileWrite(handle, reinterpret_cast<const uint8_t *>(buff), size, err); }
void fileSeek(uint8_t handle, uint32_t position, uint8_t &err);
// Methods to handle mailbox messages // Methods to handle mailbox messages
unsigned int readMessage(uint8_t *buffer, unsigned int size); unsigned int readMessage(uint8_t *buffer, unsigned int size);
void writeMessage(const uint8_t *buffer, unsigned int size); void writeMessage(const uint8_t *buffer, unsigned int size);

View File

@ -18,13 +18,18 @@
#include <FileIO.h> #include <FileIO.h>
File::File() : mode(255), filename(NULL) { File::File(BridgeClass &b) : mode(255), bridge(b) {
// Empty // Empty
} }
File::File(const char *_filename, uint8_t _mode) : mode(_mode) { File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
filename = new char[strlen(_filename)+1]; filename = _filename;
strcpy(filename, _filename); uint8_t err;
char modes[] = {'r','w','a'};
handle = bridge.fileOpen(filename, modes[mode], err);
if (err != 0)
mode = 255; // In case of error keep the file closed
buffered = 0;
} }
File::operator bool() { File::operator bool() {
@ -32,69 +37,80 @@ File::operator bool() {
} }
File::~File() { File::~File() {
if (filename) close();
delete[] filename;
}
char toHex(uint8_t c) {
if (c<10)
return '0' + c;
else
return 'A' + c - 10;
} }
size_t File::write(uint8_t c) { size_t File::write(uint8_t c) {
Process echo; return write(&c, 1);
echo.begin("arduino-append");
echo.addParameter(filename);
char chars[] = { '\\', 'x', toHex(c >> 4), toHex(c & 0x0F), '\0' };
echo.addParameter(chars);
echo.run();
return 1;
} }
size_t File::write(const uint8_t *buf, size_t size) { size_t File::write(const uint8_t *buf, size_t size) {
Process echo; if (mode == 255)
echo.begin("arduino-append"); return -1;
echo.addParameter(filename); uint8_t err;
echo.addParameter(" \"", true); bridge.fileWrite(handle, buf, size, err);
for (unsigned int i=0; i<size; i++) { if (err != 0)
// slow but requires less memory return -err;
char c = buf[i];
char chars[] = { '\\', 'x', toHex(c >> 4), toHex(c & 0x0F), '\0' };
echo.addParameter(chars, true);
}
echo.addParameter("\"", true);
echo.run();
return size; return size;
} }
int File::read() { int File::read() {
return 1; doBuffer();
if (buffered == 0)
return -1; // no chars available
else {
buffered--;
return buffer[readPos++];
}
} }
int File::peek() { int File::peek() {
return 1; doBuffer();
if (buffered == 0)
return -1; // no chars available
else
return buffer[readPos];
}
boolean File::seek(uint32_t pos) {
uint8_t err;
bridge.fileSeek(handle, pos, err);
return err==0;
}
void File::doBuffer() {
// If there are already char in buffer exit
if (buffered > 0)
return;
// Try to buffer up to 32 characters
readPos = 0;
uint8_t err;
buffered = bridge.fileRead(handle, buffer, sizeof(buffer), err);
} }
int File::available() { int File::available() {
return 1; // Look if there is new data available
doBuffer();
return buffered;
} }
void File::flush() { void File::flush() {
} }
//int read(void *buf, uint16_t nbyte) //int read(void *buf, uint16_t nbyte)
//boolean seek(uint32_t pos)
//uint32_t position() //uint32_t position()
//uint32_t size() //uint32_t size()
void File::close() { void File::close() {
if (mode == 255)
return;
bridge.fileClose(handle);
mode = 255; mode = 255;
} }
char *File::name() { const char *File::name() {
return filename; return filename.c_str();
} }
//boolean isDirectory(void) //boolean isDirectory(void)
@ -111,19 +127,14 @@ boolean SDClass::begin() {
} }
File SDClass::open(const char *filename, uint8_t mode) { File SDClass::open(const char *filename, uint8_t mode) {
if (mode == FILE_READ) { // if (mode == FILE_READ) {
if (exists(filename)) // if (exists(filename))
// return File(filename, mode);
// }
// if (mode == FILE_WRITE || mode == FILE_APPEND) {
return File(filename, mode); return File(filename, mode);
} // }
if (mode == FILE_WRITE) { // return File();
Process touch;
touch.begin(">");
touch.addParameter(filename);
int res = touch.run();
if (res == 0)
return File(filename, mode);
}
return File();
} }
boolean SDClass::exists(const char *filepath) { boolean SDClass::exists(const char *filepath) {

View File

@ -16,8 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#ifndef __SD_H__ #ifndef __FILEIO_H__
#define __SD_H__ #define __FILEIO_H__
#include <Process.h> #include <Process.h>
@ -26,12 +26,13 @@
#define FILE_READ 0 #define FILE_READ 0
#define FILE_WRITE 1 #define FILE_WRITE 1
#define FILE_APPEND 2
class File : public Process { class File : public Stream {
public: public:
File(); File(BridgeClass &b = Bridge);
File(const char *_filename, uint8_t _mode); File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
~File(); ~File();
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
@ -46,7 +47,7 @@ public:
uint32_t size(); uint32_t size();
void close(); void close();
operator bool(); operator bool();
char * name(); const char * name();
boolean isDirectory(void); boolean isDirectory(void);
//File openNextFile(uint8_t mode = O_RDONLY); //File openNextFile(uint8_t mode = O_RDONLY);
@ -55,8 +56,17 @@ public:
using Print::write; using Print::write;
private: private:
char *filename; void doBuffer();
uint8_t buffered;
uint8_t readPos;
static const int BUFFER_SIZE = 64;
uint8_t buffer[BUFFER_SIZE];
private:
BridgeClass &bridge;
String filename;
uint8_t mode; uint8_t mode;
uint8_t handle;
}; };
class SDClass { class SDClass {