SDInterface.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "SDInterface.h"
  2. #include "lang_var.h"
  3. bool SDInterface::initSD() {
  4. String display_string = "";
  5. #ifdef KIT
  6. pinMode(SD_DET, INPUT);
  7. if (digitalRead(SD_DET) == LOW) {
  8. Serial.println(F("SD Card Detect Pin Detected"));
  9. }
  10. else {
  11. Serial.println(F("SD Card Detect Pin Not Detected"));
  12. this->supported = false;
  13. return false;
  14. }
  15. #endif
  16. if (!SD.begin(SD_CS)) {
  17. Serial.println(F("Failed to mount SD Card"));
  18. this->supported = false;
  19. return false;
  20. }
  21. else {
  22. this->supported = true;
  23. this->cardType = SD.cardType();
  24. if (cardType == CARD_MMC)
  25. Serial.println(F("SD: MMC Mounted"));
  26. else if(cardType == CARD_SD)
  27. Serial.println(F("SD: SDSC Mounted"));
  28. else if(cardType == CARD_SDHC)
  29. Serial.println(F("SD: SDHC Mounted"));
  30. else
  31. Serial.println(F("SD: UNKNOWN Card Mounted"));
  32. this->cardSizeMB = SD.cardSize() / (1024 * 1024);
  33. Serial.printf("SD Card Size: %lluMB\n", this->cardSizeMB);
  34. if (this->supported) {
  35. const int NUM_DIGITS = log10(this->cardSizeMB) + 1;
  36. char sz[NUM_DIGITS + 1];
  37. sz[NUM_DIGITS] = 0;
  38. for ( size_t i = NUM_DIGITS; i--; this->cardSizeMB /= 10)
  39. {
  40. sz[i] = '0' + (this->cardSizeMB % 10);
  41. display_string.concat((String)sz[i]);
  42. }
  43. this->card_sz = sz;
  44. }
  45. buffer_obj = Buffer();
  46. if (!SD.exists("/SCRIPTS")) {
  47. Serial.println("/SCRIPTS does not exist. Creating...");
  48. SD.mkdir("/SCRIPTS");
  49. Serial.println("/SCRIPTS created");
  50. }
  51. return true;
  52. }
  53. }
  54. void SDInterface::addPacket(uint8_t* buf, uint32_t len) {
  55. if ((this->supported) && (this->do_save)) {
  56. buffer_obj.addPacket(buf, len);
  57. }
  58. }
  59. void SDInterface::openCapture(String file_name) {
  60. if (this->supported)
  61. buffer_obj.open(&SD, file_name);
  62. }
  63. void SDInterface::runUpdate() {
  64. #ifdef HAS_SCREEN
  65. display_obj.tft.setTextWrap(false);
  66. display_obj.tft.setFreeFont(NULL);
  67. display_obj.tft.setCursor(0, 100);
  68. display_obj.tft.setTextSize(1);
  69. display_obj.tft.setTextColor(TFT_WHITE);
  70. display_obj.tft.println(F(text15));
  71. #endif
  72. File updateBin = SD.open("/update.bin");
  73. if (updateBin) {
  74. if(updateBin.isDirectory()){
  75. #ifdef HAS_SCREEN
  76. display_obj.tft.setTextColor(TFT_RED);
  77. display_obj.tft.println(F(text_table2[0]));
  78. #endif
  79. Serial.println(F("Error, update.bin is not a file"));
  80. #ifdef HAS_SCREEN
  81. display_obj.tft.setTextColor(TFT_WHITE);
  82. #endif
  83. updateBin.close();
  84. return;
  85. }
  86. size_t updateSize = updateBin.size();
  87. if (updateSize > 0) {
  88. #ifdef HAS_SCREEN
  89. display_obj.tft.println(F(text_table2[1]));
  90. #endif
  91. Serial.println(F("Try to start update"));
  92. this->performUpdate(updateBin, updateSize);
  93. }
  94. else {
  95. #ifdef HAS_SCREEN
  96. display_obj.tft.setTextColor(TFT_RED);
  97. display_obj.tft.println(F(text_table2[2]));
  98. #endif
  99. Serial.println(F("Error, file is empty"));
  100. #ifdef HAS_SCREEN
  101. display_obj.tft.setTextColor(TFT_WHITE);
  102. #endif
  103. return;
  104. }
  105. updateBin.close();
  106. // whe finished remove the binary from sd card to indicate end of the process
  107. #ifdef HAS_SCREEN
  108. display_obj.tft.println(F(text_table2[3]));
  109. #endif
  110. Serial.println(F("rebooting..."));
  111. //SD.remove("/update.bin");
  112. delay(1000);
  113. ESP.restart();
  114. }
  115. else {
  116. #ifdef HAS_SCREEN
  117. display_obj.tft.setTextColor(TFT_RED);
  118. display_obj.tft.println(F(text_table2[4]));
  119. #endif
  120. Serial.println(F("Could not load update.bin from sd root"));
  121. #ifdef HAS_SCREEN
  122. display_obj.tft.setTextColor(TFT_WHITE);
  123. #endif
  124. }
  125. }
  126. void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
  127. if (Update.begin(updateSize)) {
  128. #ifdef HAS_SCREEN
  129. display_obj.tft.println(text_table2[5] + String(updateSize));
  130. display_obj.tft.println(F(text_table2[6]));
  131. #endif
  132. size_t written = Update.writeStream(updateSource);
  133. if (written == updateSize) {
  134. #ifdef HAS_SCREEN
  135. display_obj.tft.println(text_table2[7] + String(written) + text_table2[10]);
  136. #endif
  137. Serial.println("Written : " + String(written) + " successfully");
  138. }
  139. else {
  140. #ifdef HAS_SCREEN
  141. display_obj.tft.println(text_table2[8] + String(written) + "/" + String(updateSize) + text_table2[9]);
  142. #endif
  143. Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  144. }
  145. if (Update.end()) {
  146. Serial.println("OTA done!");
  147. if (Update.isFinished()) {
  148. #ifdef HAS_SCREEN
  149. display_obj.tft.println(F(text_table2[11]));
  150. #endif
  151. Serial.println(F("Update successfully completed. Rebooting."));
  152. }
  153. else {
  154. #ifdef HAS_SCREEN
  155. display_obj.tft.setTextColor(TFT_RED);
  156. display_obj.tft.println(text_table2[12]);
  157. #endif
  158. Serial.println("Update not finished? Something went wrong!");
  159. #ifdef HAS_SCREEN
  160. display_obj.tft.setTextColor(TFT_WHITE);
  161. #endif
  162. }
  163. }
  164. else {
  165. #ifdef HAS_SCREEN
  166. display_obj.tft.println(text_table2[13] + String(Update.getError()));
  167. #endif
  168. Serial.println("Error Occurred. Error #: " + String(Update.getError()));
  169. }
  170. }
  171. else
  172. {
  173. #ifdef HAS_SCREEN
  174. display_obj.tft.println(text_table2[14]);
  175. #endif
  176. Serial.println("Not enough space to begin OTA");
  177. }
  178. }
  179. bool SDInterface::checkDetectPin() {
  180. #ifdef KIT
  181. if (digitalRead(SD_DET) == LOW)
  182. return true;
  183. else
  184. return false;
  185. #endif
  186. return false;
  187. }
  188. void SDInterface::main() {
  189. if ((this->supported) && (this->do_save)) {
  190. //Serial.println("Saving packet...");
  191. buffer_obj.forceSave(&SD);
  192. }
  193. else if (!this->supported) {
  194. if (checkDetectPin()) {
  195. delay(100);
  196. this->initSD();
  197. }
  198. }
  199. }