From a5898ae26af99d6397da3dc2dc7355b2526020c3 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 19 Nov 2010 22:30:40 -0500 Subject: [PATCH] Revising SD library API. Open now returns a file object (which has a close() method); exists() no longer accepts a parent directory. --- libraries/SD/File.cpp | 39 +++++++++++++++++++++++++++++++++++++++ libraries/SD/SD.cpp | 40 ++++++++++++++++++++-------------------- libraries/SD/SD.h | 16 +++++++++++++--- 3 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 libraries/SD/File.cpp diff --git a/libraries/SD/File.cpp b/libraries/SD/File.cpp new file mode 100644 index 000000000..91aaa557b --- /dev/null +++ b/libraries/SD/File.cpp @@ -0,0 +1,39 @@ +/* + + SD - a slightly more friendly wrapper for sdfatlib + + This library aims to expose a subset of SD card functionality + in the form of a higher level "wrapper" object. + + License: GNU General Public License V3 + (Because sdfatlib is licensed with this.) + + (C) Copyright 2010 SparkFun Electronics + + */ + +#include + +void File::write(uint8_t val) { + SD.file.write(val); +} + +void File::write(const char *str) { + SD.file.write(str); +} + +void File::write(const uint8_t *buf, size_t size) { + SD.file.write(buf, size); +} + +int File::read() { + return SD.file.read(); +} + +void File::close() { + SD.file.close(); +} + +File::operator bool() { + return SD.file.isOpen(); +} diff --git a/libraries/SD/SD.cpp b/libraries/SD/SD.cpp index 86bd5919a..05c130dad 100644 --- a/libraries/SD/SD.cpp +++ b/libraries/SD/SD.cpp @@ -327,7 +327,7 @@ void SDClass::begin(uint8_t csPin) { } -boolean SDClass::open(char *filepath, +File SDClass::open(char *filepath, boolean write, boolean append) { /* @@ -368,19 +368,19 @@ boolean SDClass::open(char *filepath, fileOpenMode = oflag; walkPath(filepath, root, callback_openPath, this); - // TODO: Actually return something useful. + return File(); } -boolean SDClass::close() { - /* - - Closes the file opened by the `open` method. - - */ - file.close(); -} +//boolean SDClass::close() { +// /* +// +// Closes the file opened by the `open` method. +// +// */ +// file.close(); +//} boolean SDClass::exists(char *filepath) { @@ -389,19 +389,19 @@ boolean SDClass::exists(char *filepath) { Returns true if the supplied file path exists. */ - return exists(filepath, root); + return walkPath(filepath, root, callback_pathExists); } -boolean SDClass::exists(char *filepath, SdFile& parentDir) { - /* - - Returns true if the supplied file path rooted at `parentDir` - exists. - - */ - return walkPath(filepath, parentDir, callback_pathExists); -} +//boolean SDClass::exists(char *filepath, SdFile& parentDir) { +// /* +// +// Returns true if the supplied file path rooted at `parentDir` +// exists. +// +// */ +// return walkPath(filepath, parentDir, callback_pathExists); +//} boolean SDClass::makeDir(char *filepath) { diff --git a/libraries/SD/SD.h b/libraries/SD/SD.h index 1c0479798..5e8d7c9e9 100644 --- a/libraries/SD/SD.h +++ b/libraries/SD/SD.h @@ -23,6 +23,16 @@ // Use this to configure the chip select pin of the SD card. #define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield +class File : public Print { + public: + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); + int read(); + void close(); + operator bool(); +}; + class SDClass { private: @@ -40,14 +50,14 @@ class SDClass { // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Once opened the file can be accessed via the // `MemoryCard.file` field which is a standard `sdfatlib` file object. - boolean open(char *filename, boolean write = false, boolean append = true); + File open(char *filename, boolean write = false, boolean append = true); // Close an opened file object. - boolean close(); + //boolean close(); // Methods to determine if the requested file path exists. boolean exists(char *filepath); - boolean exists(char *filepath, SdFile& parentDir); + //boolean exists(char *filepath, SdFile& parentDir); // Create the requested directory heirarchy--if intermediate directories // do not exist they will be created.