Index: AD5941_interface09302025/ad5941_library_extension.cpp =================================================================== diff -u -r4c761dff9ac08e7007d8ef61c9992831c9963050 -rc8fe9095198504232955adfecb5b2b65aaec5fff --- AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 4c761dff9ac08e7007d8ef61c9992831c9963050) +++ AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision c8fe9095198504232955adfecb5b2b65aaec5fff) @@ -34,20 +34,22 @@ #include // Stores the instance of currently connected / detected Serial port -Stream* activeSerial = nullptr; +Stream *activeSerial = nullptr; -Stream* serialPorts[] = { - &Serial, // USB virtual serial - &Serial1, // pins 0, 1 - &Serial2, // pins 7, 8 - &Serial3, // pins 14, 15 - &Serial4, // pins 16, 17 - &Serial5, // pins 21, 20 - &Serial6, // pins 24, 25 - &Serial7 // pins 28, 29 +Stream *serialPorts[] = +{ + &Serial, // USB virtual serial + &Serial1, // pins 0, 1 + &Serial2, // pins 7, 8 + &Serial3, // pins 14, 15 + &Serial4, // pins 16, 17 + &Serial5, // pins 21, 20 + &Serial6, // pins 24, 25 + &Serial7 // pins 28, 29 }; -const char* serialNames[] = { +const char *serialNames[] = +{ "Serial (USB)", "Serial1 (pins 0,1)", "Serial2 (pins 7,8)", @@ -56,19 +58,19 @@ "Serial5 (pins 21,20)", "Serial6 (pins 24,25)", "Serial7 (pins 28,29)" -}; + }; -static const char* cfgSettingsParam[] = { - "sinfreq", - "dacpp", - "bias", - "rtia", - "pga", - "dftnum", - "avgnum" - }; +static const char *cfgSettingsParam[] = + { + "sinfreq", + "dacpp", + "bias", + "rtia", + "pga", + "dftnum", + "avgnum" + }; - uint32_t AppBuff[APPBUFF_SIZE]; // Arrays to store up to 5 measurements @@ -124,13 +126,13 @@ STATUS_SKIPPED, STATUS_SKIPPED, STATUS_SKIPPED, STATUS_SKIPPED, STATUS_SKIPPED, STATUS_SKIPPED}; -static bool isInitialized; // Set to true if the initialization function has been completely executed atleast once. -static Init_Status initStatus; // Stores the phase of initialization -static sensorDataPacketStruct sensorPacket[MAX_NUM_OF_SENSORS]; // Stores Impedance and RTD measurements for all the sensors. -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 measurementSettingsStruct tempSettings; // Stores received measurement settings to be updated. +static bool isInitialized; // Set to true if the initialization function has been completely executed atleast once. +static Init_Status initStatus; // Stores the phase of initialization +static sensorDataPacketStruct sensorPacket[MAX_NUM_OF_SENSORS]; // Stores Impedance and RTD measurements for all the sensors. +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 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_INVALID_CMD; @@ -1361,7 +1363,7 @@ return; } - // Handle save command for doubles (format: save,value1,value2,value3,value4) + // Handle save command for doubles (format: save,value1,value2,value3,value4) if (command.startsWith("upe,")) { updateEEPROMdata(command); @@ -1386,9 +1388,10 @@ break; case 'a': { - isInitialized = false; - while(!initializeSelectedUnits()); - if ( true == isInitialized) + isInitialized = false; + while (!initializeSelectedUnits()) + ; + if (true == isInitialized) { getInitStatus(); } @@ -1550,8 +1553,8 @@ // Print magnitude in ohms and phase in degrees // Note: Converting phase from radians to degrees (phase * 180 / π) activeSerial->printf("RzMag: %.2f Ohm, RzPhase: %.2f deg", - medianMag, - medianPhase); + medianMag, + medianPhase); // Add separator and perform RTD (temperature) measurement activeSerial->print(";"); @@ -1614,6 +1617,7 @@ case 'j': // Perform system startup and configuration { selectUnit(command.substring(2).toInt()); + activeSerial->write((uint8_t*)&singleSensorPacket.sensorNum, sizeof(uint32_t)); } break; @@ -1696,8 +1700,13 @@ case 'g': { // Mesaure and transmit for all the initialized sensors - if(false == getAllMesaurements()) + if (true == getAllMesaurements()) { + // Send all the sensor data to DD + activeSerial->write((uint8_t *)&sensorPacket, sizeof(sensorPacket)); + } + else + { activeSerial->write((uint8_t)0); } } @@ -1712,31 +1721,32 @@ case 'k': { - // Get conductivity mesaurement settings in binary format. + // Get conductivity mesaurement settings in binary format. getMesaurementSettings(); } break; case 'l': { - // Get Initialization Status + // Get Initialization Status getInitStatus(); } break; case 'm': { - sendSensorData = true; + sendSensorData = true; sendAllSensorData(); } break; case 'n': { sendSensorData = false; - sendAllSensorData(); + activeSerial->write((uint8_t)1); + // sendAllSensorData(); } break; - + default: // Handle unknown commands { activeSerial->println("Unknown command. Type ? for help."); @@ -1813,9 +1823,8 @@ pinMode(TEENSY_RESET_PIN, OUTPUT); pinMode(AD5940_INT_PIN, INPUT); - - singleSensorPacket.sensorNum = unitIn; // Conductivity Sensor number i.e unit 1 to 6. - + singleSensorPacket.sensorNum = unitIn; // Conductivity Sensor number i.e unit 1 to 6. + // activeSerial->print("Selected Unit: "); // activeSerial->print(unitIn); // activeSerial->print(" | Reset Pin: "); @@ -2340,7 +2349,7 @@ { initializeSingleUnit(unit); } - isInitialized = true; + isInitialized = true; // Print summary of unit status // printUnitStatusSummary(); @@ -2665,8 +2674,8 @@ // Print magnitude in ohms and phase in degrees // Note: Converting phase from radians to degrees (phase * 180 / π) activeSerial->printf("RzMag: %.2f Ohm, RzPhase: %.2f deg", - medianMag, - medianPhase); + medianMag, + medianPhase); // Add separator activeSerial->print(";"); @@ -2682,7 +2691,6 @@ propagateSettingsChanges = 0; } - float calculateMedian(float arr[], int size) { // Create a temporary copy of the array @@ -2750,83 +2758,81 @@ return saturated; } - // ************************************ Diality ********************************************** -void detectSerial() +void detectSerial() { - // First check Serial7 explicitly - beginSerial(&Serial7, 115200); - delay(50); - if (Serial7.available()) - { - activeSerial = &Serial7; - // activeSerial->println("Using Serial7 (pins 28,29)"); - return; // Done, since Serial7 is preferred - } - + // First check Serial7 explicitly + beginSerial(&Serial7, 115200); + delay(50); + if (Serial7.available()) + { + activeSerial = &Serial7; + // activeSerial->println("Using Serial7 (pins 28,29)"); + return; // Done, since Serial7 is preferred + } + else + { for (int i = 0; i < 8; i++) - { - beginSerial(serialPorts[i], 115200); - delay(50); // Give time for init - // For USB Serial, wait for connection - if (serialPorts[i] == &Serial) - { - unsigned long start = millis(); - while (!Serial && millis() - start < 2000) - { - // Wait up to 2s for USB - } + { + beginSerial(serialPorts[i], 115200); + delay(50); // Give time for init + // For USB Serial, wait for connection + if (serialPorts[i] == &Serial) + { + unsigned long start = millis(); + while (!Serial && millis() - start < 2000) + { + // Wait up to 2s for USB } - // Assume first port is good (or add handshake logic) activeSerial = serialPorts[i]; - // activeSerial->println(String("Using ") + serialNames[i]); break; + } } + } } - -void beginSerial(Stream* s, unsigned long baud) +void beginSerial(Stream *s, unsigned long baud) { - if (s == &Serial) - { - Serial.begin(baud); - } - else if (s == &Serial1) - { - Serial1.begin(baud); - } - else if (s == &Serial2) - { - Serial2.begin(baud); - } - else if (s == &Serial3) - { - Serial3.begin(baud); - } - else if (s == &Serial4) - { - Serial4.begin(baud); - } - else if (s == &Serial5) - { - Serial5.begin(baud); - } - else if (s == &Serial6) - { - Serial6.begin(baud); - } - else if (s == &Serial7) - { - Serial7.begin(baud); - } + if (s == &Serial) + { + Serial.begin(baud); + } + else if (s == &Serial1) + { + Serial1.begin(baud); + } + else if (s == &Serial2) + { + Serial2.begin(baud); + } + else if (s == &Serial3) + { + Serial3.begin(baud); + } + else if (s == &Serial4) + { + Serial4.begin(baud); + } + else if (s == &Serial5) + { + Serial5.begin(baud); + } + else if (s == &Serial6) + { + Serial6.begin(baud); + } + else if (s == &Serial7) + { + Serial7.begin(baud); + } } // Function to measure all successfully initialized units bool getAllMesaurements(void) { int successCount = 0; - bool retVal = true; // Success + bool retVal = true; // Success // Count successful units first for (int i = 0; i < 6; i++) @@ -2927,194 +2933,210 @@ previousMillis = millis(); - // Check for "ERROR: AD5940 wakeup timeout" - if(-1000.0f == rtd_res) + // Check for "ERROR: AD5940 wakeup timeout" + if (-1000.0f == rtd_res) { // In case of an error, send all 0's initPacketToDefault(unit); } - if(false == isSensorInitialized(unit)) + if (false == isSensorInitialized(unit)) { // In case the unit is not initialized, send all 0's initPacketToDefault(unit); } + // Pack data for case 'g' else { // 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; - sensorPacket[sensorIdx].sensorNum = unit; // Conductivity Sensor number i.e unit 1 to 6. - sensorPacket[sensorIdx].impFreq = storedFrequency; // Impedance Frequency - sensorPacket[sensorIdx].impDataPoints = 1; // Impedance Data Points. Hard coded to 1 - sensorPacket[sensorIdx].impRzMag = medianMag; // Value of medianMag - sensorPacket[sensorIdx].impRzPhase = medianPhase; // Value of medianPhase - sensorPacket[sensorIdx].rtdFreq = 0; // RTD Frequency. Hard coded to 0.0 - sensorPacket[sensorIdx].rtdDataPoints = 1; // Impedance Data Points. Hard coded to 1 - sensorPacket[sensorIdx].rtdRzMag = rtd_res; // Value of rtd_resistance - sensorPacket[sensorIdx].rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 + sensorPacket[sensorIdx].sensorNum = unit; // Conductivity Sensor number i.e unit 1 to 6. + sensorPacket[sensorIdx].impFreq = storedFrequency; // Impedance Frequency + sensorPacket[sensorIdx].impDataPoints = 1; // Impedance Data Points. Hard coded to 1 + sensorPacket[sensorIdx].impRzMag = medianMag; // Value of medianMag + sensorPacket[sensorIdx].impRzPhase = medianPhase; // Value of medianPhase + sensorPacket[sensorIdx].rtdFreq = 0; // RTD Frequency. Hard coded to 0.0 + sensorPacket[sensorIdx].rtdDataPoints = 1; // Impedance Data Points. Hard coded to 1 + sensorPacket[sensorIdx].rtdRzMag = rtd_res; // Value of rtd_resistance + sensorPacket[sensorIdx].rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 + + // Reusing this function for case 'm' + if(sendSensorData) + { + singleSensorPacket.sensorNum = sensorPacket[sensorIdx].sensorNum ; + singleSensorPacket.impFreq = sensorPacket[sensorIdx].impFreq; // Impedance Frequency + singleSensorPacket.impDataPoints = sensorPacket[sensorIdx].impDataPoints; // Impedance Data Points. Hard coded to 1 + singleSensorPacket.impRzMag = sensorPacket[sensorIdx].impRzMag ; // Value of medianMag + singleSensorPacket.impRzPhase = sensorPacket[sensorIdx].impRzPhase; // Value of medianPhase + singleSensorPacket.rtdFreq = sensorPacket[sensorIdx].rtdFreq; // RTD Frequency. Hard coded to 0.0 + singleSensorPacket.rtdDataPoints = sensorPacket[sensorIdx].rtdDataPoints; // Impedance Data Points. Hard coded to 1 + singleSensorPacket.rtdRzMag = sensorPacket[sensorIdx].rtdRzMag; // Value of rtd_resistance + singleSensorPacket.rtdRzPhase = sensorPacket[sensorIdx].rtdRzPhase ; // RTD Rz Phase. Hard coded to 0.0 + activeSerial->write((uint8_t *)&singleSensorPacket, sizeof(singleSensorPacket)); + } + } } - // Send all the sensor data to DD - activeSerial->write((uint8_t *)&sensorPacket, sizeof(sensorPacket)); propagateSettingsChanges = 0; return retVal; } void getSelectedSensorMeasurements(void) { - // quick patch for fixing spontaneous measurements - unsigned long currentMillis = millis(); - if (currentMillis - previousMillis >= jitterTimeout) - { + // quick patch for fixing spontaneous measurements + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= jitterTimeout) + { - // Run multiple settling measurements in a loop - for (int settling = 0; settling < numSettlingMeasurements; settling++) + // Run multiple settling measurements in a loop + for (int settling = 0; settling < numSettlingMeasurements; settling++) + { + if (settling == 0) { - if (settling == 0) - { - // activeSerial->println("Performing settling measurements..."); - } - if (verboseMode) - { - // activeSerial->printf(" Settling Measurement#: %u ", settling); - // activeSerial->println(" "); - } - // Run one complete measurement cycle but discard results - AppIMPInit(AppBuff, APPBUFF_SIZE); - AppIMPCtrl(IMPCTRL_START, 0); - - // Wait for completion (simplified polling) - int timeout_count = 0; - while (AD5940_GetMCUIntFlag() != 1 && timeout_count < 1000) - { - delay(1); - timeout_count++; - } - - if (AD5940_GetMCUIntFlag() == 1) - { - AD5940_ClrMCUIntFlag(); - uint32_t dummy_size = APPBUFF_SIZE; - AppIMPISR(AppBuff, &dummy_size); // Discard this data - } - - AppIMPCtrl(IMPCTRL_STOPSYNC, 0); - - // Small delay between settling measurements - if (settling < numSettlingMeasurements - 1) - { - AD5940_Delay10us(1000); // 10ms between measurements - } + // activeSerial->println("Performing settling measurements..."); } if (verboseMode) { - // activeSerial->println(" End Settling Measurements"); + // activeSerial->printf(" Settling Measurement#: %u ", settling); + // activeSerial->println(" "); } - } + // Run one complete measurement cycle but discard results + AppIMPInit(AppBuff, APPBUFF_SIZE); + AppIMPCtrl(IMPCTRL_START, 0); - if (verboseMode) - { - // activeSerial->println("Conductivity"); - // activeSerial->print(" Conductivity TIA Resistor: "); - switch (DEFAULT_RTIA) + // Wait for completion (simplified polling) + int timeout_count = 0; + while (AD5940_GetMCUIntFlag() != 1 && timeout_count < 1000) { - case HSTIARTIA_200: - // activeSerial->println("200 Ohm"); - break; - case HSTIARTIA_1K: - // activeSerial->println("1K Ohm"); - break; - case HSTIARTIA_5K: - // activeSerial->println("5K Ohm"); - break; - case HSTIARTIA_10K: - // activeSerial->println("10K Ohm"); - break; - case HSTIARTIA_20K: - // activeSerial->println("20K Ohm"); - break; - case HSTIARTIA_40K: - // activeSerial->println("40K Ohm"); - break; - case HSTIARTIA_80K: - // activeSerial->println("80K Ohm"); - break; - case HSTIARTIA_160K: - // activeSerial->println("160K Ohm"); - break; - default: - // activeSerial->println("Unknown"); - break; + delay(1); + timeout_count++; } - } - // Perform impedance measurement - for (int i = 0; i < repeatNumber; i++) - { - AppIMPMeasure(i); - } - if (verboseMode) - { - if (AD5940_IsAdcMaxSaturatedAndClear()) + + if (AD5940_GetMCUIntFlag() == 1) { - // activeSerial->println("SaturationFlag tripped!"); + AD5940_ClrMCUIntFlag(); + uint32_t dummy_size = APPBUFF_SIZE; + AppIMPISR(AppBuff, &dummy_size); // Discard this data } - else + + AppIMPCtrl(IMPCTRL_STOPSYNC, 0); + + // Small delay between settling measurements + if (settling < numSettlingMeasurements - 1) { - // activeSerial->println("SaturationFlag NOT tripped!"); + AD5940_Delay10us(1000); // 10ms between measurements } } - float medianMag; - float medianPhase; - // take median and output results - if (repeatNumber == 1) + if (verboseMode) { - medianMag = magnitudeArray[0]; - medianPhase = phaseArray[0]; + // activeSerial->println(" End Settling Measurements"); } + } + + if (verboseMode) + { + // activeSerial->println("Conductivity"); + // activeSerial->print(" Conductivity TIA Resistor: "); + switch (DEFAULT_RTIA) + { + case HSTIARTIA_200: + // activeSerial->println("200 Ohm"); + break; + case HSTIARTIA_1K: + // activeSerial->println("1K Ohm"); + break; + case HSTIARTIA_5K: + // activeSerial->println("5K Ohm"); + break; + case HSTIARTIA_10K: + // activeSerial->println("10K Ohm"); + break; + case HSTIARTIA_20K: + // activeSerial->println("20K Ohm"); + break; + case HSTIARTIA_40K: + // activeSerial->println("40K Ohm"); + break; + case HSTIARTIA_80K: + // activeSerial->println("80K Ohm"); + break; + case HSTIARTIA_160K: + // activeSerial->println("160K Ohm"); + break; + default: + // activeSerial->println("Unknown"); + break; + } + } + // Perform impedance measurement + for (int i = 0; i < repeatNumber; i++) + { + AppIMPMeasure(i); + } + if (verboseMode) + { + if (AD5940_IsAdcMaxSaturatedAndClear()) + { + // activeSerial->println("SaturationFlag tripped!"); + } else { - medianMag = calculateMedian(magnitudeArray, repeatNumber); - medianPhase = calculateMedian(phaseArray, repeatNumber); + // activeSerial->println("SaturationFlag NOT tripped!"); } + } + float medianMag; + float medianPhase; + // take median and output results + if (repeatNumber == 1) + { + medianMag = magnitudeArray[0]; + medianPhase = phaseArray[0]; + } + else + { + medianMag = calculateMedian(magnitudeArray, repeatNumber); + medianPhase = calculateMedian(phaseArray, repeatNumber); + } - // activeSerial->printf("Freq: %.2f Hz ", storedFrequency); + // activeSerial->printf("Freq: %.2f Hz ", storedFrequency); - // Print the number of data points - // activeSerial->printf("DataPoints: %lu ", 1); + // Print the number of data points + // activeSerial->printf("DataPoints: %lu ", 1); - // Print magnitude in ohms and phase in degrees - // Note: Converting phase from radians to degrees (phase * 180 / π) - // activeSerial->printf("RzMag: %.2f Ohm, RzPhase: %.2f deg", - // medianMag, - // medianPhase); + // Print magnitude in ohms and phase in degrees + // Note: Converting phase from radians to degrees (phase * 180 / π) + // activeSerial->printf("RzMag: %.2f Ohm, RzPhase: %.2f deg", + // medianMag, + // medianPhase); - // Add separator and perform RTD (temperature) measurement - // activeSerial->print(";"); - float rtd_res = getRTDMeasurements(rtdVoltageValue); - // activeSerial->print("*"); - previousMillis = millis(); + // Add separator and perform RTD (temperature) measurement + // activeSerial->print(";"); + float rtd_res = getRTDMeasurements(rtdVoltageValue); + // activeSerial->print("*"); + previousMillis = millis(); - // Check for "ERROR: AD5940 wakeup timeout" - if(-1000.0f == rtd_res) - { - // In case of an error, send all 0's - initPacketToDefault(singleSensorPacket.sensorNum); - } - else - { - singleSensorPacket.impFreq = storedFrequency; // Impedance Frequency - singleSensorPacket.impDataPoints = 1; // Impedance Data Points. Hard coded to 1 - singleSensorPacket.impRzMag = medianMag; // Value of medianMag - singleSensorPacket.impRzPhase = medianPhase; // Value of medianPhase - singleSensorPacket.rtdFreq = 0; // RTD Frequency. Hard coded to 0.0 - singleSensorPacket.rtdDataPoints = 1; // Impedance Data Points. Hard coded to 1 - singleSensorPacket.rtdRzMag = rtd_res; // Value of rtd_resistance - singleSensorPacket.rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 - } - activeSerial->write((uint8_t *)&singleSensorPacket, sizeof(singleSensorPacket)); + // Check for "ERROR: AD5940 wakeup timeout" + if (-1000.0f == rtd_res) + { + // In case of an error, send all 0's + initPacketToDefault(singleSensorPacket.sensorNum); } + else + { + singleSensorPacket.impFreq = storedFrequency; // Impedance Frequency + singleSensorPacket.impDataPoints = 1; // Impedance Data Points. Hard coded to 1 + singleSensorPacket.impRzMag = medianMag; // Value of medianMag + singleSensorPacket.impRzPhase = medianPhase; // Value of medianPhase + singleSensorPacket.rtdFreq = 0; // RTD Frequency. Hard coded to 0.0 + singleSensorPacket.rtdDataPoints = 1; // Impedance Data Points. Hard coded to 1 + singleSensorPacket.rtdRzMag = rtd_res; // Value of rtd_resistance + singleSensorPacket.rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 + } + activeSerial->write((uint8_t *)&singleSensorPacket, sizeof(singleSensorPacket)); +} + float getRTDMeasurements(float sensor_mV) { AFERefCfg_Type aferef_cfg; @@ -3242,7 +3264,7 @@ // Verbose output if (verboseMode) - { + { activeSerial->println(" "); // activeSerial->println("RTD Measurement"); @@ -3346,7 +3368,7 @@ if (valueIndex == 0) { // activeSerial->println("Error: No valid values found"); - updateEepromStatus = UPDATE_EEPROM_STATUS_NO_VALID_VALUES; + updateEepromStatus = UPDATE_EEPROM_STATUS_NO_VALID_VALUES; return; } if (valueIndex > maxTotal) @@ -3443,75 +3465,77 @@ void handleMstCommand(String cmd) { - // Check if the first 4 characters are "mst," - if (cmd.startsWith("mst,")) - { - // Remove "mst," prefix - String dataPart = cmd.substring(4); + // 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; + // 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 + 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); + 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; + // 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_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_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_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_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_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; - } + 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); + // Call handleMstCommand for this parameter + updateMeasurementSettings(update_param, idx); - start = end + 1; - 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)) + if ((idx >= CONDUCTIVITY_MST_PARAM_IDX_SINFREQ) && (idx < MAX_CONDUCTIVITY_MST_PARAM_IDX)) { updateCfgStatus[idx] = UPDATE_CFG_STATUS_SUCCESS; } @@ -3617,7 +3641,7 @@ rtia = HSTIARTIA_160K; else updateCfgStatus[idx] = UPDATE_CFG_STATUS_NO_CHANGE_TO_TIA; - // activeSerial->print("no change to tia"); + // activeSerial->print("no change to tia"); pImpedanceCfg->HstiaRtiaSel = rtia; // activeSerial->printf("Set RTIA to: %s\n", value.c_str()); @@ -3736,144 +3760,130 @@ saveCurrentConfig(); } - void getMesaurementSettings(void) - { - // Pointer to store configuration structure - AppIMPCfg_Type *cfg; +void getMesaurementSettings(void) +{ + // Pointer to store configuration structure + AppIMPCfg_Type *cfg; - // Get the current impedance measurement configuration - AppIMPGetCfg(&cfg); + // Get the current impedance measurement configuration + AppIMPGetCfg(&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)); - } + 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)); +} - void getInitStatus(void) - { - // Count units by status - int skippedCount = 0; - int failedCount = 0; - int successCount = 0; +void getInitStatus(void) +{ + // Count units by status + int skippedCount = 0; + int failedCount = 0; + int successCount = 0; - // If Initialization function has been completely executed - if (true == isInitialized) + // If Initialization function has been completely executed + if (true == isInitialized) + { + // Count number of skipped, failed and successful sensor initialization + for (int i = 0; i < 6; i++) { - // Count number of skipped, failed and successful sensor initialization - for (int i = 0; i < 6; i++) + if (unitStatus[i] == STATUS_SKIPPED) { - if (unitStatus[i] == STATUS_SKIPPED) - { - skippedCount++; - } - else if (unitStatus[i] == STATUS_FAILED) - { - failedCount++; - } - else - { // STATUS_SUCCESS - successCount++; - } + skippedCount++; } - - // If any of the sensor was skipped or failed, then - if ((skippedCount > 0) || (failedCount > 0)) + else if (unitStatus[i] == STATUS_FAILED) { - // Set init status to failed - initStatus = INIT_STATUS_FAILED; + failedCount++; } - // 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. + { // STATUS_SUCCESS + successCount++; } } - activeSerial->write((uint8_t *)&initStatus, sizeof(initStatus)); + // 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. + } } - void sendAllSensorData(void) + activeSerial->write((uint8_t *)&initStatus, sizeof(initStatus)); +} + +void sendAllSensorData(void) +{ + while (sendSensorData) { - int i = 0; - while (sendSensorData) - { - // Read a line from the serial port until newline character String command = activeSerial->readStringUntil('\n'); - if (command.length() > 0) { - break; + activeSerial->write((uint8_t)1); + break; // Exit if command received } else { - // Get measurements for a - if (true == isSensorValid(i)) - { - if (true == isSensorInitialized(i)) - { - selectUnit(i); - getSelectedSensorMeasurements(); - } - } - if (i > 6) - { - i = 1; - } + getAllMesaurements(); + delay(250); } - i = i + 1; - } } +} - void initPacketToDefault(int unit) +void initPacketToDefault(int unit) +{ + if (true == isSensorValid(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; - sensorPacket[sensorIdx].sensorNum = unit; // Conductivity Sensor number i.e unit 1 to 6. - sensorPacket[sensorIdx].impFreq = 0.0; // Impedance Frequency - sensorPacket[sensorIdx].impDataPoints = 0; // Impedance Data Points. Hard coded to 1 - sensorPacket[sensorIdx].impRzMag = 0.0; // Value of medianMag - sensorPacket[sensorIdx].impRzPhase = 0.0; // Value of medianPhase - sensorPacket[sensorIdx].rtdFreq = 0.0; // RTD Frequency. Hard coded to 0.0 - sensorPacket[sensorIdx].rtdDataPoints = 0; // Impedance Data Points. Hard coded to 1 - sensorPacket[sensorIdx].rtdRzMag = 0.0; // Value of rtd_resistance - sensorPacket[sensorIdx].rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 + // 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; + sensorPacket[sensorIdx].sensorNum = unit; // Conductivity Sensor number i.e unit 1 to 6. + sensorPacket[sensorIdx].impFreq = 0.0; // Impedance Frequency + sensorPacket[sensorIdx].impDataPoints = 0; // Impedance Data Points. Hard coded to 1 + sensorPacket[sensorIdx].impRzMag = 0.0; // Value of medianMag + sensorPacket[sensorIdx].impRzPhase = 0.0; // Value of medianPhase + sensorPacket[sensorIdx].rtdFreq = 0.0; // RTD Frequency. Hard coded to 0.0 + sensorPacket[sensorIdx].rtdDataPoints = 0; // Impedance Data Points. Hard coded to 1 + sensorPacket[sensorIdx].rtdRzMag = 0.0; // Value of rtd_resistance + sensorPacket[sensorIdx].rtdRzPhase = 0.0; // RTD Rz Phase. Hard coded to 0.0 } } -bool isSensorValid (int unit) +bool isSensorValid(int unit) { bool retVal = false; - if ( ( unit >= 1 ) && ( unit <= 6) ) + if ((unit >= 1) && (unit <= 6)) { retVal = true; } return retVal; } -bool isSensorInitialized (int unit) +bool isSensorInitialized(int unit) { bool retVal = false; - if( true == isSensorValid (unit) ) + if (true == isSensorValid(unit)) { int sensorIdx = unit - 1; - if(unitStatus[sensorIdx] == STATUS_SUCCESS) + if (unitStatus[sensorIdx] == STATUS_SUCCESS) { retVal = true; } } - return retVal; + return retVal; } #endif /* ad5940_library_extension_C */