My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SdBaseFile.h 18KB

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