SDInterface.cpp 6.0 KB

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