Skip to content

Commit

Permalink
[P177] Corrections in data initialization, handling and calculation,
Browse files Browse the repository at this point in the history
add settings to generate events or use raw data
  • Loading branch information
tonhuisman committed Feb 23, 2025
1 parent 398ed46 commit 4a2d0e3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
19 changes: 15 additions & 4 deletions src/_P177_XDB_pressure.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -89,14 +98,16 @@ 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;
}

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;
}

Expand Down
48 changes: 31 additions & 17 deletions src/src/PluginStructs/P177_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -34,18 +39,18 @@ 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: ");
# endif // if P177_LOG
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
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/src/PluginStructs/P177_data_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,18 +34,20 @@ 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);

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
Expand Down

0 comments on commit 4a2d0e3

Please sign in to comment.