Index: AD5941_interface09302025/ad5941_library_extension.cpp =================================================================== diff -u -r4ed225961d7e2ebf9a71c19b1a3bb5788c349c12 -r9347ab61cc26e8afa4f0ac6071b7f9d9dcccbda5 --- AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 4ed225961d7e2ebf9a71c19b1a3bb5788c349c12) +++ AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 9347ab61cc26e8afa4f0ac6071b7f9d9dcccbda5) @@ -58,7 +58,17 @@ "Serial7 (pins 28,29)" }; +static const char* cfgSettingsParam[] = { + "sinfreq", + "dacpp", + "bias", + "rtia", + "pga", + "dftnum", + "avgnum" + }; + uint32_t AppBuff[APPBUFF_SIZE]; // Arrays to store up to 5 measurements @@ -120,8 +130,8 @@ static sensorDataPacketStruct singleSensorPacket; // Stores Impedance and RTD measurements for only singularly selected sensor. static eepromDataPacketStruct eepromDataPacket; // Stores all the eeprom data static measurementSettingsStruct measurementSettingsPacket; // Stores all the conductivity measurement settings - -static Update_cfg_Status updateCfgStataus = UPDATE_CFG_STATUS_SUCCESS; +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; // Global variable to store the last known good configuration @@ -1333,16 +1343,14 @@ // activeSerial->println(command); // activeSerial->print("*"); propagateSettingsChanges = 1; - - activeSerial->println((int)updateCfgStataus); return; } if (command.startsWith("mst,")) { - updateMeasurementSettings(command); + handleMstCommand(command); propagateSettingsChanges = 1; - activeSerial->write((uint8_t *)&updateCfgStataus, sizeof(updateCfgStataus)); + activeSerial->write((uint8_t *)&updateCfgStatus, sizeof(updateCfgStatus)); return; } @@ -3513,9 +3521,85 @@ activeSerial->write((uint8_t *)&eepromDataPacket, sizeof(eepromDataPacket)); } -void updateMeasurementSettings(String cmd) +void handleMstCommand(String cmd) { - updateCfgStataus = UPDATE_CFG_STATUS_SUCCESS; + // Check if the first 4 characters are "mst," + if (cmd.startsWith("mst,")) + { + // Remove "mst," prefix + String dataPart = cmd.substring(4); + + // Split by comma + int idx = 0; + int start = 0; + int end = 0; + + while (idx < MAX_CONDUCTIVITY_MST_PARAM_IDX && start < dataPart.length()) + { + end = dataPart.indexOf(',', start); + if (end == -1) end = dataPart.length(); // Last value + + String update_param = "mst,"; + String token = dataPart.substring(start, end); + + // Assign to tempSettings + switch (idx) { + case CONDUCTIVITY_MST_PARAM_IDX_SINFREQ: + tempSettings.SinFreq = token.toFloat(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.SinFreq, 4); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_DACPP: + tempSettings.DacVoltPP = token.toFloat(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.DacVoltPP, 4); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_BIAS: + tempSettings.BiasVolt = token.toFloat(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.BiasVolt, 4); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_RTIA: + tempSettings.HstiaRtiaSel = token.toInt(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.HstiaRtiaSel); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_PGA: + tempSettings.AdcPgaGain = token.toInt(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.AdcPgaGain); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_DFTNUM: + tempSettings.DftNum = token.toInt(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.DftNum); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_AVGNUM: + tempSettings.ADCAvgNum = token.toInt(); + update_param += String(cfgSettingsParam[idx]) + "," + String(tempSettings.ADCAvgNum); + break; + } + + // Call handleMstCommand for this parameter + updateMeasurementSettings(update_param, idx); + + start = end + 1; + idx++; + } + } +} + +void updateMeasurementSettings(String cmd, uint8_t idx) +{ + if((idx >= CONDUCTIVITY_MST_PARAM_IDX_SINFREQ) && (idx < MAX_CONDUCTIVITY_MST_PARAM_IDX)) + { + updateCfgStatus[idx] = UPDATE_CFG_STATUS_SUCCESS; + } + else + { + return; + } + // Command format: cfg,parameter,value // Examples: // - cfg,sinfreq,1000.0 (Set sine wave frequency to 1000 Hz) @@ -3612,7 +3696,7 @@ else if (valueTest == 7) rtia = HSTIARTIA_160K; else - updateCfgStataus = UPDATE_CFG_STATUS_NO_CHANGE_TO_TIA; + updateCfgStatus[idx] = UPDATE_CFG_STATUS_NO_CHANGE_TO_TIA; // activeSerial->print("no change to tia"); pImpedanceCfg->HstiaRtiaSel = rtia; @@ -3638,7 +3722,7 @@ else if (value == "9") pga = ADCPGA_9; else - updateCfgStataus = UPDATE_CFG_STATUS_NO_CHANGE_TO_PGA; + updateCfgStatus[idx] = UPDATE_CFG_STATUS_NO_CHANGE_TO_PGA; pImpedanceCfg->AdcPgaGain = pga; // activeSerial->printf("Set PGA gain to: %s\n", value.c_str()); @@ -3660,7 +3744,7 @@ else if (value == "256") dft = DFTNUM_256; else - updateCfgStataus = UPDATE_CFG_STATUS_NO_CHANGE_TO_DFT_NUM; + updateCfgStatus[idx] = UPDATE_CFG_STATUS_NO_CHANGE_TO_DFT_NUM; pImpedanceCfg->DftNum = dft; // activeSerial->printf("Set DFT number to: %s\n", value.c_str()); @@ -3680,7 +3764,7 @@ else if (value == "16") avg = ADCAVGNUM_16; else - updateCfgStataus = UPDATE_CFG_STATUS_NO_CHANGE_TO_AVG_NUM; + updateCfgStatus[idx] = UPDATE_CFG_STATUS_NO_CHANGE_TO_AVG_NUM; pImpedanceCfg->ADCAvgNum = avg; // activeSerial->printf("Set ADC average number to: %s\n", value.c_str()); @@ -3721,7 +3805,7 @@ { // Handle unrecognized parameter // activeSerial->println("Malformed command: unrecognized parameter"); - updateCfgStataus = UPDATE_CFG_STATUS_ERR_UNRECOGNIZED_PARAM; + updateCfgStatus[idx] = UPDATE_CFG_STATUS_ERR_UNRECOGNIZED_PARAM; return; } @@ -3731,108 +3815,109 @@ // Automatically save the configuration after any successful change saveCurrentConfig(); } -void getMesaurementSettings(void) -{ - // Pointer to store configuration structure - AppIMPCfg_Type *cfg; - // Get the current impedance measurement configuration - AppIMPGetCfg(&cfg); + void getMesaurementSettings(void) + { + // Pointer to store configuration structure + AppIMPCfg_Type *cfg; - measurementSettingsPacket.SinFreq = cfg->SinFreq; - measurementSettingsPacket.DacVoltPP = cfg->DacVoltPP; - measurementSettingsPacket.BiasVolt = cfg->BiasVolt; - measurementSettingsPacket.HstiaRtiaSel = cfg->HstiaRtiaSel; - measurementSettingsPacket.AdcPgaGain = cfg->AdcPgaGain; - measurementSettingsPacket.DftNum = cfg->DftNum; - measurementSettingsPacket.ADCAvgNum = cfg->ADCAvgNum; - activeSerial->write((uint8_t *)&measurementSettingsPacket, sizeof(measurementSettingsPacket)); -} + // Get the current impedance measurement configuration + AppIMPGetCfg(&cfg); -void getInitStatus(void) -{ - // Count units by status - int skippedCount = 0; - int failedCount = 0; - int successCount = 0; + measurementSettingsPacket.SinFreq = cfg->SinFreq; + measurementSettingsPacket.DacVoltPP = cfg->DacVoltPP; + measurementSettingsPacket.BiasVolt = cfg->BiasVolt; + measurementSettingsPacket.HstiaRtiaSel = cfg->HstiaRtiaSel; + measurementSettingsPacket.AdcPgaGain = cfg->AdcPgaGain; + measurementSettingsPacket.DftNum = cfg->DftNum; + measurementSettingsPacket.ADCAvgNum = cfg->ADCAvgNum; + activeSerial->write((uint8_t *)&measurementSettingsPacket, sizeof(measurementSettingsPacket)); + } - // If Initialization function has been completely executed - if(true == isInitialized) + void getInitStatus(void) { - // Count number of skipped, failed and successful sensor initialization - for (int i = 0; i < 6; i++) + // Count units by status + int skippedCount = 0; + int failedCount = 0; + int successCount = 0; + + // If Initialization function has been completely executed + if (true == isInitialized) { - if (unitStatus[i] == STATUS_SKIPPED) + // Count number of skipped, failed and successful sensor initialization + for (int i = 0; i < 6; i++) { - skippedCount++; + if (unitStatus[i] == STATUS_SKIPPED) + { + skippedCount++; + } + else if (unitStatus[i] == STATUS_FAILED) + { + failedCount++; + } + else + { // STATUS_SUCCESS + successCount++; + } } - else if (unitStatus[i] == STATUS_FAILED) + + // If any of the sensor was skipped or failed, then + if ((skippedCount > 0) || (failedCount > 0)) { - failedCount++; + // Set init status to failed + initStatus = INIT_STATUS_FAILED; } + // If all the sensors were initialized successfully, then + else if (successCount == MAX_NUM_OF_SENSORS) + { + // Set the init status to initialized. + initStatus = INIT_STATUS_INITIALIZED; + } else - { // STATUS_SUCCESS - successCount++; + { + // Still need to decide, what to do here. } } - // If any of the sensor was skipped or failed, then - if ((skippedCount > 0) || (failedCount > 0)) - { - // Set init status to failed - initStatus = INIT_STATUS_FAILED; - } - // If all the sensors were initialized successfully, then - else if(successCount == MAX_NUM_OF_SENSORS) - { - // Set the init status to initialized. - initStatus = INIT_STATUS_INITIALIZED; - } - else - { - // Still need to decide, what to do here. - } + activeSerial->write((uint8_t *)&initStatus, sizeof(initStatus)); } - activeSerial->write((uint8_t *)&initStatus, sizeof(initStatus)); -} - -void sendAllSensorData( void ) -{ - int i = 0; - while(sendSensorData) + void sendAllSensorData(void) { - // Read a line from the serial port until newline character - String command = activeSerial->readStringUntil('\n'); - - if(command.length() > 0) + int i = 0; + while (sendSensorData) { - break; - } - else - { - // Get measurements for a - if ( true == isSensorValid( i ) ) + // Read a line from the serial port until newline character + String command = activeSerial->readStringUntil('\n'); + + if (command.length() > 0) { - if (true == isSensorInitialized( i ) ) + break; + } + else + { + // Get measurements for a + if (true == isSensorValid(i)) { - selectUnit(i); - getSelectedSensorMeasurements(); + if (true == isSensorInitialized(i)) + { + selectUnit(i); + getSelectedSensorMeasurements(); + } } + if (i > 6) + { + i = 1; + } } - if ( i > 6) - { - i = 1; - } + i = i + 1; } - i = i + 1; } -} -void initPacketToDefault ( int unit ) -{ - if(true == isSensorValid(unit)) + void initPacketToDefault(int unit) { + if (true == isSensorValid(unit)) + { // Pack the sensor data // The ALY code loops from 1 to 6. Our packet index from 0 to 5. So, index at i-1. int sensorIdx = unit - 1;