| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include "SDInterface.h"
- bool SDInterface::startSD() {
- Serial.println("Starting SD card...");
- if (SD.begin(SD_CS)) {
- buffer_obj = Buffer();
- this->sd_running = true;
- return true;
- }
- else {
- //delete &buffer_obj;
- //buffer_obj = NULL;
- this->sd_running = false;
- return false;
- }
- }
- bool SDInterface::stopSD() {
- Serial.println("Stopping SD card");
- //delete &buffer_obj;
- //buffer_obj = NULL;
- //this->sd_running = false;
- //sdcard_uninit();
- //SD.end();
- Serial.println(ESP.getFreeHeap());
- }
- bool SDInterface::initSD() {
- String display_string = "";
-
- if (!this->startSD()) {
- Serial.println("Failed to mount SD Card");
- this->supported = false;
- this->stopSD();
- return false;
- }
- // else {
- // SD.end(); // Release memory used for SD
- // return false;
- // }
- else {
- this->supported = true;
- this->cardType = SD.cardType();
- if (cardType == CARD_MMC)
- Serial.println("SD: MMC Mounted");
- else if(cardType == CARD_SD)
- Serial.println("SD: SDSC Mounted");
- else if(cardType == CARD_SDHC)
- Serial.println("SD: SDHC Mounted");
- else
- Serial.println("SD: UNKNOWN Card Mounted");
- //this->cardSizeBT = SD.cardSize();
- //this->cardSizeKB = SD.cardSize() / 1024;
- this->cardSizeMB = SD.cardSize() / (1024 * 1024);
- //this->cardSizeGB = SD.cardSize() / (1024 * 1024 * 1024);
-
- //Serial.printf("SD Card Size: %llu Bytes\n", this->cardSizeBT);
- //Serial.printf("SD Card Size: %lluKB\n", this->cardSizeKB);
- Serial.printf("SD Card Size: %lluMB\n", this->cardSizeMB);
- //Serial.printf("SD Card Size: %lluGB\n", this->cardSizeGB);
- if (this->supported) {
- //display_obj.tft.println((byte)(sd_obj.cardSizeMB % 10));
- const int NUM_DIGITS = log10(this->cardSizeMB) + 1;
-
- char sz[NUM_DIGITS + 1];
-
- sz[NUM_DIGITS] = 0;
- for ( size_t i = NUM_DIGITS; i--; this->cardSizeMB /= 10)
- {
- sz[i] = '0' + (this->cardSizeMB % 10);
- display_string.concat((String)sz[i]);
- }
-
- //this->card_sz = display_string;
- this->card_sz = sz;
- }
- buffer_obj = Buffer();
- //if (this->supported)
- // buffer_obj.open(&SD);
- // Clean up the SD
- this->stopSD();
-
- return true;
- }
- }
- void SDInterface::addPacket(uint8_t* buf, uint32_t len) {
- if ((this->supported) && (this->do_save)) {
- //Serial.println("Adding packet to buffer...");
- buffer_obj.addPacket(buf, len);
- }
- }
- void SDInterface::openCapture(String file_name) {
- if (this->supported)
- buffer_obj.open(&SD, file_name);
- }
- void SDInterface::runUpdate() {
- this->startSD();
- //display_obj.clearScreen();
- display_obj.tft.setTextWrap(false);
- display_obj.tft.setFreeFont(NULL);
- display_obj.tft.setCursor(0, 100);
- display_obj.tft.setTextSize(1);
- display_obj.tft.setTextColor(TFT_WHITE);
- display_obj.tft.println("Opening /update.bin...");
- File updateBin = SD.open("/update.bin");
- if (updateBin) {
- if(updateBin.isDirectory()){
- display_obj.tft.setTextColor(TFT_RED);
- display_obj.tft.println("Error, could not find update.bin");
- Serial.println("Error, update.bin is not a file");
- display_obj.tft.setTextColor(TFT_WHITE);
- updateBin.close();
- return;
- }
- size_t updateSize = updateBin.size();
- if (updateSize > 0) {
- display_obj.tft.println("Starting SD Update...");
- Serial.println("Try to start update");
- this->performUpdate(updateBin, updateSize);
- }
- else {
- display_obj.tft.setTextColor(TFT_RED);
- display_obj.tft.println("Error, update.bin is empty");
- Serial.println("Error, file is empty");
- display_obj.tft.setTextColor(TFT_WHITE);
- return;
- }
- updateBin.close();
- this->stopSD();
-
- // whe finished remove the binary from sd card to indicate end of the process
- display_obj.tft.println("rebooting...");
- Serial.println("rebooting...");
- //SD.remove("/update.bin");
- delay(1000);
- ESP.restart();
- }
- else {
- display_obj.tft.setTextColor(TFT_RED);
- display_obj.tft.println("Could not load update.bin from /");
- Serial.println("Could not load update.bin from sd root");
- display_obj.tft.setTextColor(TFT_WHITE);
- }
- }
- void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
- if (Update.begin(updateSize)) {
- display_obj.tft.println("File size: " + String(updateSize));
- display_obj.tft.println("Writing file to partition...");
- size_t written = Update.writeStream(updateSource);
- if (written == updateSize) {
- display_obj.tft.println("Written: " + String(written) + " successfully");
- Serial.println("Written : " + String(written) + " successfully");
- }
- else {
- display_obj.tft.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
- Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
- }
- if (Update.end()) {
- Serial.println("OTA done!");
- if (Update.isFinished()) {
- display_obj.tft.println("Update complete");
- Serial.println("Update successfully completed. Rebooting.");
- }
- else {
- display_obj.tft.setTextColor(TFT_RED);
- display_obj.tft.println("Update could not complete");
- Serial.println("Update not finished? Something went wrong!");
- display_obj.tft.setTextColor(TFT_WHITE);
- }
- }
- else {
- display_obj.tft.println("Error Occurred. Error #: " + String(Update.getError()));
- Serial.println("Error Occurred. Error #: " + String(Update.getError()));
- }
- }
- else
- {
- display_obj.tft.println("Not enough space to begin OTA");
- Serial.println("Not enough space to begin OTA");
- }
- }
- void SDInterface::main() {
- if ((this->supported) && (this->do_save) && (this->sd_running)) {
- //Serial.println("Saving packet...");
- buffer_obj.forceSave(&SD);
- }
- }
- //void SDInterface::savePacket(uint8_t* buf, uint32_t len) {
- // if (this->supported)
- // buffer_obj.save(
- //}
|