SDInterface.cpp 5.9 KB

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