My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SdBaseFile.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Arduino SdFat Library
  2. * Copyright (C) 2009 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef SdBaseFile_h
  21. #define SdBaseFile_h
  22. /**
  23. * \file
  24. * \brief SdBaseFile class
  25. */
  26. #include <avr/pgmspace.h>
  27. #define HardwareSerial_h // trick to disable the standard HWserial
  28. #if ARDUINO < 100
  29. #include <WProgram.h>
  30. #else // ARDUINO
  31. #include <Arduino.h>
  32. #endif // ARDUINO
  33. #include "MarlinSerial.h"
  34. #include "SdFatConfig.h"
  35. #include "SdVolume.h"
  36. //------------------------------------------------------------------------------
  37. /**
  38. * \struct fpos_t
  39. * \brief internal type for istream
  40. * do not use in user apps
  41. */
  42. struct fpos_t {
  43. /** stream position */
  44. uint32_t position;
  45. /** cluster for position */
  46. uint32_t cluster;
  47. fpos_t() : position(0), cluster(0) {}
  48. };
  49. // use the gnu style oflag in open()
  50. /** open() oflag for reading */
  51. uint8_t const O_READ = 0X01;
  52. /** open() oflag - same as O_IN */
  53. uint8_t const O_RDONLY = O_READ;
  54. /** open() oflag for write */
  55. uint8_t const O_WRITE = 0X02;
  56. /** open() oflag - same as O_WRITE */
  57. uint8_t const O_WRONLY = O_WRITE;
  58. /** open() oflag for reading and writing */
  59. uint8_t const O_RDWR = (O_READ | O_WRITE);
  60. /** open() oflag mask for access modes */
  61. uint8_t const O_ACCMODE = (O_READ | O_WRITE);
  62. /** The file offset shall be set to the end of the file prior to each write. */
  63. uint8_t const O_APPEND = 0X04;
  64. /** synchronous writes - call sync() after each write */
  65. uint8_t const O_SYNC = 0X08;
  66. /** truncate the file to zero length */
  67. uint8_t const O_TRUNC = 0X10;
  68. /** set the initial position at the end of the file */
  69. uint8_t const O_AT_END = 0X20;
  70. /** create the file if nonexistent */
  71. uint8_t const O_CREAT = 0X40;
  72. /** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
  73. uint8_t const O_EXCL = 0X80;
  74. // SdBaseFile class static and const definitions
  75. // flags for ls()
  76. /** ls() flag to print modify date */
  77. uint8_t const LS_DATE = 1;
  78. /** ls() flag to print file size */
  79. uint8_t const LS_SIZE = 2;
  80. /** ls() flag for recursive list of subdirectories */
  81. uint8_t const LS_R = 4;
  82. // flags for timestamp
  83. /** set the file's last access date */
  84. uint8_t const T_ACCESS = 1;
  85. /** set the file's creation date and time */
  86. uint8_t const T_CREATE = 2;
  87. /** Set the file's write date and time */
  88. uint8_t const T_WRITE = 4;
  89. // values for type_
  90. /** This file has not been opened. */
  91. uint8_t const FAT_FILE_TYPE_CLOSED = 0;
  92. /** A normal file */
  93. uint8_t const FAT_FILE_TYPE_NORMAL = 1;
  94. /** A FAT12 or FAT16 root directory */
  95. uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
  96. /** A FAT32 root directory */
  97. uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
  98. /** A subdirectory file*/
  99. uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
  100. /** Test value for directory type */
  101. uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
  102. /** date field for FAT directory entry
  103. * \param[in] year [1980,2107]
  104. * \param[in] month [1,12]
  105. * \param[in] day [1,31]
  106. *
  107. * \return Packed date for dir_t entry.
  108. */
  109. static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
  110. return (year - 1980) << 9 | month << 5 | day;
  111. }
  112. /** year part of FAT directory date field
  113. * \param[in] fatDate Date in packed dir format.
  114. *
  115. * \return Extracted year [1980,2107]
  116. */
  117. static inline uint16_t FAT_YEAR(uint16_t fatDate) {
  118. return 1980 + (fatDate >> 9);
  119. }
  120. /** month part of FAT directory date field
  121. * \param[in] fatDate Date in packed dir format.
  122. *
  123. * \return Extracted month [1,12]
  124. */
  125. static inline uint8_t FAT_MONTH(uint16_t fatDate) {
  126. return (fatDate >> 5) & 0XF;
  127. }
  128. /** day part of FAT directory date field
  129. * \param[in] fatDate Date in packed dir format.
  130. *
  131. * \return Extracted day [1,31]
  132. */
  133. static inline uint8_t FAT_DAY(uint16_t fatDate) {
  134. return fatDate & 0X1F;
  135. }
  136. /** time field for FAT directory entry
  137. * \param[in] hour [0,23]
  138. * \param[in] minute [0,59]
  139. * \param[in] second [0,59]
  140. *
  141. * \return Packed time for dir_t entry.
  142. */
  143. static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
  144. return hour << 11 | minute << 5 | second >> 1;
  145. }
  146. /** hour part of FAT directory time field
  147. * \param[in] fatTime Time in packed dir format.
  148. *
  149. * \return Extracted hour [0,23]
  150. */
  151. static inline uint8_t FAT_HOUR(uint16_t fatTime) {
  152. return fatTime >> 11;
  153. }
  154. /** minute part of FAT directory time field
  155. * \param[in] fatTime Time in packed dir format.
  156. *
  157. * \return Extracted minute [0,59]
  158. */
  159. static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
  160. return(fatTime >> 5) & 0X3F;
  161. }
  162. /** second part of FAT directory time field
  163. * Note second/2 is stored in packed time.
  164. *
  165. * \param[in] fatTime Time in packed dir format.
  166. *
  167. * \return Extracted second [0,58]
  168. */
  169. static inline uint8_t FAT_SECOND(uint16_t fatTime) {
  170. return 2*(fatTime & 0X1F);
  171. }
  172. /** Default date for file timestamps is 1 Jan 2000 */
  173. uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
  174. /** Default time for file timestamp is 1 am */
  175. uint16_t const FAT_DEFAULT_TIME = (1 << 11);
  176. //------------------------------------------------------------------------------
  177. /**
  178. * \class SdBaseFile
  179. * \brief Base class for SdFile with Print and C++ streams.
  180. */
  181. class SdBaseFile {
  182. public:
  183. /** Create an instance. */
  184. SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
  185. SdBaseFile(const char* path, uint8_t oflag);
  186. ~SdBaseFile() {if(isOpen()) close();}
  187. /**
  188. * writeError is set to true if an error occurs during a write().
  189. * Set writeError to false before calling print() and/or write() and check
  190. * for true after calls to print() and/or write().
  191. */
  192. bool writeError;
  193. //----------------------------------------------------------------------------
  194. // helpers for stream classes
  195. /** get position for streams
  196. * \param[out] pos struct to receive position
  197. */
  198. void getpos(fpos_t* pos);
  199. /** set position for streams
  200. * \param[out] pos struct with value for new position
  201. */
  202. void setpos(fpos_t* pos);
  203. //----------------------------------------------------------------------------
  204. bool close();
  205. bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  206. bool createContiguous(SdBaseFile* dirFile,
  207. const char* path, uint32_t size);
  208. /** \return The current cluster number for a file or directory. */
  209. uint32_t curCluster() const {return curCluster_;}
  210. /** \return The current position for a file or directory. */
  211. uint32_t curPosition() const {return curPosition_;}
  212. /** \return Current working directory */
  213. static SdBaseFile* cwd() {return cwd_;}
  214. /** Set the date/time callback function
  215. *
  216. * \param[in] dateTime The user's call back function. The callback
  217. * function is of the form:
  218. *
  219. * \code
  220. * void dateTime(uint16_t* date, uint16_t* time) {
  221. * uint16_t year;
  222. * uint8_t month, day, hour, minute, second;
  223. *
  224. * // User gets date and time from GPS or real-time clock here
  225. *
  226. * // return date using FAT_DATE macro to format fields
  227. * *date = FAT_DATE(year, month, day);
  228. *
  229. * // return time using FAT_TIME macro to format fields
  230. * *time = FAT_TIME(hour, minute, second);
  231. * }
  232. * \endcode
  233. *
  234. * Sets the function that is called when a file is created or when
  235. * a file's directory entry is modified by sync(). All timestamps,
  236. * access, creation, and modify, are set when a file is created.
  237. * sync() maintains the last access date and last modify date/time.
  238. *
  239. * See the timestamp() function.
  240. */
  241. static void dateTimeCallback(
  242. void (*dateTime)(uint16_t* date, uint16_t* time)) {
  243. dateTime_ = dateTime;
  244. }
  245. /** Cancel the date/time callback function. */
  246. static void dateTimeCallbackCancel() {dateTime_ = 0;}
  247. bool dirEntry(dir_t* dir);
  248. static void dirName(const dir_t& dir, char* name);
  249. bool exists(const char* name);
  250. int16_t fgets(char* str, int16_t num, char* delim = 0);
  251. /** \return The total number of bytes in a file or directory. */
  252. uint32_t fileSize() const {return fileSize_;}
  253. /** \return The first cluster number for a file or directory. */
  254. uint32_t firstCluster() const {return firstCluster_;}
  255. bool getFilename(char* name);
  256. /** \return True if this is a directory else false. */
  257. bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
  258. /** \return True if this is a normal file else false. */
  259. bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
  260. /** \return True if this is an open file/directory else false. */
  261. bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
  262. /** \return True if this is a subdirectory else false. */
  263. bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
  264. /** \return True if this is the root directory. */
  265. bool isRoot() const {
  266. return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
  267. }
  268. void ls( uint8_t flags = 0, uint8_t indent = 0);
  269. bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
  270. // alias for backward compactability
  271. bool makeDir(SdBaseFile* dir, const char* path) {
  272. return mkdir(dir, path, false);
  273. }
  274. bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  275. bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  276. bool open(const char* path, uint8_t oflag = O_READ);
  277. bool openNext(SdBaseFile* dirFile, uint8_t oflag);
  278. bool openRoot(SdVolume* vol);
  279. int peek();
  280. static void printFatDate(uint16_t fatDate);
  281. static void printFatTime( uint16_t fatTime);
  282. bool printName();
  283. int16_t read();
  284. int16_t read(void* buf, uint16_t nbyte);
  285. int8_t readDir(dir_t* dir);
  286. static bool remove(SdBaseFile* dirFile, const char* path);
  287. bool remove();
  288. /** Set the file's current position to zero. */
  289. void rewind() {seekSet(0);}
  290. bool rename(SdBaseFile* dirFile, const char* newPath);
  291. bool rmdir();
  292. // for backward compatibility
  293. bool rmDir() {return rmdir();}
  294. bool rmRfStar();
  295. /** Set the files position to current position + \a pos. See seekSet().
  296. * \param[in] offset The new position in bytes from the current position.
  297. * \return true for success or false for failure.
  298. */
  299. bool seekCur(int32_t offset) {
  300. return seekSet(curPosition_ + offset);
  301. }
  302. /** Set the files position to end-of-file + \a offset. See seekSet().
  303. * \param[in] offset The new position in bytes from end-of-file.
  304. * \return true for success or false for failure.
  305. */
  306. bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
  307. bool seekSet(uint32_t pos);
  308. bool sync();
  309. bool timestamp(SdBaseFile* file);
  310. bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
  311. uint8_t hour, uint8_t minute, uint8_t second);
  312. /** Type of file. You should use isFile() or isDir() instead of type()
  313. * if possible.
  314. *
  315. * \return The file or directory type.
  316. */
  317. uint8_t type() const {return type_;}
  318. bool truncate(uint32_t size);
  319. /** \return SdVolume that contains this file. */
  320. SdVolume* volume() const {return vol_;}
  321. int16_t write(const void* buf, uint16_t nbyte);
  322. //------------------------------------------------------------------------------
  323. private:
  324. // allow SdFat to set cwd_
  325. friend class SdFat;
  326. // global pointer to cwd dir
  327. static SdBaseFile* cwd_;
  328. // data time callback function
  329. static void (*dateTime_)(uint16_t* date, uint16_t* time);
  330. // bits defined in flags_
  331. // should be 0X0F
  332. static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
  333. // sync of directory entry required
  334. static uint8_t const F_FILE_DIR_DIRTY = 0X80;
  335. // private data
  336. uint8_t flags_; // See above for definition of flags_ bits
  337. uint8_t fstate_; // error and eof indicator
  338. uint8_t type_; // type of file see above for values
  339. uint32_t curCluster_; // cluster for current file position
  340. uint32_t curPosition_; // current file position in bytes from beginning
  341. uint32_t dirBlock_; // block for this files directory entry
  342. uint8_t dirIndex_; // index of directory entry in dirBlock
  343. uint32_t fileSize_; // file size in bytes
  344. uint32_t firstCluster_; // first cluster of file
  345. SdVolume* vol_; // volume where file is located
  346. /** experimental don't use */
  347. bool openParent(SdBaseFile* dir);
  348. // private functions
  349. bool addCluster();
  350. bool addDirCluster();
  351. dir_t* cacheDirEntry(uint8_t action);
  352. int8_t lsPrintNext( uint8_t flags, uint8_t indent);
  353. static bool make83Name(const char* str, uint8_t* name, const char** ptr);
  354. bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
  355. bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
  356. bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
  357. dir_t* readDirCache();
  358. //------------------------------------------------------------------------------
  359. // to be deleted
  360. static void printDirName( const dir_t& dir,
  361. uint8_t width, bool printSlash);
  362. //------------------------------------------------------------------------------
  363. // Deprecated functions - suppress cpplint warnings with NOLINT comment
  364. #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
  365. public:
  366. /** \deprecated Use:
  367. * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  368. * \param[out] bgnBlock the first block address for the file.
  369. * \param[out] endBlock the last block address for the file.
  370. * \return true for success or false for failure.
  371. */
  372. bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
  373. return contiguousRange(&bgnBlock, &endBlock);
  374. }
  375. /** \deprecated Use:
  376. * bool createContiguous(SdBaseFile* dirFile,
  377. * const char* path, uint32_t size)
  378. * \param[in] dirFile The directory where the file will be created.
  379. * \param[in] path A path with a valid DOS 8.3 file name.
  380. * \param[in] size The desired file size.
  381. * \return true for success or false for failure.
  382. */
  383. bool createContiguous(SdBaseFile& dirFile, // NOLINT
  384. const char* path, uint32_t size) {
  385. return createContiguous(&dirFile, path, size);
  386. }
  387. /** \deprecated Use:
  388. * static void dateTimeCallback(
  389. * void (*dateTime)(uint16_t* date, uint16_t* time));
  390. * \param[in] dateTime The user's call back function.
  391. */
  392. static void dateTimeCallback(
  393. void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
  394. oldDateTime_ = dateTime;
  395. dateTime_ = dateTime ? oldToNew : 0;
  396. }
  397. /** \deprecated Use: bool dirEntry(dir_t* dir);
  398. * \param[out] dir Location for return of the file's directory entry.
  399. * \return true for success or false for failure.
  400. */
  401. bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
  402. /** \deprecated Use:
  403. * bool mkdir(SdBaseFile* dir, const char* path);
  404. * \param[in] dir An open SdFat instance for the directory that will contain
  405. * the new directory.
  406. * \param[in] path A path with a valid 8.3 DOS name for the new directory.
  407. * \return true for success or false for failure.
  408. */
  409. bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT
  410. return mkdir(&dir, path);
  411. }
  412. /** \deprecated Use:
  413. * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  414. * \param[in] dirFile An open SdFat instance for the directory containing the
  415. * file to be opened.
  416. * \param[in] path A path with a valid 8.3 DOS name for the file.
  417. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  418. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  419. * \return true for success or false for failure.
  420. */
  421. bool open(SdBaseFile& dirFile, // NOLINT
  422. const char* path, uint8_t oflag) {
  423. return open(&dirFile, path, oflag);
  424. }
  425. /** \deprecated Do not use in new apps
  426. * \param[in] dirFile An open SdFat instance for the directory containing the
  427. * file to be opened.
  428. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  429. * \return true for success or false for failure.
  430. */
  431. bool open(SdBaseFile& dirFile, const char* path) { // NOLINT
  432. return open(dirFile, path, O_RDWR);
  433. }
  434. /** \deprecated Use:
  435. * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  436. * \param[in] dirFile An open SdFat instance for the directory.
  437. * \param[in] index The \a index of the directory entry for the file to be
  438. * opened. The value for \a index is (directory file position)/32.
  439. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  440. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  441. * \return true for success or false for failure.
  442. */
  443. bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
  444. return open(&dirFile, index, oflag);
  445. }
  446. /** \deprecated Use: bool openRoot(SdVolume* vol);
  447. * \param[in] vol The FAT volume containing the root directory to be opened.
  448. * \return true for success or false for failure.
  449. */
  450. bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
  451. /** \deprecated Use: int8_t readDir(dir_t* dir);
  452. * \param[out] dir The dir_t struct that will receive the data.
  453. * \return bytes read for success zero for eof or -1 for failure.
  454. */
  455. int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
  456. /** \deprecated Use:
  457. * static uint8_t remove(SdBaseFile* dirFile, const char* path);
  458. * \param[in] dirFile The directory that contains the file.
  459. * \param[in] path The name of the file to be removed.
  460. * \return true for success or false for failure.
  461. */
  462. static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT
  463. return remove(&dirFile, path);
  464. }
  465. //------------------------------------------------------------------------------
  466. // rest are private
  467. private:
  468. static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
  469. static void oldToNew(uint16_t* date, uint16_t* time) {
  470. uint16_t d;
  471. uint16_t t;
  472. oldDateTime_(d, t);
  473. *date = d;
  474. *time = t;
  475. }
  476. #endif // ALLOW_DEPRECATED_FUNCTIONS
  477. };
  478. #endif // SdBaseFile_h