2010-11-13 19:42:17 +01:00
|
|
|
/*
|
|
|
|
|
2010-11-14 17:48:21 +01:00
|
|
|
SD - a slightly more friendly wrapper for sdfatlib
|
2010-11-13 19:42:17 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-11-20 17:01:38 +01:00
|
|
|
#ifndef __SD_H__
|
|
|
|
#define __SD_H__
|
2010-11-13 19:42:17 +01:00
|
|
|
|
2011-03-02 01:52:13 +01:00
|
|
|
#include <Arduino.h>
|
2010-11-13 19:42:17 +01:00
|
|
|
|
2010-11-13 20:01:37 +01:00
|
|
|
#include <utility/SdFat.h>
|
2010-11-13 20:34:49 +01:00
|
|
|
#include <utility/SdFatUtil.h>
|
2010-11-13 19:42:17 +01:00
|
|
|
|
2010-11-28 20:56:22 +01:00
|
|
|
#define FILE_READ O_READ
|
2011-02-26 00:44:43 +01:00
|
|
|
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
|
2010-11-28 19:43:13 +01:00
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
class File : public Stream {
|
2011-04-18 23:58:54 +02:00
|
|
|
private:
|
|
|
|
char _name[13]; // our name
|
|
|
|
SdFile *_file; // underlying file pointer
|
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
public:
|
2011-04-18 23:58:54 +02:00
|
|
|
File(SdFile f, char *name); // wraps an underlying SdFile
|
|
|
|
File(void); // 'empty' constructor
|
|
|
|
~File(void); // destructor
|
2010-11-20 17:45:05 +01:00
|
|
|
virtual void write(uint8_t);
|
|
|
|
virtual void write(const char *str);
|
|
|
|
virtual void write(const uint8_t *buf, size_t size);
|
|
|
|
virtual int read();
|
|
|
|
virtual int peek();
|
|
|
|
virtual int available();
|
|
|
|
virtual void flush();
|
2011-04-18 23:58:54 +02:00
|
|
|
int read(void *buf, uint16_t nbyte);
|
2010-12-22 22:11:54 +01:00
|
|
|
boolean seek(uint32_t pos);
|
2010-12-22 22:52:09 +01:00
|
|
|
uint32_t position();
|
2010-12-22 22:11:54 +01:00
|
|
|
uint32_t size();
|
2010-11-20 17:45:05 +01:00
|
|
|
void close();
|
|
|
|
operator bool();
|
2011-04-18 23:58:54 +02:00
|
|
|
char * name();
|
|
|
|
|
|
|
|
boolean isDirectory(void);
|
|
|
|
File openNextFile(uint8_t mode = O_RDONLY);
|
|
|
|
void rewindDirectory(void);
|
2010-11-20 04:30:40 +01:00
|
|
|
};
|
|
|
|
|
2010-11-13 20:01:37 +01:00
|
|
|
class SDClass {
|
2010-11-13 19:42:17 +01:00
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
private:
|
2010-11-13 19:42:17 +01:00
|
|
|
// These are required for initialisation and use of sdfatlib
|
|
|
|
Sd2Card card;
|
|
|
|
SdVolume volume;
|
|
|
|
SdFile root;
|
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
// my quick&dirty iterator, should be replaced
|
|
|
|
SdFile getParentDir(char *filepath, int *indx);
|
2010-11-20 17:45:05 +01:00
|
|
|
public:
|
2010-11-20 17:01:38 +01:00
|
|
|
// This needs to be called to set up the connection to the SD card
|
2010-11-13 19:42:17 +01:00
|
|
|
// before other methods are used.
|
2010-11-20 19:50:45 +01:00
|
|
|
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
|
2010-11-13 19:42:17 +01:00
|
|
|
|
|
|
|
// Open the specified file/directory with the supplied mode (e.g. read or
|
2010-11-20 17:01:38 +01:00
|
|
|
// write, etc). Returns a File object for interacting with the file.
|
|
|
|
// Note that currently only one file can be open at a time.
|
2010-11-28 20:56:22 +01:00
|
|
|
File open(char *filename, uint8_t mode = FILE_READ);
|
2010-11-13 19:42:17 +01:00
|
|
|
|
|
|
|
// Methods to determine if the requested file path exists.
|
|
|
|
boolean exists(char *filepath);
|
|
|
|
|
|
|
|
// Create the requested directory heirarchy--if intermediate directories
|
|
|
|
// do not exist they will be created.
|
2010-11-20 18:31:49 +01:00
|
|
|
boolean mkdir(char *filepath);
|
2010-11-20 19:07:59 +01:00
|
|
|
|
|
|
|
// Delete the file.
|
2010-11-20 20:49:20 +01:00
|
|
|
boolean remove(char *filepath);
|
|
|
|
|
|
|
|
boolean rmdir(char *filepath);
|
2010-11-13 19:42:17 +01:00
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
private:
|
2010-11-13 19:42:17 +01:00
|
|
|
|
|
|
|
// This is used to determine the mode used to open a file
|
|
|
|
// it's here because it's the easiest place to pass the
|
|
|
|
// information through the directory walking function. But
|
|
|
|
// it's probably not the best place for it.
|
|
|
|
// It shouldn't be set directly--it is set via the parameters to `open`.
|
2010-11-20 17:01:38 +01:00
|
|
|
int fileOpenMode;
|
|
|
|
|
|
|
|
friend class File;
|
|
|
|
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
|
2010-11-13 19:42:17 +01:00
|
|
|
};
|
|
|
|
|
2010-11-13 20:01:37 +01:00
|
|
|
extern SDClass SD;
|
|
|
|
|
2010-11-13 19:42:17 +01:00
|
|
|
#endif
|