Skip to content

Commit efe95d1

Browse files
author
Nathan Seidle
committed
Remove i2c begin from begin(). This causes the I2C hardware to reset. This may break code.
Other changes: change from boolean to bool typedef for better cross compiler support.
1 parent 703f658 commit efe95d1

File tree

2 files changed

+94
-92
lines changed

2 files changed

+94
-92
lines changed

src/SparkFun_SCD30_Arduino_Library.cpp

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ SCD30::SCD30(void)
3333
}
3434

3535
//Initialize the Serial port
36-
boolean SCD30::begin(TwoWire &wirePort)
36+
bool SCD30::begin(TwoWire &wirePort)
3737
{
3838
_i2cPort = &wirePort; //Grab which port the user wants us to use
3939

40-
//We expect caller to begin their I2C port, with the speed of their choice external to the library
41-
//But if they forget, we start the hardware here.
42-
_i2cPort->begin();
43-
4440
/* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)!
4541
* The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
4642
*
@@ -54,17 +50,17 @@ boolean SCD30::begin(TwoWire &wirePort)
5450
* and now wait for clock stretch to be controlled by the client.
5551
*/
5652

57-
#if defined(ARDUINO_ARCH_ESP8266)
58-
_i2cPort->setClockStretchLimit(200000);
59-
#endif
53+
#if defined(ARDUINO_ARCH_ESP8266)
54+
_i2cPort->setClockStretchLimit(200000);
55+
#endif
6056

6157
//Check for device to respond correctly
62-
if(beginMeasuring() == true) //Start continuous measurements
58+
if (beginMeasuring() == true) //Start continuous measurements
6359
{
64-
setMeasurementInterval(2); //2 seconds between measurements
65-
setAutoSelfCalibration(true); //Enable auto-self-calibration
66-
67-
return (true);
60+
setMeasurementInterval(2); //2 seconds between measurements
61+
setAutoSelfCalibration(true); //Enable auto-self-calibration
62+
63+
return (true);
6864
}
6965

7066
return (false); //Something went wrong
@@ -75,7 +71,7 @@ boolean SCD30::begin(TwoWire &wirePort)
7571
uint16_t SCD30::getCO2(void)
7672
{
7773
if (co2HasBeenReported == true) //Trigger a new read
78-
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
74+
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
7975

8076
co2HasBeenReported = true;
8177

@@ -87,7 +83,7 @@ uint16_t SCD30::getCO2(void)
8783
float SCD30::getHumidity(void)
8884
{
8985
if (humidityHasBeenReported == true) //Trigger a new read
90-
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
86+
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
9187

9288
humidityHasBeenReported = true;
9389

@@ -99,15 +95,15 @@ float SCD30::getHumidity(void)
9995
float SCD30::getTemperature(void)
10096
{
10197
if (temperatureHasBeenReported == true) //Trigger a new read
102-
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
98+
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
10399

104100
temperatureHasBeenReported = true;
105101

106102
return temperature;
107103
}
108104

109105
//Enables or disables the ASC
110-
bool SCD30::setAutoSelfCalibration(boolean enable)
106+
bool SCD30::setAutoSelfCalibration(bool enable)
111107
{
112108
if (enable)
113109
return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 1); //Activate continuous ASC
@@ -119,7 +115,8 @@ bool SCD30::setAutoSelfCalibration(boolean enable)
119115
//The reference CO2 concentration has to be within the range 400 ppm ≤ cref(CO2) ≤ 2000 ppm.
120116
bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
121117
{
122-
if(concentration < 400 || concentration > 2000) {
118+
if (concentration < 400 || concentration > 2000)
119+
{
123120
return false; //Error check.
124121
}
125122
return sendCommand(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, concentration);
@@ -129,7 +126,7 @@ bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
129126
float SCD30::getTemperatureOffset()
130127
{
131128
uint16_t response = readRegister(COMMAND_SET_TEMPERATURE_OFFSET);
132-
return (float) response / 100;
129+
return (float)response / 100;
133130
}
134131

135132
//Set the temperature offset. See 1.3.8.
@@ -149,7 +146,7 @@ bool SCD30::setAltitudeCompensation(uint16_t altitude)
149146
//mbar can be 700 to 1200
150147
bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
151148
{
152-
if(pressure_mbar < 700 || pressure_mbar > 1200)
149+
if (pressure_mbar < 700 || pressure_mbar > 1200)
153150
{
154151
return false;
155152
}
@@ -161,15 +158,15 @@ bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
161158
//is powered down while continuous measurement mode is active SCD30 will measure
162159
//continuously after repowering without sending the measurement command.
163160
//Returns true if successful
164-
boolean SCD30::beginMeasuring(uint16_t pressureOffset)
161+
bool SCD30::beginMeasuring(uint16_t pressureOffset)
165162
{
166-
return(sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
163+
return (sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
167164
}
168165

169166
//Overload - no pressureOffset
170-
boolean SCD30::beginMeasuring(void)
167+
bool SCD30::beginMeasuring(void)
171168
{
172-
return(beginMeasuring(0));
169+
return (beginMeasuring(0));
173170
}
174171

175172
//Sets interval between measurements
@@ -180,18 +177,19 @@ bool SCD30::setMeasurementInterval(uint16_t interval)
180177
}
181178

182179
//Returns true when data is available
183-
boolean SCD30::dataAvailable()
180+
bool SCD30::dataAvailable()
184181
{
185182
uint16_t response = readRegister(COMMAND_GET_DATA_READY);
186183

187-
if (response == 1) return (true);
184+
if (response == 1)
185+
return (true);
188186
return (false);
189187
}
190188

191189
//Get 18 bytes from SCD30
192190
//Updates global variables with floats
193191
//Returns true if success
194-
boolean SCD30::readMeasurement()
192+
bool SCD30::readMeasurement()
195193
{
196194
//Verify we have data from the sensor
197195
if (dataAvailable() == false)
@@ -202,7 +200,7 @@ boolean SCD30::readMeasurement()
202200
uint32_t tempTemperature = 0;
203201

204202
_i2cPort->beginTransmission(SCD30_ADDRESS);
205-
_i2cPort->write(COMMAND_READ_MEASUREMENT >> 8); //MSB
203+
_i2cPort->write(COMMAND_READ_MEASUREMENT >> 8); //MSB
206204
_i2cPort->write(COMMAND_READ_MEASUREMENT & 0xFF); //LSB
207205
if (_i2cPort->endTransmission() != 0)
208206
return (0); //Sensor did not ACK
@@ -212,52 +210,56 @@ boolean SCD30::readMeasurement()
212210
if (_i2cPort->available())
213211
{
214212
byte bytesToCrc[2];
215-
for (byte x = 0 ; x < 18 ; x++)
213+
for (byte x = 0; x < 18; x++)
216214
{
217215
byte incoming = _i2cPort->read();
218216

219217
switch (x)
220218
{
221-
case 0:
222-
case 1:
223-
case 3:
224-
case 4:
225-
tempCO2 <<= 8;
226-
tempCO2 |= incoming;
227-
bytesToCrc[x%3] = incoming;
228-
break;
229-
case 6:
230-
case 7:
231-
case 9:
232-
case 10:
233-
tempTemperature <<= 8;
234-
tempTemperature |= incoming;
235-
bytesToCrc[x%3] = incoming;
236-
break;
237-
case 12:
238-
case 13:
239-
case 15:
240-
case 16:
241-
tempHumidity <<= 8;
242-
tempHumidity |= incoming;
243-
bytesToCrc[x%3] = incoming;
244-
break;
245-
default:
246-
//Validate CRC
247-
const uint8_t foundCrc = computeCRC8(bytesToCrc, 2);
248-
if (foundCrc != incoming) {
249-
Serial.printf("Found CRC in byte %u, expected %u, got %u\n", x, incoming, foundCrc);
250-
error = true;
251-
}
252-
break;
219+
case 0:
220+
case 1:
221+
case 3:
222+
case 4:
223+
tempCO2 <<= 8;
224+
tempCO2 |= incoming;
225+
bytesToCrc[x % 3] = incoming;
226+
break;
227+
case 6:
228+
case 7:
229+
case 9:
230+
case 10:
231+
tempTemperature <<= 8;
232+
tempTemperature |= incoming;
233+
bytesToCrc[x % 3] = incoming;
234+
break;
235+
case 12:
236+
case 13:
237+
case 15:
238+
case 16:
239+
tempHumidity <<= 8;
240+
tempHumidity |= incoming;
241+
bytesToCrc[x % 3] = incoming;
242+
break;
243+
default:
244+
//Validate CRC
245+
const uint8_t foundCrc = computeCRC8(bytesToCrc, 2);
246+
if (foundCrc != incoming)
247+
{
248+
Serial.printf("Found CRC in byte %u, expected %u, got %u\n", x, incoming, foundCrc);
249+
error = true;
250+
}
251+
break;
253252
}
254253
}
255-
} else {
254+
}
255+
else
256+
{
256257
Serial.printf("No SCD30 data found from I2C, i2c claims we should receive %u bytes\n", receivedBytes);
257258
return false;
258259
}
259260

260-
if (error) {
261+
if (error)
262+
{
261263
Serial.println("Encountered error reading SCD30 data.");
262264
return false;
263265
}
@@ -278,7 +280,7 @@ boolean SCD30::readMeasurement()
278280
uint16_t SCD30::readRegister(uint16_t registerAddress)
279281
{
280282
_i2cPort->beginTransmission(SCD30_ADDRESS);
281-
_i2cPort->write(registerAddress >> 8); //MSB
283+
_i2cPort->write(registerAddress >> 8); //MSB
282284
_i2cPort->write(registerAddress & 0xFF); //LSB
283285
if (_i2cPort->endTransmission() != 0)
284286
return (0); //Sensor did not ACK
@@ -294,17 +296,17 @@ uint16_t SCD30::readRegister(uint16_t registerAddress)
294296
}
295297

296298
//Sends a command along with arguments and CRC
297-
boolean SCD30::sendCommand(uint16_t command, uint16_t arguments)
299+
bool SCD30::sendCommand(uint16_t command, uint16_t arguments)
298300
{
299301
uint8_t data[2];
300302
data[0] = arguments >> 8;
301303
data[1] = arguments & 0xFF;
302304
uint8_t crc = computeCRC8(data, 2); //Calc CRC on the arguments only, not the command
303305

304306
_i2cPort->beginTransmission(SCD30_ADDRESS);
305-
_i2cPort->write(command >> 8); //MSB
306-
_i2cPort->write(command & 0xFF); //LSB
307-
_i2cPort->write(arguments >> 8); //MSB
307+
_i2cPort->write(command >> 8); //MSB
308+
_i2cPort->write(command & 0xFF); //LSB
309+
_i2cPort->write(arguments >> 8); //MSB
308310
_i2cPort->write(arguments & 0xFF); //LSB
309311
_i2cPort->write(crc);
310312
if (_i2cPort->endTransmission() != 0)
@@ -314,10 +316,10 @@ boolean SCD30::sendCommand(uint16_t command, uint16_t arguments)
314316
}
315317

316318
//Sends just a command, no arguments, no CRC
317-
boolean SCD30::sendCommand(uint16_t command)
319+
bool SCD30::sendCommand(uint16_t command)
318320
{
319321
_i2cPort->beginTransmission(SCD30_ADDRESS);
320-
_i2cPort->write(command >> 8); //MSB
322+
_i2cPort->write(command >> 8); //MSB
321323
_i2cPort->write(command & 0xFF); //LSB
322324
if (_i2cPort->endTransmission() != 0)
323325
return (false); //Sensor did not ACK
@@ -334,11 +336,11 @@ uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len)
334336
{
335337
uint8_t crc = 0xFF; //Init with 0xFF
336338

337-
for (uint8_t x = 0 ; x < len ; x++)
339+
for (uint8_t x = 0; x < len; x++)
338340
{
339341
crc ^= data[x]; // XOR-in the next input byte
340342

341-
for (uint8_t i = 0 ; i < 8 ; i++)
343+
for (uint8_t i = 0; i < 8; i++)
342344
{
343345
if ((crc & 0x80) != 0)
344346
crc = (uint8_t)((crc << 1) ^ 0x31);

src/SparkFun_SCD30_Arduino_Library.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
along with this program. If not, see <http://www.gnu.org/licenses/>.
2626
*/
2727

28-
#pragma once
28+
#ifndef __SparkFun_SCD30_ARDUINO_LIBARARY_H__
29+
#define __SparkFun_SCD30_ARDUINO_LIBARARY_H__
2930

3031
#include "Arduino.h"
31-
3232
#include <Wire.h>
3333

3434
//The default I2C address for the SCD30 is 0x61.
@@ -47,13 +47,13 @@
4747

4848
class SCD30
4949
{
50-
public:
51-
SCD30(void);
50+
public:
51+
SCD30(void);
5252

53-
boolean begin(TwoWire &wirePort = Wire); //By default use Wire port
53+
bool begin(TwoWire &wirePort = Wire); //By default use Wire port
5454

55-
boolean beginMeasuring(uint16_t pressureOffset);
56-
boolean beginMeasuring(void);
55+
bool beginMeasuring(uint16_t pressureOffset);
56+
bool beginMeasuring(void);
5757

5858
uint16_t getCO2(void);
5959
float getHumidity(void);
@@ -63,33 +63,33 @@ class SCD30
6363
bool setMeasurementInterval(uint16_t interval);
6464
bool setAmbientPressure(uint16_t pressure_mbar);
6565
bool setAltitudeCompensation(uint16_t altitude);
66-
bool setAutoSelfCalibration(boolean enable);
66+
bool setAutoSelfCalibration(bool enable);
6767
bool setForcedRecalibrationFactor(uint16_t concentration);
6868
bool setTemperatureOffset(float tempOffset);
6969

70-
boolean dataAvailable();
71-
boolean readMeasurement();
70+
bool dataAvailable();
71+
bool readMeasurement();
7272

73-
boolean sendCommand(uint16_t command, uint16_t arguments);
74-
boolean sendCommand(uint16_t command);
73+
bool sendCommand(uint16_t command, uint16_t arguments);
74+
bool sendCommand(uint16_t command);
7575

7676
uint16_t readRegister(uint16_t registerAddress);
7777

7878
uint8_t computeCRC8(uint8_t data[], uint8_t len);
7979

80-
private:
81-
//Variables
82-
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
80+
private:
81+
//Variables
82+
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
8383

8484
//Global main datums
8585
float co2 = 0;
8686
float temperature = 0;
8787
float humidity = 0;
88-
88+
8989
//These track the staleness of the current data
9090
//This allows us to avoid calling readMeasurement() every time individual datums are requested
91-
boolean co2HasBeenReported = true;
92-
boolean humidityHasBeenReported = true;
93-
boolean temperatureHasBeenReported = true;
94-
91+
bool co2HasBeenReported = true;
92+
bool humidityHasBeenReported = true;
93+
bool temperatureHasBeenReported = true;
9594
};
95+
#endif

0 commit comments

Comments
 (0)