My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cardreader.cpp 14KB

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