size_t countHexLines(const char* progmemString) {
size_t count = 0;
for (size_t i = 0; pgm_read_byte(&progmemString[i]) != '\0'; i++) {
if (pgm_read_byte(&progmemString[i]) == ':') {
count++;
}
}
return count;
}
const char* getNextLine(const char* largeArray, size_t OTA_arraySize) {
static const char* currentPosition = nullptr; // Static variable to keep track of the current position
// Initialize currentPosition on the first call
if (currentPosition == nullptr) {
currentPosition = largeArray;
}
// Check if currentPosition is still within the array bounds
if (currentPosition >= largeArray + OTA_arraySize) {
// End of array reached
currentPosition = nullptr; // Reset for future calls
return nullptr;
}
// Find the end of the current line
const char* endOfLine = currentPosition;
while (endOfLine < largeArray + OTA_arraySize && *endOfLine != '\n') {
endOfLine++;
}
// Calculate the length of the line
size_t lineLength = endOfLine - currentPosition;
// Create a buffer for the line (including null terminator)
static char lineBuffer[256]; // Adjust buffer size as needed
memset(lineBuffer, 0, sizeof(lineBuffer));
// Copy the line into the buffer
strncpy(lineBuffer, currentPosition, lineLength);
// Move currentPosition to the beginning of the next line
currentPosition = endOfLine + 1;
// Return a pointer to the line buffer
return lineBuffer;
}
bool stop = false;
void test_performUpdate() {
MTP.loop();
if (!stop) {
const char* currentLine;
if (OTA_TotalLines == 0) {
if (firmware_buffer_init(&OTA_buffer_addr, &OTA_buffer_size) == 0) {
Serial.printf("unable to create buffer\n");
for (;;) {}
}
Serial.printf("created buffer = %1luK %s (%08lX - %08lX)\n", OTA_buffer_size/1024, IN_FLASH(OTA_buffer_addr) ? "FLASH" : "RAM", OTA_buffer_addr, OTA_buffer_addr + OTA_buffer_size);
OTA_arraySize = sizeof(hexString) - 1;
OTA_TotalLines = countHexLines(hexString);
}
if (OTA_Counter <= OTA_TotalLines) {
currentLine = getNextLine(hexString, OTA_arraySize);
File dataFile = myfs.open("test.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(currentLine);
Serial.println(currentLine);
dataFile.close();
}
//custom_update_firmware(currentLine, serial, OTA_buffer_addr, OTA_buffer_size);
OTA_Counter++;
} else {
stop = true;
}
}