39
39
40
40
#include " SparkFun_SGP30_Library.h"
41
41
42
- const uint8_t init_air_quality[2 ] = {0x20 , 0x03 };
43
- const uint8_t measure_air_quality[2 ] = {0x20 , 0x08 };
44
- const uint8_t get_baseline[2 ] = {0x20 , 0x15 };
45
- const uint8_t set_baseline[2 ] = {0x20 , 0x1E };
46
- const uint8_t set_humidity[2 ] = {0x20 , 0x61 };
47
- const uint8_t measure_test[2 ] = {0x20 , 0x32 };
48
- const uint8_t get_feature_set_version[2 ] = {0x20 , 0x2F };
49
- const uint8_t get_serial_id[2 ] = {0x36 , 0x82 };
50
- const uint8_t measure_raw_signals[2 ] = {0x20 , 0x50 };
42
+ // const uint8_t init_air_quality[2] = {0x20, 0x03};
43
+ // const uint8_t measure_air_quality[2] = {0x20, 0x08};
44
+ // const uint8_t get_baseline[2] = {0x20, 0x15};
45
+ // const uint8_t set_baseline[2] = {0x20, 0x1E};
46
+ // const uint8_t set_humidity[2] = {0x20, 0x61};
47
+ // const uint8_t measure_test[2] = {0x20, 0x32};
48
+ // const uint8_t get_feature_set_version[2] = {0x20, 0x2F};
49
+ // const uint8_t get_serial_id[2] = {0x36, 0x82};
50
+ // const uint8_t measure_raw_signals[2] = {0x20, 0x50};
51
51
52
52
// Constructor
53
53
SGP30::SGP30 () {
@@ -65,6 +65,7 @@ SGP30::SGP30() {
65
65
}
66
66
67
67
// Start I2C communication using specified port
68
+ // Returns SUCCESS if successful or other error code if unsuccessful
68
69
SGP30ERR SGP30::begin (TwoWire &wirePort) {
69
70
_i2cPort = &wirePort; // Grab which port the user wants us to use
70
71
_i2cPort->begin ();
@@ -86,7 +87,7 @@ void SGP30::initAirQuality(void) {
86
87
// Call in regular intervals of 1 second to maintain synamic baseline calculations
87
88
// CO2 returned in ppm, Total Volatile Organic Compounds (TVOC) returned in ppb
88
89
// Will give fixed values of CO2=400 and TVOC=0 for first 15 seconds after init
89
- // returns ERR_BAD_CRC if CRC8 check failed and SUCCESS if successful
90
+ // Returns SUCCESS if successful or other error code if unsuccessful
90
91
SGP30ERR SGP30::measureAirQuality (void ) {
91
92
_i2cPort->beginTransmission (_SGP30Address);
92
93
_i2cPort->write (measure_air_quality, 2 ); // command to measure air quality
@@ -110,7 +111,6 @@ SGP30ERR SGP30::measureAirQuality(void) {
110
111
_TVOC |= _i2cPort->read (); // store LSB in TVOC
111
112
checkSum = _i2cPort->read (); // verify checksum
112
113
if (checkSum != _CRC8 (_TVOC)) return ERR_BAD_CRC; // checksum failed
113
- _i2cPort->endTransmission ();
114
114
CO2 = _CO2; // publish valid data
115
115
TVOC = _TVOC; // publish valid data
116
116
return SUCCESS;
@@ -121,7 +121,7 @@ SGP30ERR SGP30::measureAirQuality(void) {
121
121
// Save baseline periodically to non volatile memory
122
122
// (like EEPROM) to restore after new power up or
123
123
// after soft reset using setBaseline();
124
- // returns ERR_BAD_CRC if CRC8 check failed and SUCCESS if successful
124
+ // Returns SUCCESS if successful or other error code if unsuccessful
125
125
SGP30ERR SGP30::getBaseline (void ) {
126
126
_i2cPort->beginTransmission (_SGP30Address);
127
127
_i2cPort->write (get_baseline, 2 );
@@ -145,7 +145,6 @@ SGP30ERR SGP30::getBaseline(void) {
145
145
_baselineTVOC |= _i2cPort->read (); // store LSB in _baselineTVOC
146
146
checkSum = _i2cPort->read (); // verify checksum
147
147
if (checkSum != _CRC8 (_baselineTVOC)) return ERR_BAD_CRC; // checksum failed
148
- _i2cPort->endTransmission ();
149
148
baselineCO2 = _baselineCO2; // publish valid data
150
149
baselineTVOC = _baselineTVOC; // publish valid data
151
150
return SUCCESS;
@@ -183,7 +182,7 @@ void SGP30::setHumidity(uint16_t humidity) {
183
182
}
184
183
185
184
// gives feature set version number (see data sheet)
186
- // returns ERR_BAD_CRC if CRC8 check failed and SUCCESS if successful
185
+ // Returns SUCCESS if successful or other error code if unsuccessful
187
186
SGP30ERR SGP30::getFeatureSetVersion (void ) {
188
187
_i2cPort->beginTransmission (_SGP30Address);
189
188
_i2cPort->write (get_feature_set_version, 2 ); // command to get feature version
@@ -203,13 +202,13 @@ SGP30ERR SGP30::getFeatureSetVersion(void) {
203
202
_featureSetVersion |= _i2cPort->read (); // store LSB in featureSetVersion
204
203
uint8_t checkSum = _i2cPort->read (); // verify checksum
205
204
if (checkSum != _CRC8 (_featureSetVersion)) return ERR_BAD_CRC; // checksum failed
206
- _i2cPort->endTransmission ();
207
205
featureSetVersion = _featureSetVersion; // publish valid data
208
206
return SUCCESS;
209
207
}
210
208
211
209
// Intended for part verification and testing
212
210
// these raw signals are used as inputs to the onchip calibrations and algorithms
211
+ // Returns SUCCESS if successful or other error code if unsuccessful
213
212
SGP30ERR SGP30::measureRawSignals (void ) {
214
213
_i2cPort->beginTransmission (_SGP30Address);
215
214
_i2cPort->write (measure_raw_signals, 2 ); // command to measure raw signals
@@ -233,7 +232,6 @@ SGP30ERR SGP30::measureRawSignals(void) {
233
232
_ethanol |= _i2cPort->read (); // store LSB in ethanol
234
233
checkSum = _i2cPort->read (); // verify checksum
235
234
if (checkSum != _CRC8 (_ethanol)) return ERR_BAD_CRC; // checksum failed
236
- _i2cPort->endTransmission ();
237
235
H2 = _H2; // publish valid data
238
236
ethanol = _ethanol; // publish valid data
239
237
return SUCCESS;
@@ -248,7 +246,7 @@ void SGP30::generalCallReset(void) {
248
246
}
249
247
250
248
// readout of serial ID register can identify chip and verify sensor presence
251
- // returns ERR_BAD_CRC if CRC8 check failed and SUCCESS if successful
249
+ // Returns SUCCESS if successful or other error code if unsuccessful
252
250
SGP30ERR SGP30::getSerialID (void ) {
253
251
_i2cPort->beginTransmission (_SGP30Address);
254
252
_i2cPort->write (get_serial_id, 2 ); // command to get serial ID
@@ -267,22 +265,21 @@ SGP30ERR SGP30::getSerialID(void) {
267
265
_serialID1 = _i2cPort->read () << 8 ; // store MSB to top of _serialID1
268
266
_serialID1 |= _i2cPort->read (); // store next byte in _serialID1
269
267
uint8_t checkSum1 = _i2cPort->read (); // verify checksum
268
+ if (checkSum1 != _CRC8 (_serialID1)) return ERR_BAD_CRC; // checksum failed
270
269
_serialID2 = _i2cPort->read () << 8 ; // store next byte to top of _serialID2
271
270
_serialID2 |= _i2cPort->read (); // store next byte in _serialID2
272
271
uint8_t checkSum2 = _i2cPort->read (); // verify checksum
272
+ if (checkSum2 != _CRC8 (_serialID2)) return ERR_BAD_CRC; // checksum failed
273
273
_serialID3 = _i2cPort->read () << 8 ; // store next byte to top of _serialID3
274
274
_serialID3 |= _i2cPort->read () ; // store LSB in _serialID3
275
275
uint8_t checkSum3 = _i2cPort->read (); // verify checksum
276
- if (checkSum1 != _CRC8 (_serialID1)) return ERR_BAD_CRC; // checksum failed
277
- if (checkSum2 != _CRC8 (_serialID2)) return ERR_BAD_CRC; // checksum failed
278
276
if (checkSum3 != _CRC8 (_serialID3)) return ERR_BAD_CRC; // checksum failed
279
277
serialID = ((uint64_t )_serialID1 << 32 ) + ((uint64_t )_serialID2 << 16 ) + ((uint64_t )_serialID3); // publish valid data
280
- _i2cPort->endTransmission ();
281
278
return SUCCESS;
282
279
}
283
280
284
281
// Sensor runs on chip self test
285
- // returns SUCCESS if successful
282
+ // Returns SUCCESS if successful or other error code if unsuccessful
286
283
SGP30ERR SGP30::measureTest (void ) {
287
284
_i2cPort->beginTransmission (_SGP30Address);
288
285
_i2cPort->write (measure_test, 2 ); // command to get self test
@@ -304,7 +301,6 @@ SGP30ERR SGP30::measureTest(void) {
304
301
uint8_t checkSum = _i2cPort->read (); // verify checksum
305
302
if (checkSum != _CRC8 (results)) return ERR_BAD_CRC; // checksum failed
306
303
if (results != 0xD400 ) return SELF_TEST_FAIL; // self test results incorrect
307
- _i2cPort->endTransmission ();
308
304
return SUCCESS;
309
305
}
310
306
0 commit comments