Skip to content

Commit

Permalink
Add isConnected(). Add test for readMeasurement() before returning re…
Browse files Browse the repository at this point in the history
…ading.
  • Loading branch information
nseidle committed Jan 28, 2022
1 parent 9b41691 commit bd64802
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SCD30 KEYWORD1

SCD30 KEYWORD2
begin KEYWORD2
isConnected KEYWORD2
enableDebugging KEYWORD2
beginMeasuring KEYWORD2
StopMeasurement KEYWORD2
Expand Down
37 changes: 26 additions & 11 deletions src/SparkFun_SCD30_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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;

Expand Down
29 changes: 15 additions & 14 deletions src/SparkFun_SCD30_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
#include <Wire.h>
#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
Expand All @@ -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);
Expand Down Expand Up @@ -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

0 comments on commit bd64802

Please sign in to comment.