@@ -50,8 +50,7 @@ boolean SCD30::begin(TwoWire *theWire) {
50
50
/* *************************************************************************/
51
51
52
52
boolean SCD30::readWordFromCommand (uint8_t command[], uint8_t commandLength,
53
- uint16_t delayms,
54
- uint16_t *readdata,
53
+ uint16_t delayms, uint16_t *readdata,
55
54
uint8_t readlen) {
56
55
uint8_t data;
57
56
@@ -119,6 +118,13 @@ boolean SCD30::readWordFromCommand(uint8_t command[], uint8_t commandLength,
119
118
return true ;
120
119
}
121
120
121
+ /* *
122
+ * @brief
123
+ *
124
+ * @param data
125
+ * @param datalen
126
+ * @return uint8_t
127
+ */
122
128
uint8_t SCD30::generateCRC (uint8_t *data, uint8_t datalen) {
123
129
// calculates 8-Bit checksum with given polynomial
124
130
uint8_t crc = SCD30_CRC8_INIT;
@@ -135,6 +141,11 @@ uint8_t SCD30::generateCRC(uint8_t *data, uint8_t datalen) {
135
141
return crc;
136
142
}
137
143
144
+ /* *
145
+ * @brief
146
+ *
147
+ * @return boolean
148
+ */
138
149
boolean SCD30::readMeasurement () {
139
150
// TODO: add dataready check
140
151
// TOREMEMBER: readWordFromCommand already handled byteswap at word level.
@@ -157,68 +168,63 @@ boolean SCD30::readMeasurement() {
157
168
return false ;
158
169
}
159
170
171
+ /* *
172
+ * @brief
173
+ *
174
+ * @param ambientPressure
175
+ * @return boolean
176
+ */
160
177
boolean SCD30::trigSingleMeasurement (uint16_t ambientPressure) {
161
- uint8_t command[5 ];
162
- command[0 ] = 0 ;
163
- command[1 ] = 0x06 ;
178
+ uint8_t command[2 ] = {0x00 , 0x06 };
164
179
if (ambientPressure < 700 || ambientPressure > 1200 ) {
165
180
ambientPressure = 0 ;
166
181
}
167
- command[2 ] = (ambientPressure >> 8 ) & 0xFF ;
168
- command[3 ] = ambientPressure & 0xFF ;
169
- command[4 ] = generateCRC (&command[2 ], SCD30_WORD_LEN);
170
- _i2c->beginTransmission (_i2caddr);
171
- if (_i2c->write (command, sizeof (command)) != sizeof (command))
172
- return false ;
173
- _i2c->endTransmission ();
174
- return true ;
182
+ return setParamByCommand (command, ambientPressure);
175
183
}
176
184
185
+ /* *
186
+ * @brief
187
+ *
188
+ * @param ambientPressure
189
+ * @return boolean
190
+ */
177
191
boolean SCD30::trigContinuousMeasurement (uint16_t ambientPressure) {
178
- uint8_t command[5 ];
179
- command[0 ] = 0 ;
180
- command[1 ] = 0x10 ;
192
+ uint8_t command[2 ] = {0x00 , 0x10 };
181
193
if (ambientPressure < 700 || ambientPressure > 1200 ) {
182
194
ambientPressure = 0 ;
183
195
}
184
- command[2 ] = (ambientPressure >> 8 ) & 0xFF ;
185
- command[3 ] = ambientPressure & 0xFF ;
186
- command[4 ] = generateCRC (&command[2 ], SCD30_WORD_LEN);
187
- _i2c->beginTransmission (_i2caddr);
188
- if (_i2c->write (command, sizeof (command)) != sizeof (command))
189
- return false ;
190
- _i2c->endTransmission ();
191
- return true ;
196
+ return setParamByCommand (command, ambientPressure);
192
197
}
193
198
199
+ /* *
200
+ * @brief
201
+ *
202
+ * @return boolean
203
+ */
194
204
boolean SCD30::stopContinuousMeasurement () {
195
- uint8_t command[2 ];
196
- command[0 ] = 0 ;
197
- command[1 ] = 0x06 ;
198
- _i2c->beginTransmission (_i2caddr);
199
- if (_i2c->write (command, sizeof (command)) != sizeof (command))
200
- return false ;
201
- _i2c->endTransmission ();
202
- return true ;
205
+ uint8_t command[2 ] = {0x00 , 0x06 };
206
+ return setParamByCommand (command, 0 , 0 );
203
207
}
204
208
209
+ /* *
210
+ * @brief
211
+ *
212
+ * @param uInterval
213
+ * @return boolean
214
+ */
205
215
boolean SCD30::setMeasurementInterval (uint16_t uInterval) {
206
- uint8_t command[5 ];
207
- command[0 ] = 0x46 ;
208
- command[1 ] = 0x00 ;
216
+ uint8_t command[2 ] = {0x46 , 0x00 };
209
217
if (uInterval < 2 || uInterval > 1200 ) {
210
218
uInterval = 2 ;
211
219
}
212
- command[2 ] = (uInterval >> 8 ) & 0xFF ;
213
- command[3 ] = uInterval & 0xFF ;
214
- command[4 ] = generateCRC (&command[2 ], SCD30_WORD_LEN);
215
- _i2c->beginTransmission (_i2caddr);
216
- if (_i2c->write (command, sizeof (command)) != sizeof (command))
217
- return false ;
218
- _i2c->endTransmission ();
219
- return true ;
220
+ return setParamByCommand (command, uInterval);
220
221
}
221
222
223
+ /* *
224
+ * @brief
225
+ *
226
+ * @return boolean
227
+ */
222
228
boolean SCD30::getDataReadyStatus () {
223
229
uint8_t command[2 ] = {0x02 , 0x02 };
224
230
uint16_t resp16[1 ];
@@ -229,8 +235,82 @@ boolean SCD30::getDataReadyStatus() {
229
235
return false ;
230
236
}
231
237
232
- boolean SCD30::setTemperatureOffset (uint16_t tempOffset) { ; }
233
- boolean SCD30::setAltitudeOffset (uint16_t altitude) { ; }
238
+ /* *
239
+ * @brief
240
+ *
241
+ * @param tempOffset
242
+ * @return boolean
243
+ */
244
+ boolean SCD30::setTemperatureOffset (uint16_t tempOffset) {
245
+ uint8_t command[2 ] = {0x54 , 0x03 };
246
+ return setParamByCommand (command, tempOffset);
247
+ }
248
+
249
+ /* *
250
+ * @brief
251
+ *
252
+ * @param altitude
253
+ * @return boolean
254
+ */
255
+ boolean SCD30::setAltitudeOffset (uint16_t altitude) {
256
+ uint8_t command[2 ] = {0x51 , 0x02 };
257
+ return setParamByCommand (command, altitude);
258
+ }
259
+
260
+ /* *
261
+ * @brief
262
+ *
263
+ * @param enable
264
+ * @return boolean
265
+ */
266
+ boolean SCD30::setASC (boolean enable) {
267
+ uint8_t command[2 ] = {0x53 , 0x06 };
268
+ uint16_t enableCmd = enable == true ? 1 : 0 ;
269
+ return setParamByCommand (command, enableCmd);
270
+ }
271
+
272
+ /* *
273
+ * @brief
274
+ *
275
+ * @param co2baseline
276
+ * @return boolean
277
+ */
278
+ boolean SCD30::setFRCValue (uint16_t co2baseline) {
279
+ uint8_t command[2 ] = {0x52 , 0x04 };
234
280
235
- boolean SCD30::setASC (boolean enable) { ; }
236
- boolean SCD30::setFRCValue (uint16_t co2baseline) { ; }
281
+ if (co2baseline < 400 || co2baseline > 2000 ) {
282
+ return false ;
283
+ }
284
+ return setParamByCommand (command, co2baseline);
285
+ }
286
+
287
+ /* *
288
+ * @brief
289
+ *
290
+ * @param command
291
+ * @param param
292
+ * @param param_length
293
+ * @return boolean
294
+ */
295
+ boolean SCD30::setParamByCommand (uint8_t command[], uint16_t param,
296
+ uint8_t param_length) {
297
+ uint8_t __command[5 ];
298
+ __command[0 ] = command[0 ];
299
+ __command[1 ] = command[1 ];
300
+ if (param_length == 0 ) {
301
+ _i2c->beginTransmission (_i2caddr);
302
+ if (_i2c->write (__command, SCD30_WORD_LEN) != SCD30_WORD_LEN)
303
+ return false ;
304
+ _i2c->endTransmission ();
305
+ return true ;
306
+ } else {
307
+ __command[2 ] = (param >> 8 ) & 0xFF ;
308
+ __command[3 ] = param & 0xFF ;
309
+ __command[4 ] = generateCRC (&__command[2 ], SCD30_WORD_LEN);
310
+ _i2c->beginTransmission (_i2caddr);
311
+ if (_i2c->write (__command, sizeof (__command)) != sizeof (__command))
312
+ return false ;
313
+ _i2c->endTransmission ();
314
+ return true ;
315
+ }
316
+ }
0 commit comments