SD_MMCInterface.cpp 5.0 KB

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