SDInterface.cpp 6.1 KB

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