From 842a235ec4352b4d4d040743a87bc7b69daef4f9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 7 Jun 2013 17:39:03 +0200 Subject: Bridge: Added File I/O --- libraries/Bridge/Bridge.cpp | 47 ++++++++++ libraries/Bridge/Bridge.h | 14 +++ libraries/Bridge/FileIO.cpp | 173 +++++++++++++++++++++++++++++++++++ libraries/Bridge/FileIO.cpp.disabled | 162 -------------------------------- libraries/Bridge/FileIO.h | 24 +++-- 5 files changed, 251 insertions(+), 169 deletions(-) create mode 100644 libraries/Bridge/FileIO.cpp delete mode 100644 libraries/Bridge/FileIO.cpp.disabled 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> 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(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(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(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 b/libraries/Bridge/FileIO.cpp new file mode 100644 index 0000000..04abc0d --- /dev/null +++ b/libraries/Bridge/FileIO.cpp @@ -0,0 +1,173 @@ +/* + Copyright (c) 2013 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +File::File(BridgeClass &b) : mode(255), bridge(b) { + // Empty +} + +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() { + return (mode != 255); +} + +File::~File() { + close(); +} + +size_t File::write(uint8_t c) { + return write(&c, 1); +} + +size_t File::write(const uint8_t *buf, size_t size) { + if (mode == 255) + return -1; + uint8_t err; + bridge.fileWrite(handle, buf, size, err); + if (err != 0) + return -err; + return size; +} + +int File::read() { + doBuffer(); + if (buffered == 0) + return -1; // no chars available + else { + buffered--; + return buffer[readPos++]; + } +} + +int File::peek() { + 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() { + // Look if there is new data available + doBuffer(); + return buffered; +} + +void File::flush() { +} + +//int read(void *buf, uint16_t nbyte) +//uint32_t position() +//uint32_t size() + +void File::close() { + if (mode == 255) + return; + bridge.fileClose(handle); + mode = 255; +} + +const char *File::name() { + return filename.c_str(); +} + +//boolean isDirectory(void) +//File openNextFile(uint8_t mode = O_RDONLY); +//void rewindDirectory(void) + + + + + + +boolean SDClass::begin() { + return true; +} + +File SDClass::open(const char *filename, uint8_t mode) { +// 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) { + Process ls; + ls.begin("ls"); + ls.addParameter(filepath); + int res = ls.run(); + return (res == 0); +} + +boolean SDClass::mkdir(const char *filepath) { + Process mk; + mk.begin("mkdir"); + mk.addParameter("-p"); + mk.addParameter(filepath); + int res = mk.run(); + return (res == 0); +} + +boolean SDClass::remove(const char *filepath) { + Process rm; + rm.begin("rm"); + rm.addParameter(filepath); + int res = rm.run(); + return (res == 0); +} + +boolean SDClass::rmdir(const char *filepath) { + Process rm; + rm.begin("rmdir"); + rm.addParameter(filepath); + int res = rm.run(); + return (res == 0); +} + +SDClass SD; diff --git a/libraries/Bridge/FileIO.cpp.disabled b/libraries/Bridge/FileIO.cpp.disabled deleted file mode 100644 index fc6d3df..0000000 --- a/libraries/Bridge/FileIO.cpp.disabled +++ /dev/null @@ -1,162 +0,0 @@ -/* - Copyright (c) 2013 Arduino LLC. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include - -File::File() : mode(255), filename(NULL) { - // Empty -} - -File::File(const char *_filename, uint8_t _mode) : mode(_mode) { - filename = new char[strlen(_filename)+1]; - strcpy(filename, _filename); -} - -File::operator bool() { - return (mode != 255); -} - -File::~File() { - if (filename) - 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) { - 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; -} - -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> 4), toHex(c & 0x0F), '\0' }; - echo.addParameter(chars, true); - } - echo.addParameter("\"", true); - echo.run(); - return size; -} - -int File::read() { - return 1; -} - -int File::peek() { - return 1; -} - -int File::available() { - return 1; -} - -void File::flush() { -} - -//int read(void *buf, uint16_t nbyte) -//boolean seek(uint32_t pos) -//uint32_t position() -//uint32_t size() - -void File::close() { - mode = 255; -} - -char *File::name() { - return filename; -} - -//boolean isDirectory(void) -//File openNextFile(uint8_t mode = O_RDONLY); -//void rewindDirectory(void) - - - - - - -boolean SDClass::begin() { - return true; -} - -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(); -} - -boolean SDClass::exists(const char *filepath) { - Process ls; - ls.begin("ls"); - ls.addParameter(filepath); - int res = ls.run(); - return (res == 0); -} - -boolean SDClass::mkdir(const char *filepath) { - Process mk; - mk.begin("mkdir"); - mk.addParameter("-p"); - mk.addParameter(filepath); - int res = mk.run(); - return (res == 0); -} - -boolean SDClass::remove(const char *filepath) { - Process rm; - rm.begin("rm"); - rm.addParameter(filepath); - int res = rm.run(); - return (res == 0); -} - -boolean SDClass::rmdir(const char *filepath) { - Process rm; - rm.begin("rmdir"); - rm.addParameter(filepath); - int res = rm.run(); - return (res == 0); -} - -SDClass SD; 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 @@ -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 { -- cgit v1.2.3-18-g5258