SDInterface.cpp 5.9 KB

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