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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifdef SDSUPPORT
  2. #include "cardreader.h"
  3. CardReader::CardReader()
  4. {
  5. filesize = 0;
  6. sdpos = 0;
  7. sdmode = false;
  8. sdactive = false;
  9. savetosd = 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. sdactive = 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. sdactive = true;
  41. SERIAL_ECHOLN("SD card ok");
  42. }
  43. #endif //SDSS
  44. }
  45. void CardReader::write_command(char *buf)
  46. {
  47. char* begin = buf;
  48. char* npos = 0;
  49. char* end = buf + strlen(buf) - 1;
  50. file.writeError = false;
  51. if((npos = strchr(buf, 'N')) != NULL)
  52. {
  53. begin = strchr(npos, ' ') + 1;
  54. end = strchr(npos, '*') - 1;
  55. }
  56. end[1] = '\r';
  57. end[2] = '\n';
  58. end[3] = '\0';
  59. file.write(begin);
  60. if (file.writeError)
  61. {
  62. SERIAL_ERRORLN("error writing to file");
  63. }
  64. }
  65. void CardReader::checkautostart(bool force)
  66. {
  67. if(!force)
  68. {
  69. if(!autostart_stilltocheck)
  70. return;
  71. if(autostart_atmillis<millis())
  72. return;
  73. }
  74. autostart_stilltocheck=false;
  75. if(!sdactive)
  76. {
  77. initsd();
  78. if(!sdactive) //fail
  79. return;
  80. }
  81. static int lastnr=0;
  82. char autoname[30];
  83. sprintf(autoname,"auto%i.g",lastnr);
  84. for(int i=0;i<(int)strlen(autoname);i++)
  85. autoname[i]=tolower(autoname[i]);
  86. dir_t p;
  87. root.rewind();
  88. bool found=false;
  89. while (root.readDir(p) > 0)
  90. {
  91. for(int i=0;i<(int)strlen((char*)p.name);i++)
  92. p.name[i]=tolower(p.name[i]);
  93. //Serial.print((char*)p.name);
  94. //Serial.print(" ");
  95. //Serial.println(autoname);
  96. if(p.name[9]!='~') //skip safety copies
  97. if(strncmp((char*)p.name,autoname,5)==0)
  98. {
  99. char cmd[30];
  100. sprintf(cmd,"M23 %s",autoname);
  101. enquecommand(cmd);
  102. enquecommand("M24");
  103. found=true;
  104. }
  105. }
  106. if(!found)
  107. lastnr=-1;
  108. else
  109. lastnr++;
  110. }
  111. void CardReader::closefile()
  112. {
  113. file.sync();
  114. file.close();
  115. savetosd = false;
  116. }
  117. void CardReader::getfilename(const uint8_t nr)
  118. {
  119. dir_t p;
  120. root.rewind();
  121. uint8_t cnt=0;
  122. filename[0]='\0';
  123. while (root.readDir(p) > 0)
  124. {
  125. if (p.name[0] == DIR_NAME_FREE) break;
  126. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  127. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  128. if(p.name[8]!='G') continue;
  129. if(p.name[9]=='~') continue;
  130. if(cnt++!=nr) continue;
  131. //Serial.println((char*)p.name);
  132. uint8_t writepos=0;
  133. for (uint8_t i = 0; i < 11; i++)
  134. {
  135. if (p.name[i] == ' ') continue;
  136. if (i == 8) {
  137. filename[writepos++]='.';
  138. }
  139. filename[writepos++]=p.name[i];
  140. }
  141. filename[writepos++]=0;
  142. }
  143. }
  144. uint8_t CardReader::getnrfilenames()
  145. {
  146. dir_t p;
  147. root.rewind();
  148. uint8_t cnt=0;
  149. while (root.readDir(p) > 0)
  150. {
  151. if (p.name[0] == DIR_NAME_FREE) break;
  152. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  153. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  154. if(p.name[8]!='G') continue;
  155. if(p.name[9]=='~') continue;
  156. cnt++;
  157. }
  158. return cnt;
  159. }
  160. #endif //SDSUPPORT