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.pde 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifdef SDSUPPORT
  2. #include "cardreader.h"
  3. CardReader::CardReader()
  4. {
  5. filesize = 0;
  6. sdpos = 0;
  7. sdprinting = false;
  8. cardOK = false;
  9. saving = false;
  10. autostart_atmillis=0;
  11. autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  12. //power to SD reader
  13. #if SDPOWER > -1
  14. SET_OUTPUT(SDPOWER);
  15. WRITE(SDPOWER,HIGH);
  16. #endif //SDPOWER
  17. autostart_atmillis=millis()+5000;
  18. }
  19. void CardReader::initsd()
  20. {
  21. cardOK = false;
  22. #if SDSS >- 1
  23. if(root.isOpen())
  24. root.close();
  25. if (!card.init(SPI_FULL_SPEED,SDSS))
  26. {
  27. //if (!card.init(SPI_HALF_SPEED,SDSS))
  28. SERIAL_ECHOLN("SD init fail");
  29. }
  30. else if (!volume.init(&card))
  31. {
  32. SERIAL_ERRORLN("volume.init failed");
  33. }
  34. else if (!root.openRoot(&volume))
  35. {
  36. SERIAL_ERRORLN("openRoot failed");
  37. }
  38. else
  39. {
  40. cardOK = true;
  41. SERIAL_ECHOLN("SD card ok");
  42. }
  43. #endif //SDSS
  44. }
  45. void CardReader::release()
  46. {
  47. sdprinting = false;
  48. cardOK = false;
  49. }
  50. void CardReader::startFileprint()
  51. {
  52. if(cardOK)
  53. {
  54. sdprinting = true;
  55. }
  56. }
  57. void CardReader::pauseSDPrint()
  58. {
  59. if(sdprinting)
  60. {
  61. sdprinting = false;
  62. }
  63. }
  64. void CardReader::selectFile(char* name)
  65. {
  66. if(cardOK){
  67. sdprinting = false;
  68. file.close();
  69. if (file.open(&root, name, O_READ)) {
  70. Serial.print("File opened:");
  71. Serial.print(name);
  72. Serial.print(" Size:");
  73. filesize = file.fileSize();
  74. Serial.println(filesize);
  75. sdpos = 0;
  76. Serial.println("File selected");
  77. }
  78. else{
  79. Serial.println("file.open failed");
  80. }
  81. }
  82. }
  83. void CardReader::startFilewrite(char *name)
  84. {
  85. if(cardOK)
  86. {
  87. file.close();
  88. sdprinting = false;
  89. if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  90. {
  91. Serial.print("open failed, File: ");
  92. Serial.print(name);
  93. Serial.print(".");
  94. }
  95. else{
  96. saving = true;
  97. Serial.print("Writing to file: ");
  98. Serial.println(name);
  99. }
  100. }
  101. }
  102. void CardReader::getStatus()
  103. {
  104. if(cardOK){
  105. Serial.print("SD printing byte ");
  106. Serial.print(sdpos);
  107. Serial.print("/");
  108. Serial.println(filesize);
  109. }
  110. else{
  111. Serial.println("Not SD printing");
  112. }
  113. }
  114. void CardReader::write_command(char *buf)
  115. {
  116. char* begin = buf;
  117. char* npos = 0;
  118. char* end = buf + strlen(buf) - 1;
  119. file.writeError = false;
  120. if((npos = strchr(buf, 'N')) != NULL)
  121. {
  122. begin = strchr(npos, ' ') + 1;
  123. end = strchr(npos, '*') - 1;
  124. }
  125. end[1] = '\r';
  126. end[2] = '\n';
  127. end[3] = '\0';
  128. file.write(begin);
  129. if (file.writeError)
  130. {
  131. SERIAL_ERRORLN("error writing to file");
  132. }
  133. }
  134. void CardReader::checkautostart(bool force)
  135. {
  136. if(!force)
  137. {
  138. if(!autostart_stilltocheck)
  139. return;
  140. if(autostart_atmillis<millis())
  141. return;
  142. }
  143. autostart_stilltocheck=false;
  144. if(!cardOK)
  145. {
  146. initsd();
  147. if(!cardOK) //fail
  148. return;
  149. }
  150. static int lastnr=0;
  151. char autoname[30];
  152. sprintf(autoname,"auto%i.g",lastnr);
  153. for(int i=0;i<(int)strlen(autoname);i++)
  154. autoname[i]=tolower(autoname[i]);
  155. dir_t p;
  156. root.rewind();
  157. bool found=false;
  158. while (root.readDir(p) > 0)
  159. {
  160. for(int i=0;i<(int)strlen((char*)p.name);i++)
  161. p.name[i]=tolower(p.name[i]);
  162. //Serial.print((char*)p.name);
  163. //Serial.print(" ");
  164. //Serial.println(autoname);
  165. if(p.name[9]!='~') //skip safety copies
  166. if(strncmp((char*)p.name,autoname,5)==0)
  167. {
  168. char cmd[30];
  169. sprintf(cmd,"M23 %s",autoname);
  170. enquecommand(cmd);
  171. enquecommand("M24");
  172. found=true;
  173. }
  174. }
  175. if(!found)
  176. lastnr=-1;
  177. else
  178. lastnr++;
  179. }
  180. void CardReader::closefile()
  181. {
  182. file.sync();
  183. file.close();
  184. saving = false;
  185. }
  186. void CardReader::getfilename(const uint8_t nr)
  187. {
  188. dir_t p;
  189. root.rewind();
  190. uint8_t cnt=0;
  191. filename[0]='\0';
  192. while (root.readDir(p) > 0)
  193. {
  194. if (p.name[0] == DIR_NAME_FREE) break;
  195. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  196. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  197. if(p.name[8]!='G') continue;
  198. if(p.name[9]=='~') continue;
  199. if(cnt++!=nr) continue;
  200. //Serial.println((char*)p.name);
  201. uint8_t writepos=0;
  202. for (uint8_t i = 0; i < 11; i++)
  203. {
  204. if (p.name[i] == ' ') continue;
  205. if (i == 8) {
  206. filename[writepos++]='.';
  207. }
  208. filename[writepos++]=p.name[i];
  209. }
  210. filename[writepos++]=0;
  211. }
  212. }
  213. uint8_t CardReader::getnrfilenames()
  214. {
  215. dir_t p;
  216. root.rewind();
  217. uint8_t cnt=0;
  218. while (root.readDir(p) > 0)
  219. {
  220. if (p.name[0] == DIR_NAME_FREE) break;
  221. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  222. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  223. if(p.name[8]!='G') continue;
  224. if(p.name[9]=='~') continue;
  225. cnt++;
  226. }
  227. return cnt;
  228. }
  229. #endif //SDSUPPORT