@@ -33,14 +33,10 @@ SCD30::SCD30(void)
33
33
}
34
34
35
35
// Initialize the Serial port
36
- boolean SCD30::begin (TwoWire &wirePort)
36
+ bool SCD30::begin (TwoWire &wirePort)
37
37
{
38
38
_i2cPort = &wirePort; // Grab which port the user wants us to use
39
39
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
-
44
40
/* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)!
45
41
* The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
46
42
*
@@ -54,17 +50,17 @@ boolean SCD30::begin(TwoWire &wirePort)
54
50
* and now wait for clock stretch to be controlled by the client.
55
51
*/
56
52
57
- #if defined(ARDUINO_ARCH_ESP8266)
58
- _i2cPort->setClockStretchLimit (200000 );
59
- #endif
53
+ #if defined(ARDUINO_ARCH_ESP8266)
54
+ _i2cPort->setClockStretchLimit (200000 );
55
+ #endif
60
56
61
57
// Check for device to respond correctly
62
- if (beginMeasuring () == true ) // Start continuous measurements
58
+ if (beginMeasuring () == true ) // Start continuous measurements
63
59
{
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 );
68
64
}
69
65
70
66
return (false ); // Something went wrong
@@ -75,7 +71,7 @@ boolean SCD30::begin(TwoWire &wirePort)
75
71
uint16_t SCD30::getCO2 (void )
76
72
{
77
73
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
79
75
80
76
co2HasBeenReported = true ;
81
77
@@ -87,7 +83,7 @@ uint16_t SCD30::getCO2(void)
87
83
float SCD30::getHumidity (void )
88
84
{
89
85
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
91
87
92
88
humidityHasBeenReported = true ;
93
89
@@ -99,15 +95,15 @@ float SCD30::getHumidity(void)
99
95
float SCD30::getTemperature (void )
100
96
{
101
97
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
103
99
104
100
temperatureHasBeenReported = true ;
105
101
106
102
return temperature;
107
103
}
108
104
109
105
// Enables or disables the ASC
110
- bool SCD30::setAutoSelfCalibration (boolean enable)
106
+ bool SCD30::setAutoSelfCalibration (bool enable)
111
107
{
112
108
if (enable)
113
109
return sendCommand (COMMAND_AUTOMATIC_SELF_CALIBRATION, 1 ); // Activate continuous ASC
@@ -119,7 +115,8 @@ bool SCD30::setAutoSelfCalibration(boolean enable)
119
115
// The reference CO2 concentration has to be within the range 400 ppm ≤ cref(CO2) ≤ 2000 ppm.
120
116
bool SCD30::setForcedRecalibrationFactor (uint16_t concentration)
121
117
{
122
- if (concentration < 400 || concentration > 2000 ) {
118
+ if (concentration < 400 || concentration > 2000 )
119
+ {
123
120
return false ; // Error check.
124
121
}
125
122
return sendCommand (COMMAND_SET_FORCED_RECALIBRATION_FACTOR, concentration);
@@ -129,7 +126,7 @@ bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
129
126
float SCD30::getTemperatureOffset ()
130
127
{
131
128
uint16_t response = readRegister (COMMAND_SET_TEMPERATURE_OFFSET);
132
- return (float ) response / 100 ;
129
+ return (float )response / 100 ;
133
130
}
134
131
135
132
// Set the temperature offset. See 1.3.8.
@@ -149,7 +146,7 @@ bool SCD30::setAltitudeCompensation(uint16_t altitude)
149
146
// mbar can be 700 to 1200
150
147
bool SCD30::setAmbientPressure (uint16_t pressure_mbar)
151
148
{
152
- if (pressure_mbar < 700 || pressure_mbar > 1200 )
149
+ if (pressure_mbar < 700 || pressure_mbar > 1200 )
153
150
{
154
151
return false ;
155
152
}
@@ -161,15 +158,15 @@ bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
161
158
// is powered down while continuous measurement mode is active SCD30 will measure
162
159
// continuously after repowering without sending the measurement command.
163
160
// Returns true if successful
164
- boolean SCD30::beginMeasuring (uint16_t pressureOffset)
161
+ bool SCD30::beginMeasuring (uint16_t pressureOffset)
165
162
{
166
- return (sendCommand (COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
163
+ return (sendCommand (COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
167
164
}
168
165
169
166
// Overload - no pressureOffset
170
- boolean SCD30::beginMeasuring (void )
167
+ bool SCD30::beginMeasuring (void )
171
168
{
172
- return (beginMeasuring (0 ));
169
+ return (beginMeasuring (0 ));
173
170
}
174
171
175
172
// Sets interval between measurements
@@ -180,18 +177,19 @@ bool SCD30::setMeasurementInterval(uint16_t interval)
180
177
}
181
178
182
179
// Returns true when data is available
183
- boolean SCD30::dataAvailable ()
180
+ bool SCD30::dataAvailable ()
184
181
{
185
182
uint16_t response = readRegister (COMMAND_GET_DATA_READY);
186
183
187
- if (response == 1 ) return (true );
184
+ if (response == 1 )
185
+ return (true );
188
186
return (false );
189
187
}
190
188
191
189
// Get 18 bytes from SCD30
192
190
// Updates global variables with floats
193
191
// Returns true if success
194
- boolean SCD30::readMeasurement ()
192
+ bool SCD30::readMeasurement ()
195
193
{
196
194
// Verify we have data from the sensor
197
195
if (dataAvailable () == false )
@@ -202,7 +200,7 @@ boolean SCD30::readMeasurement()
202
200
uint32_t tempTemperature = 0 ;
203
201
204
202
_i2cPort->beginTransmission (SCD30_ADDRESS);
205
- _i2cPort->write (COMMAND_READ_MEASUREMENT >> 8 ); // MSB
203
+ _i2cPort->write (COMMAND_READ_MEASUREMENT >> 8 ); // MSB
206
204
_i2cPort->write (COMMAND_READ_MEASUREMENT & 0xFF ); // LSB
207
205
if (_i2cPort->endTransmission () != 0 )
208
206
return (0 ); // Sensor did not ACK
@@ -212,52 +210,56 @@ boolean SCD30::readMeasurement()
212
210
if (_i2cPort->available ())
213
211
{
214
212
byte bytesToCrc[2 ];
215
- for (byte x = 0 ; x < 18 ; x++)
213
+ for (byte x = 0 ; x < 18 ; x++)
216
214
{
217
215
byte incoming = _i2cPort->read ();
218
216
219
217
switch (x)
220
218
{
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 ;
253
252
}
254
253
}
255
- } else {
254
+ }
255
+ else
256
+ {
256
257
Serial.printf (" No SCD30 data found from I2C, i2c claims we should receive %u bytes\n " , receivedBytes);
257
258
return false ;
258
259
}
259
260
260
- if (error) {
261
+ if (error)
262
+ {
261
263
Serial.println (" Encountered error reading SCD30 data." );
262
264
return false ;
263
265
}
@@ -278,7 +280,7 @@ boolean SCD30::readMeasurement()
278
280
uint16_t SCD30::readRegister (uint16_t registerAddress)
279
281
{
280
282
_i2cPort->beginTransmission (SCD30_ADDRESS);
281
- _i2cPort->write (registerAddress >> 8 ); // MSB
283
+ _i2cPort->write (registerAddress >> 8 ); // MSB
282
284
_i2cPort->write (registerAddress & 0xFF ); // LSB
283
285
if (_i2cPort->endTransmission () != 0 )
284
286
return (0 ); // Sensor did not ACK
@@ -294,17 +296,17 @@ uint16_t SCD30::readRegister(uint16_t registerAddress)
294
296
}
295
297
296
298
// 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)
298
300
{
299
301
uint8_t data[2 ];
300
302
data[0 ] = arguments >> 8 ;
301
303
data[1 ] = arguments & 0xFF ;
302
304
uint8_t crc = computeCRC8 (data, 2 ); // Calc CRC on the arguments only, not the command
303
305
304
306
_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
308
310
_i2cPort->write (arguments & 0xFF ); // LSB
309
311
_i2cPort->write (crc);
310
312
if (_i2cPort->endTransmission () != 0 )
@@ -314,10 +316,10 @@ boolean SCD30::sendCommand(uint16_t command, uint16_t arguments)
314
316
}
315
317
316
318
// Sends just a command, no arguments, no CRC
317
- boolean SCD30::sendCommand (uint16_t command)
319
+ bool SCD30::sendCommand (uint16_t command)
318
320
{
319
321
_i2cPort->beginTransmission (SCD30_ADDRESS);
320
- _i2cPort->write (command >> 8 ); // MSB
322
+ _i2cPort->write (command >> 8 ); // MSB
321
323
_i2cPort->write (command & 0xFF ); // LSB
322
324
if (_i2cPort->endTransmission () != 0 )
323
325
return (false ); // Sensor did not ACK
@@ -334,11 +336,11 @@ uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len)
334
336
{
335
337
uint8_t crc = 0xFF ; // Init with 0xFF
336
338
337
- for (uint8_t x = 0 ; x < len ; x++)
339
+ for (uint8_t x = 0 ; x < len; x++)
338
340
{
339
341
crc ^= data[x]; // XOR-in the next input byte
340
342
341
- for (uint8_t i = 0 ; i < 8 ; i++)
343
+ for (uint8_t i = 0 ; i < 8 ; i++)
342
344
{
343
345
if ((crc & 0x80 ) != 0 )
344
346
crc = (uint8_t )((crc << 1 ) ^ 0x31 );
0 commit comments