Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SD / SD.h @ 58d82c77

History | View | Annotate | Download (2.67 KB)

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
  ~File(void);     // destructor
35
  virtual size_t write(uint8_t);
36
  virtual size_t write(const uint8_t *buf, size_t size);
37
  virtual int read();
38
  virtual int peek();
39
  virtual int available();
40
  virtual void flush();
41
  int read(void *buf, uint16_t nbyte);
42
  boolean seek(uint32_t pos);
43
  uint32_t position();
44
  uint32_t size();
45
  void close();
46
  operator bool();
47
  char * name();
48

    
49
  boolean isDirectory(void);
50
  File openNextFile(uint8_t mode = O_RDONLY);
51
  void rewindDirectory(void);
52
  
53
  using Print::write;
54
};
55

    
56
class SDClass {
57

    
58
private:
59
  // These are required for initialisation and use of sdfatlib
60
  Sd2Card card;
61
  SdVolume volume;
62
  SdFile root;
63
  
64
  // my quick&dirty iterator, should be replaced
65
  SdFile getParentDir(const char *filepath, int *indx);
66
public:
67
  // This needs to be called to set up the connection to the SD card
68
  // before other methods are used.
69
  boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
70
  
71
  // Open the specified file/directory with the supplied mode (e.g. read or
72
  // write, etc). Returns a File object for interacting with the file.
73
  // Note that currently only one file can be open at a time.
74
  File open(const char *filename, uint8_t mode = FILE_READ);
75

    
76
  // Methods to determine if the requested file path exists.
77
  boolean exists(char *filepath);
78

    
79
  // Create the requested directory heirarchy--if intermediate directories
80
  // do not exist they will be created.
81
  boolean mkdir(char *filepath);
82
  
83
  // Delete the file.
84
  boolean remove(char *filepath);
85
  
86
  boolean rmdir(char *filepath);
87

    
88
private:
89

    
90
  // This is used to determine the mode used to open a file
91
  // it's here because it's the easiest place to pass the 
92
  // information through the directory walking function. But
93
  // it's probably not the best place for it.
94
  // It shouldn't be set directly--it is set via the parameters to `open`.
95
  int fileOpenMode;
96
  
97
  friend class File;
98
  friend boolean callback_openPath(SdFile&, char *, boolean, void *); 
99
};
100

    
101
extern SDClass SD;
102

    
103
#endif