Index: AD5941_interface09302025/ad5941_library_extension.cpp =================================================================== diff -u -r9347ab61cc26e8afa4f0ac6071b7f9d9dcccbda5 -r4c761dff9ac08e7007d8ef61c9992831c9963050 --- AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 9347ab61cc26e8afa4f0ac6071b7f9d9dcccbda5) +++ AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 4c761dff9ac08e7007d8ef61c9992831c9963050) @@ -132,7 +132,7 @@ static measurementSettingsStruct measurementSettingsPacket; // Stores all the conductivity measurement settings static measurementSettingsStruct tempSettings; // Stores received measurement settings to be updated. static Update_cfg_Status updateCfgStatus[MAX_CONDUCTIVITY_MST_PARAM_IDX]; -static Update_EEPROM_Status updateEepromStatus = UPDATE_EEPROM_STATUS_SUCCESS; +static Update_EEPROM_Status updateEepromStatus = UPDATE_EEPROM_STATUS_INVALID_CMD; // Global variable to store the last known good configuration SavedConfig lastConfig; @@ -3351,147 +3351,67 @@ } if (valueIndex > maxTotal) { - // activeSerial->print("Error: Too many values, maximum is "); - // activeSerial->print(maxTotal); - // activeSerial->print(" ("); - // activeSerial->print(DOUBLE_COUNT); - // activeSerial->print(" doubles + "); - // activeSerial->print(maxFloats); - // activeSerial->println(" floats)"); updateEepromStatus = UPDATE_EEPROM_STATUS_TOO_MANY_VALUES; return; } int doubleCount = min(valueIndex, DOUBLE_COUNT); int floatCount = max(0, valueIndex - DOUBLE_COUNT); - // Display the values and ask for confirmation - // activeSerial->print("Preparing to save "); - // activeSerial->print(valueIndex); - // activeSerial->print(" values to EEPROM ("); - // activeSerial->print(doubleCount); - // activeSerial->print(" doubles, "); - // activeSerial->print(floatCount); - // activeSerial->println(" floats):"); + unsigned int baseAddress = 0; + writeMixedValues(baseAddress, doubleValues, doubleCount, floatValues, floatCount); + // activeSerial->println("save attempted"); - // // Display double values - // for (int i = 0; i < doubleCount; i++) - // { - // activeSerial->print(i); - // activeSerial->print(" (double): "); - // activeSerial->println(doubleValues[i], 10); - // } + // Read back the values to confirm they were written correctly + double readDoubles[DOUBLE_COUNT]; + float readFloats[maxFloats]; + readMixedValues(baseAddress, readDoubles, doubleCount, readFloats, floatCount); - // // Display float values - // for (int i = 0; i < floatCount; i++) - // { - // activeSerial->print(i + DOUBLE_COUNT); - // activeSerial->print(" (float): "); - // activeSerial->println(floatValues[i], 6); // Floats typically have 6-7 significant digits - // } + // activeSerial->println("Verification - Values read back from EEPROM:"); + bool DoubleVerificationSuccess = true; + bool FloatVerificationSuccess = true; - // activeSerial->println("Confirm write to EEPROM? (y/n)"); - updateEepromStatus = UPDATE_EEPROM_STATUS_WAITING_FOR_CONFIRMATION; - activeSerial->write((uint8_t *)&updateEepromStatus, sizeof(updateEepromStatus)); - - // Wait for user confirmation - unsigned long startTime = millis(); - const unsigned long timeout = 30000; // 30 seconds timeout - while (true) + // Verify doubles + for (int i = 0; i < doubleCount; i++) { - // Check for timeout - if (millis() - startTime > timeout) + // activeSerial->print(i); + // activeSerial->print(" (double): "); + // activeSerial->println(readDoubles[i], 10); + if (abs(doubleValues[i] - readDoubles[i]) > 0.000000001) { - updateEepromStatus = UPDATE_EEPROM_STATUS_CONFIRMATION_TIMEOUT; - // activeSerial->println("Timed out waiting for confirmation. Operation aborted."); - return; + DoubleVerificationSuccess = false; + break; + // activeSerial->print("Warning: Double value "); + // activeSerial->print(i); + // activeSerial->println(" does not match exactly what was written"); } - // Check if data is available to read - if (activeSerial->available() > 0) - { - char response = activeSerial->read(); - // Clear any remaining characters in buffer (like newline) - while (activeSerial->available() > 0) - { - activeSerial->read(); - } - // Process response - if (response == 'y' || response == 'Y') - { - // User confirmed, write to EEPROM - unsigned int baseAddress = 0; - writeMixedValues(baseAddress, doubleValues, doubleCount, floatValues, floatCount); - // activeSerial->println("save attempted"); + } - // Read back the values to confirm they were written correctly - double readDoubles[DOUBLE_COUNT]; - float readFloats[maxFloats]; - readMixedValues(baseAddress, readDoubles, doubleCount, readFloats, floatCount); - - // activeSerial->println("Verification - Values read back from EEPROM:"); - bool verificationSuccess = true; - - // Verify doubles - for (int i = 0; i < doubleCount; i++) - { - // activeSerial->print(i); - // activeSerial->print(" (double): "); - // activeSerial->println(readDoubles[i], 10); - if (abs(doubleValues[i] - readDoubles[i]) > 0.000000001) - { - verificationSuccess = false; - // activeSerial->print("Warning: Double value "); - // activeSerial->print(i); - // activeSerial->println(" does not match exactly what was written"); - updateEepromStatus = UPDATE_EEPROM_STATUS_ERR_DOUBLE_VALUES; - } - } - - // Verify floats - for (int i = 0; i < floatCount; i++) - { - // activeSerial->print(i + DOUBLE_COUNT); - // activeSerial->print(" (float): "); - // activeSerial->println(readFloats[i], 6); - if (abs(floatValues[i] - readFloats[i]) > 0.000001) - { - verificationSuccess = false; - // activeSerial->print("Warning: Float value "); - // activeSerial->print(i + DOUBLE_COUNT); - // activeSerial->println(" does not match exactly what was written"); - updateEepromStatus = UPDATE_EEPROM_STATUS_ERR_FLOAT_VALUES; - } - } - - if (verificationSuccess) - { - updateEepromStatus = UPDATE_EEPROM_STATUS_VALUES_VERIFIED; - // activeSerial->println("Verification successful: All values match!"); - } - // else - // { - // // activeSerial->println("Verification Failed, confirm device attached and powered."); - // } - return; - } - else if (response == 'n' || response == 'N') - { - - updateEepromStatus = UPDATE_EEPROM_STATUS_OPERATION_ABORTED; - // User aborted - // activeSerial->println("Operation aborted by user."); - return; - } - else - { - updateEepromStatus = UPDATE_EEPROM_STATUS_INVALID_RESPONSE; - // Invalid response, ask again - // activeSerial->println("Please enter 'y' to confirm or 'n' to abort:"); - } + // Verify floats + for (int i = 0; i < floatCount; i++) + { + // activeSerial->print(i + DOUBLE_COUNT); + // activeSerial->print(" (float): "); + // activeSerial->println(readFloats[i], 6); + if (abs(floatValues[i] - readFloats[i]) > 0.000001) + { + FloatVerificationSuccess = false; + break; + // activeSerial->print("Warning: Float value "); + // activeSerial->print(i + DOUBLE_COUNT); + // activeSerial->println(" does not match exactly what was written"); } - // Small delay to prevent CPU hogging - delay(10); } + + if (DoubleVerificationSuccess && FloatVerificationSuccess) + { + updateEepromStatus = UPDATE_EEPROM_STATUS_SUCCESS; + // activeSerial->println("Verification successful: All values match!"); + } + else + { + updateEepromStatus = UPDATE_EEPROM_STATUS_VALUES_VERIFICATION_FAILED; + } } void getEEPROMdata(void)