aboutsummaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@bug.st>2013-06-07 17:39:03 +0200
committerCristian Maglie <c.maglie@bug.st>2013-06-07 17:39:03 +0200
commit842a235ec4352b4d4d040743a87bc7b69daef4f9 (patch)
tree09d15fa176de76e78fd239c4cc007d76aef96a1e /libraries
parent514b18bb059f86a7e9f125c529befa914a1cde48 (diff)
Bridge: Added File I/O
Diffstat (limited to 'libraries')
-rw-r--r--libraries/Bridge/Bridge.cpp47
-rw-r--r--libraries/Bridge/Bridge.h14
-rw-r--r--libraries/Bridge/FileIO.cpp (renamed from libraries/Bridge/FileIO.cpp.disabled)113
-rw-r--r--libraries/Bridge/FileIO.h24
4 files changed, 140 insertions, 58 deletions
diff --git a/libraries/Bridge/Bridge.cpp b/libraries/Bridge/Bridge.cpp
index e612ad8..2f30750 100644
--- a/libraries/Bridge/Bridge.cpp
+++ b/libraries/Bridge/Bridge.cpp
@@ -101,6 +101,53 @@ void BridgeClass::writeCommandInput(uint8_t handle,
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) {
uint8_t tmp[] = { 'm' };
return transfer(tmp, 1, buff, size);
diff --git a/libraries/Bridge/Bridge.h b/libraries/Bridge/Bridge.h
index c7b5361..6043e15 100644
--- a/libraries/Bridge/Bridge.h
+++ b/libraries/Bridge/Bridge.h
@@ -42,6 +42,20 @@ public:
void writeCommandInput(uint8_t handle, const char *buff, unsigned int 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
unsigned int readMessage(uint8_t *buffer, unsigned int size);
void writeMessage(const uint8_t *buffer, unsigned int size);
diff --git a/libraries/Bridge/FileIO.cpp.disabled b/libraries/Bridge/FileIO.cpp
index fc6d3df..04abc0d 100644
--- a/libraries/Bridge/FileIO.cpp.disabled
+++ b/libraries/Bridge/FileIO.cpp
@@ -18,13 +18,18 @@
#include <FileIO.h>
-File::File() : mode(255), filename(NULL) {
+File::File(BridgeClass &b) : mode(255), bridge(b) {
// Empty
}
-File::File(const char *_filename, uint8_t _mode) : mode(_mode) {
- filename = new char[strlen(_filename)+1];
- strcpy(filename, _filename);
+File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
+ 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() {
@@ -32,69 +37,80 @@ File::operator bool() {
}
File::~File() {
- if (filename)
- delete[] filename;
-}
-
-char toHex(uint8_t c) {
- if (c<10)
- return '0' + c;
- else
- return 'A' + c - 10;
+ close();
}
size_t File::write(uint8_t c) {
- Process echo;
- echo.begin("arduino-append");
- echo.addParameter(filename);
- char chars[] = { '\\', 'x', toHex(c >> 4), toHex(c & 0x0F), '\0' };
- echo.addParameter(chars);
- echo.run();
- return 1;
+ return write(&c, 1);
}
size_t File::write(const uint8_t *buf, size_t size) {
- Process echo;
- echo.begin("arduino-append");
- echo.addParameter(filename);
- echo.addParameter(" \"", true);
- for (unsigned int i=0; i<size; i++) {
- // slow but requires less memory
- char c = buf[i];
- char chars[] = { '\\', 'x', toHex(c >> 4), toHex(c & 0x0F), '\0' };
- echo.addParameter(chars, true);
- }
- echo.addParameter("\"", true);
- echo.run();
+ if (mode == 255)
+ return -1;
+ uint8_t err;
+ bridge.fileWrite(handle, buf, size, err);
+ if (err != 0)
+ return -err;
return size;
}
int File::read() {
- return 1;
+ doBuffer();
+ if (buffered == 0)
+ return -1; // no chars available
+ else {
+ buffered--;
+ return buffer[readPos++];
+ }
}
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() {
- return 1;
+ // Look if there is new data available
+ doBuffer();
+ return buffered;
}
void File::flush() {
}
//int read(void *buf, uint16_t nbyte)
-//boolean seek(uint32_t pos)
//uint32_t position()
//uint32_t size()
void File::close() {
+ if (mode == 255)
+ return;
+ bridge.fileClose(handle);
mode = 255;
}
-char *File::name() {
- return filename;
+const char *File::name() {
+ return filename.c_str();
}
//boolean isDirectory(void)
@@ -111,19 +127,14 @@ boolean SDClass::begin() {
}
File SDClass::open(const char *filename, uint8_t mode) {
- if (mode == FILE_READ) {
- if (exists(filename))
- return File(filename, mode);
- }
- if (mode == FILE_WRITE) {
- Process touch;
- touch.begin(">");
- touch.addParameter(filename);
- int res = touch.run();
- if (res == 0)
- return File(filename, mode);
- }
- return File();
+// if (mode == FILE_READ) {
+// if (exists(filename))
+// return File(filename, mode);
+// }
+// if (mode == FILE_WRITE || mode == FILE_APPEND) {
+ return File(filename, mode);
+// }
+// return File();
}
boolean SDClass::exists(const char *filepath) {
diff --git a/libraries/Bridge/FileIO.h b/libraries/Bridge/FileIO.h
index a90ff46..19da646 100644
--- a/libraries/Bridge/FileIO.h
+++ b/libraries/Bridge/FileIO.h
@@ -16,8 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifndef __SD_H__
-#define __SD_H__
+#ifndef __FILEIO_H__
+#define __FILEIO_H__
#include <Process.h>
@@ -26,12 +26,13 @@
#define FILE_READ 0
#define FILE_WRITE 1
+#define FILE_APPEND 2
-class File : public Process {
+class File : public Stream {
public:
- File();
- File(const char *_filename, uint8_t _mode);
+ File(BridgeClass &b = Bridge);
+ File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
~File();
virtual size_t write(uint8_t);
@@ -46,17 +47,26 @@ public:
uint32_t size();
void close();
operator bool();
- char * name();
+ const char * name();
boolean isDirectory(void);
//File openNextFile(uint8_t mode = O_RDONLY);
void rewindDirectory(void);
using Print::write;
+
+private:
+ void doBuffer();
+ uint8_t buffered;
+ uint8_t readPos;
+ static const int BUFFER_SIZE = 64;
+ uint8_t buffer[BUFFER_SIZE];
private:
- char *filename;
+ BridgeClass &bridge;
+ String filename;
uint8_t mode;
+ uint8_t handle;
};
class SDClass {