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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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(SDSLOW)
  157. #define SPI_SPEED SPI_HALF_SPEED
  158. #else
  159. #define SPI_SPEED SPI_FULL_SPEED
  160. #endif
  161. if (!card.init(SPI_SPEED,SDSS)
  162. #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
  163. && !card.init(SPI_SPEED, LCD_SDSS)
  164. #endif
  165. ) {
  166. //if (!card.init(SPI_HALF_SPEED,SDSS))
  167. SERIAL_ECHO_START;
  168. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  169. }
  170. else if (!volume.init(&card)) {
  171. SERIAL_ERROR_START;
  172. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  173. }
  174. else if (!root.openRoot(&volume)) {
  175. SERIAL_ERROR_START;
  176. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  177. }
  178. else {
  179. cardOK = true;
  180. SERIAL_ECHO_START;
  181. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  182. }
  183. workDir = root;
  184. curDir = &root;
  185. /*
  186. if (!workDir.openRoot(&volume)) {
  187. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  188. }
  189. */
  190. }
  191. void CardReader::setroot() {
  192. /*if (!workDir.openRoot(&volume)) {
  193. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  194. }*/
  195. workDir = root;
  196. curDir = &workDir;
  197. }
  198. void CardReader::release() {
  199. sdprinting = false;
  200. cardOK = false;
  201. }
  202. void CardReader::startFileprint() {
  203. if (cardOK) {
  204. sdprinting = true;
  205. }
  206. }
  207. void CardReader::pauseSDPrint() {
  208. if (sdprinting) sdprinting = false;
  209. }
  210. void CardReader::openLogFile(char* name) {
  211. logging = true;
  212. openFile(name, false);
  213. }
  214. void CardReader::getAbsFilename(char *t) {
  215. uint8_t cnt = 0;
  216. *t = '/'; t++; cnt++;
  217. for (uint8_t i = 0; i < workDirDepth; i++) {
  218. workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
  219. while(*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
  220. }
  221. if (cnt < MAXPATHNAMELENGTH - FILENAME_LENGTH)
  222. file.getFilename(t);
  223. else
  224. t[0] = 0;
  225. }
  226. void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) {
  227. if (!cardOK) return;
  228. if (file.isOpen()) { //replacing current file by new file, or subfile call
  229. if (!replace_current) {
  230. if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
  231. SERIAL_ERROR_START;
  232. SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
  233. SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
  234. kill(PSTR(MSG_KILLED));
  235. return;
  236. }
  237. SERIAL_ECHO_START;
  238. SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
  239. SERIAL_ECHO(name);
  240. SERIAL_ECHOPGM("\" parent:\"");
  241. //store current filename and position
  242. getAbsFilename(filenames[file_subcall_ctr]);
  243. SERIAL_ECHO(filenames[file_subcall_ctr]);
  244. SERIAL_ECHOPGM("\" pos");
  245. SERIAL_ECHOLN(sdpos);
  246. filespos[file_subcall_ctr] = sdpos;
  247. file_subcall_ctr++;
  248. }
  249. else {
  250. SERIAL_ECHO_START;
  251. SERIAL_ECHOPGM("Now doing file: ");
  252. SERIAL_ECHOLN(name);
  253. }
  254. file.close();
  255. }
  256. else { //opening fresh file
  257. file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure
  258. SERIAL_ECHO_START;
  259. SERIAL_ECHOPGM("Now fresh file: ");
  260. SERIAL_ECHOLN(name);
  261. }
  262. sdprinting = false;
  263. SdFile myDir;
  264. curDir = &root;
  265. char *fname = name;
  266. char *dirname_start, *dirname_end;
  267. if (name[0] == '/') {
  268. dirname_start = &name[1];
  269. while (dirname_start > 0) {
  270. dirname_end = strchr(dirname_start, '/');
  271. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  272. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  273. if (dirname_end > 0 && dirname_end > dirname_start) {
  274. char subdirname[FILENAME_LENGTH];
  275. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  276. subdirname[dirname_end - dirname_start] = 0;
  277. SERIAL_ECHOLN(subdirname);
  278. if (!myDir.open(curDir, subdirname, O_READ)) {
  279. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  280. SERIAL_PROTOCOL(subdirname);
  281. SERIAL_PROTOCOLCHAR('.');
  282. return;
  283. }
  284. else {
  285. //SERIAL_ECHOLN("dive ok");
  286. }
  287. curDir = &myDir;
  288. dirname_start = dirname_end + 1;
  289. }
  290. else { // the remainder after all /fsa/fdsa/ is the filename
  291. fname = dirname_start;
  292. //SERIAL_ECHOLN("remainder");
  293. //SERIAL_ECHOLN(fname);
  294. break;
  295. }
  296. }
  297. }
  298. else { //relative path
  299. curDir = &workDir;
  300. }
  301. if (read) {
  302. if (file.open(curDir, fname, O_READ)) {
  303. filesize = file.fileSize();
  304. SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
  305. SERIAL_PROTOCOL(fname);
  306. SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
  307. SERIAL_PROTOCOLLN(filesize);
  308. sdpos = 0;
  309. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  310. getfilename(0, fname);
  311. lcd_setstatus(longFilename[0] ? longFilename : fname);
  312. }
  313. else {
  314. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  315. SERIAL_PROTOCOL(fname);
  316. SERIAL_PROTOCOLPGM(".\n");
  317. }
  318. }
  319. else { //write
  320. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  321. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  322. SERIAL_PROTOCOL(fname);
  323. SERIAL_PROTOCOLPGM(".\n");
  324. }
  325. else {
  326. saving = true;
  327. SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
  328. SERIAL_PROTOCOLLN(name);
  329. lcd_setstatus(fname);
  330. }
  331. }
  332. }
  333. void CardReader::removeFile(char* name) {
  334. if (!cardOK) return;
  335. file.close();
  336. sdprinting = false;
  337. SdFile myDir;
  338. curDir = &root;
  339. char *fname = name;
  340. char *dirname_start, *dirname_end;
  341. if (name[0] == '/') {
  342. dirname_start = strchr(name, '/') + 1;
  343. while (dirname_start > 0) {
  344. dirname_end = strchr(dirname_start, '/');
  345. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  346. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  347. if (dirname_end > 0 && dirname_end > dirname_start) {
  348. char subdirname[FILENAME_LENGTH];
  349. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  350. subdirname[dirname_end - dirname_start] = 0;
  351. SERIAL_ECHOLN(subdirname);
  352. if (!myDir.open(curDir, subdirname, O_READ)) {
  353. SERIAL_PROTOCOLPGM("open failed, File: ");
  354. SERIAL_PROTOCOL(subdirname);
  355. SERIAL_PROTOCOLCHAR('.');
  356. return;
  357. }
  358. else {
  359. //SERIAL_ECHOLN("dive ok");
  360. }
  361. curDir = &myDir;
  362. dirname_start = dirname_end + 1;
  363. }
  364. else { // the remainder after all /fsa/fdsa/ is the filename
  365. fname = dirname_start;
  366. //SERIAL_ECHOLN("remainder");
  367. //SERIAL_ECHOLN(fname);
  368. break;
  369. }
  370. }
  371. }
  372. else { // relative path
  373. curDir = &workDir;
  374. }
  375. if (file.remove(curDir, fname)) {
  376. SERIAL_PROTOCOLPGM("File deleted:");
  377. SERIAL_PROTOCOLLN(fname);
  378. sdpos = 0;
  379. }
  380. else {
  381. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  382. SERIAL_PROTOCOL(fname);
  383. SERIAL_PROTOCOLCHAR('.');
  384. }
  385. }
  386. void CardReader::getStatus() {
  387. if (cardOK) {
  388. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  389. SERIAL_PROTOCOL(sdpos);
  390. SERIAL_PROTOCOLCHAR('/');
  391. SERIAL_PROTOCOLLN(filesize);
  392. }
  393. else {
  394. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  395. }
  396. }
  397. void CardReader::write_command(char *buf) {
  398. char* begin = buf;
  399. char* npos = 0;
  400. char* end = buf + strlen(buf) - 1;
  401. file.writeError = false;
  402. if ((npos = strchr(buf, 'N')) != NULL) {
  403. begin = strchr(npos, ' ') + 1;
  404. end = strchr(npos, '*') - 1;
  405. }
  406. end[1] = '\r';
  407. end[2] = '\n';
  408. end[3] = '\0';
  409. file.write(begin);
  410. if (file.writeError) {
  411. SERIAL_ERROR_START;
  412. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  413. }
  414. }
  415. void CardReader::checkautostart(bool force) {
  416. if (!force && (!autostart_stilltocheck || next_autostart_ms < millis()))
  417. return;
  418. autostart_stilltocheck = false;
  419. if (!cardOK) {
  420. initsd();
  421. if (!cardOK) return; // fail
  422. }
  423. char autoname[10];
  424. sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
  425. for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
  426. dir_t p;
  427. root.rewind();
  428. bool found = false;
  429. while (root.readDir(p, NULL) > 0) {
  430. for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
  431. if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
  432. char cmd[4 + (FILENAME_LENGTH + 1) * MAX_DIR_DEPTH + 2];
  433. sprintf_P(cmd, PSTR("M23 %s"), autoname);
  434. enqueuecommand(cmd);
  435. enqueuecommands_P(PSTR("M24"));
  436. found = true;
  437. }
  438. }
  439. if (!found)
  440. autostart_index = -1;
  441. else
  442. autostart_index++;
  443. }
  444. void CardReader::closefile(bool store_location) {
  445. file.sync();
  446. file.close();
  447. saving = logging = false;
  448. if (store_location) {
  449. //future: store printer state, filename and position for continuing a stopped print
  450. // so one can unplug the printer and continue printing the next day.
  451. }
  452. }
  453. /**
  454. * Get the name of a file in the current directory by index
  455. */
  456. void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
  457. curDir = &workDir;
  458. lsAction = LS_GetFilename;
  459. nrFiles = nr;
  460. curDir->rewind();
  461. lsDive("", *curDir, match);
  462. }
  463. uint16_t CardReader::getnrfilenames() {
  464. curDir = &workDir;
  465. lsAction = LS_Count;
  466. nrFiles = 0;
  467. curDir->rewind();
  468. lsDive("", *curDir);
  469. //SERIAL_ECHOLN(nrFiles);
  470. return nrFiles;
  471. }
  472. void CardReader::chdir(const char * relpath) {
  473. SdFile newfile;
  474. SdFile *parent = &root;
  475. if (workDir.isOpen()) parent = &workDir;
  476. if (!newfile.open(*parent, relpath, O_READ)) {
  477. SERIAL_ECHO_START;
  478. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  479. SERIAL_ECHOLN(relpath);
  480. }
  481. else {
  482. if (workDirDepth < MAX_DIR_DEPTH) {
  483. ++workDirDepth;
  484. for (int d = workDirDepth; d--;) workDirParents[d + 1] = workDirParents[d];
  485. workDirParents[0] = *parent;
  486. }
  487. workDir = newfile;
  488. }
  489. }
  490. void CardReader::updir() {
  491. if (workDirDepth > 0) {
  492. --workDirDepth;
  493. workDir = workDirParents[0];
  494. for (uint16_t d = 0; d < workDirDepth; d++)
  495. workDirParents[d] = workDirParents[d+1];
  496. }
  497. }
  498. void CardReader::printingHasFinished() {
  499. st_synchronize();
  500. if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
  501. file.close();
  502. file_subcall_ctr--;
  503. openFile(filenames[file_subcall_ctr], true, true);
  504. setIndex(filespos[file_subcall_ctr]);
  505. startFileprint();
  506. }
  507. else {
  508. file.close();
  509. sdprinting = false;
  510. if (SD_FINISHED_STEPPERRELEASE) {
  511. //finishAndDisableSteppers();
  512. enqueuecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  513. }
  514. autotempShutdown();
  515. }
  516. }
  517. #endif //SDSUPPORT