From b41d62b516cb75f0cf930da16c9b5e250c599269 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 10 Jun 2013 11:23:14 +0200 Subject: [PATCH] Fixed return type for Bridge.transfer(). Refactored File I/O class. --- .../arduino/avr/libraries/Bridge/Bridge.cpp | 49 +-------------- .../arduino/avr/libraries/Bridge/Bridge.h | 28 +++------ .../arduino/avr/libraries/Bridge/FileIO.cpp | 63 ++++++++++++------- 3 files changed, 49 insertions(+), 91 deletions(-) diff --git a/hardware/arduino/avr/libraries/Bridge/Bridge.cpp b/hardware/arduino/avr/libraries/Bridge/Bridge.cpp index 266905c96..4787cb911 100644 --- a/hardware/arduino/avr/libraries/Bridge/Bridge.cpp +++ b/hardware/arduino/avr/libraries/Bridge/Bridge.cpp @@ -105,53 +105,6 @@ 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); @@ -204,7 +157,7 @@ bool BridgeClass::crcCheck(uint16_t _CRC) { return CRC == _CRC; } -uint8_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1, +uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1, const uint8_t *buff2, uint16_t len2, const uint8_t *buff3, uint16_t len3, uint8_t *rxbuff, uint16_t rxlen) diff --git a/hardware/arduino/avr/libraries/Bridge/Bridge.h b/hardware/arduino/avr/libraries/Bridge/Bridge.h index 6e54bbd64..0c8617563 100644 --- a/hardware/arduino/avr/libraries/Bridge/Bridge.h +++ b/hardware/arduino/avr/libraries/Bridge/Bridge.h @@ -42,20 +42,6 @@ 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); @@ -79,20 +65,20 @@ public: void flush() { stream.flush(); } // Trasnfer a frame (with error correction and response) - uint8_t transfer(const uint8_t *buff1, uint16_t len1, + uint16_t transfer(const uint8_t *buff1, uint16_t len1, const uint8_t *buff2, uint16_t len2, const uint8_t *buff3, uint16_t len3, uint8_t *rxbuff, uint16_t rxlen); // multiple inline versions of the same function to allow efficient frame concatenation - uint8_t transfer(const uint8_t *buff1, uint16_t len1) - { transfer(buff1, len1, NULL, 0); } - uint8_t transfer(const uint8_t *buff1, uint16_t len1, + uint16_t transfer(const uint8_t *buff1, uint16_t len1) + { return transfer(buff1, len1, NULL, 0); } + uint16_t transfer(const uint8_t *buff1, uint16_t len1, uint8_t *rxbuff, uint16_t rxlen) - { transfer(buff1, len1, NULL, 0, rxbuff, rxlen); } - uint8_t transfer(const uint8_t *buff1, uint16_t len1, + { return transfer(buff1, len1, NULL, 0, rxbuff, rxlen); } + uint16_t transfer(const uint8_t *buff1, uint16_t len1, const uint8_t *buff2, uint16_t len2, uint8_t *rxbuff, uint16_t rxlen) - { transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); } + { return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); } private: uint8_t index; int timedRead(unsigned int timeout); diff --git a/hardware/arduino/avr/libraries/Bridge/FileIO.cpp b/hardware/arduino/avr/libraries/Bridge/FileIO.cpp index 04abc0dd2..bd8f48729 100644 --- a/hardware/arduino/avr/libraries/Bridge/FileIO.cpp +++ b/hardware/arduino/avr/libraries/Bridge/FileIO.cpp @@ -24,11 +24,15 @@ File::File(BridgeClass &b) : mode(255), bridge(b) { 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) + uint8_t cmd[] = {'F', modes[mode]}; + uint8_t res[2]; + bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2); + if (res[0] != 0) { // res[0] contains error code mode = 255; // In case of error keep the file closed + return; + } + handle = res[1]; buffered = 0; } @@ -47,10 +51,11 @@ size_t File::write(uint8_t c) { 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; + uint8_t cmd[] = {'g', handle}; + uint8_t res[1]; + bridge.transfer(cmd, 2, buf, size, res, 1); + if (res[0] != 0) // res[0] contains error code + return -res[0]; return size; } @@ -72,10 +77,23 @@ int File::peek() { return buffer[readPos]; } -boolean File::seek(uint32_t pos) { - uint8_t err; - bridge.fileSeek(handle, pos, err); - return err==0; +boolean File::seek(uint32_t position) { + uint8_t cmd[] = { + 's', + handle, + (position >> 24) & 0xFF, + (position >> 16) & 0xFF, + (position >> 8) & 0xFF, + position & 0xFF + }; + uint8_t res[1]; + bridge.transfer(cmd, 6, res, 1); + if (res[0]==0) { + // If seek succeed then flush buffers + buffered = 0; + return true; + } + return false; } void File::doBuffer() { @@ -85,8 +103,14 @@ void File::doBuffer() { // Try to buffer up to 32 characters readPos = 0; - uint8_t err; - buffered = bridge.fileRead(handle, buffer, sizeof(buffer), err); + uint8_t cmd[] = {'G', handle, sizeof(buffer)}; + buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer)) - 1; + //err = buff[0]; // First byte is error code + if (buffered>0) { + // Shift the reminder of buffer + for (uint8_t i=0; i