diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index b0ea4dcc..ae1b4a3a 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -99,6 +99,25 @@ #define ANALOG_DELAY 0 // Delay between samples in micros #endif +//Use the following to perform scaling of raw analog values +// scaledRead = ( factor * rawRead ) + offset +// +//Please take note that the offset is not affected by the scaling factor + +#ifndef ANALOG_FACTOR +#define ANALOG_FACTOR 1.0 // Multiply raw reading by this factor +#endif + +#ifndef ANALOG_OFFSET +#define ANALOG_OFFSET 0.0 // Add this offset to *scaled* value +#endif + +// Round to this number of decimals +#ifndef ANALOG_DECIMALS +#define ANALOG_DECIMALS 2 +#endif + + //------------------------------------------------------------------------------ // BH1750 // Enable support by passing BH1750_SUPPORT=1 build flag diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 48fef408..dc9476b3 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -52,6 +52,7 @@ unsigned char _magnitudeDecimals(unsigned char type) { // Hardcoded decimals (these should be linked to the unit, instead of the magnitude) + if (type == MAGNITUDE_ANALOG) return ANALOG_DECIMALS; if (type == MAGNITUDE_ENERGY || type == MAGNITUDE_ENERGY_DELTA) { if (_sensor_energy_units == ENERGY_KWH) return 3; @@ -347,6 +348,9 @@ void _sensorLoad() { AnalogSensor * sensor = new AnalogSensor(); sensor->setSamples(ANALOG_SAMPLES); sensor->setDelay(ANALOG_DELAY); + //CICM For analog scaling + sensor->setFactor(ANALOG_FACTOR); + sensor->setOffset(ANALOG_OFFSET); _sensors.push_back(sensor); } #endif diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index e13d2cee..f5e0a2df 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -35,6 +35,16 @@ class AnalogSensor : public BaseSensor { _micros = micros; } + void setFactor(double factor) { + //DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(factor,6).c_str()); + _factor = factor; + } + + void setOffset(double offset) { + //DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(offset,6).c_str()); + _offset = offset; + } + // --------------------------------------------------------------------- unsigned int getSamples() { @@ -45,6 +55,14 @@ class AnalogSensor : public BaseSensor { return _micros; } + double getFactor() { + return _factor; + } + + double getOffset() { + return _offset; + } + // --------------------------------------------------------------------- // Sensor API // --------------------------------------------------------------------- @@ -77,6 +95,7 @@ class AnalogSensor : public BaseSensor { } // Current value for slot # index + // Changed return type as moving to scaled value double value(unsigned char index) { if (index == 0) return _read(); return 0; @@ -84,7 +103,9 @@ class AnalogSensor : public BaseSensor { protected: - unsigned int _read() { + //CICM: this should be for raw values + // renaming protected function "_read" to "_rawRead" + unsigned int _rawRead() { if (1 == _samples) return analogRead(0); unsigned long sum = 0; for (unsigned int i=0; i<_samples; i++) { @@ -94,8 +115,28 @@ class AnalogSensor : public BaseSensor { return sum / _samples; } + //CICM: and proper read should be scalable and thus needs sign + //and decimal part + double _read() { + //Raw measure could also be a class variable with getter so that can + //be reported through MQTT, ... + unsigned int rawValue; + double scaledValue; + // Debugging doubles to string + //DEBUG_MSG(("[ANALOG_SENSOR] Started standard read, factor: %s , offset: %s, decimals: %d \n"), String(_factor).c_str(), String(_offset).c_str(), ANALOG_DECIMALS); + rawValue = _rawRead(); + //DEBUG_MSG(("[ANALOG_SENSOR] Raw read received: %d \n"), rawValue); + scaledValue = _factor*rawValue + _offset; + //DEBUG_MSG(("[ANALOG_SENSOR] Scaled value result: %s \n"), String(scaledValue).c_str()); + return scaledValue; + } + + unsigned int _samples = 1; unsigned long _micros = 0; + //CICM: for scaling and offset, also with getters and setters + double _factor = 1.0; + double _offset = 0.0; };