Skip to content

Commit

Permalink
[SenseAir] Keep SenseAir code in sync with PR letscontrolit#4550
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Apr 16, 2023
1 parent 8e66d61 commit 6253f44
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/_P052_SenseAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,4 @@ boolean Plugin_052(uint8_t function, struct EventStruct *event, String& string)
return success;
}

#endif // USES_P052
#endif // USES_P052
57 changes: 55 additions & 2 deletions src/src/PluginStructs/P052_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const __FlashStringHelper * P052_data_struct::Plugin_052_valuename(uint8_t value
F("ABC period"), F("abc_per"),
F("Error Status"), F("err")
};
const size_t index = (2* value_nr) + (displayString ? 0 : 1);
const size_t index = 2 * value_nr + (displayString ? 0 : 1);
constexpr size_t nrStrings = sizeof(strings) / sizeof(strings[0]);

if (index < nrStrings) {
Expand All @@ -38,5 +38,58 @@ const __FlashStringHelper * P052_data_struct::Plugin_052_valuename(uint8_t value
return F("");
}

void P052_data_struct::setABCperiod(int hours)
{
// Enable: write 1 ... 65534 to HR14 and bit1 set to 0 at HR19
// Disable: write 0 or 65535 to HR14 and bit1 set to 1 at HR19

#endif // ifdef USES_P052

// Read HR19
uint8_t errorcode = 0;
int value = modbus.readHoldingRegister(P052_HR19_METER_CONTROL, errorcode);

// Clear bit 1 in register and write back HR19
if (bitRead(value, 1)) {
bitClear(value, 1);
modbus.writeSingleRegister(P052_HR19_METER_CONTROL, value, errorcode);
}

// Read HR14 and verify desired ABC period
value = modbus.readHoldingRegister(P052_HR14_ABC_PERIOD, errorcode);

// If HR14 (ABC period) is not the desired period,
// write desired ABC period to HR14
if (value != hours) {
modbus.writeSingleRegister(P052_HR14_ABC_PERIOD, hours, errorcode);
}
}

uint32_t P052_data_struct::getSensorID()
{
uint8_t errorcode = 0;
const uint32_t sensorId = (modbus.readInputRegister(P052_IR30_SENSOR_ID_HIGH, errorcode) << 16) |
modbus.readInputRegister(P052_IR31_SENSOR_ID_LOW, errorcode);

if (errorcode == 0) {
return sensorId;
}
return 0;
}

bool P052_data_struct::readInputRegister(short addr, int& value)
{
uint8_t errorcode = 0;

value = modbus.readInputRegister(addr, errorcode);
return errorcode == 0;
}

bool P052_data_struct::readHoldingRegister(short addr, int& value)
{
uint8_t errorcode = 0;

value = modbus.readHoldingRegister(addr, errorcode);
return errorcode == 0;
}

#endif // ifdef USES_P052

0 comments on commit 6253f44

Please sign in to comment.