From bd6480215bd4600427741d23f65e569e869a72f5 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Fri, 28 Jan 2022 14:53:48 -0700 Subject: [PATCH] Add isConnected(). Add test for readMeasurement() before returning reading. --- keywords.txt | 1 + src/SparkFun_SCD30_Arduino_Library.cpp | 37 ++++++++++++++++++-------- src/SparkFun_SCD30_Arduino_Library.h | 29 ++++++++++---------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/keywords.txt b/keywords.txt index 8e7d45b..e1a66d7 100644 --- a/keywords.txt +++ b/keywords.txt @@ -14,6 +14,7 @@ SCD30 KEYWORD1 SCD30 KEYWORD2 begin KEYWORD2 +isConnected KEYWORD2 enableDebugging KEYWORD2 beginMeasuring KEYWORD2 StopMeasurement KEYWORD2 diff --git a/src/SparkFun_SCD30_Arduino_Library.cpp b/src/SparkFun_SCD30_Arduino_Library.cpp index c61eca6..f83746f 100644 --- a/src/SparkFun_SCD30_Arduino_Library.cpp +++ b/src/SparkFun_SCD30_Arduino_Library.cpp @@ -58,16 +58,9 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin) _i2cPort->setClockStretchLimit(200000); #endif - uint16_t fwVer; - if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails. + if (isConnected() == false) return (false); - if (_printDebug == true) - { - _debugPort->print(F("SCD30 begin: got firmware version 0x")); - _debugPort->println(fwVer, HEX); - } - if (measBegin == false) // Exit now if measBegin is false return (true); @@ -83,6 +76,22 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin) return (false); // Something went wrong } +// Returns true if device responds to a firmware request +bool SCD30::isConnected() +{ + uint16_t fwVer; + if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails. + return (false); + + if (_printDebug == true) + { + _debugPort->print(F("Firmware version 0x")); + _debugPort->println(fwVer, HEX); + } + + return (true); +} + // Calling this function with nothing sets the debug port to Serial // You can also call it with other streams like Serial1, SerialUSB, etc. void SCD30::enableDebugging(Stream &debugPort) @@ -96,7 +105,11 @@ void SCD30::enableDebugging(Stream &debugPort) uint16_t SCD30::getCO2(void) { if (co2HasBeenReported == true) // Trigger a new read - readMeasurement(); // Pull in new co2, humidity, and temp into global vars + { + if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars + co2 = 0; // Failed to read sensor + } + co2HasBeenReported = true; return (uint16_t)co2; // Cut off decimal as co2 is 0 to 10,000 @@ -107,7 +120,8 @@ uint16_t SCD30::getCO2(void) float SCD30::getHumidity(void) { if (humidityHasBeenReported == true) // Trigger a new read - readMeasurement(); // Pull in new co2, humidity, and temp into global vars + if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars + humidity = 0; // Failed to read sensor humidityHasBeenReported = true; @@ -119,7 +133,8 @@ float SCD30::getHumidity(void) float SCD30::getTemperature(void) { if (temperatureHasBeenReported == true) // Trigger a new read - readMeasurement(); // Pull in new co2, humidity, and temp into global vars + if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars + temperature = 0; // Failed to read sensor temperatureHasBeenReported = true; diff --git a/src/SparkFun_SCD30_Arduino_Library.h b/src/SparkFun_SCD30_Arduino_Library.h index af39b12..c5e7d25 100644 --- a/src/SparkFun_SCD30_Arduino_Library.h +++ b/src/SparkFun_SCD30_Arduino_Library.h @@ -40,10 +40,10 @@ #include #endif -//The default I2C address for the SCD30 is 0x61. +// The default I2C address for the SCD30 is 0x61. #define SCD30_ADDRESS 0x61 -//Available commands +// Available commands #define COMMAND_CONTINUOUS_MEASUREMENT 0x0010 #define COMMAND_SET_MEASUREMENT_INTERVAL 0x4600 @@ -70,12 +70,13 @@ class SCD30 bool begin(bool autoCalibrate) { return begin(Wire, autoCalibrate); } #ifdef USE_TEENSY3_I2C_LIB - bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port + bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port #else - bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port + bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port #endif - void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used. + bool isConnected(); + void enableDebugging(Stream &debugPort = Serial); // Turn on debug printing. If user doesn't specify then Serial will be used. bool beginMeasuring(uint16_t pressureOffset); bool beginMeasuring(void); @@ -120,25 +121,25 @@ class SCD30 uint8_t computeCRC8(uint8_t data[], uint8_t len); private: - //Variables + // Variables #ifdef USE_TEENSY3_I2C_LIB - i2c_t3 *_i2cPort; //The generic connection to user's chosen I2C hardware + i2c_t3 *_i2cPort; // The generic connection to user's chosen I2C hardware #else - TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware + TwoWire *_i2cPort; // The generic connection to user's chosen I2C hardware #endif - //Global main datums + // Global main datums float co2 = 0; float temperature = 0; float humidity = 0; - //These track the staleness of the current data - //This allows us to avoid calling readMeasurement() every time individual datums are requested + // These track the staleness of the current data + // This allows us to avoid calling readMeasurement() every time individual datums are requested bool co2HasBeenReported = true; bool humidityHasBeenReported = true; bool temperatureHasBeenReported = true; - //Debug - Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial. - boolean _printDebug = false; //Flag to print debugging variables + // Debug + Stream *_debugPort; // The stream to send debug messages to if enabled. Usually Serial. + boolean _printDebug = false; // Flag to print debugging variables }; #endif