diff --git a/src/_P177_XDB_pressure.ino b/src/_P177_XDB_pressure.ino index 1b1c4e4573..a2bacf60a0 100644 --- a/src/_P177_XDB_pressure.ino +++ b/src/_P177_XDB_pressure.ino @@ -6,6 +6,8 @@ // ####################################################################################################### /** Changelog: + * 2025-02-23 tonhuisman: Add Generate events on pressure change and Raw data options + * Fix pressure and temperature calculation * 2025-02-22 tonhuisman: Initial plugin development, direct I2C access, * based on this documentation: https://www.letscontrolit.com/forum/viewtopic.php?t=10568#p71993 (attachment) */ @@ -69,18 +71,25 @@ boolean Plugin_177(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_SET_DEFAULTS: { - P177_PRESSURE_SCALE_FACTOR = 1; // Default to 1 + P177_PRESSURE_SCALE_FACTOR = 1000; // Default to 1000 + P177_GENERATE_EVENTS = 1; // Enabled break; } case PLUGIN_WEBFORM_LOAD: { - addFormNumericBox(F("Pressure scaling factor"), F("scale"), P177_PRESSURE_SCALE_FACTOR, 1); + addFormNumericBox(F("Sensor Full-scale Pressure"), F("scale"), P177_PRESSURE_SCALE_FACTOR, 1); + addUnit(F("psi, mBar, hPa, etc.")); - addFormNumericBox(F("Temperature offset"), F("tempoffset"), P177_TEMPERATURE_OFFSET); + addFormNumericBox(F("Temperature offset"), F("tempoffset"), P177_TEMPERATURE_OFFSET); addUnit(F("x 0.1C")); addFormNote(F("Offset in units of 0.1 degree Celsius")); + addFormCheckBox(F("Generate events on Pressure change"), F("pevents"), P177_GENERATE_EVENTS == 1); + + addFormCheckBox(F("Use raw data from sensor"), F("praw"), P177_RAW_DATA == 1); + addFormNote(F("When checked: Pressure scaling factor and Temperature offset will be ignored!")); + success = true; break; } @@ -89,6 +98,8 @@ boolean Plugin_177(uint8_t function, struct EventStruct *event, String& string) { P177_PRESSURE_SCALE_FACTOR = getFormItemInt(F("scale"), 1000); P177_TEMPERATURE_OFFSET = getFormItemInt(F("tempoffset"), 0); + P177_GENERATE_EVENTS = isFormItemChecked(F("pevents")) ? 1 : 0; + P177_RAW_DATA = isFormItemChecked(F("praw")) ? 1 : 0; success = true; break; @@ -96,7 +107,7 @@ boolean Plugin_177(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - success = initPluginTaskData(event->TaskIndex, new (std::nothrow) P177_data_struct()); + success = initPluginTaskData(event->TaskIndex, new (std::nothrow) P177_data_struct(event)); break; } diff --git a/src/src/PluginStructs/P177_data_struct.cpp b/src/src/PluginStructs/P177_data_struct.cpp index f70caef01c..56795f3a46 100644 --- a/src/src/PluginStructs/P177_data_struct.cpp +++ b/src/src/PluginStructs/P177_data_struct.cpp @@ -3,6 +3,11 @@ #ifdef USES_P177 +P177_data_struct::P177_data_struct(struct EventStruct *event) { + _sendEvents = P177_GENERATE_EVENTS == 1; + _rawData = P177_RAW_DATA == 1; +} + P177_data_struct::~P177_data_struct() {} bool P177_data_struct::plugin_ten_per_second(struct EventStruct *event) { @@ -34,8 +39,8 @@ bool P177_data_struct::plugin_ten_per_second(struct EventStruct *event) { _sensorMode = P177_SensorMode_e::ReportingMode; } - uint32_t prData; - uint16_t tmData; + uint32_t prData{}; + uint32_t tmData{}; # if P177_LOG String log = F("P177 : RAW data: "); @@ -43,9 +48,9 @@ bool P177_data_struct::plugin_ten_per_second(struct EventStruct *event) { size_t i = 0; for (; i < 3; ++i) { // first 3 bytes pressure - prData << 8; + prData <<= 8; byteData = I2C_read8_reg(P177_I2C_ADDR, P177_DATA_REG_1 + i); - prData |= byteData; + prData += byteData; # if P177_LOG log += strformat(F("0x%02X "), byteData); # endif // if P177_LOG @@ -55,9 +60,9 @@ bool P177_data_struct::plugin_ten_per_second(struct EventStruct *event) { # endif // if P177_LOG for (; i < P177_DATA_BYTES; ++i) { // 2 more bytes temperature - tmData << 8; + tmData <<= 8; byteData = I2C_read8_reg(P177_I2C_ADDR, P177_DATA_REG_1 + i); - tmData |= byteData; + tmData += byteData; # if P177_LOG log += strformat(F("0x%02X "), byteData); # endif // if P177_LOG @@ -69,13 +74,20 @@ bool P177_data_struct::plugin_ten_per_second(struct EventStruct *event) { # endif // if P177_LOG _sensorMode = P177_SensorMode_e::IdleMode; // Start a new cycle + const bool pressureChanged = (prData != _rawPressure); - if ((prData == _rawPressure) && (tmData == _rawTemperature)) { + if (!pressureChanged && (tmData == _rawTemperature)) { return false; } _rawPressure = prData; _rawTemperature = tmData; _updated = true; + + if (_sendEvents && pressureChanged) { + plugin_read(event); + sendData(event); + _updated = true; // Is reset in plugin_read() + } return true; } @@ -86,22 +98,24 @@ bool P177_data_struct::plugin_read(struct EventStruct *event) { } if (_updated) { - float _pressure = _rawPressure; + float _pressure = 1.0f * _rawPressure; + float _temperature = 1.0f * _rawTemperature; - if (_rawPressure & 0x80000) { - _pressure = -1 * (_rawPressure & 0x3FFFF); + if (_rawPressure & 0x800000) { + _pressure = -1.0f * (_rawPressure & 0x7FFFFF); } - _pressure /= (0x80000 / P177_PRESSURE_SCALE_FACTOR); - - float _temperature = _rawTemperature; if (_rawTemperature & 0x8000) { - _temperature = -1 * (_rawTemperature & 0x3FFF); + _temperature = -1.0f * (_rawTemperature & 0x7FFF); } - _temperature /= 256; + _temperature /= 256.0f; // Shift 5 unused bits off + + if (!_rawData) { + _pressure /= (1.0f * (8388608.0 / P177_PRESSURE_SCALE_FACTOR)); - if (P177_TEMPERATURE_OFFSET != 0) { - _temperature += (P177_TEMPERATURE_OFFSET / 10); // In 0.1 degree steps + if (P177_TEMPERATURE_OFFSET != 0) { + _temperature += (P177_TEMPERATURE_OFFSET / 10.0f); // In 0.1 degree steps + } } UserVar.setFloat(event->TaskIndex, 0, _pressure); diff --git a/src/src/PluginStructs/P177_data_struct.h b/src/src/PluginStructs/P177_data_struct.h index 6816e49719..17fcf37885 100644 --- a/src/src/PluginStructs/P177_data_struct.h +++ b/src/src/PluginStructs/P177_data_struct.h @@ -19,6 +19,8 @@ # define P177_PRESSURE_SCALE_FACTOR PCONFIG_LONG(0) // Pressure scaling factor # define P177_TEMPERATURE_OFFSET PCONFIG(0) // Temperature compensation in 0.1 degree steps +# define P177_GENERATE_EVENTS PCONFIG(1) // To generate an event on Pressure change +# define P177_RAW_DATA PCONFIG(2) // Present raw data enum class P177_SensorMode_e : uint8_t { IdleMode = 0u, @@ -32,7 +34,7 @@ enum class P177_SensorMode_e : uint8_t { struct P177_data_struct : public PluginTaskData_base { public: - P177_data_struct() = default; + P177_data_struct(struct EventStruct *event); ~P177_data_struct(); bool plugin_ten_per_second(struct EventStruct *event); @@ -40,10 +42,12 @@ struct P177_data_struct : public PluginTaskData_base { bool plugin_read(struct EventStruct *event); uint32_t _rawPressure{}; - uint16_t _rawTemperature{}; + uint32_t _rawTemperature{}; P177_SensorMode_e _sensorMode = P177_SensorMode_e::IdleMode; uint8_t _cycles = 1; // Start cycle immediately bool _updated = false; + bool _sendEvents; + bool _rawData; }; #endif // ifdef USES_P177