Skip to content

Commit 18f1986

Browse files
committed
first complete
1 parent 174d3c9 commit 18f1986

File tree

3 files changed

+132
-47
lines changed

3 files changed

+132
-47
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
{
2+
"files.associations": {
3+
"*.CI": "vb",
4+
"random": "cpp"
5+
}
26
}

SCD30.cpp

Lines changed: 127 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ boolean SCD30::begin(TwoWire *theWire) {
5050
/**************************************************************************/
5151

5252
boolean SCD30::readWordFromCommand(uint8_t command[], uint8_t commandLength,
53-
uint16_t delayms,
54-
uint16_t *readdata,
53+
uint16_t delayms, uint16_t *readdata,
5554
uint8_t readlen) {
5655
uint8_t data;
5756

@@ -119,6 +118,13 @@ boolean SCD30::readWordFromCommand(uint8_t command[], uint8_t commandLength,
119118
return true;
120119
}
121120

121+
/**
122+
* @brief
123+
*
124+
* @param data
125+
* @param datalen
126+
* @return uint8_t
127+
*/
122128
uint8_t SCD30::generateCRC(uint8_t *data, uint8_t datalen) {
123129
// calculates 8-Bit checksum with given polynomial
124130
uint8_t crc = SCD30_CRC8_INIT;
@@ -135,6 +141,11 @@ uint8_t SCD30::generateCRC(uint8_t *data, uint8_t datalen) {
135141
return crc;
136142
}
137143

144+
/**
145+
* @brief
146+
*
147+
* @return boolean
148+
*/
138149
boolean SCD30::readMeasurement() {
139150
// TODO: add dataready check
140151
// TOREMEMBER: readWordFromCommand already handled byteswap at word level.
@@ -157,68 +168,63 @@ boolean SCD30::readMeasurement() {
157168
return false;
158169
}
159170

171+
/**
172+
* @brief
173+
*
174+
* @param ambientPressure
175+
* @return boolean
176+
*/
160177
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};
164179
if (ambientPressure < 700 || ambientPressure > 1200) {
165180
ambientPressure = 0;
166181
}
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);
175183
}
176184

185+
/**
186+
* @brief
187+
*
188+
* @param ambientPressure
189+
* @return boolean
190+
*/
177191
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};
181193
if (ambientPressure < 700 || ambientPressure > 1200) {
182194
ambientPressure = 0;
183195
}
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);
192197
}
193198

199+
/**
200+
* @brief
201+
*
202+
* @return boolean
203+
*/
194204
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);
203207
}
204208

209+
/**
210+
* @brief
211+
*
212+
* @param uInterval
213+
* @return boolean
214+
*/
205215
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};
209217
if (uInterval < 2 || uInterval > 1200) {
210218
uInterval = 2;
211219
}
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);
220221
}
221222

223+
/**
224+
* @brief
225+
*
226+
* @return boolean
227+
*/
222228
boolean SCD30::getDataReadyStatus() {
223229
uint8_t command[2] = {0x02, 0x02};
224230
uint16_t resp16[1];
@@ -229,8 +235,82 @@ boolean SCD30::getDataReadyStatus() {
229235
return false;
230236
}
231237

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};
234280

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+
}

SCD30.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ class SCD30 {
4343
void read(uint8_t address, uint8_t *data, uint8_t n);
4444
boolean readWordFromCommand(uint8_t command[], uint8_t commandLength, uint16_t delayms = 0, uint16_t *readdata = NULL, uint8_t readlen = 0);
4545
uint8_t generateCRC(uint8_t data[], uint8_t datalen);
46+
boolean setParamByCommand(uint8_t command[], uint16_t param, uint8_t param_length = 1);
4647
};

0 commit comments

Comments
 (0)