Skip to content

Commit bd64802

Browse files
committed
Add isConnected(). Add test for readMeasurement() before returning reading.
1 parent 9b41691 commit bd64802

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SCD30 KEYWORD1
1414

1515
SCD30 KEYWORD2
1616
begin KEYWORD2
17+
isConnected KEYWORD2
1718
enableDebugging KEYWORD2
1819
beginMeasuring KEYWORD2
1920
StopMeasurement KEYWORD2

src/SparkFun_SCD30_Arduino_Library.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,9 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin)
5858
_i2cPort->setClockStretchLimit(200000);
5959
#endif
6060

61-
uint16_t fwVer;
62-
if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails.
61+
if (isConnected() == false)
6362
return (false);
6463

65-
if (_printDebug == true)
66-
{
67-
_debugPort->print(F("SCD30 begin: got firmware version 0x"));
68-
_debugPort->println(fwVer, HEX);
69-
}
70-
7164
if (measBegin == false) // Exit now if measBegin is false
7265
return (true);
7366

@@ -83,6 +76,22 @@ bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin)
8376
return (false); // Something went wrong
8477
}
8578

79+
// Returns true if device responds to a firmware request
80+
bool SCD30::isConnected()
81+
{
82+
uint16_t fwVer;
83+
if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails.
84+
return (false);
85+
86+
if (_printDebug == true)
87+
{
88+
_debugPort->print(F("Firmware version 0x"));
89+
_debugPort->println(fwVer, HEX);
90+
}
91+
92+
return (true);
93+
}
94+
8695
// Calling this function with nothing sets the debug port to Serial
8796
// You can also call it with other streams like Serial1, SerialUSB, etc.
8897
void SCD30::enableDebugging(Stream &debugPort)
@@ -96,7 +105,11 @@ void SCD30::enableDebugging(Stream &debugPort)
96105
uint16_t SCD30::getCO2(void)
97106
{
98107
if (co2HasBeenReported == true) // Trigger a new read
99-
readMeasurement(); // Pull in new co2, humidity, and temp into global vars
108+
{
109+
if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
110+
co2 = 0; // Failed to read sensor
111+
}
112+
100113
co2HasBeenReported = true;
101114

102115
return (uint16_t)co2; // Cut off decimal as co2 is 0 to 10,000
@@ -107,7 +120,8 @@ uint16_t SCD30::getCO2(void)
107120
float SCD30::getHumidity(void)
108121
{
109122
if (humidityHasBeenReported == true) // Trigger a new read
110-
readMeasurement(); // Pull in new co2, humidity, and temp into global vars
123+
if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
124+
humidity = 0; // Failed to read sensor
111125

112126
humidityHasBeenReported = true;
113127

@@ -119,7 +133,8 @@ float SCD30::getHumidity(void)
119133
float SCD30::getTemperature(void)
120134
{
121135
if (temperatureHasBeenReported == true) // Trigger a new read
122-
readMeasurement(); // Pull in new co2, humidity, and temp into global vars
136+
if (readMeasurement() == false) // Pull in new co2, humidity, and temp into global vars
137+
temperature = 0; // Failed to read sensor
123138

124139
temperatureHasBeenReported = true;
125140

src/SparkFun_SCD30_Arduino_Library.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
#include <Wire.h>
4141
#endif
4242

43-
//The default I2C address for the SCD30 is 0x61.
43+
// The default I2C address for the SCD30 is 0x61.
4444
#define SCD30_ADDRESS 0x61
4545

46-
//Available commands
46+
// Available commands
4747

4848
#define COMMAND_CONTINUOUS_MEASUREMENT 0x0010
4949
#define COMMAND_SET_MEASUREMENT_INTERVAL 0x4600
@@ -70,12 +70,13 @@ class SCD30
7070

7171
bool begin(bool autoCalibrate) { return begin(Wire, autoCalibrate); }
7272
#ifdef USE_TEENSY3_I2C_LIB
73-
bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port
73+
bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port
7474
#else
75-
bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); //By default use Wire port
75+
bool begin(TwoWire &wirePort = Wire, bool autoCalibrate = false, bool measBegin = true); // By default use Wire port
7676
#endif
7777

78-
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used.
78+
bool isConnected();
79+
void enableDebugging(Stream &debugPort = Serial); // Turn on debug printing. If user doesn't specify then Serial will be used.
7980

8081
bool beginMeasuring(uint16_t pressureOffset);
8182
bool beginMeasuring(void);
@@ -120,25 +121,25 @@ class SCD30
120121
uint8_t computeCRC8(uint8_t data[], uint8_t len);
121122

122123
private:
123-
//Variables
124+
// Variables
124125
#ifdef USE_TEENSY3_I2C_LIB
125-
i2c_t3 *_i2cPort; //The generic connection to user's chosen I2C hardware
126+
i2c_t3 *_i2cPort; // The generic connection to user's chosen I2C hardware
126127
#else
127-
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
128+
TwoWire *_i2cPort; // The generic connection to user's chosen I2C hardware
128129
#endif
129-
//Global main datums
130+
// Global main datums
130131
float co2 = 0;
131132
float temperature = 0;
132133
float humidity = 0;
133134

134-
//These track the staleness of the current data
135-
//This allows us to avoid calling readMeasurement() every time individual datums are requested
135+
// These track the staleness of the current data
136+
// This allows us to avoid calling readMeasurement() every time individual datums are requested
136137
bool co2HasBeenReported = true;
137138
bool humidityHasBeenReported = true;
138139
bool temperatureHasBeenReported = true;
139140

140-
//Debug
141-
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
142-
boolean _printDebug = false; //Flag to print debugging variables
141+
// Debug
142+
Stream *_debugPort; // The stream to send debug messages to if enabled. Usually Serial.
143+
boolean _printDebug = false; // Flag to print debugging variables
143144
};
144145
#endif

0 commit comments

Comments
 (0)