Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 119 lines (90 sloc) 3.123 kb
b707f7a @ladyada added seek() and buffered read()
ladyada authored
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
587d4ec @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
18 #if ARDUINO >= 100
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
19 #include "Arduino.h"
587d4ec @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
20 #else
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
21 #include "WProgram.h"
587d4ec @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
22 #endif
b707f7a @ladyada added seek() and buffered read()
ladyada authored
23
e05acdc @driverblock Include SPI.h
driverblock authored
24 #include <SPI.h>
b707f7a @ladyada added seek() and buffered read()
ladyada authored
25 #include <utility/SdFat.h>
26 #include <utility/SdFatUtil.h>
27
28 #define FILE_READ O_READ
bc92377 @ladyada fresh start :/
ladyada authored
29 #define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
b707f7a @ladyada added seek() and buffered read()
ladyada authored
30
31 class File : public Stream {
3a9ad07 @ladyada Multifile! you can now have many files open at once which allows recu…
ladyada authored
32 private:
33 char _name[13]; // our name
34 SdFile *_file; // underlying file pointer
35
b707f7a @ladyada added seek() and buffered read()
ladyada authored
36 public:
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
37 File(SdFile f, const char *name); // wraps an underlying SdFile
3a9ad07 @ladyada Multifile! you can now have many files open at once which allows recu…
ladyada authored
38 File(void); // 'empty' constructor
39 ~File(void); // destructor
587d4ec @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
40 #if ARDUINO >= 100
41 virtual size_t write(uint8_t);
42 virtual size_t write(const uint8_t *buf, size_t size);
43 #else
b707f7a @ladyada added seek() and buffered read()
ladyada authored
44 virtual void write(uint8_t);
45 virtual void write(const uint8_t *buf, size_t size);
587d4ec @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
46 #endif
b707f7a @ladyada added seek() and buffered read()
ladyada authored
47 virtual int read();
48 virtual int peek();
49 virtual int available();
50 virtual void flush();
d912252 @ladyada Added buffered read
ladyada authored
51 int read(void *buf, uint16_t nbyte);
bc92377 @ladyada fresh start :/
ladyada authored
52 boolean seek(uint32_t pos);
53 uint32_t position();
54 uint32_t size();
b707f7a @ladyada added seek() and buffered read()
ladyada authored
55 void close();
56 operator bool();
3a9ad07 @ladyada Multifile! you can now have many files open at once which allows recu…
ladyada authored
57 char * name();
58
59 boolean isDirectory(void);
60 File openNextFile(uint8_t mode = O_RDONLY);
61 void rewindDirectory(void);
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
62
63 using Print::write;
b707f7a @ladyada added seek() and buffered read()
ladyada authored
64 };
65
66 class SDClass {
67
bc92377 @ladyada fresh start :/
ladyada authored
68 private:
b707f7a @ladyada added seek() and buffered read()
ladyada authored
69 // These are required for initialisation and use of sdfatlib
70 Sd2Card card;
71 SdVolume volume;
bc92377 @ladyada fresh start :/
ladyada authored
72 SdFile root;
73
3a9ad07 @ladyada Multifile! you can now have many files open at once which allows recu…
ladyada authored
74 // my quick&dirty iterator, should be replaced
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
75 SdFile getParentDir(const char *filepath, int *indx);
bc92377 @ladyada fresh start :/
ladyada authored
76 public:
b707f7a @ladyada added seek() and buffered read()
ladyada authored
77 // This needs to be called to set up the connection to the SD card
78 // before other methods are used.
3216c04 @ladyada can now select any 4 pins with card.init(), needs testing
ladyada authored
79 boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, int8_t mosi = -1, int8_t miso = -1, int8_t sck = -1);
b707f7a @ladyada added seek() and buffered read()
ladyada authored
80
f44a011 @driverblock Added end() function
driverblock authored
81 //call this when a card is removed. It will allow you to inster and initialise a new card.
82 void end();
83
b707f7a @ladyada added seek() and buffered read()
ladyada authored
84 // Open the specified file/directory with the supplied mode (e.g. read or
85 // write, etc). Returns a File object for interacting with the file.
86 // Note that currently only one file can be open at a time.
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
87 File open(const char *filename, uint8_t mode = FILE_READ);
b707f7a @ladyada added seek() and buffered read()
ladyada authored
88
89 // Methods to determine if the requested file path exists.
90 boolean exists(char *filepath);
91
92 // Create the requested directory heirarchy--if intermediate directories
93 // do not exist they will be created.
94 boolean mkdir(char *filepath);
95
96 // Delete the file.
97 boolean remove(char *filepath);
98
99 boolean rmdir(char *filepath);
d33ff08 @driverblock Support soft SPI on any pins with Arduino Due
driverblock authored
100
bd7b667 @PaintYourDragon CRC16 calc for Eye-Fi X2 cards
PaintYourDragon authored
101 void enableCRC(boolean mode);
102
b707f7a @ladyada added seek() and buffered read()
ladyada authored
103 private:
104
105 // This is used to determine the mode used to open a file
106 // it's here because it's the easiest place to pass the
107 // information through the directory walking function. But
108 // it's probably not the best place for it.
109 // It shouldn't be set directly--it is set via the parameters to `open`.
110 int fileOpenMode;
111
112 friend class File;
113 friend boolean callback_openPath(SdFile&, char *, boolean, void *);
114 };
115
116 extern SDClass SD;
117
118 #endif
Something went wrong with that request. Please try again.