]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/SD/SD.h
first commit
[arduino] / books / pdummies / Libraries / SD / SD.h
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 #if ARDUINO >= 100
19  #include "Arduino.h"
20 #else
21  #include "WProgram.h"
22 #endif
23
24 #include <utility/SdFat.h>
25 #include <utility/SdFatUtil.h>
26
27 #define FILE_READ O_READ
28 #define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
29
30 class File : public Stream {
31  private:
32   char _name[13]; // our name
33   SdFile *_file;  // underlying file pointer
34
35 public:
36   File(SdFile f, char *name);     // wraps an underlying SdFile
37   File(void);      // 'empty' constructor
38   ~File(void);     // destructor
39 #if ARDUINO >= 100
40   virtual size_t write(uint8_t);
41   virtual size_t write(const char *str);
42   virtual size_t write(const uint8_t *buf, size_t size);
43 #else
44   virtual void write(uint8_t);
45   virtual void write(const char *str);
46   virtual void write(const uint8_t *buf, size_t size);
47 #endif
48   virtual int read();
49   virtual int peek();
50   virtual int available();
51   virtual void flush();
52   int read(void *buf, uint16_t nbyte);
53   boolean seek(uint32_t pos);
54   uint32_t position();
55   uint32_t size();
56   void close();
57   operator bool();
58   char * name();
59
60   boolean isDirectory(void);
61   File openNextFile(uint8_t mode = O_RDONLY);
62   void rewindDirectory(void);
63 };
64
65 class SDClass {
66
67 private:
68   // These are required for initialisation and use of sdfatlib
69   Sd2Card card;
70   SdVolume volume;
71   SdFile root;
72   
73   // my quick&dirty iterator, should be replaced
74   SdFile getParentDir(char *filepath, int *indx);
75 public:
76   // This needs to be called to set up the connection to the SD card
77   // before other methods are used.
78   boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, int8_t mosi = -1, int8_t miso = -1, int8_t sck = -1);
79   
80   // Open the specified file/directory with the supplied mode (e.g. read or
81   // write, etc). Returns a File object for interacting with the file.
82   // Note that currently only one file can be open at a time.
83   File open(char *filename, uint8_t mode = FILE_READ);
84
85   // Methods to determine if the requested file path exists.
86   boolean exists(char *filepath);
87
88   // Create the requested directory heirarchy--if intermediate directories
89   // do not exist they will be created.
90   boolean mkdir(char *filepath);
91   
92   // Delete the file.
93   boolean remove(char *filepath);
94   
95   boolean rmdir(char *filepath);
96
97   void enableCRC(boolean mode);
98
99 private:
100
101   // This is used to determine the mode used to open a file
102   // it's here because it's the easiest place to pass the 
103   // information through the directory walking function. But
104   // it's probably not the best place for it.
105   // It shouldn't be set directly--it is set via the parameters to `open`.
106   int fileOpenMode;
107   
108   friend class File;
109   friend boolean callback_openPath(SdFile&, char *, boolean, void *); 
110 };
111
112 extern SDClass SD;
113
114 #endif