SDInterface.cpp 5.5 KB

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