My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cardreader.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. #include "Marlin.h"
  23. #include "cardreader.h"
  24. #include "ultralcd.h"
  25. #include "stepper.h"
  26. #include "temperature.h"
  27. #include "language.h"
  28. #if ENABLED(SDSUPPORT)
  29. CardReader::CardReader() {
  30. filesize = 0;
  31. sdpos = 0;
  32. sdprinting = false;
  33. cardOK = false;
  34. saving = false;
  35. logging = false;
  36. workDirDepth = 0;
  37. file_subcall_ctr = 0;
  38. memset(workDirParents, 0, sizeof(workDirParents));
  39. autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
  40. autostart_index = 0;
  41. //power to SD reader
  42. #if SDPOWER > -1
  43. OUT_WRITE(SDPOWER, HIGH);
  44. #endif //SDPOWER
  45. next_autostart_ms = millis() + 5000;
  46. }
  47. char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
  48. char *pos = buffer;
  49. for (uint8_t i = 0; i < 11; i++) {
  50. if (p.name[i] == ' ') continue;
  51. if (i == 8) *pos++ = '.';
  52. *pos++ = p.name[i];
  53. }
  54. *pos++ = 0;
  55. return buffer;
  56. }
  57. /**
  58. * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
  59. * LS_Count - Add +1 to nrFiles for every file within the parent
  60. * LS_GetFilename - Get the filename of the file indexed by nrFiles
  61. * LS_SerialPrint - Print the full path of each file to serial output
  62. */
  63. void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
  64. dir_t p;
  65. uint8_t cnt = 0;
  66. // Read the next entry from a directory
  67. while (parent.readDir(p, longFilename) > 0) {
  68. // If the entry is a directory and the action is LS_SerialPrint
  69. if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
  70. // Get the short name for the item, which we know is a folder
  71. char lfilename[FILENAME_LENGTH];
  72. createFilename(lfilename, p);
  73. // Allocate enough stack space for the full path to a folder, trailing slash, and nul
  74. boolean prepend_is_empty = (prepend[0] == '\0');
  75. int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1;
  76. char path[len];
  77. // Append the FOLDERNAME12/ to the passed string.
  78. // It contains the full path to the "parent" argument.
  79. // We now have the full path to the item in this folder.
  80. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
  81. strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
  82. strcat(path, "/"); // 1 character
  83. // Serial.print(path);
  84. // Get a new directory object using the full path
  85. // and dive recursively into it.
  86. SdFile dir;
  87. if (!dir.open(parent, lfilename, O_READ)) {
  88. if (lsAction == LS_SerialPrint) {
  89. SERIAL_ECHO_START;
  90. SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
  91. SERIAL_ECHOLN(lfilename);
  92. }
  93. }
  94. lsDive(path, dir);
  95. // close() is done automatically by destructor of SdFile
  96. }
  97. else {
  98. uint8_t pn0 = p.name[0];
  99. if (pn0 == DIR_NAME_FREE) break;
  100. if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
  101. if (longFilename[0] == '.') continue;
  102. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  103. filenameIsDir = DIR_IS_SUBDIR(&p);
  104. if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
  105. switch (lsAction) {
  106. case LS_Count:
  107. nrFiles++;
  108. break;
  109. case LS_SerialPrint:
  110. createFilename(filename, p);
  111. SERIAL_PROTOCOL(prepend);
  112. SERIAL_PROTOCOLLN(filename);
  113. break;
  114. case LS_GetFilename:
  115. createFilename(filename, p);
  116. if (match != NULL) {
  117. if (strcasecmp(match, filename) == 0) return;
  118. }
  119. else if (cnt == nrFiles) return;
  120. cnt++;
  121. break;
  122. }
  123. }
  124. } // while readDir
  125. }
  126. void CardReader::ls() {
  127. lsAction = LS_SerialPrint;
  128. root.rewind();
  129. lsDive("", root);
  130. }
  131. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  132. /**
  133. * Get a long pretty path based on a DOS 8.3 path
  134. */
  135. void CardReader::printLongPath(char *path) {
  136. lsAction = LS_GetFilename;
  137. int i, pathLen = strlen(path);
  138. // SERIAL_ECHOPGM("Full Path: "); SERIAL_ECHOLN(path);
  139. // Zero out slashes to make segments
  140. for (i = 0; i < pathLen; i++) if (path[i] == '/') path[i] = '\0';
  141. SdFile diveDir = root; // start from the root for segment 1
  142. for (i = 0; i < pathLen;) {
  143. if (path[i] == '\0') i++; // move past a single nul
  144. char *segment = &path[i]; // The segment after most slashes
  145. // If a segment is empty (extra-slash) then exit
  146. if (!*segment) break;
  147. // Go to the next segment
  148. while (path[++i]) { }
  149. // SERIAL_ECHOPGM("Looking for segment: "); SERIAL_ECHOLN(segment);
  150. // Find the item, setting the long filename
  151. diveDir.rewind();
  152. lsDive("", diveDir, segment);
  153. // Print /LongNamePart to serial output
  154. SERIAL_PROTOCOLCHAR('/');
  155. SERIAL_PROTOCOL(longFilename[0] ? longFilename : "???");
  156. // If the filename was printed then that's it
  157. if (!filenameIsDir) break;
  158. // SERIAL_ECHOPGM("Opening dir: "); SERIAL_ECHOLN(segment);
  159. // Open the sub-item as the new dive parent
  160. SdFile dir;
  161. if (!dir.open(diveDir, segment, O_READ)) {
  162. SERIAL_EOL;
  163. SERIAL_ECHO_START;
  164. SERIAL_ECHOPGM(MSG_SD_CANT_OPEN_SUBDIR);
  165. SERIAL_ECHO(segment);
  166. break;
  167. }
  168. diveDir.close();
  169. diveDir = dir;
  170. } // while i<pathLen
  171. SERIAL_EOL;
  172. }
  173. #endif // LONG_FILENAME_HOST_SUPPORT
  174. void CardReader::initsd() {
  175. cardOK = false;
  176. if (root.isOpen()) root.close();
  177. #ifndef SPI_SPEED
  178. #define SPI_SPEED SPI_FULL_SPEED
  179. #endif
  180. if (!card.init(SPI_SPEED,SDSS)
  181. #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
  182. && !card.init(SPI_SPEED, LCD_SDSS)
  183. #endif
  184. ) {
  185. //if (!card.init(SPI_HALF_SPEED,SDSS))
  186. SERIAL_ECHO_START;
  187. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  188. }
  189. else if (!volume.init(&card)) {
  190. SERIAL_ERROR_START;
  191. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  192. }
  193. else if (!root.openRoot(&volume)) {
  194. SERIAL_ERROR_START;
  195. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  196. }
  197. else {
  198. cardOK = true;
  199. SERIAL_ECHO_START;
  200. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  201. }
  202. workDir = root;
  203. curDir = &root;
  204. /**
  205. if (!workDir.openRoot(&volume)) {
  206. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  207. }
  208. */
  209. }
  210. void CardReader::setroot() {
  211. /*if (!workDir.openRoot(&volume)) {
  212. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  213. }*/
  214. workDir = root;
  215. curDir = &workDir;
  216. }
  217. void CardReader::release() {
  218. sdprinting = false;
  219. cardOK = false;
  220. }
  221. void CardReader::openAndPrintFile(const char *name) {
  222. char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
  223. sprintf_P(cmd, PSTR("M23 %s"), name);
  224. for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
  225. enqueue_and_echo_command(cmd);
  226. enqueue_and_echo_commands_P(PSTR("M24"));
  227. }
  228. void CardReader::startFileprint() {
  229. if (cardOK)
  230. sdprinting = true;
  231. }
  232. void CardReader::pauseSDPrint() {
  233. if (sdprinting) sdprinting = false;
  234. }
  235. void CardReader::openLogFile(char* name) {
  236. logging = true;
  237. openFile(name, false);
  238. }
  239. void CardReader::getAbsFilename(char *t) {
  240. uint8_t cnt = 0;
  241. *t = '/'; t++; cnt++;
  242. for (uint8_t i = 0; i < workDirDepth; i++) {
  243. workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
  244. while (*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
  245. }
  246. if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH))
  247. file.getFilename(t);
  248. else
  249. t[0] = 0;
  250. }
  251. void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
  252. if (!cardOK) return;
  253. if (file.isOpen()) { //replacing current file by new file, or subfile call
  254. if (push_current) {
  255. if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
  256. SERIAL_ERROR_START;
  257. SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
  258. SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
  259. kill(PSTR(MSG_KILLED));
  260. return;
  261. }
  262. SERIAL_ECHO_START;
  263. SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
  264. SERIAL_ECHO(name);
  265. SERIAL_ECHOPGM("\" parent:\"");
  266. //store current filename and position
  267. getAbsFilename(proc_filenames[file_subcall_ctr]);
  268. SERIAL_ECHO(proc_filenames[file_subcall_ctr]);
  269. SERIAL_ECHOPGM("\" pos");
  270. SERIAL_ECHOLN(sdpos);
  271. filespos[file_subcall_ctr] = sdpos;
  272. file_subcall_ctr++;
  273. }
  274. else {
  275. SERIAL_ECHO_START;
  276. SERIAL_ECHOPGM("Now doing file: ");
  277. SERIAL_ECHOLN(name);
  278. }
  279. file.close();
  280. }
  281. else { //opening fresh file
  282. file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure
  283. SERIAL_ECHO_START;
  284. SERIAL_ECHOPGM("Now fresh file: ");
  285. SERIAL_ECHOLN(name);
  286. }
  287. sdprinting = false;
  288. SdFile myDir;
  289. curDir = &root;
  290. char *fname = name;
  291. char *dirname_start, *dirname_end;
  292. if (name[0] == '/') {
  293. dirname_start = &name[1];
  294. while (dirname_start > 0) {
  295. dirname_end = strchr(dirname_start, '/');
  296. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  297. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  298. if (dirname_end > 0 && dirname_end > dirname_start) {
  299. char subdirname[FILENAME_LENGTH];
  300. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  301. subdirname[dirname_end - dirname_start] = 0;
  302. SERIAL_ECHOLN(subdirname);
  303. if (!myDir.open(curDir, subdirname, O_READ)) {
  304. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  305. SERIAL_PROTOCOL(subdirname);
  306. SERIAL_PROTOCOLCHAR('.');
  307. return;
  308. }
  309. else {
  310. //SERIAL_ECHOLN("dive ok");
  311. }
  312. curDir = &myDir;
  313. dirname_start = dirname_end + 1;
  314. }
  315. else { // the remainder after all /fsa/fdsa/ is the filename
  316. fname = dirname_start;
  317. //SERIAL_ECHOLN("remainder");
  318. //SERIAL_ECHOLN(fname);
  319. break;
  320. }
  321. }
  322. }
  323. else { //relative path
  324. curDir = &workDir;
  325. }
  326. if (read) {
  327. if (file.open(curDir, fname, O_READ)) {
  328. filesize = file.fileSize();
  329. SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
  330. SERIAL_PROTOCOL(fname);
  331. SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
  332. SERIAL_PROTOCOLLN(filesize);
  333. sdpos = 0;
  334. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  335. getfilename(0, fname);
  336. lcd_setstatus(longFilename[0] ? longFilename : fname);
  337. }
  338. else {
  339. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  340. SERIAL_PROTOCOL(fname);
  341. SERIAL_PROTOCOLPGM(".\n");
  342. }
  343. }
  344. else { //write
  345. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  346. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  347. SERIAL_PROTOCOL(fname);
  348. SERIAL_PROTOCOLPGM(".\n");
  349. }
  350. else {
  351. saving = true;
  352. SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
  353. SERIAL_PROTOCOLLN(name);
  354. lcd_setstatus(fname);
  355. }
  356. }
  357. }
  358. void CardReader::removeFile(char* name) {
  359. if (!cardOK) return;
  360. file.close();
  361. sdprinting = false;
  362. SdFile myDir;
  363. curDir = &root;
  364. char *fname = name;
  365. char *dirname_start, *dirname_end;
  366. if (name[0] == '/') {
  367. dirname_start = strchr(name, '/') + 1;
  368. while (dirname_start > 0) {
  369. dirname_end = strchr(dirname_start, '/');
  370. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  371. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  372. if (dirname_end > 0 && dirname_end > dirname_start) {
  373. char subdirname[FILENAME_LENGTH];
  374. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  375. subdirname[dirname_end - dirname_start] = 0;
  376. SERIAL_ECHOLN(subdirname);
  377. if (!myDir.open(curDir, subdirname, O_READ)) {
  378. SERIAL_PROTOCOLPGM("open failed, File: ");
  379. SERIAL_PROTOCOL(subdirname);
  380. SERIAL_PROTOCOLCHAR('.');
  381. return;
  382. }
  383. else {
  384. //SERIAL_ECHOLN("dive ok");
  385. }
  386. curDir = &myDir;
  387. dirname_start = dirname_end + 1;
  388. }
  389. else { // the remainder after all /fsa/fdsa/ is the filename
  390. fname = dirname_start;
  391. //SERIAL_ECHOLN("remainder");
  392. //SERIAL_ECHOLN(fname);
  393. break;
  394. }
  395. }
  396. }
  397. else { // relative path
  398. curDir = &workDir;
  399. }
  400. if (file.remove(curDir, fname)) {
  401. SERIAL_PROTOCOLPGM("File deleted:");
  402. SERIAL_PROTOCOLLN(fname);
  403. sdpos = 0;
  404. }
  405. else {
  406. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  407. SERIAL_PROTOCOL(fname);
  408. SERIAL_PROTOCOLCHAR('.');
  409. }
  410. }
  411. void CardReader::getStatus() {
  412. if (cardOK) {
  413. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  414. SERIAL_PROTOCOL(sdpos);
  415. SERIAL_PROTOCOLCHAR('/');
  416. SERIAL_PROTOCOLLN(filesize);
  417. }
  418. else {
  419. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  420. }
  421. }
  422. void CardReader::write_command(char *buf) {
  423. char* begin = buf;
  424. char* npos = 0;
  425. char* end = buf + strlen(buf) - 1;
  426. file.writeError = false;
  427. if ((npos = strchr(buf, 'N')) != NULL) {
  428. begin = strchr(npos, ' ') + 1;
  429. end = strchr(npos, '*') - 1;
  430. }
  431. end[1] = '\r';
  432. end[2] = '\n';
  433. end[3] = '\0';
  434. file.write(begin);
  435. if (file.writeError) {
  436. SERIAL_ERROR_START;
  437. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  438. }
  439. }
  440. void CardReader::checkautostart(bool force) {
  441. if (!force && (!autostart_stilltocheck || next_autostart_ms < millis()))
  442. return;
  443. autostart_stilltocheck = false;
  444. if (!cardOK) {
  445. initsd();
  446. if (!cardOK) return; // fail
  447. }
  448. char autoname[10];
  449. sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
  450. for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
  451. dir_t p;
  452. root.rewind();
  453. bool found = false;
  454. while (root.readDir(p, NULL) > 0) {
  455. for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
  456. if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
  457. openAndPrintFile(autoname);
  458. found = true;
  459. }
  460. }
  461. if (!found)
  462. autostart_index = -1;
  463. else
  464. autostart_index++;
  465. }
  466. void CardReader::closefile(bool store_location) {
  467. file.sync();
  468. file.close();
  469. saving = logging = false;
  470. if (store_location) {
  471. //future: store printer state, filename and position for continuing a stopped print
  472. // so one can unplug the printer and continue printing the next day.
  473. }
  474. }
  475. /**
  476. * Get the name of a file in the current directory by index
  477. */
  478. void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
  479. curDir = &workDir;
  480. lsAction = LS_GetFilename;
  481. nrFiles = nr;
  482. curDir->rewind();
  483. lsDive("", *curDir, match);
  484. }
  485. uint16_t CardReader::getnrfilenames() {
  486. curDir = &workDir;
  487. lsAction = LS_Count;
  488. nrFiles = 0;
  489. curDir->rewind();
  490. lsDive("", *curDir);
  491. //SERIAL_ECHOLN(nrFiles);
  492. return nrFiles;
  493. }
  494. void CardReader::chdir(const char * relpath) {
  495. SdFile newfile;
  496. SdFile *parent = &root;
  497. if (workDir.isOpen()) parent = &workDir;
  498. if (!newfile.open(*parent, relpath, O_READ)) {
  499. SERIAL_ECHO_START;
  500. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  501. SERIAL_ECHOLN(relpath);
  502. }
  503. else {
  504. if (workDirDepth < MAX_DIR_DEPTH)
  505. workDirParents[workDirDepth++] = *parent;
  506. workDir = newfile;
  507. }
  508. }
  509. void CardReader::updir() {
  510. if (workDirDepth > 0)
  511. workDir = workDirParents[--workDirDepth];
  512. }
  513. void CardReader::printingHasFinished() {
  514. st_synchronize();
  515. if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
  516. file.close();
  517. file_subcall_ctr--;
  518. openFile(proc_filenames[file_subcall_ctr], true, true);
  519. setIndex(filespos[file_subcall_ctr]);
  520. startFileprint();
  521. }
  522. else {
  523. file.close();
  524. sdprinting = false;
  525. if (SD_FINISHED_STEPPERRELEASE)
  526. enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  527. autotempShutdown();
  528. }
  529. }
  530. #endif //SDSUPPORT