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 13KB

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