[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | SD - a slightly more friendly wrapper for sdfatlib
|
---|
| 4 |
|
---|
| 5 | This library aims to expose a subset of SD card functionality
|
---|
| 6 | in the form of a higher level "wrapper" object.
|
---|
| 7 |
|
---|
| 8 | License: GNU General Public License V3
|
---|
| 9 | (Because sdfatlib is licensed with this.)
|
---|
| 10 |
|
---|
| 11 | (C) Copyright 2010 SparkFun Electronics
|
---|
| 12 |
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #ifndef __SD_H__
|
---|
| 16 | #define __SD_H__
|
---|
| 17 |
|
---|
| 18 | #include <Arduino.h>
|
---|
| 19 |
|
---|
| 20 | #include <utility/SdFat.h>
|
---|
| 21 | #include <utility/SdFatUtil.h>
|
---|
| 22 |
|
---|
| 23 | #define FILE_READ O_READ
|
---|
| 24 | #define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
|
---|
| 25 |
|
---|
| 26 | class File : public Stream {
|
---|
| 27 | private:
|
---|
| 28 | char _name[13]; // our name
|
---|
| 29 | SdFile *_file; // underlying file pointer
|
---|
| 30 |
|
---|
| 31 | public:
|
---|
| 32 | File(SdFile f, const char *name); // wraps an underlying SdFile
|
---|
| 33 | File(void); // 'empty' constructor
|
---|
| 34 | virtual size_t write(uint8_t);
|
---|
| 35 | virtual size_t write(const uint8_t *buf, size_t size);
|
---|
| 36 | virtual int read();
|
---|
| 37 | virtual int peek();
|
---|
| 38 | virtual int available();
|
---|
| 39 | virtual void flush();
|
---|
| 40 | int read(void *buf, uint16_t nbyte);
|
---|
| 41 | boolean seek(uint32_t pos);
|
---|
| 42 | uint32_t position();
|
---|
| 43 | uint32_t size();
|
---|
| 44 | void close();
|
---|
| 45 | operator bool();
|
---|
| 46 | char * name();
|
---|
| 47 |
|
---|
| 48 | boolean isDirectory(void);
|
---|
| 49 | File openNextFile(uint8_t mode = O_RDONLY);
|
---|
| 50 | void rewindDirectory(void);
|
---|
| 51 |
|
---|
| 52 | using Print::write;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | class SDClass {
|
---|
| 56 |
|
---|
| 57 | private:
|
---|
| 58 | // These are required for initialisation and use of sdfatlib
|
---|
| 59 | Sd2Card card;
|
---|
| 60 | SdVolume volume;
|
---|
| 61 | SdFile root;
|
---|
| 62 |
|
---|
| 63 | // my quick&dirty iterator, should be replaced
|
---|
| 64 | SdFile getParentDir(const char *filepath, int *indx);
|
---|
| 65 | public:
|
---|
| 66 | // This needs to be called to set up the connection to the SD card
|
---|
| 67 | // before other methods are used.
|
---|
| 68 | boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
|
---|
| 69 |
|
---|
| 70 | // Open the specified file/directory with the supplied mode (e.g. read or
|
---|
| 71 | // write, etc). Returns a File object for interacting with the file.
|
---|
| 72 | // Note that currently only one file can be open at a time.
|
---|
| 73 | File open(const char *filename, uint8_t mode = FILE_READ);
|
---|
| 74 |
|
---|
| 75 | // Methods to determine if the requested file path exists.
|
---|
| 76 | boolean exists(char *filepath);
|
---|
| 77 |
|
---|
| 78 | // Create the requested directory heirarchy--if intermediate directories
|
---|
| 79 | // do not exist they will be created.
|
---|
| 80 | boolean mkdir(char *filepath);
|
---|
| 81 |
|
---|
| 82 | // Delete the file.
|
---|
| 83 | boolean remove(char *filepath);
|
---|
| 84 |
|
---|
| 85 | boolean rmdir(char *filepath);
|
---|
| 86 |
|
---|
| 87 | private:
|
---|
| 88 |
|
---|
| 89 | // This is used to determine the mode used to open a file
|
---|
| 90 | // it's here because it's the easiest place to pass the
|
---|
| 91 | // information through the directory walking function. But
|
---|
| 92 | // it's probably not the best place for it.
|
---|
| 93 | // It shouldn't be set directly--it is set via the parameters to `open`.
|
---|
| 94 | int fileOpenMode;
|
---|
| 95 |
|
---|
| 96 | friend class File;
|
---|
| 97 | friend boolean callback_openPath(SdFile&, char *, boolean, void *);
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | extern SDClass SD;
|
---|
| 101 |
|
---|
| 102 | #endif
|
---|