From acdccaa962d02b24f81d9c6721c69b4337aa3d53 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Sun, 16 Jul 2023 12:47:13 +0200 Subject: [PATCH] added some nullptr validations and improved init --- src/Sensors.cpp | 18 ++++++++++++------ src/drivers/geiger.cpp | 6 +----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 28ac6ce1..5752df78 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -552,9 +552,9 @@ float Sensors::getUnitValue(UNIT unit) { case GAS: return gas; case CPM: - return rad->getTics(); + return getGeigerCPM(); case RAD: - return rad->getUSvh(); + return getGeigerMicroSievertHour(); case NH3: return nh3; case CO: @@ -1649,13 +1649,13 @@ void Sensors::resetAllVariables() { pres = 0.0; nh3 = 0; co = 0; - rad->clear(); + if (rad !=nullptr) rad->clear(); } // ######################################################################### void Sensors::geigerRead(){ - if(rad->read()){ + if(rad !=nullptr && rad->read()){ unitRegister(UNIT::CPM); unitRegister(UNIT::RAD); } @@ -1666,16 +1666,22 @@ void Sensors::geigerRead(){ */ void Sensors::enableGeigerSensor(int gpio){ sensorAnnounce(SENSORS::SCAJOE); + if (gpio < 0) { + if (devmode) Serial.printf("[W][SLIB] undefined Geiger pin\t: %i\r\t", gpio); + return; + } rad = new GEIGER(gpio,devmode); sensorRegister(SENSORS::SCAJOE); } uint32_t Sensors::getGeigerCPM(void) { - return rad->getTics(); + if (rad == nullptr) return 0; + else return rad->getTics(); } float Sensors::getGeigerMicroSievertHour(void) { - return rad->getUSvh(); + if (rad == nullptr) return 0; + else return rad->getUSvh(); } // ######################################################################### diff --git a/src/drivers/geiger.cpp b/src/drivers/geiger.cpp index 4194e4d3..2ea1bd74 100644 --- a/src/drivers/geiger.cpp +++ b/src/drivers/geiger.cpp @@ -31,11 +31,7 @@ void IRAM_ATTR onGeigerTimer() { // ######################################################################### // Initialize Geiger counter GEIGER::GEIGER(int gpio, bool debug) { -#ifdef ESP32 - if (gpio < 0) { - if (debug) Serial.println("[E][SLIB] undefined Geiger pin"); - return; - } +#ifdef ESP32 devmode = debug; tics_cnt = 0U; // tics in 1000ms tics_tot = 0U; // total tics since boot