Arduino  1.8.10
Fat16 クラス

Fat16 implements a minimal Arduino FAT16 Library [詳解]

#include <Fat16.h>

Fat16 の継承関係図
Print

公開メンバ関数

 Fat16 (void)
 
fat_t curCluster (void) const
 
uint8_t close (void)
 
uint32_t curPosition (void) const
 
uint8_t dirEntry (dir_t *dir)
 
uint32_t fileSize (void) const
 
uint8_t isOpen (void) const
 
uint8_t open (const char *fileName, uint8_t oflag)
 
uint8_t open (uint16_t entry, uint8_t oflag)
 
int16_t read (void)
 
int16_t read (void *buf, uint16_t nbyte)
 
uint8_t remove (void)
 
void rewind (void)
 
uint8_t seekCur (uint32_t pos)
 
uint8_t seekEnd (void)
 
uint8_t seekSet (uint32_t pos)
 
uint8_t sync (void)
 
uint8_t timestamp (uint8_t flag, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
 
uint8_t truncate (uint32_t size)
 
int16_t write (const void *buf, uint16_t nbyte)
 
void write (uint8_t b)
 
void write (const char *str)
 
void write_P (PGM_P str)
 
void writeln_P (PGM_P str)
 
- 基底クラス Print に属する継承公開メンバ関数
 Print ()
 
int getWriteError ()
 
void clearWriteError ()
 
size_t write (const char *str)
 
virtual size_t write (const uint8_t *buffer, size_t size)
 
size_t write (const char *buffer, size_t size)
 
virtual int availableForWrite ()
 
size_t print (const __FlashStringHelper *)
 
size_t print (const String &)
 
size_t print (const char[])
 
size_t print (char)
 
size_t print (unsigned char, int=DEC)
 
size_t print (int, int=DEC)
 
size_t print (unsigned int, int=DEC)
 
size_t print (long, int=DEC)
 
size_t print (unsigned long, int=DEC)
 
size_t print (double, int=2)
 
size_t print (const Printable &)
 
size_t println (const __FlashStringHelper *)
 
size_t println (const String &s)
 
size_t println (const char[])
 
size_t println (char)
 
size_t println (unsigned char, int=DEC)
 
size_t println (int, int=DEC)
 
size_t println (unsigned int, int=DEC)
 
size_t println (long, int=DEC)
 
size_t println (unsigned long, int=DEC)
 
size_t println (double, int=2)
 
size_t println (const Printable &)
 
size_t println (void)
 
virtual void flush ()
 

静的公開メンバ関数

static fat_t clusterCount (void)
 
static uint8_t clusterSize (void)
 
static void dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time))
 
static void dateTimeCallbackCancel (void)
 
static uint8_t init (SdCard *dev, uint8_t part)
 
static uint8_t init (SdCard *dev)
 
static void ls (uint8_t flags=0)
 
static void printDirName (const dir_t &dir, uint8_t width)
 
static void printFatDate (uint16_t fatDate)
 
static void printFatTime (uint16_t fatTime)
 
static void printTwoDigits (uint8_t v)
 
static uint8_t readDir (dir_t *dir, uint16_t *index, uint8_t skip=(DIR_ATT_VOLUME_ID|DIR_ATT_DIRECTORY))
 
static uint8_t remove (const char *fileName)
 
static uint16_t rootDirEntryCount (void)
 

公開変数類

bool writeError
 

その他の継承メンバ

- 基底クラス Print に属する継承限定公開メンバ関数
void setWriteError (int err=1)
 

詳解

Fat16 implements a minimal Arduino FAT16 Library

Fat16 does not support subdirectories or long file names.

Fat16.h141 行目に定義があります。

構築子と解体子

◆ Fat16()

Fat16::Fat16 ( void  )
inline

create with file closed

Fat16.h147 行目に定義があります。

147 : flags_(0) {}

関数詳解

◆ close()

uint8_t Fat16::close ( void  )

Close a file and force cached data and directory information to be written to the storage device.

戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include no file is open or an I/O error.

Fat16.cpp155 行目に定義があります。

155  {
156  if (!sync()) return false;
157  flags_ = 0;
158  return true;
159 }
uint8_t sync(void)
Definition: Fat16.cpp:723

◆ clusterCount()

static fat_t Fat16::clusterCount ( void  )
inlinestatic
戻り値
The count of clusters in the FAT16 volume.

Fat16.h152 行目に定義があります。

152 {return clusterCount_;}

◆ clusterSize()

static uint8_t Fat16::clusterSize ( void  )
inlinestatic
戻り値
The number of 512 byte blocks in a cluster

Fat16.h154 行目に定義があります。

154 {return blocksPerCluster_;}

◆ curCluster()

fat_t Fat16::curCluster ( void  ) const
inline
戻り値
The current cluster number.

Fat16.h149 行目に定義があります。

149 {return curCluster_;}

◆ curPosition()

uint32_t Fat16::curPosition ( void  ) const
inline
戻り値
The current file position.

Fat16.h156 行目に定義があります。

156 {return curPosition_;}

◆ dateTimeCallback()

static void Fat16::dateTimeCallback ( void(*)(uint16_t *date, uint16_t *time dateTime)
inlinestatic

Set the date/time callback function

引数
[in]dateTimeThe user's callback function. The callback function is of the form:
void dateTime(uint16_t* date, uint16_t* time) {
uint16_t year;
uint8_t month, day, hour, minute, second;
// User gets date and time from GPS or real-time clock here
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(year, month, day);
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(hour, minute, second);
}

Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.

See the timestamp() function.

Fat16.h185 行目に定義があります。

186  {
187  dateTime_ = dateTime;
188  }

◆ dateTimeCallbackCancel()

static void Fat16::dateTimeCallbackCancel ( void  )
inlinestatic

Cancel the date/time callback function.

Fat16.h192 行目に定義があります。

192 {dateTime_ = NULL;}
#define NULL
Definition: def.h:44

◆ dirEntry()

uint8_t Fat16::dirEntry ( dir_t dir)

Return a files directory entry

引数
[out]dirLocation for return of the files directory entry.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure.

Fat16.cpp169 行目に定義があります。

169  {
170  if (!sync()) return false;
171  dir_t* p = cacheDirEntry(dirEntryIndex_, CACHE_FOR_WRITE);
172  if (!p) return false;
173  memcpy(dir, p, sizeof(dir_t));
174  return true;
175 }
uint8_t sync(void)
Definition: Fat16.cpp:723
void * memcpy(void *, const void *, size_t)
Copy a memory area.
FAT short directory entry
Definition: FatStructs.h:321

◆ fileSize()

uint32_t Fat16::fileSize ( void  ) const
inline
戻り値
The file's size in bytes.

Fat16.h196 行目に定義があります。

196 {return fileSize_;}

◆ init() [1/2]

uint8_t Fat16::init ( SdCard dev,
uint8_t  part 
)
static

Initialize a FAT16 volume.

引数
[in]devThe SdCard where the volume is located.
[in]partThe partition to be used. Legal values for part are 1-4 to use the corresponding partition on a device formatted with a MBR, Master Boot Record, or zero if the device is formatted as a super floppy with the FAT boot sector in block zero.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. reasons for failure include not finding a valid FAT16 file system in the specified partition, a call to init() after a volume has been successful initialized or an I/O error.

Fat16.cpp229 行目に定義があります。

229  {
230  // error if invalid partition
231  if (part > 4) return false;
232  rawDev_ = dev;
233  uint32_t volumeStartBlock = 0;
234  // if part == 0 assume super floppy with FAT16 boot sector in block zero
235  // if part > 0 assume mbr volume with partition table
236  if (part) {
237  if (!cacheRawBlock(volumeStartBlock)) return false;
238  volumeStartBlock = cacheBuffer_.mbr.part[part - 1].firstSector;
239  }
240  if (!cacheRawBlock(volumeStartBlock)) return false;
241  // check boot block signature
242  if (cacheBuffer_.data[510] != BOOTSIG0 ||
243  cacheBuffer_.data[511] != BOOTSIG1) return false;
244  bpb_t* bpb = &cacheBuffer_.fbs.bpb;
245  fatCount_ = bpb->fatCount;
246  blocksPerCluster_ = bpb->sectorsPerCluster;
247  blocksPerFat_ = bpb->sectorsPerFat16;
248  rootDirEntryCount_ = bpb->rootDirEntryCount;
249  fatStartBlock_ = volumeStartBlock + bpb->reservedSectorCount;
250  rootDirStartBlock_ = fatStartBlock_ + bpb->fatCount*bpb->sectorsPerFat16;
251  dataStartBlock_ = rootDirStartBlock_
252  + ((32*bpb->rootDirEntryCount + 511)/512);
253  uint32_t totalBlocks = bpb->totalSectors16 ?
255  clusterCount_ = (totalBlocks - (dataStartBlock_ - volumeStartBlock))
257  // verify valid FAT16 volume
258  if (bpb->bytesPerSector != 512 // only allow 512 byte blocks
259  || bpb->sectorsPerFat16 == 0 // zero for FAT32
260  || clusterCount_ < 4085 // FAT12 if true
261  || totalBlocks > 0X800000 // Max size for FAT16 volume
262  || bpb->reservedSectorCount == 0 // invalid volume
263  || bpb->fatCount == 0 // invalid volume
264  || bpb->sectorsPerFat16 < (clusterCount_ >> 8) // invalid volume
265  || bpb->sectorsPerCluster == 0 // invalid volume
266  // power of 2 test
268  // not a usable FAT16 bpb
269  return false;
270  }
271  volumeInitialized_ = 1;
272  return true;
273 }
uint32_t totalSectors32
Definition: FatStructs.h:197
uint32_t firstSector
Definition: FatStructs.h:90
uint16_t totalSectors16
Definition: FatStructs.h:167
uint16_t sectorsPerFat16
Definition: FatStructs.h:180
uint8_t sectorsPerCluster
Definition: FatStructs.h:139
uint16_t bytesPerSector
Definition: FatStructs.h:133
part_t part[4]
Definition: FatStructs.h:42
BIOS parameter block
Definition: FatStructs.h:128
uint8_t fatCount
Definition: FatStructs.h:148
part_t part[4]
Definition: FatStructs.h:112
uint16_t rootDirEntryCount
Definition: FatStructs.h:157
bpb_t bpb
Definition: FatStructs.h:40
mbr_t mbr
Definition: Fat16.h:131
uint8_t data[512]
Definition: Fat16.h:125
uint8_t const BOOTSIG0
Definition: FatStructs.h:32
uint8_t const BOOTSIG1
Definition: FatStructs.h:34
fbs_t fbs
Definition: Fat16.h:133
uint16_t reservedSectorCount
Definition: FatStructs.h:144

◆ init() [2/2]

static uint8_t Fat16::init ( SdCard dev)
inlinestatic

Initialize a FAT16 volume.

First try partition 1 then try super floppy format.

引数
[in]devThe SdCard where the volume is located.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. reasons for failure include not finding a valid FAT16 file system, a call to init() after a volume has been successful initialized or an I/O error.

Fat16.h212 行目に定義があります。

212  {
213  return init(dev, 1) ? true : init(dev, 0);
214  }
#define true
Definition: compiler.h:279
static uint8_t init(SdCard *dev, uint8_t part)
Definition: Fat16.cpp:229

◆ isOpen()

uint8_t Fat16::isOpen ( void  ) const
inline

Checks the file's open/closed status for this instance of Fat16.

戻り値
The value true if a file is open otherwise false;

Fat16.h219 行目に定義があります。

219 {return (flags_ & O_ACCMODE) != 0;}
uint8_t const O_ACCMODE
Definition: Fat16.h:54

◆ ls()

void Fat16::ls ( uint8_t  flags = 0)
static

List directory contents to Serial.

引数
[in]flagsThe inclusive OR of

LS_DATE - Print file modification date

LS_SIZE - Print file size.

Fat16.cpp283 行目に定義があります。

283  {
284  dir_t d;
285  for (uint16_t index = 0; readDir(&d, &index, DIR_ATT_VOLUME_ID); index++) {
286  // print file name with possible blank fill
287  printDirName(d, flags & (LS_DATE | LS_SIZE) ? 14 : 0);
288 
289  // print modify date/time if requested
290  if (flags & LS_DATE) {
292  Serial.write(' ');
294  }
295 
296  // print size if requested
297  if (DIR_IS_FILE(&d) && (flags & LS_SIZE)) {
298  Serial.write(' ');
299  Serial.print(d.fileSize);
300  }
301  Serial.println();
302  }
303 }
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint8_t const LS_DATE
Definition: Fat16.h:38
struct flags_struct flags
uint32_t fileSize
Definition: FatStructs.h:368
static uint8_t readDir(dir_t *dir, uint16_t *index, uint8_t skip=(DIR_ATT_VOLUME_ID|DIR_ATT_DIRECTORY))
Definition: Fat16.cpp:609
uint16_t lastWriteTime
Definition: FatStructs.h:362
static void printDirName(const dir_t &dir, uint8_t width)
Definition: Fat16.cpp:455
uint8_t const DIR_ATT_VOLUME_ID
Definition: FatStructs.h:388
FAT short directory entry
Definition: FatStructs.h:321
static void printFatTime(uint16_t fatTime)
Definition: Fat16.cpp:496
static void printFatDate(uint16_t fatDate)
Definition: Fat16.cpp:482
uint8_t const LS_SIZE
Definition: Fat16.h:40

◆ open() [1/2]

uint8_t Fat16::open ( const char *  fileName,
uint8_t  oflag 
)

Open a file by file name.

覚え書き
The file must be in the root directory and must have a DOS 8.3 name.
引数
[in]fileNameA valid 8.3 DOS name for a file in the root directory.
[in]oflagValues for oflag are constructed by a bitwise-inclusive OR of flags from the following list

O_READ - Open for reading.

O_RDONLY - Same as O_READ.

O_WRITE - Open for writing.

O_WRONLY - Same as O_WRITE.

O_RDWR - Open for reading and writing.

O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.

O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created

O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.

O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. These functions do character a time writes so sync() will be called after each byte.

O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.

戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the FAT volume has not been initialized, a file is already open, fileName is invalid, the file does not exist, is a directory, or can't be opened in the access mode specified by oflag.

Fat16.cpp348 行目に定義があります。

348  {
349  uint8_t dname[11]; // name formated for dir entry
350  int16_t empty = -1; // index of empty slot
351  dir_t* p; // pointer to cached dir entry
352 
353  if (!volumeInitialized_ || isOpen()) return false;
354 
355  // error if invalid name
356  if (!make83Name(fileName, dname)) return false;
357 
358  for (uint16_t index = 0; index < rootDirEntryCount_; index++) {
359  if (!(p = cacheDirEntry(index))) return false;
360  if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) {
361  // remember first empty slot
362  if (empty < 0) empty = index;
363  // done if no entries follow
364  if (p->name[0] == DIR_NAME_FREE) break;
365  } else if (!memcmp(dname, p->name, 11)) {
366  // don't open existing file if O_CREAT and O_EXCL
367  if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) return false;
368 
369  // open existing file
370  return open(index, oflag);
371  }
372  }
373  // error if directory is full
374  if (empty < 0) return false;
375 
376  // only create file if O_CREAT and O_WRITE
377  if ((oflag & (O_CREAT | O_WRITE)) != (O_CREAT | O_WRITE)) return false;
378 
379  if (!(p = cacheDirEntry(empty, CACHE_FOR_WRITE))) return false;
380 
381  // initialize as empty file
382  memset(p, 0, sizeof(dir_t));
383  memcpy(p->name, dname, 11);
384 
385  // set timestamps
386  if (dateTime_) {
387  // call user function
388  dateTime_(&p->creationDate, &p->creationTime);
389  } else {
390  // use default date/time
393  }
395  p->lastWriteDate = p->creationDate;
396  p->lastWriteTime = p->creationTime;
397 
398  // insure created directory entry will be written to storage device
399  if (!cacheFlush()) return false;
400 
401  // open entry
402  return open(empty, oflag);
403 }
uint8_t const O_EXCL
Definition: Fat16.h:62
uint16_t lastWriteDate
Definition: FatStructs.h:364
void * memcpy(void *, const void *, size_t)
Copy a memory area.
uint16_t creationDate
Definition: FatStructs.h:349
void * memset(void *, int, size_t)
Fill memory with a constant byte.
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint16_t lastAccessDate
Definition: FatStructs.h:355
uint16_t creationTime
Definition: FatStructs.h:347
int memcmp(const void *, const void *, size_t) __ATTR_PURE__
Compare memory areas
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint16_t lastWriteTime
Definition: FatStructs.h:362
uint16_t const FAT_DEFAULT_DATE
Definition: Fat16.h:107
uint8_t isOpen(void) const
Definition: Fat16.h:219
FAT short directory entry
Definition: FatStructs.h:321
uint8_t open(const char *fileName, uint8_t oflag)
Definition: Fat16.cpp:348
uint16_t const FAT_DEFAULT_TIME
Definition: Fat16.h:109
uint8_t const O_CREAT
Definition: Fat16.h:60
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ open() [2/2]

uint8_t Fat16::open ( uint16_t  index,
uint8_t  oflag 
)

Open a file by file index.

引数
[in]indexThe root directory index of the file to be opened. See readDir().
[in]oflagSee Fat16::open(const char*, uint8_t).
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the FAT volume has not been initialized, a file is already open, index is invalid or is not the index of a file or the file cannot be opened in the access mode specified by oflag.

Fat16.cpp419 行目に定義があります。

419  {
420  if (!volumeInitialized_ || isOpen()) return false;
421  if ((oflag & O_TRUNC) && !(oflag & O_WRITE)) return false;
422  dir_t* d = cacheDirEntry(index);
423  // if bad file index or I/O error
424  if (!d) return false;
425 
426  // error if unused entry
427  if (d->name[0] == DIR_NAME_FREE || d->name[0] == DIR_NAME_DELETED) {
428  return false;
429  }
430  // error if long name, volume label or subdirectory
431  if ((d->attributes & (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY)) != 0) {
432  return false;
433  }
434  // don't allow write or truncate if read-only
435  if (d->attributes & DIR_ATT_READ_ONLY) {
436  if (oflag & (O_WRITE | O_TRUNC)) return false;
437  }
438 
439  curCluster_ = 0;
440  curPosition_ = 0;
441  dirEntryIndex_ = index;
442  fileSize_ = d->fileSize;
443  firstCluster_ = d->firstClusterLow;
444  flags_ = oflag & (O_ACCMODE | O_SYNC | O_APPEND);
445 
446  if (oflag & O_TRUNC ) return truncate(0);
447  return true;
448 }
uint8_t attributes
Definition: FatStructs.h:334
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t const O_APPEND
Definition: Fat16.h:56
uint8_t name[11]
Definition: FatStructs.h:327
uint32_t fileSize
Definition: FatStructs.h:368
uint8_t const DIR_ATT_READ_ONLY
Definition: FatStructs.h:382
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t const O_TRUNC
Definition: Fat16.h:64
uint16_t firstClusterLow
Definition: FatStructs.h:366
uint8_t const O_ACCMODE
Definition: Fat16.h:54
uint8_t isOpen(void) const
Definition: Fat16.h:219
uint8_t const DIR_ATT_VOLUME_ID
Definition: FatStructs.h:388
uint8_t const O_SYNC
Definition: Fat16.h:58
FAT short directory entry
Definition: FatStructs.h:321
uint8_t truncate(uint32_t size)
Definition: Fat16.cpp:820
uint8_t const DIR_ATT_DIRECTORY
Definition: FatStructs.h:390
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ printDirName()

void Fat16::printDirName ( const dir_t dir,
uint8_t  width 
)
static

Print the name field of a directory entry in 8.3 format to Serial.

引数
[in]dirThe directory structure containing the name.
[in]widthBlank fill name if length is less than width.

Fat16.cpp455 行目に定義があります。

455  {
456  uint8_t w = 0;
457  for (uint8_t i = 0; i < 11; i++) {
458  if (dir.name[i] == ' ') continue;
459  if (i == 8) {
460  Serial.write('.');
461  w++;
462  }
463  Serial.write(dir.name[i]);
464  w++;
465  }
466  if (DIR_IS_SUBDIR(&dir)) {
467  Serial.write('/');
468  w++;
469  }
470  while (w < width) {
471  Serial.write(' ');
472  w++;
473  }
474 }
uint8_t i
uint8_t name[11]
Definition: FatStructs.h:327

◆ printFatDate()

void Fat16::printFatDate ( uint16_t  fatDate)
static

Print a directory date field to Serial.

Format is yyyy-mm-dd.

引数
[in]fatDateThe date field from a directory entry.

Fat16.cpp482 行目に定義があります。

482  {
483  Serial.print(FAT_YEAR(fatDate));
484  Serial.write('-');
485  printTwoDigits(FAT_MONTH(fatDate));
486  Serial.write('-');
487  printTwoDigits(FAT_DAY(fatDate));
488 }
static void printTwoDigits(uint8_t v)
Definition: Fat16.cpp:509

◆ printFatTime()

void Fat16::printFatTime ( uint16_t  fatTime)
static

Print a directory time field to Serial.

Format is hh:mm:ss.

引数
[in]fatTimeThe time field from a directory entry.

Fat16.cpp496 行目に定義があります。

496  {
497  printTwoDigits(FAT_HOUR(fatTime));
498  Serial.write(':');
499  printTwoDigits(FAT_MINUTE(fatTime));
500  Serial.write(':');
501  printTwoDigits(FAT_SECOND(fatTime));
502 }
static void printTwoDigits(uint8_t v)
Definition: Fat16.cpp:509

◆ printTwoDigits()

void Fat16::printTwoDigits ( uint8_t  v)
static

Print a value as two digits to Serial.

引数
[in]vValue to be printed, 0 <= v <= 99

Fat16.cpp509 行目に定義があります。

509  {
510  char str[3];
511  str[0] = '0' + v/10;
512  str[1] = '0' + v % 10;
513  str[2] = 0;
514  Serial.print(str);
515 }
const char * str
Definition: console.c:41

◆ read() [1/2]

int16_t Fat16::read ( void  )

Read the next byte from a file.

戻り値
For success read returns the next byte in the file as an int. If an error occurs or end of file is reached -1 is returned.

Fat16.cpp523 行目に定義があります。

523  {
524  uint8_t b;
525  return read(&b, 1) == 1 ? b : -1;
526 }
int16_t read(void)
Definition: Fat16.cpp:523

◆ read() [2/2]

int16_t Fat16::read ( void *  buf,
uint16_t  nbyte 
)

Read data from a file at starting at the current file position.

引数
[out]bufPointer to the location that will receive the data.
[in]nbyteMaximum number of bytes to read.
戻り値
For success read returns the number of bytes read. A value less than nbyte, including zero, may be returned if end of file is reached. If an error occurs, read returns -1. Possible errors include read called before a file has been opened, the file has not been opened in read mode, a corrupt file system, or an I/O error.

Fat16.cpp542 行目に定義があります。

542  {
543  // convert void pointer to uin8_t pointer
544  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
545 
546  // error if not open for read
547  if (!(flags_ & O_READ)) return -1;
548 
549  // don't read beyond end of file
550  if ((curPosition_ + nbyte) > fileSize_) nbyte = fileSize_ - curPosition_;
551 
552  // bytes left to read in loop
553  uint16_t nToRead = nbyte;
554  while (nToRead > 0) {
555  uint8_t blkOfCluster = blockOfCluster(curPosition_);
556  uint16_t blockOffset = cacheDataOffset(curPosition_);
557  if (blkOfCluster == 0 && blockOffset == 0) {
558  // start next cluster
559  if (curCluster_ == 0) {
560  curCluster_ = firstCluster_;
561  } else {
562  if (!fatGet(curCluster_, &curCluster_)) return -1;
563  }
564  // return error if bad cluster chain
565  if (curCluster_ < 2 || isEOC(curCluster_)) return -1;
566  }
567  // cache data block
568  if (!cacheRawBlock(dataBlockLba(curCluster_, blkOfCluster))) return -1;
569 
570  // location of data in cache
571  uint8_t* src = cacheBuffer_.data + blockOffset;
572 
573  // max number of byte available in block
574  uint16_t n = 512 - blockOffset;
575 
576  // lesser of available and amount to read
577  if (n > nToRead) n = nToRead;
578 
579  // copy data to caller
580  memcpy(dst, src, n);
581 
582  curPosition_ += n;
583  dst += n;
584  nToRead -= n;
585  }
586  return nbyte;
587 }
void * memcpy(void *, const void *, size_t)
Copy a memory area.
uint8_t const O_READ
Definition: Fat16.h:44
uint8_t data[512]
Definition: Fat16.h:125

◆ readDir()

uint8_t Fat16::readDir ( dir_t dir,
uint16_t *  index,
uint8_t  skip = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY) 
)
static

Read the next short, 8.3, directory entry.

Unused entries and entries for long names are skipped.

引数
[out]dirLocation that will receive the entry.
[in,out]indexThe search starts at index and index is updated with the root directory index of the found directory entry. If the entry is a file, it may be opened by calling Fat16::open(uint16_t, uint8_t).
[in]skipSkip entries that have these attributes. If skip is not specified, the default is to skip the volume label and directories.
戻り値
The value one, true, is returned for success and the value zero, false, is returned if an error occurs or the end of the root directory is reached. On success, entry is set to the index of the found directory entry.

Fat16.cpp609 行目に定義があります。

609  {
610  dir_t* p;
611  for (uint16_t i = *index; ; i++) {
612  if (i >= rootDirEntryCount_) return false;
613  if (!(p = cacheDirEntry(i))) return false;
614 
615  // done if beyond last used entry
616  if (p->name[0] == DIR_NAME_FREE) return false;
617 
618  // skip deleted entry
619  if (p->name[0] == DIR_NAME_DELETED) continue;
620 
621  // skip long names
622  if ((p->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME) continue;
623 
624  // skip if attribute match
625  if (p->attributes & skip) continue;
626 
627  // return found index
628  *index = i;
629  break;
630  }
631  memcpy(dir, p, sizeof(dir_t));
632  return true;
633 }
uint8_t attributes
Definition: FatStructs.h:334
void * memcpy(void *, const void *, size_t)
Copy a memory area.
uint8_t const DIR_ATT_LONG_NAME_MASK
Definition: FatStructs.h:397
uint8_t i
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t const DIR_ATT_LONG_NAME
Definition: FatStructs.h:395
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
FAT short directory entry
Definition: FatStructs.h:321

◆ remove() [1/2]

uint8_t Fat16::remove ( void  )

Remove a file. The directory entry and all data for the file are deleted.

覚え書き
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is not open for write or an I/O error occurred.

Fat16.cpp647 行目に定義があります。

647  {
648  // error if file is not open for write
649  if (!(flags_ & O_WRITE)) return false;
650  if (firstCluster_) {
651  if (!freeChain(firstCluster_)) return false;
652  }
653  dir_t* d = cacheDirEntry(dirEntryIndex_, CACHE_FOR_WRITE);
654  if (!d) return false;
655  d->name[0] = DIR_NAME_DELETED;
656  flags_ = 0;
657  return cacheFlush();
658 }
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
FAT short directory entry
Definition: FatStructs.h:321
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ remove() [2/2]

uint8_t Fat16::remove ( const char *  fileName)
static

Remove a file.

The directory entry and all data for the file are deleted.

引数
[in]fileNameThe name of the file to be removed.
覚え書き
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is read only, fileName is not found or an I/O error occurred.

Fat16.cpp676 行目に定義があります。

676  {
677  Fat16 file;
678  if (!file.open(fileName, O_WRITE)) return false;
679  return file.remove();
680 }
Fat16 implements a minimal Arduino FAT16 Library
Definition: Fat16.h:141
uint8_t remove(void)
Definition: Fat16.cpp:647
uint8_t open(const char *fileName, uint8_t oflag)
Definition: Fat16.cpp:348
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ rewind()

void Fat16::rewind ( void  )
inline

Sets the file's current position to zero.

Fat16.h235 行目に定義があります。

235 {curPosition_ = curCluster_ = 0;}

◆ rootDirEntryCount()

static uint16_t Fat16::rootDirEntryCount ( void  )
inlinestatic
戻り値
The number of entries in the root directory.

Fat16.h237 行目に定義があります。

237 {return rootDirEntryCount_;}

◆ seekCur()

uint8_t Fat16::seekCur ( uint32_t  pos)
inline

Seek to current position plus pos bytes. See Fat16::seekSet().

Fat16.h239 行目に定義があります。

239 {return seekSet(curPosition_ + pos);}
uint8_t seekSet(uint32_t pos)
Definition: Fat16.cpp:690

◆ seekEnd()

uint8_t Fat16::seekEnd ( void  )
inline

Seek to end of file. See Fat16::seekSet().

Fat16.h241 行目に定義があります。

241 {return seekSet(fileSize_);}
uint8_t seekSet(uint32_t pos)
Definition: Fat16.cpp:690

◆ seekSet()

uint8_t Fat16::seekSet ( uint32_t  pos)

Sets the file's read/write position.

引数
[in]posThe new position in bytes from the beginning of the file.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure.

Fat16.cpp690 行目に定義があります。

690  {
691  // error if file not open or seek past end of file
692  if (!isOpen() || pos > fileSize_) return false;
693  if (pos == 0) {
694  // set position to start of file
695  curCluster_ = 0;
696  curPosition_ = 0;
697  return true;
698  }
699  fat_t n = ((pos - 1) >> 9)/blocksPerCluster_;
700  if (pos < curPosition_ || curPosition_ == 0) {
701  // must follow chain from first cluster
702  curCluster_ = firstCluster_;
703  } else {
704  // advance from curPosition
705  n -= ((curPosition_ - 1) >> 9)/blocksPerCluster_;
706  }
707  while (n--) {
708  if (!fatGet(curCluster_, &curCluster_)) return false;
709  }
710  curPosition_ = pos;
711  return true;
712 }
uint16_t fat_t
Type for FAT16 entry
Definition: Fat16.h:116
uint8_t isOpen(void) const
Definition: Fat16.h:219

◆ sync()

uint8_t Fat16::sync ( void  )

The sync() call causes all modified data and directory fields to be written to the storage device.

戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include a call to sync() before a file has been opened or an I/O error.

Fat16.cpp723 行目に定義があります。

723  {
724  if (flags_ & F_FILE_DIR_DIRTY) {
725  // cache directory entry
726  dir_t* d = cacheDirEntry(dirEntryIndex_, CACHE_FOR_WRITE);
727  if (!d) return false;
728 
729  // update file size and first cluster
730  d->fileSize = fileSize_;
731  d->firstClusterLow = firstCluster_;
732 
733  // set modify time if user supplied a callback date/time function
734  if (dateTime_) {
735  dateTime_(&d->lastWriteDate, &d->lastWriteTime);
737  }
738  flags_ &= ~F_FILE_DIR_DIRTY;
739  }
740  return cacheFlush();
741 }
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint16_t lastAccessDate
Definition: FatStructs.h:355
uint32_t fileSize
Definition: FatStructs.h:368
uint16_t firstClusterLow
Definition: FatStructs.h:366
uint16_t lastWriteTime
Definition: FatStructs.h:362
FAT short directory entry
Definition: FatStructs.h:321

◆ timestamp()

uint8_t Fat16::timestamp ( uint8_t  flags,
uint16_t  year,
uint8_t  month,
uint8_t  day,
uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

The timestamp() call sets a file's timestamps in its directory entry.

引数
[in]flagsValues for flags are constructed by a bitwise-inclusive OR of flags from the following list

T_ACCESS - Set the file's last access date.

T_CREATE - Set the file's creation date and time.

T_WRITE - Set the file's last write/modification date and time.

引数
[in]yearValid range 1980 - 2107 inclusive.
[in]monthValid range 1 - 12 inclusive.
[in]dayValid range 1 - 31 inclusive.
[in]hourValid range 0 - 23 inclusive.
[in]minuteValid range 0 - 59 inclusive.
[in]secondValid range 0 - 59 inclusive
覚え書き
It is possible to set an invalid date since there is no check for the number of days in a month.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure.

Fat16.cpp773 行目に定義があります。

774  {
775  if (!isOpen()
776  || year < 1980
777  || year > 2107
778  || month < 1
779  || month > 12
780  || day < 1
781  || day > 31
782  || hour > 23
783  || minute > 59
784  || second > 59) {
785  return false;
786  }
787  dir_t* d = cacheDirEntry(dirEntryIndex_, CACHE_FOR_WRITE);
788  if (!d) return false;
789  uint16_t dirDate = FAT_DATE(year, month, day);
790  uint16_t dirTime = FAT_TIME(hour, minute, second);
791  if (flags & T_ACCESS) {
792  d->lastAccessDate = dirDate;
793  }
794  if (flags & T_CREATE) {
795  d->creationDate = dirDate;
796  d->creationTime = dirTime;
797  // seems to be units of 1/100 second not 1/10 as Microsoft standard states
798  d->creationTimeTenths = second & 1 ? 100 : 0;
799  }
800  if (flags & T_WRITE) {
801  d->lastWriteDate = dirDate;
802  d->lastWriteTime = dirTime;
803  }
804  cacheSetDirty();
805  return sync();
806 }
uint8_t sync(void)
Definition: Fat16.cpp:723
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint8_t const T_WRITE
Definition: Fat16.h:72
uint16_t creationDate
Definition: FatStructs.h:349
uint16_t lastAccessDate
Definition: FatStructs.h:355
struct flags_struct flags
uint16_t creationTime
Definition: FatStructs.h:347
uint8_t const T_CREATE
Definition: Fat16.h:70
uint8_t const T_ACCESS
Definition: Fat16.h:68
uint8_t creationTimeTenths
Definition: FatStructs.h:345
uint16_t lastWriteTime
Definition: FatStructs.h:362
uint8_t isOpen(void) const
Definition: Fat16.h:219
FAT short directory entry
Definition: FatStructs.h:321

◆ truncate()

uint8_t Fat16::truncate ( uint32_t  length)

Truncate a file to a specified length. The current file position will be maintained if it is less than or equal to length otherwise it will be set to end of file.

引数
[in]lengthThe desired length for the file.
戻り値
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include file is read only, file is a directory, length is greater than the current file size or an I/O error occurs.

Fat16.cpp820 行目に定義があります。

820  {
821  // error if file is not open for write
822  if (!(flags_ & O_WRITE)) return false;
823 
824  if (length > fileSize_) return false;
825 
826  // fileSize and length are zero - nothing to do
827  if (fileSize_ == 0) return true;
828  uint32_t newPos = curPosition_ > length ? length : curPosition_;
829  if (length == 0) {
830  // free all clusters
831  if (!freeChain(firstCluster_)) return false;
832  curCluster_ = firstCluster_ = 0;
833  } else {
834  fat_t toFree;
835  if (!seekSet(length)) return false;
836  if (!fatGet(curCluster_, &toFree)) return false;
837  if (!isEOC(toFree)) {
838  // free extra clusters
839  if (!fatPut(curCluster_, FAT16EOC)) return false;
840  if (!freeChain(toFree)) return false;
841  }
842  }
843  fileSize_ = length;
844  flags_ |= F_FILE_DIR_DIRTY;
845  if (!sync()) return false;
846  return seekSet(newPos);
847 }
uint8_t sync(void)
Definition: Fat16.cpp:723
uint16_t const FAT16EOC
Definition: FatStructs.h:278
uint8_t seekSet(uint32_t pos)
Definition: Fat16.cpp:690
uint16_t fat_t
Type for FAT16 entry
Definition: Fat16.h:116
union length_union length
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ write() [1/3]

int16_t Fat16::write ( const void *  buf,
uint16_t  nbyte 
)

Write data at the current position of an open file.

覚え書き
Data is moved to the cache but may not be written to the storage device until sync() is called.
引数
[in]bufPointer to the location of the data to be written.
[in]nbyteNumber of bytes to write.
戻り値
For success write() returns the number of bytes written, always nbyte. If an error occurs, write() returns -1. Possible errors include write() is called before a file has been opened, the file has not been opened for write, device is full, a corrupt file system or an I/O error.

Fat16.cpp865 行目に定義があります。

865  {
866  uint16_t nToWrite = nbyte;
867  const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
868 
869  // error if file is not open for write
870  if (!(flags_ & O_WRITE)) goto writeErrorReturn;
871 
872  // go to end of file if O_APPEND
873  if ((flags_ & O_APPEND) && curPosition_ != fileSize_) {
874  if (!seekEnd()) goto writeErrorReturn;
875  }
876  while (nToWrite > 0) {
877  uint8_t blkOfCluster = blockOfCluster(curPosition_);
878  uint16_t blockOffset = cacheDataOffset(curPosition_);
879  if (blkOfCluster == 0 && blockOffset == 0) {
880  // start of new cluster
881  if (curCluster_ == 0) {
882  if (firstCluster_ == 0) {
883  // allocate first cluster of file
884  if (!addCluster()) goto writeErrorReturn;
885  } else {
886  curCluster_ = firstCluster_;
887  }
888  } else {
889  fat_t next;
890  if (!fatGet(curCluster_, &next)) goto writeErrorReturn;
891  if (isEOC(next)) {
892  // add cluster if at end of chain
893  if (!addCluster()) goto writeErrorReturn;
894  } else {
895  curCluster_ = next;
896  }
897  }
898  }
899  uint32_t lba = dataBlockLba(curCluster_, blkOfCluster);
900  if (blockOffset == 0 && curPosition_ >= fileSize_) {
901  // start of new block don't need to read into cache
902  if (!cacheFlush()) goto writeErrorReturn;
903  cacheBlockNumber_ = lba;
904  cacheSetDirty();
905  } else {
906  // rewrite part of block
907  if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return -1;
908  }
909  uint8_t* dst = cacheBuffer_.data + blockOffset;
910 
911  // max space in block
912  uint16_t n = 512 - blockOffset;
913 
914  // lesser of space and amount to write
915  if (n > nToWrite) n = nToWrite;
916 
917  // copy data to cache
918  memcpy(dst, src, n);
919 
920  curPosition_ += n;
921  nToWrite -= n;
922  src += n;
923  }
924  if (curPosition_ > fileSize_) {
925  // update fileSize and insure sync will update dir entry
926  fileSize_ = curPosition_;
927  flags_ |= F_FILE_DIR_DIRTY;
928  } else if (dateTime_ && nbyte) {
929  // insure sync will update modified date and time
930  flags_ |= F_FILE_DIR_DIRTY;
931  }
932 
933  if (flags_ & O_SYNC) {
934  if (!sync()) goto writeErrorReturn;
935  }
936  return nbyte;
937 
938  writeErrorReturn:
939  writeError = true;
940  return -1;
941 }
uint8_t sync(void)
Definition: Fat16.cpp:723
void * memcpy(void *, const void *, size_t)
Copy a memory area.
uint16_t fat_t
Type for FAT16 entry
Definition: Fat16.h:116
uint8_t const O_APPEND
Definition: Fat16.h:56
uint8_t seekEnd(void)
Definition: Fat16.h:241
bool writeError
Definition: Fat16.h:251
uint8_t const O_SYNC
Definition: Fat16.h:58
uint8_t data[512]
Definition: Fat16.h:125
uint8_t const O_WRITE
Definition: Fat16.h:48

◆ write() [2/3]

void Fat16::write ( uint8_t  b)
virtual

Write a byte to a file. Required by the Arduino Print class.

Use Fat16::writeError to check for errors.

Printを実装しています。

Fat16.cpp949 行目に定義があります。

949  {
950  write(&b, 1);
951 }
int16_t write(const void *buf, uint16_t nbyte)
Definition: Fat16.cpp:865

◆ write() [3/3]

void Fat16::write ( const char *  str)

Write a string to a file. Used by the Arduino Print class.

Use Fat16::writeError to check for errors.

Fat16.cpp964 行目に定義があります。

964  {
965  write(str, strlen(str));
966 }
const char * str
Definition: console.c:41
size_t strlen(const char *) __ATTR_PURE__
Calculate the length of a string.
int16_t write(const void *buf, uint16_t nbyte)
Definition: Fat16.cpp:865

◆ write_P()

void Fat16::write_P ( PGM_P  str)

Write a PROGMEM string to a file.

Use Fat16::writeError to check for errors.

Fat16.cpp978 行目に定義があります。

978  {
979  for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
980 }
const char * str
Definition: console.c:41
#define pgm_read_byte(address_short)
Definition: pgmspace.h:1055
int16_t write(const void *buf, uint16_t nbyte)
Definition: Fat16.cpp:865

◆ writeln_P()

void Fat16::writeln_P ( PGM_P  str)

Write a PROGMEM string followed by CR/LF to a file.

Use Fat16::writeError to check for errors.

Fat16.cpp987 行目に定義があります。

987  {
988  write_P(str);
989  println();
990 }
void write_P(PGM_P str)
Definition: Fat16.cpp:978
const char * str
Definition: console.c:41
size_t println(void)
Definition: Print.cpp:126

メンバ詳解

◆ writeError

bool Fat16::writeError

Fat16::writeError is set to true if an error occurs during a write(). Set Fat16::writeError to false before calling print() and/or write() and check for true after calls to write() and/or print().

Fat16.h251 行目に定義があります。


このクラス詳解は次のファイルから抽出されました: