diff --git a/src/src/PluginStructs/P163_data_struct.cpp b/src/src/PluginStructs/P163_data_struct.cpp index 91e736d15..1173fb23a 100644 --- a/src/src/PluginStructs/P163_data_struct.cpp +++ b/src/src/PluginStructs/P163_data_struct.cpp @@ -25,11 +25,10 @@ P163_data_struct::~P163_data_struct() { * init *****************************************************/ bool P163_data_struct::init(struct EventStruct *event) { + delete sensor; // In case init() is called again sensor = new (std::nothrow) CG_RadSens(RS_DEFAULT_I2C_ADDRESS); if ((nullptr != sensor) && sensor->init()) { - initialized = true; - sensor->setLPmode(_lowPowerMode); sensor->setLedState(_ledState); @@ -39,6 +38,7 @@ bool P163_data_struct::init(struct EventStruct *event) { } } else { delete sensor; // Clean up if initialization fails + sensor = nullptr; addLog(LOG_LEVEL_ERROR, F("RadSens: Initialization failed!")); } return isInitialized(); @@ -59,6 +59,8 @@ bool P163_data_struct::plugin_read(struct EventStruct *event) { bool P163_data_struct::setOutputValues(struct EventStruct *event) { bool result = false; + if (nullptr == sensor) { return result; } + const uint32_t count = _readIncrement ? sensor->getNumberOfPulses() : sensor->getNumberOfNewPulses(); const uint32_t incCount = sensor->getNumberOfPulses(); const float iDynamic = sensor->getRadIntensyDynamic(); diff --git a/src/src/PluginStructs/P163_data_struct.h b/src/src/PluginStructs/P163_data_struct.h index a8d0db22e..b79d8ae08 100644 --- a/src/src/PluginStructs/P163_data_struct.h +++ b/src/src/PluginStructs/P163_data_struct.h @@ -39,7 +39,7 @@ public: bool plugin_write(struct EventStruct *event, String & string); bool isInitialized() const { - return initialized; + return nullptr != sensor; } private: @@ -57,8 +57,6 @@ private: bool _lowPowerMode = false; bool _ledState = true; bool _changeOnly = false; - - bool initialized = false; }; #endif // ifdef USES_P163