You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 line
2.7KB

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