SdFatFile Class

SdFatFile Class

Description
Defines a class of SD FAT File.

Syntax
class SdFatFile

Members

Public Constructors 
SdFatFile::SdFatFileConstructs a SdFatFile object
SdFatFile::~SdFatFileDestructs a SdFatFile object
Public Methods 
SdFatFile::writeWrite 1 byte/bytes to file
SdFatFile::readRead 1 byte/bytes from the file
SdFatFile::peekRead 1 byte from file without move curser
SdFatFile::availableCheck if the cursor is at EOF (End-Of-File)
SdFatFile::flushEnsure that any bytes written to the file are saved to the SD card
SdFatFile::seekChange cursor to a specific position
SdFatFile::closeClose file

SdFatFile::write

Description
Write 1 byte or bytes to the file.

Syntax
size_t SdFatFile::write(uint8_t c);
size_t SdFatFile::write(const uint8_t *buf, size_t size);

Parameters
c: The character to be written.
buf: The buffer to be written.
size: The length of buffer to be written.

Returns
The function returns the number of byte count that has been successfully written to the file.

Example Code
Example: time_lapse_photography
This example shows how to capture images in .jpeg format from the UVC camera and store it into the SD card.

#include <SdFatFs.h>    
#include <UVC.h>    
  
SdFatFs fs;  
  
#define CAPTURE_INTERVAL (3 * 1000)  
  
unsigned char jpegbuf[40 * 1024];  
  
void setup() {  
  
  UVC.begin(UVC_MJPEG, 640, 480, 30, 0, JPEG_CAPTURE);  
  
  // wait until UVC is ready for streaming  
  while (!UVC.available()) {  
    delay(100);  
  }  
  delay(1000);  
  Serial.println("UVC is ready");  
  
  fs.begin();  
  
}  
  
int file_index = 0;  
void loop() {  
  char filename[128];  
  int status;  
  
  int len = UVC.getJPEG(jpegbuf);  
  if (len > 0) {  
    if (fs.status()) {  
  
      sprintf(filename, "%s%04d.jpeg", fs.getRootPath(), file_index);  
      printf("capture at \"%s\"\r\n", filename);  
  
      SdFatFile file = fs.open(filename);  
      file.write(jpegbuf, len);  
      file.close();  
  
      file_index++;  
  
    } else {  
      printf("fail to detect sdcard!\r\n");  
    }  
  }  
  delay(CAPTURE_INTERVAL);  
}  

Notes and Warnings
Include “SdFatFile.h” to use the class function. 

SdFatFile:: read

Description
Read 1 byte or bytes from the file.

Syntax
int SdFatFile::read(void);
int SdFatFile::read(void *buf, uint16_t nbyte);

Parameters
buf: The buffer to store the content.
nbyte: The buffer size. (Or can be regarded as the desired length to read).

Returns
The function returns a read character or the read size of the buffer.

Example Code
Example: create_folder;
This example shows how to create a folder and open a file under it.

#include <SdFatFs.h>   
  
char dirname[] = "testdir";  
char filename[] = "test.txt";  
char write_content[] = "hello world!";  
  
SdFatFs fs;  
  
void setup() {  
  char buf[128];  
  char absolute_filename[128];  
  
  fs.begin();  
  
  sprintf(absolute_filename, "%s%s", fs.getRootPath(), dirname);  
  fs.mkdir(absolute_filename);  
  printf("create dir at \"%s\"\r\n", absolute_filename);  
  
  sprintf(absolute_filename, "%s%s/%s", fs.getRootPath(), dirname, filename);  
  SdFatFile file = fs.open(absolute_filename);  
  file.println(write_content);  
  file.close();  
  printf("create file at \"%s\"\r\n", absolute_filename);  
  
  printf("read back from \"%s\"\r\n", absolute_filename);  
  file = fs.open(absolute_filename);  
  
  memset(buf, 0, sizeof(buf));  
  file.read(buf, sizeof(buf));  
  
  file.close();  
  printf("==== content ====\r\n");  
  printf("%s", buf);  
  printf("====   end   ====\r\n");  
  
  fs.end();  
}  
  
void loop() {  
}  

Example: file_read_write;
This example shows how to open/close files and perform read/write to it.

#include <SdFatFs.h>   
  
char filename[] = "test.txt";  
char write_content[] = "hello world!";  
  
SdFatFs fs;  
  
void setup() {  
  char buf[128];  
  char absolute_filename[128];  
  
  fs.begin();  
  
  printf("write something to \"%s\"\r\n", filename);  
  sprintf(absolute_filename, "%s%s", fs.getRootPath(), filename);  
  SdFatFile file = fs.open(absolute_filename);  
  
  file.println(write_content);  
  
  file.close();  
  printf("write finish\r\n\r\n");  
  
  
  printf("read back from \"%s\"\r\n", filename);  
  file = fs.open(absolute_filename);  
  
  memset(buf, 0, sizeof(buf));  
  file.read(buf, sizeof(buf));  
  
  file.close();  
  printf("==== content ====\r\n");  
  printf("%s", buf);  
  printf("====   end   ====\r\n");  
  
  fs.end();  
}  
  
void loop() {  
  
}  

Example: time_lapse_photography;
This example shows how to capture images in .jpeg format from the UVC camera and store it into the SD card. Please refer to section SdFatFile:: write for the full code.

Notes and Warnings
Include “SdFatFile.h” to use the class function. 

SdFatFile:: peek

Description
Read one byte from the file without moving the curser.

Syntax
int SdFatFile::peek(void);

Parameters
The function requires no input parameter.

Returns
The function returns the read character as an integer number.

Example Code
NA

Notes and Warnings
Include “SdFatFile.h” to use the class function. 

SdFatFile:: available

Description
Check if the cursor is at EOF.

Syntax
int SdFatFile::available(void);

Parameters
The function requires no input parameter.

Returns
The function returns “0” if the cursor is at EOF, else returns “1”.

Example Code
NA

Notes and Warnings
Include “SdFatFile.h” to use the class function. 

SdFatFile:: flush

Description
It is a nop. This is an inherited function from class Stream. And it does not affect SD File.

Syntax
void SdFatFile::flush(void);

Parameters
The function requires no input parameter.

Returns
The function returns nothing.

Example Code
NA

Notes and Warnings
Include “SdFatFile.h” to use the class function. 

SdFatFile:: seek

Description
Change cursor to a specific position.

Syntax
int SdFatFile::seek(uint32_t pos);

Parameters
pos: The desired position.

Returns
The function returns 0 if success otherwise returns a negative value.

Example Code
NA

Notes and Warnings
Include “SdFatFile.h” in order to use the class function. 

SdFatFile:: close

Description
Close file.

Syntax
int SdFatFile::close(void);

Parameters
The function requires no input parameter.

Returns
The function returns 0 if runs successfully otherwise it returns a negative value.

Example Code
Example: last_modified_time;
The example shows how to get and set last modified time of a file.

#include <SdFatFs.h>   
  
SdFatFs fs;  
  
char filename[] = "test.txt";  
  
void setup() {  
  char absolute_filename[128];  
  
  uint16_t year   = 2016;  
  uint16_t month  = 6;  
  uint16_t date   = 3;  
  uint16_t hour   = 16;  
  uint16_t minute = 53;  
  uint16_t second = 0;  
  
  fs.begin();  
  
  sprintf(absolute_filename, "%s%s", fs.getRootPath(), filename);  
  SdFatFile file = fs.open(absolute_filename);  
  file.close();  
  
  fs.setLastModTime(absolute_filename, year, month, date, hour, minute, second);  
  
  fs.getLastModTime(absolute_filename, &year, &month, &date, &hour, &minute, &second);  
  printf("filename:\"%s\"\r\n", absolute_filename);  
  printf("time mod:%04d/%02d/%02d %02d:%02d:%02d\r\n", year, month, date, hour, minute, second);  
  
  fs.end();  
}  
  
void loop() {  
}  

Example: create_folder;
This example shows how to create a folder and open a file under it. The details of the code can be found in the section of SdFatFile:: read.

Example: file_read_write;
This example shows how to open/close files and perform read/write to it. The details of the code can be found in the section of SdFatFile:: read.

Example: time_lapse_photography;
This example shows how to capture images in .jpeg format from the UVC camera and store it into the SD card. Please refer to section SdFatFile:: write for the full code.

Notes and Warnings
Include “SdFatFile.h” in order to use the class function.

请先确认已安装QQ通讯软体