35 #include "stream/Stream.h"
38 #include "fatfs-drivers.h"
41 #define FILE_READ FA_READ
42 #define FILE_WRITE (FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_APPEND)
45 using namespace fatfs;
55 class File :
public Stream {
64 virtual size_t write(uint8_t ch) {
65 if (fs ==
nullptr)
return 0;
66 int rc = fs->
f_putc(ch, &file);
67 return rc == EOF ? 0 : 1;
69 virtual size_t write(
const uint8_t *buf,
size_t size) {
70 if (fs ==
nullptr)
return 0;
72 FRESULT rc = fs->
f_write(&file, buf, size, &result);
73 return rc == FR_OK ? result : 0;
79 readBytes((uint8_t *)buf, 1);
80 return result == 1 ? buf[0] : -1;
84 uint32_t pos = position();
89 virtual int available() {
return info.fsize - position(); }
91 virtual void flush() {
92 if (!isDirectory()) fs->
f_sync(&file);
95 size_t readBytes(uint8_t *data,
size_t len)
override {
96 if (isDirectory())
return 0;
98 auto rc = fs->
f_read(&file, data, len, &result);
99 return rc == FR_OK ? result : 0;
103 int read(
void *buf,
size_t nbyte) {
104 return readBytes((uint8_t*)buf, nbyte);
107 bool seek(uint32_t pos) {
108 if (isDirectory())
return 0;
110 return fs->
f_lseek(&file, pos);
113 uint32_t position() {
114 if (isDirectory())
return 0;
115 return fs->f_tell(&file);
118 uint32_t size() {
return fs->f_size(&file); }
128 char *name() {
return info.fname; }
131 void getName(
char *name,
int len) { strncpy(name, info.fname, len); }
133 bool isDirectory(
void) {
return info.fattrib & AM_DIR; }
135 File openNextFile(uint8_t mode = FA_READ) {
137 FRESULT rc = fs->
f_findnext(&dir, &result.info);
139 result.is_open =
true;
140 if (!result.isDirectory()) {
141 fs->
f_open(&result.file, result.name(), mode);
143 fs->
f_opendir(&result.dir, result.name());
149 void rewindDirectory(
void) { fs->f_rewinddir(&dir); }
151 operator bool() {
return is_open; }
154 if (isDirectory())
return false;
155 return fs->f_eof(&file);
159 using Print::println;
163 FIL *
getFIL() {
return isDirectory() ? nullptr : &file; }
165 DIR *
getDIR() {
return isDirectory() ? &dir :
nullptr; }
176 bool is_open =
false;
180 is_open = fat_fs.
f_stat(filepath, &info) == FR_OK;
209 if (
getDriver() ==
nullptr)
return false;
210 return handleError(
getDriver()->mount(fat_fs));
215 bool begin(
int cs, SPIClass &spi = SPI) {
224 delete[] (work_buffer);
225 work_buffer =
nullptr;
231 File open(
const char *filename, uint8_t mode = FILE_READ) {
234 if (mode & FA_WRITE || file.
update_stat(fat_fs, filename)) {
235 if (file.isDirectory()) {
236 result = fat_fs.
f_opendir(&file.dir, filename);
238 result = fat_fs.
f_open(&file.file, filename, mode);
240 file.is_open = handleError(result);
246 File open(
const String &filename, uint8_t mode = FILE_READ) {
247 return open(filename.c_str(), mode);
253 FRESULT rc = fat_fs.
f_stat(filepath, &info);
257 bool exists(
const String &filepath) {
return exists(filepath.c_str()); }
261 bool mkdir(
const char *filepath) {
return fat_fs.
f_mkdir(filepath) == FR_OK; }
262 bool mkdir(
const String &filepath) {
return mkdir(filepath.c_str()); }
266 return fat_fs.
f_unlink(filepath) == FR_OK;
268 bool remove(
const String &filepath) {
return remove(filepath.c_str()); }
270 bool rmdir(
const char *filepath) {
271 return fat_fs.
f_unlink(filepath) == FR_OK;
273 bool rmdir(
const String &filepath) {
return rmdir(filepath.c_str()); }
276 bool chdir(
const char *filepath) {
return fat_fs.
f_chdir(filepath) == FR_OK; }
278 bool chdir(String filepath) {
return chdir(filepath.c_str()); }
282 return fat_fs.
f_getcwd(buff, len) == FR_OK;
287 bool mkfs(
int workBufferSize = FF_MAX_SS) {
288 if (work_buffer ==
nullptr) work_buffer =
new uint8_t[workBufferSize];
289 return handleError(fat_fs.
f_mkfs(
"",
nullptr, work_buffer, workBufferSize));
305 uint8_t *work_buffer =
nullptr;
307 bool handleError(FRESULT rc) {
309 Serial.print(
"fatfs: error no: ");
310 Serial.println((
int)rc);
319 #if !defined(FATFS_NO_NAMESPACE)
320 using namespace fatfs;
API for FatFS See http://elm-chan.org/fsw/ff/00index_e.html.
Definition: ff.h:34
FRESULT f_close(FIL *fp)
Definition: ff.cpp:4065
FRESULT f_getcwd(TCHAR *buff, UINT len)
FRESULT f_findnext(DIR *dp, FILINFO *fno)
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
Definition: ff.cpp:4625
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition: ff.cpp:4443
FRESULT f_unlink(const TCHAR *path)
Definition: ff.cpp:4800
IO * getDriver()
Definition: ff.h:40
FRESULT f_sync(FIL *fp)
Definition: ff.cpp:3984
FRESULT f_mkfs(const TCHAR *path, const MKFS_PARM *opt, void *work, UINT len)
Definition: ff.cpp:5708
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition: ff.cpp:4280
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
Definition: ff.cpp:3862
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition: ff.cpp:3761
FRESULT f_chdir(const TCHAR *path)
FRESULT f_mkdir(const TCHAR *path)
Definition: ff.cpp:4894
FRESULT f_closedir(DIR *dp)
Definition: ff.cpp:4509
int f_putc(TCHAR c, FIL *fp)
Definition: ff.cpp:6530
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.cpp:3569
File implementation for fatfs.
Definition: fatfs.h:55
virtual int peek()
Very inefficient: to be avoided.
Definition: fatfs.h:83
IO * getDriver()
Access lo low level driver.
Definition: fatfs.h:169
void getName(char *name, int len)
Provides the name: getName() is supported by the SDFat Library.
Definition: fatfs.h:131
DIR * getDIR()
Access to low level FatFS api.
Definition: fatfs.h:165
virtual int read()
Very inefficient: to be avoided.
Definition: fatfs.h:76
FatFs * getFatFs()
Access to low level FatFS api to use functionality not exposed by this API.
Definition: fatfs.h:167
FIL * getFIL()
Access to low level FatFS api.
Definition: fatfs.h:163
bool update_stat(FatFs &fat_fs, const char *filepath)
update fs, info and is_open
Definition: fatfs.h:179
FatFS interface definition.
Definition: IO.h:74
virtual FRESULT un_mount(FatFs &fs)
unmount the file system
Definition: IO.cpp:8
Accessing a SD card via the Arduino SPI API.
Definition: SDArduinoSPIIO.h:48
SDClass: starting driver, access to files.
Definition: fatfs.h:190
bool chdir(const char *filepath)
Change directoy: extended functionality not available in Arduino SD API.
Definition: fatfs.h:276
void setDriver(IO &driver)
Set the driver.
Definition: fatfs.h:296
void end()
Definition: fatfs.h:222
bool mkdir(const char *filepath)
Definition: fatfs.h:261
bool chdir(String filepath)
Change directoy: extended functionality not available in Arduino SD API.
Definition: fatfs.h:278
bool begin(IO &driver)
Initialization of SD card: Use before other methods.
Definition: fatfs.h:201
bool mkfs(int workBufferSize=FF_MAX_SS)
format drive
Definition: fatfs.h:287
File open(const char *filename, uint8_t mode=FILE_READ)
Definition: fatfs.h:231
bool getcwd(char *buff, size_t len)
Definition: fatfs.h:281
bool exists(const char *filepath)
Methods to determine if the requested file path exists.
Definition: fatfs.h:251
bool begin()
Initialization of SD card. We use the SPI SD driver if nothing has been defined in the constructor.
Definition: fatfs.h:208
FatFs * getFatFs()
Access to low level FatFS api to use functionality not exposed by this API.
Definition: fatfs.h:293
IO * getDriver()
Access lo low level driver.
Definition: fatfs.h:298
bool remove(const char *filepath)
Delete the file.
Definition: fatfs.h:265