Skip to content

Commit

Permalink
Provide channel for neccessary ValueType
Browse files Browse the repository at this point in the history
To support OA that have 2 PMS sensor
  • Loading branch information
samuelbles07 committed Oct 13, 2024
1 parent ea91cf9 commit 6925b1a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
80 changes: 59 additions & 21 deletions src/AgValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
void Measurements::maxUpdate(AgValueType type, int max) {
switch (type) {
case AgValueType::Temperature:
_temperature.update.max = max;
_temperature[0].update.max = max;
_temperature[1].update.max = max;
break;
case AgValueType::Humidity:
_humidity.update.max = max;
_humidity[0].update.max = max;
_humidity[1].update.max = max;
break;
case AgValueType::CO2:
_co2.update.max = max;
Expand All @@ -29,21 +31,35 @@ void Measurements::maxUpdate(AgValueType type, int max) {
_nox_raw.update.max = max;
break;
case AgValueType::PM25:
_pm_25.update.max = max;
_pm_25[0].update.max = max;
_pm_25[1].update.max = max;
break;
case AgValueType::PM01:
_pm_01.update.max = max;
_pm_01[0].update.max = max;
_pm_01[1].update.max = max;
break;
case AgValueType::PM10:
_pm_10.update.max = max;
_pm_10[0].update.max = max;
_pm_10[1].update.max = max;
break;
case AgValueType::PM03_PC:
_pm_03_pc.update.max = max;
_pm_03_pc[0].update.max = max;
_pm_03_pc[1].update.max = max;
break;
};
}

bool Measurements::updateValue(AgValueType type, int val) {
bool Measurements::updateValue(AgValueType type, int val, int ch) {
// Validate channel
if (ch != 1 || ch != 2) {
Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);
// TODO: Perhaps just do assert
return false;
}

// Follow array indexing just for get address of the value type
ch = ch - 1;

// Define data point source
IntegerValue *temporary = nullptr;
float invalidValue = 0;
Expand All @@ -69,19 +85,19 @@ bool Measurements::updateValue(AgValueType type, int val) {
invalidValue = utils::getInvalidNOx();
break;
case AgValueType::PM25:
temporary = &_pm_25;
temporary = &_pm_25[ch];
invalidValue = utils::getInvalidPmValue();
break;
case AgValueType::PM01:
temporary = &_pm_01;
temporary = &_pm_01[ch];
invalidValue = utils::getInvalidPmValue();
break;
case AgValueType::PM10:
temporary = &_pm_10;
temporary = &_pm_10[ch];
invalidValue = utils::getInvalidPmValue();
break;
case AgValueType::PM03_PC:
temporary = &_pm_03_pc;
temporary = &_pm_03_pc[ch];
invalidValue = utils::getInvalidPmValue();
break;
default:
Expand All @@ -92,9 +108,12 @@ bool Measurements::updateValue(AgValueType type, int val) {
if (temporary == nullptr) {
Serial.printf("%s is not defined for integer data type\n", agValueTypeStr(type));
// TODO: Just assert?
return;
return false;
}

// Restore channel value for debugging purpose
ch = ch + 1;

// Update new value when value provided is not the invalid one
if (val != invalidValue) {
temporary->lastValue = val;
Expand All @@ -107,14 +126,17 @@ bool Measurements::updateValue(AgValueType type, int val) {

// Calculate value average when maximum set is reached
if (temporary->update.counter >= temporary->update.max) {
// TODO: Need to check if SUCCESS == 0, what should we do?
// Calculate the average
temporary->avg = temporary->sumValues / temporary->update.success;
Serial.printf("%s{%d} count reached! Average value %d\n", agValueTypeStr(type), ch,
temporary->avg);

// This is just for notifying
// Notify if there's are invalid value when updating
int miss = temporary->update.max - temporary->update.success;
if (miss != 0) {
Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
temporary->update.max);
Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch,
miss, temporary->update.max);
}

// Resets average related variable calculation
Expand All @@ -127,17 +149,27 @@ bool Measurements::updateValue(AgValueType type, int val) {
return false;
}

bool Measurements::updateValue(AgValueType type, float val) {
bool Measurements::updateValue(AgValueType type, float val, int ch) {
// Validate channel
if (ch != 1 || ch != 2) {
Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);
// TODO: Perhaps just do assert
return false;
}

// Follow array indexing just for get address of the value type
ch = ch - 1;

// Define data point source
FloatValue *temporary = nullptr;
float invalidValue = 0;
switch (type) {
case AgValueType::Temperature:
temporary = &_temperature;
temporary = &_temperature[ch];
invalidValue = utils::getInvalidTemperature();
break;
case AgValueType::Humidity:
temporary = &_humidity;
temporary = &_humidity[ch];
invalidValue = utils::getInvalidHumidity();
break;
default:
Expand All @@ -148,9 +180,12 @@ bool Measurements::updateValue(AgValueType type, float val) {
if (temporary == nullptr) {
Serial.printf("%s is not defined for float data type\n", agValueTypeStr(type));
// TODO: Just assert?
return;
return false;
}

// Restore channel value for debugging purpose
ch = ch + 1;

// Update new value when value provided is not the invalid one
if (val != invalidValue) {
temporary->lastValue = val;
Expand All @@ -163,14 +198,17 @@ bool Measurements::updateValue(AgValueType type, float val) {

// Calculate value average when maximum set is reached
if (temporary->update.counter >= temporary->update.max) {
// TODO: Need to check if SUCCESS == 0
// Calculate the average
temporary->avg = temporary->sumValues / temporary->update.success;
Serial.printf("%s{%d} count reached! Average value %0.2f\n", agValueTypeStr(type),
temporary->avg);

// This is just for notifying
int miss = temporary->update.max - temporary->update.success;
if (miss != 0) {
Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
temporary->update.max);
Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch,
miss, temporary->update.max);
}

// Resets average related variable calculation
Expand Down
21 changes: 13 additions & 8 deletions src/AgValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class Measurements {
*
* @param type (AgValueType) value type that will be updated
* @param val (int) the new value
* @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel.
* Currently maximum channel is 2. Default: 1 (channel 1)
* @return true if update counter reached and new average value is calculated
* @return false otherwise
*/
bool updateValue(AgValueType type, int val);
bool updateValue(AgValueType type, int val, int ch = 1);

/**
* @brief update target type value with new value.
Expand All @@ -98,10 +100,12 @@ class Measurements {
*
* @param type (AgValueType) value type that will be updated
* @param val (float) the new value
* @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel.
* Currently maximum channel is 2. Default: 1 (channel 1)
* @return true if update counter reached and new average value is calculated
* @return false otherwise
*/
bool updateValue(AgValueType type, float val);
bool updateValue(AgValueType type, float val, int ch = 1);

float Temperature;
int Humidity;
Expand Down Expand Up @@ -144,17 +148,18 @@ class Measurements {
String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void *_ag, void *_config);

private:
FloatValue _temperature;
FloatValue _humidity;
// Some declared as an array (channel), because FW_MODE_O_1PPx has two PMS5003T
FloatValue _temperature[2];
FloatValue _humidity[2];
IntegerValue _co2;
IntegerValue _tvoc;
IntegerValue _tvoc_raw;
IntegerValue _nox;
IntegerValue _nox_raw;
IntegerValue _pm_25;
IntegerValue _pm_01;
IntegerValue _pm_10;
IntegerValue _pm_03_pc; // particle count 0.3
IntegerValue _pm_25[2];
IntegerValue _pm_01[2];
IntegerValue _pm_10[2];
IntegerValue _pm_03_pc[2]; // particle count 0.3

/**
* @brief Get PMS5003 firmware version string
Expand Down

0 comments on commit 6925b1a

Please sign in to comment.