SDInterface.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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() {
  54. if (this->supported)
  55. buffer_obj.open(&SD);
  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, 0);
  62. display_obj.tft.setTextSize(1);
  63. display_obj.tft.setTextColor(TFT_MAGENTA);
  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.println("Error, could not find update.bin");
  69. Serial.println("Error, update.bin is not a file");
  70. updateBin.close();
  71. return;
  72. }
  73. size_t updateSize = updateBin.size();
  74. if (updateSize > 0) {
  75. display_obj.tft.println("Starting SD Update...");
  76. Serial.println("Try to start update");
  77. this->performUpdate(updateBin, updateSize);
  78. }
  79. else {
  80. display_obj.tft.println("Error, update.bin is empty");
  81. Serial.println("Error, file is empty");
  82. }
  83. updateBin.close();
  84. // whe finished remove the binary from sd card to indicate end of the process
  85. display_obj.tft.println("Exiting update process...");
  86. Serial.println("Exiting update process...");
  87. //SD.remove("/update.bin");
  88. }
  89. else {
  90. display_obj.tft.println("Could not load update.bin from /");
  91. Serial.println("Could not load update.bin from sd root");
  92. }
  93. }
  94. void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
  95. if (Update.begin(updateSize)) {
  96. display_obj.tft.println("File size: " + String(updateSize));
  97. display_obj.tft.println("Writing file to partition...");
  98. size_t written = Update.writeStream(updateSource);
  99. if (written == updateSize) {
  100. display_obj.tft.println("Written: " + String(written) + " successfully");
  101. Serial.println("Written : " + String(written) + " successfully");
  102. }
  103. else {
  104. display_obj.tft.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  105. Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  106. }
  107. if (Update.end()) {
  108. Serial.println("OTA done!");
  109. if (Update.isFinished()) {
  110. display_obj.tft.println("Update complete");
  111. Serial.println("Update successfully completed. Rebooting.");
  112. }
  113. else {
  114. display_obj.tft.println("Update could not complete");
  115. Serial.println("Update not finished? Something went wrong!");
  116. }
  117. }
  118. else {
  119. display_obj.tft.println("Error Occurred. Error #: " + String(Update.getError()));
  120. Serial.println("Error Occurred. Error #: " + String(Update.getError()));
  121. }
  122. }
  123. else
  124. {
  125. display_obj.tft.println("Not enough space to begin OTA");
  126. Serial.println("Not enough space to begin OTA");
  127. }
  128. }
  129. void SDInterface::main() {
  130. if ((this->supported) && (this->do_save)) {
  131. //Serial.println("Saving packet...");
  132. buffer_obj.save(&SD);
  133. }
  134. }
  135. //void SDInterface::savePacket(uint8_t* buf, uint32_t len) {
  136. // if (this->supported)
  137. // buffer_obj.save(
  138. //}