|
| 1 | +//Update firmware if bin files found |
| 2 | +void menuFirmware() |
| 3 | +{ |
| 4 | + if (online.microSD == false) |
| 5 | + { |
| 6 | + Serial.println(F("No SD card detected")); |
| 7 | + } |
| 8 | + |
| 9 | + if (binCount == 0) |
| 10 | + { |
| 11 | + Serial.println("No valid binary files found."); |
| 12 | + delay(2000); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + while (1) |
| 17 | + { |
| 18 | + Serial.println(); |
| 19 | + Serial.println(F("Menu: Update Firmware Menu")); |
| 20 | + |
| 21 | + for (int x = 0 ; x < binCount ; x++) |
| 22 | + { |
| 23 | + Serial.printf("%d) Load %s\n", x + 1, binFileNames[x]); |
| 24 | + } |
| 25 | + |
| 26 | + Serial.println(F("x) Exit")); |
| 27 | + |
| 28 | + int incoming = getNumber(menuTimeout); //Timeout after x seconds |
| 29 | + |
| 30 | + if (incoming > 0 && incoming < (binCount + 1)) |
| 31 | + { |
| 32 | + //Adjust incoming to match array |
| 33 | + incoming--; |
| 34 | + updateFromSD(binFileNames[incoming]); |
| 35 | + } |
| 36 | + else if (incoming == STATUS_PRESSED_X) |
| 37 | + break; |
| 38 | + else if (incoming == STATUS_GETNUMBER_TIMEOUT) |
| 39 | + break; |
| 40 | + else |
| 41 | + Serial.printf("Bad value: %d\n", incoming); |
| 42 | + } |
| 43 | + |
| 44 | + while (Serial.available()) Serial.read(); //Empty buffer of any newline chars |
| 45 | +} |
| 46 | + |
| 47 | +//Looks for matching binary files in root |
| 48 | +//Loads a global called binCount |
| 49 | +void scanForFirmware() |
| 50 | +{ |
| 51 | + if (online.microSD == true) |
| 52 | + { |
| 53 | + //Count available binaries |
| 54 | + SdFile tempFile; |
| 55 | + SdFile dir; |
| 56 | + const char* BIN_EXT = "bin"; |
| 57 | + const char* BIN_HEADER = "RTK_Surveyor_Firmware"; |
| 58 | + char fname[50]; //Handle long file names |
| 59 | + |
| 60 | + dir.open("/"); //Open root |
| 61 | + |
| 62 | + while (tempFile.openNext(&dir, O_READ)) |
| 63 | + { |
| 64 | + if (tempFile.isFile()) |
| 65 | + { |
| 66 | + tempFile.getName(fname, sizeof(fname)); |
| 67 | + |
| 68 | + if (strcmp(forceFirmwareFileName, fname) == 0) |
| 69 | + updateFromSD((char *)forceFirmwareFileName); |
| 70 | + |
| 71 | + //Check for 'sfe_rtk' and 'bin' extension |
| 72 | + if (strcmp(BIN_EXT, &fname[strlen(fname) - strlen(BIN_EXT)]) == 0) |
| 73 | + { |
| 74 | + if (strstr(fname, BIN_HEADER) != NULL) |
| 75 | + { |
| 76 | + strcpy(binFileNames[binCount++], fname); //Add this to the array |
| 77 | + } |
| 78 | + else |
| 79 | + Serial.printf("Unknown: %s\n", fname); |
| 80 | + } |
| 81 | + } |
| 82 | + tempFile.close(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +//Look for firmware file on SD card and update as needed |
| 88 | +void updateFromSD(char *firmwareFileName) |
| 89 | +{ |
| 90 | + Serial.printf("Loading %s\n", firmwareFileName); |
| 91 | + if (sd.exists(firmwareFileName)) |
| 92 | + { |
| 93 | + SdFile firmwareFile; |
| 94 | + firmwareFile.open(firmwareFileName, O_READ); |
| 95 | + |
| 96 | + size_t updateSize = firmwareFile.fileSize(); |
| 97 | + if (updateSize == 0) |
| 98 | + { |
| 99 | + Serial.println(F("Error: Binary is empty")); |
| 100 | + firmwareFile.close(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (Update.begin(updateSize) == false) |
| 105 | + { |
| 106 | + Serial.println(F("Update begin failed. Not enough partition space available.")); |
| 107 | + firmwareFile.close(); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + Serial.print(F("Moving file to OTA section")); |
| 112 | + |
| 113 | + const int pageSize = 512; |
| 114 | + byte dataArray[pageSize]; |
| 115 | + int bytesWritten = 0; |
| 116 | + |
| 117 | + //Bulk write from the SD file to the EEPROM |
| 118 | + while (firmwareFile.available()) |
| 119 | + { |
| 120 | + int bytesToWrite = pageSize; //Max number of bytes to read |
| 121 | + if (firmwareFile.available() < bytesToWrite) bytesToWrite = firmwareFile.available(); //Trim this read size as needed |
| 122 | + |
| 123 | + firmwareFile.read(dataArray, bytesToWrite); //Read the next set of bytes from file into our temp array |
| 124 | + |
| 125 | + if (Update.write(dataArray, bytesToWrite) != bytesToWrite) |
| 126 | + Serial.println(F("Write failed")); |
| 127 | + else |
| 128 | + bytesWritten += bytesToWrite; |
| 129 | + |
| 130 | + if (bytesWritten % (512 * 32) == 0) Serial.print("."); //Indicate progress |
| 131 | + } |
| 132 | + Serial.println(F("\nFile move complete")); |
| 133 | + |
| 134 | + if (Update.end()) |
| 135 | + { |
| 136 | + if (Update.isFinished()) |
| 137 | + { |
| 138 | + Serial.println(F("Firmware updated successfully. Rebooting. Good bye!")); |
| 139 | + delay(1000); |
| 140 | + ESP.restart(); |
| 141 | + } |
| 142 | + else |
| 143 | + Serial.println(F("Update not finished? Something went wrong!")); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + Serial.print(F("Error Occurred. Error #: ")); |
| 148 | + Serial.println(String(Update.getError())); |
| 149 | + } |
| 150 | + |
| 151 | + firmwareFile.close(); |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + Serial.println(F("No firmware file found")); |
| 156 | + } |
| 157 | +} |
0 commit comments