SDInterface.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "SDInterface.h"
  2. bool SDInterface::initSD() {
  3. String display_string = "";
  4. if (!SD.begin(SD_CS)) {
  5. Serial.println("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("SD: MMC Mounted");
  14. else if(cardType == CARD_SD)
  15. Serial.println("SD: SDSC Mounted");
  16. else if(cardType == CARD_SDHC)
  17. Serial.println("SD: SDHC Mounted");
  18. else
  19. Serial.println("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. return true;
  45. }
  46. }
  47. void SDInterface::addPacket(uint8_t* buf, uint32_t len) {
  48. if ((this->supported) && (this->do_save)) {
  49. //Serial.println("Adding packet to buffer...");
  50. buffer_obj.addPacket(buf, len);
  51. }
  52. }
  53. void SDInterface::openCapture(String file_name) {
  54. if (this->supported)
  55. buffer_obj.open(&SD, file_name);
  56. }
  57. void SDInterface::runUpdate() {
  58. //display_obj.clearScreen();
  59. display_obj.tft.setTextWrap(false);
  60. display_obj.tft.setFreeFont(NULL);
  61. display_obj.tft.setCursor(0, 100);
  62. display_obj.tft.setTextSize(1);
  63. display_obj.tft.setTextColor(TFT_WHITE);
  64. display_obj.tft.println("Opening /update.bin...");
  65. File updateBin = SD.open("/update.bin");
  66. if (updateBin) {
  67. if(updateBin.isDirectory()){
  68. display_obj.tft.setTextColor(TFT_RED);
  69. display_obj.tft.println("Error, could not find update.bin");
  70. Serial.println("Error, update.bin is not a file");
  71. display_obj.tft.setTextColor(TFT_WHITE);
  72. updateBin.close();
  73. return;
  74. }
  75. size_t updateSize = updateBin.size();
  76. if (updateSize > 0) {
  77. display_obj.tft.println("Starting SD Update...");
  78. Serial.println("Try to start update");
  79. this->performUpdate(updateBin, updateSize);
  80. }
  81. else {
  82. display_obj.tft.setTextColor(TFT_RED);
  83. display_obj.tft.println("Error, update.bin is empty");
  84. Serial.println("Error, file is empty");
  85. display_obj.tft.setTextColor(TFT_WHITE);
  86. return;
  87. }
  88. updateBin.close();
  89. // whe finished remove the binary from sd card to indicate end of the process
  90. display_obj.tft.println("rebooting...");
  91. Serial.println("rebooting...");
  92. //SD.remove("/update.bin");
  93. delay(1000);
  94. ESP.restart();
  95. }
  96. else {
  97. display_obj.tft.setTextColor(TFT_RED);
  98. display_obj.tft.println("Could not load update.bin from /");
  99. Serial.println("Could not load update.bin from sd root");
  100. display_obj.tft.setTextColor(TFT_WHITE);
  101. }
  102. }
  103. void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
  104. if (Update.begin(updateSize)) {
  105. display_obj.tft.println("File size: " + String(updateSize));
  106. display_obj.tft.println("Writing file to partition...");
  107. size_t written = Update.writeStream(updateSource);
  108. if (written == updateSize) {
  109. display_obj.tft.println("Written: " + String(written) + " successfully");
  110. Serial.println("Written : " + String(written) + " successfully");
  111. }
  112. else {
  113. display_obj.tft.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  114. Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  115. }
  116. if (Update.end()) {
  117. Serial.println("OTA done!");
  118. if (Update.isFinished()) {
  119. display_obj.tft.println("Update complete");
  120. Serial.println("Update successfully completed. Rebooting.");
  121. }
  122. else {
  123. display_obj.tft.setTextColor(TFT_RED);
  124. display_obj.tft.println("Update could not complete");
  125. Serial.println("Update not finished? Something went wrong!");
  126. display_obj.tft.setTextColor(TFT_WHITE);
  127. }
  128. }
  129. else {
  130. display_obj.tft.println("Error Occurred. Error #: " + String(Update.getError()));
  131. Serial.println("Error Occurred. Error #: " + String(Update.getError()));
  132. }
  133. }
  134. else
  135. {
  136. display_obj.tft.println("Not enough space to begin OTA");
  137. Serial.println("Not enough space to begin OTA");
  138. }
  139. }
  140. void SDInterface::main() {
  141. if ((this->supported) && (this->do_save)) {
  142. //Serial.println("Saving packet...");
  143. buffer_obj.forceSave(&SD);
  144. }
  145. }
  146. //void SDInterface::savePacket(uint8_t* buf, uint32_t len) {
  147. // if (this->supported)
  148. // buffer_obj.save(
  149. //}