SDInterface.cpp 5.9 KB

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