My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cardreader.cpp 16KB

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