-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscd30_lib.cpp
executable file
·1079 lines (898 loc) · 33.4 KB
/
scd30_lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
*
* Based on original library from Sparkfun :
*
* This is a library written for the SCD30
* SparkFun sells these at its website: www.sparkfun.com
* Do you like this library? Help support SparkFun. Buy a board!
* https://www.sparkfun.com/products/14751
*
* The SCD30 measures CO2 with accuracy of +/- 30ppm.
*
* This library handles the initialization of the SCD30 and outputs
* CO2 levels, relative humidty, and temperature.
*
******************************************************************
* October 2018 : Changed, enhanced and extended for Raspberry Pi
* by Paul van Haastrecht (paulvha@hotmail.com)
*
* version 1.0 : initial Raspberry Pi
*
* version 2.0 : October 2018
* - some bug changes and code enhancements
* - added softreset
* - updated debug display
* - changed single measurement method
*
* Version 3.0 : October 2018
* - added dewpoint and heatindex
*
* Version 3.0.1 : November 2018
* - change remark about pull-up resistors on the SCD30
*
* Version 3.1.0 : August 2020
* Changes based on Datasheet May 2020
* - added functions : getForceRecalibration, getMeasurementInterval,
* getTemperatureOffset, getAltitudeCompensation, getFirmwareLevel
* - updates to streamline the library calls & typo's.
*
* Resources / dependencies:
* BCM2835 library (http://www.airspayce.com/mikem/bcm2835/)
* twowire library (https://github.com/paulvha/twowire)
*
* *****************************************************************
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include "SCD30.h"
/*
* 0 : no debug message
* 1 : sending and receiving data
* 2 : 1 + I2c protocol progress
*/
int SCD_DEBUG = 0;
/* Global main info */
float _co2 = 0;
float _temperature = 0;
float _humidity = 0;
bool _asc = true;
uint16_t _interval = 2;
/* These track the staleness of the current data
* This allows us to avoid calling readMeasurement() every time
* individual datums are requested */
bool co2HasBeenReported = true;
bool humidityHasBeenReported = true;
bool temperatureHasBeenReported = true;
/* global constructor for I2C (hardware of software) */
TwoWire TWI;
/* used as part of p_printf() */
bool NoColor=false;
/*******************************************
* @brief Constructor
*******************************************/
SCD30::SCD30(void)
{
settings.sda = DEF_SDA;
settings.scl = DEF_SCL;
settings.I2C_interface = soft_I2C;
settings.I2C_Address = SCD30_ADDRESS;
settings.baudrate = SCD30_SPEED;
settings.pullup = false;
}
/**************************************************************
* @brief Initialize the port and SCD30
* @param asc true : perform ASC
* @param interval >0 : set for continuous mode, else stop.
*
* @return true = OK, false is error
**************************************************************/
bool SCD30::begin(bool asc, uint16_t interval) {
_interval = interval; // save interval period
_asc = asc; // save automatic Self Calibration
/* Enable internal BCM2835 pull-up resistors on the SDA and SCL
* GPIO. BUT not on GPIO-2 and GPIO-3. The Raspberry has already
* external 1k8 pullup resistors on GPIO 2 and 3
*
* The SCD30 has 50K pull-up enabled in the processor on the board
*
* While this works, it is better to have external resistors (10K)
* for signal quality. Hence pull-up is disabled by default.
*/
if (settings.pullup) TWI.setPullup();
/* initialize the I2C hardware */
if (TWI.begin(settings.I2C_interface,settings.sda,settings.scl) != TW_SUCCESS){
if (SCD_DEBUG > 0) p_printf(RED, (char *) "Can't setup I2c !\n");
return(false);
}
/* set baudrate */
TWI.setClock(settings.baudrate);
/* The SCD30 is using clock stretching for especially after a read ACK
* This is documented in the interface guide.
*
* The MINIMUM needed for the SCD30 is 14ms (or a value of 1400).
* Setting it to max 200000 will allow for 200ms seconds as the interface
* guide states it could be up to 150ms once a day during calibration
*/
if (SCD_DEBUG > 0) p_printf(YELLOW, (char *) "setting clock stretching to 20000 (~200ms)\n");
TWI.setClockStretchLimit(200000);
/* initialize the SCD30 */
return(begin_scd30());
}
/***********************************************************
* @brief Initialize the SCD30
*
* is using _asc and _interval variables
*
* @return true = OK, false is error
***********************************************************/
bool SCD30::begin_scd30()
{
/* if continuous measurement is requested */
if (_interval > 0)
{
/* Check for device to respond correctly */
if(beginMeasuring() == true) //Start continuous measurements
{
/* set interval for continuous measurement start it else stop */
if (! setMeasurementInterval(_interval) ) return(false);
}
/* enable or disable Automatic Self Calibration */
return (setAutoSelfCalibration(_asc));
}
else
{
return(StopMeasurement());
}
}
/********************************************************************
* @brief close hardware correctly on the Raspberry Pi
*
* There is NO change to the values stored on the SCD30. That could
* be added here if needed.
********************************************************************/
void SCD30::close(void) {
TWI.close();
}
// boolean isFahrenheit: True == Fahrenheit; False == Celcius
// Using both Rothfusz and Steadman's equations
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
float SCD30::computeHeatIndex(float in_temperature, float percentHumidity, bool isFahrenheit) {
float hi, temperature;
/* if Celsius turn to Fahrenheit */
if (!isFahrenheit) temperature = (in_temperature * 1.8) + 32;
else temperature = in_temperature;
/* calculate */
hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094));
if (hi > 79) {
hi = -42.379 +
2.04901523 * temperature +
10.14333127 * percentHumidity +
-0.22475541 * temperature*percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature*pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
if((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
else if((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
}
/* if celsius was input, convert Fahrenheit to Celsius */
return isFahrenheit ? hi : (hi - 32) * 0.55555;
}
/*********************************************************************
* @brief calculate dew point
* @param temp : current temperature
* @param hum : current humidity
* @param Fahrenheit (true) or celsius (false)
*
* using the Augst-Roche-Magnus Approximation.
*
* @return dewpoint
*********************************************************************/
float SCD30::calc_dewpoint(float in_temperature, float hum, bool isFahrenheit) {
float td, H, temp;
/* if Fahrenheit turn to Celsius */
if (isFahrenheit) temp = (in_temperature- 32) * 0.55555;
else temp = in_temperature;
/* calculate */
H = log(hum/100) + ((17.625 * temp) / (243.12 + temp));
td = 243.04 * H / (17.625 - H);
/* if Fahrenheit was input, convert */
return isFahrenheit ? (td * 1.8) + 32 : td;
}
/****************************************************************
* @brief Returns the latest available CO2 level.
*
* If the current level has already been reported, trigger a new read
****************************************************************/
uint16_t SCD30::getCO2(void) {
/* trigger new read if needed */
if (co2HasBeenReported == true)
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
co2HasBeenReported = true;
return (uint16_t)_co2; //Cut off decimal as co2 is 0 to 10,000
}
/********************************************************************
* @brief Returns the latest available humidity
*
* If the current level has already been reported, trigger a new read
*********************************************************************/
float SCD30::getHumidity(void) {
/* trigger new read if needed */
if (humidityHasBeenReported == true)
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
humidityHasBeenReported = true;
return(_humidity);
}
/*****************************************************************
* @brief Returns the latest available temperature in Celsius
*
* If the cache has already been reported, perform a new read
*****************************************************************/
float SCD30::getTemperature(void) {
/* trigger new read if needed */
if (temperatureHasBeenReported == true)
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
temperatureHasBeenReported = true;
return(_temperature);
}
/*******************************************
* @brief Return temperature in Fahrenheit
******************************************/
float SCD30::getTemperatureF(void) {
float output = getTemperature();
output = (output * 9) / 5 + 32;
return(output);
}
/******************************************************************
* @brief read serial number from SCD30
*
* @param val : store the serial number
* provided val buffer must be defined at least 33 digits
* 32 serial + 0x0 termination
*
* @return true if OK (serial number in val-buffer)
* false in case of error
*
*****************************************************************/
bool SCD30::getSerialNumber(char *val) {
// request from SCD30
if (ReadFromSCD30(CMD_READ_SERIALNBR, (uint8_t *) val, SCD30_SERIAL_NUM_WORDS * 2) != SCD30_SERIAL_NUM_WORDS * 2) return(false);
val[(SCD30_SERIAL_NUM_WORDS * 2) + 1] = 0x0; // terminate
return(true);
}
/*
* Read from SCD30 the amount of requested bytes
* param val : to store the data received
* param cnt : number of data bytes requested
*
* return
* OK number of bytes read
* 0 error
*/
uint8_t SCD30::ReadFromSCD30(uint16_t command, uint8_t *val, uint8_t cnt)
{
uint8_t buff[60],data[2];
int x, y;
if (! sendCommand(command) ) return(0);
usleep (3); // datasheet may 2020
// start reading
if ( ! readbytes((char *) buff, (cnt / 2) *3) ) return(0);
if (SCD_DEBUG > 0) p_printf(YELLOW, (char *) "\nReceiving: " );
for (x = 0, y = 0 ; x < (cnt / 2) *3 ; x++)
{
if (SCD_DEBUG > 0) printf("0x%02X ", buff[x]);
if (y == 2) { // handle CRC
if (! checkCrc(data, 2, buff[x])) return(0);
y = 0;
}
else { // handle data
*val++ = buff[x];
data[y++] = buff[x];
}
}
if (SCD_DEBUG > 0) printf("\n");
return(cnt);
}
/**********************************************************
* @brief read 16 bit value from a register
* @param command : command to sent
* @param val : return the read 16 bit value
*
* @return
* true is OK, false error
*********************************************************/
bool SCD30::getSettingValue(uint16_t command, uint16_t *val)
{
uint8_t tmp[2];
*val = 0;
// request from SCD30
if (ReadFromSCD30(command, tmp, 2) != 2) return(false);
*val = tmp[0] << 8 | tmp[1];
return(true);
}
/****************************************************************
* @brief enables or disables the ASC See 1.3.6
*
* ASC status is saved in non-volatile memory. When the sensor is
* powered down while ASC is activated SCD30 will continue with
* automatic self-calibration after repowering without sending the command.
*
* At this moment it is not able to detect whether the self
* calibration has been done or finished
*
* @return true = OK, false is error
****************************************************************/
bool SCD30::setAutoSelfCalibration(bool enable) {
_asc = enable;
if (_asc)
{
return(sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 1)); //Activate continuous ASC
}
else
{
return(sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 0)); //Deactivate continuous ASC
}
}
/****************************************************************
* @brief Set the temperature offset. See 1.3.8.
*
* Temperature offset value is saved in non-volatile memory.
* The last set value will be used for temperature offset compensation
* after repowering.
*
* All this does for now is lower the SCD30 temperature reading with
* the offset value over a period of 10 min, while increasing the humidity
* readings. NO impact on the CO2 readings.
*
* The value can NOT be negative as it will cause uncontrolled
* temperature and humidity results.
*
* @return true = OK, false is error
*
*****************************************************************/
bool SCD30::setTemperatureOffset(float tempOffset) {
/* can not be negative number */
if (tempOffset < 0) return(false);
int16_t tickOffset = tempOffset * 100;
if (SCD_DEBUG > 0) p_printf(YELLOW,(char *) "set temperature offset %d\n",tickOffset);
return (sendCommand(COMMAND_SET_TEMPERATURE_OFFSET, tickOffset));
}
/******************************************************************
* @brief Set the altitude compenstation. See 1.3.9.
*
* Setting altitude is disregarded when an ambient pressure is
* given to the sensor, Altitude value is saved in non-volatile memory.
* The last set value will be used for altitude compensation after repowering.
*
* Setting the argument to zero will deactivate the
* ambient pressure compensation
*
* @return true = OK, false is error
*
***************************************************************/
bool SCD30::setAltitudeCompensation(uint16_t altitude) {
// 700 mbar ~ 3040M altitude, 1200mbar ~ -1520
if (altitude < -1520 || altitude > 3040) return(false);
return(sendCommand(COMMAND_SET_ALTITUDE_COMPENSATION, altitude));
}
/***************************************************************
* @brief Set the pressure compensation.
*
* This is passed during measurement startup. mbar can be 700 to 1200
*
* Setting altitude is disregarded when an ambient pressure is
* given to the sensor, pressure value is saved in non-volatile memory.
* The last set value will be used for altitude compensation after repowering.
*
* Setting the argument to zero will deactivate the
* ambient pressure compensation
*
* @return true = OK, false is error
*
****************************************************************/
bool SCD30::setAmbientPressure(uint16_t pressure_mbar) {
return (beginMeasuring(pressure_mbar));
}
/*****************************************************************
* @brief Set Forced Recalibration value (FRC) see 1.3.7
*
* Setting a reference CO2 concentration by the here described
* method will always overwrite the settings from ASC (see chapter 1.3.6)
* and vice-versa. The reference CO2 concentration has to be within
* the range 400 ppm ≤ c ref (CO 2 ) ≤ 2000 ppm.
*
* @return true = OK, false is error
*
******************************************************************/
bool SCD30::setForceRecalibration(uint16_t val) {
if(val < 400 || val > 2000) return(false); //Error check
if (SCD_DEBUG > 0) p_printf(YELLOW, (char *) "set forced calibration %d ppm\n", val);
return (sendCommand(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, val));
}
/*****************************************************************
* @brief Begins continuous measurements see 1.3.1
*
* Continuous measurement status is saved in non-volatile memory.
* When the sensor is powered down while continuous measurement mode
* is active SCD30 will measure continuously after repowering without
* sending the measurement command.
*
* @return true = OK, false is error
*
*****************************************************************/
bool SCD30::beginMeasuring(uint16_t pressureOffset) {
/* Error check */
if(pressureOffset < 700 || pressureOffset > 1200) pressureOffset = 0;
if (SCD_DEBUG > 0)
p_printf(YELLOW, (char *) "Begin measuring with pressure offset %d\n", pressureOffset);
return(sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
}
//Overload - no pressureOffset
bool SCD30::beginMeasuring(void) {
return(beginMeasuring(0));
}
/*******************************************************************
* @brief soft reset see 1.3.10
*
* Not only will it reset, but also re-instruct the requested settings
* for the SCD30.
*
* @return true = OK, false is error
*
*********************************************************************/
bool SCD30::SoftReset(void) {
if (sendCommand(CMD_SOFT_RESET) != true) return(false);
// reload parameters
return( begin_scd30() );
}
/*******************************************************************
* @brief Stop continuous measurement. see 1.3.2
*
* Continuous measurement is stored in the SCD30 and restarted after
* power-on. It might be needed to stop this (e.g. in case of single
* shot measurement)
*
* @return true = OK, false is error
*
*********************************************************************/
bool SCD30::StopMeasurement(void) {
return(sendCommand(CMD_STOP_MEAS));
}
/*******************************************************************
* @brief Sets interval between measurements 2 <> 1800 seconds (30 minutes)
*
* @return true = OK, false is error
*
******************************************************************/
bool SCD30::setMeasurementInterval(uint16_t interval) {
if (interval < 2 || interval > 1800)
{
if (SCD_DEBUG > 0) p_printf(RED, (char *) "invalid measurement interval %d\n", interval);
return(false);
}
// save new setting
_interval = interval;
return(sendCommand(COMMAND_SET_MEASUREMENT_INTERVAL, _interval));
}
/****************************************************************
* @brief Perform a single measurement
*
* The user program should first call stopMeasurement() to stop any
* continuous reading which starts automatically after power on
* (if it was set before last power-off)
*
* ///////////////////////////////////////////////////////////////
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* The single command "CMD_START_SINGLE_MEAS" is not working well
* on the SCD30. (this has been confirmed by supplier)
*
* Example :
* - Running in continuous mode 10x. CO2 value of 735 - 740
* - started single measurement.
* first 736
* second 1149
* third 0
* fourth 0
* etc..
* - Now starting continuous mode again: starts with zero and takes about
* 20 reads in continuous mode to return to a value around 740.
*
* Hence pseudo single measurement is implemented in this driver
* start continuous, interval 2 second
* perform a read
* stop continuous
*
* it will take max. 4 seconds for the first result !
*
* The user program should NOT check for data_available(), but
* get the CO2, temperature and humidity upon a return of true
*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* ////////////////////////////////////////////////////////////////
* @return true = OK, false is error
*
******************************************************************/
bool SCD30::StartSingleMeasurement(void) {
/* see remark above */
//return(sendCommand(CMD_START_SINGLE_MEAS, 0x0000));
int retry = 10;
bool stat = false;
/* save current ASC and interval setting */
bool save_asc = _asc;
uint16_t save_interval = _interval;
/* start continuous */
_asc = false;
_interval = 2;
if (! begin_scd30() ) goto stop_sm;
/* wait for max x times for data available from SCD30 */
do
{
// check for data
stat = dataAvailable();
// if not available
if (stat == false)
{
sleep(1);
if (retry-- == 0) goto stop_sm;
}
} while (stat == false);
/* if data available, read it */
if (stat == true) stat = readMeasurement();
stop_sm:
/* stop measurement and restore */
StopMeasurement();
_asc = save_asc;
_interval = save_interval;
return(stat);
}
/****************************************************************
* @brief checks the data ready status register. see 1.3.4
*
* @return : true if available, false if not or error
*
****************************************************************/
bool SCD30::dataAvailable() {
uint8_t tmp[2];
// request from SCD30
if (ReadFromSCD30(COMMAND_GET_DATA_READY, tmp, 2) != 2) return(false);
if (tmp[1] == 1) return(true);
return (false);
}
////////////////////////////////////////////////////////////////////
///// low level routines ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/************************************************
* @brief read amount of bytes from the SCD30
* @param buff : buffer to hold the data read
* @param len : number of bytes to read
*
* @return true if OK, false in case of error
*
************************************************/
bool SCD30::readbytes(char *buff, uint8_t len) {
Wstatus result;
int retry = 3;
/* set slave address for SCD30 */
TWI.setSlave(settings.I2C_Address);
if (SCD_DEBUG > 0)
p_printf(YELLOW, (char *) "read from I2C address 0x%x, %d bytes\n",settings.I2C_Address, len);
while(1)
{
/* read results from I2C */
result = TWI.i2c_read(buff, len);
/* if failure, then retry as long as retrycount has not been reached */
if (result != I2C_OK)
{
if (SCD_DEBUG > 1) p_printf(YELLOW, (char *) " read retrying. result %d\n", result);
if (retry-- > 0) continue;
}
/* process result */
switch(result)
{
case I2C_OK:
return(true);
case I2C_SDA_NACK :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "Read NACK error\n");
return(false);
case I2C_SCL_CLKSTR :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "Read Clock stretch error\n");
return(false);
case I2C_SDA_DATA :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "not all data has been read\n");
return(false);
default:
if (SCD_DEBUG > 1) p_printf(RED, (char *) "unkown return code\n");
return(false);
}
}
}
/*****************************************************************
* @brief check CRC for correct received information
*
* @param data : data to calculate the CRC from
* @param len : number of bytes in data
* @param crc_rec : received CRC from SCD30
*
* @return CRC values are the same, false in case of difference
*
*****************************************************************/
bool SCD30::checkCrc(uint8_t *data, uint8_t len, uint8_t crc_rec) {
uint8_t crc = computeCRC8(data, len);
if (crc_rec != crc)
{
if (SCD_DEBUG > 1) p_printf(RED, (char *) "crc error: expected %x, got %x\n", crc, crc_rec);
return(false);
}
return(true);
}
/****************************************************************
* @brief read CO2, Temperature and humidity from SCD30. see 1.3.5
*
* Updates global variables with floats
*
* @return true if OK, false in case of error
*
****************************************************************/
bool SCD30::readMeasurement(){
uint32_t tempCO2 = 0;
uint32_t tempHumidity = 0;
uint32_t tempTemperature = 0;
uint8_t temp[12];
/* Verify we have data from the sensor */
if (! dataAvailable() ) return (false);
// request from SCD30
if (ReadFromSCD30(COMMAND_READ_MEASUREMENT, temp, 12) != 12) return(false);
tempCO2 = temp[0] << 24 | temp[1] << 16 | temp[2] << 8 | temp[3];
tempTemperature = temp[4] << 24 | temp[5] << 16 | temp[6] << 8 | temp[7];
tempHumidity = temp[8] << 24 | temp[9] << 16 | temp[10] << 8 | temp[11];
/* Now copy the uint32s into their associated floats */
memcpy(&_co2, &tempCO2, sizeof(_co2));
memcpy(&_temperature, &tempTemperature, sizeof(_temperature));
memcpy(&_humidity, &tempHumidity, sizeof(_humidity));
/* Mark our global variables as fresh */
co2HasBeenReported = false;
humidityHasBeenReported = false;
temperatureHasBeenReported = false;
return (true); //Success! New data available in globals.
}
/************************************************************
* @brief Set for debugging the driver
*
* @param val : action to be performed
* 0 = disable debug messages
* 1 = sent/receive messages
* 2 = like 1 + protocol errors
*
* This can be called BEFORE performing the begin() call.
************************************************************/
void SCD30::setDebug(int val) {
SCD_DEBUG = val;
// if level 2 enable I2C driver messages
if (SCD_DEBUG == 2) TWI.setDebug(true);
else TWI.setDebug(false);
}
/*******************************************************
* @brief decode the command that is being sent
* @param command : SCD30 command
*******************************************************/
void SCD30::debug_cmd(uint16_t command) {
p_printf(YELLOW, (char *) "Command 0x%04x : ", command);
switch(command)
{
case 0x0010:
p_printf(YELLOW, (char *)"COMMAND_CONTINUOUS_MEASUREMENT");
break;
case 0x0104:
p_printf(YELLOW, (char *)"CMD_STOP_MEAS");
break;
case 0x4600:
p_printf(YELLOW, (char *)"COMMAND_SET_MEASUREMENT_INTERVAL");
break;
case 0x0202:
p_printf(YELLOW, (char *)"COMMAND_GET_DATA_READY");
break;
case 0x300:
p_printf(YELLOW, (char *)"COMMAND_READ_MEASUREMENT");
break;
case 0x5306:
p_printf(YELLOW, (char *)"COMMAND_AUTOMATIC_SELF_CALIBRATION");
break;
case 0x5204:
p_printf(YELLOW, (char *)"COMMAND_SET_FORCED_RECALIBRATION_FACTOR");
break;
case 0x5403:
p_printf(YELLOW, (char *)"COMMAND_SET_TEMPERATURE_OFFSET");
break;
case 0x5102:
p_printf(YELLOW, (char *)"COMMAND_SET_ALTITUDE_COMPENSATION");
break;
case 0xD033:
p_printf(YELLOW, (char *)"CMD_READ_SERIALNBR");
break;
case 0xD025:
p_printf(YELLOW, (char *)"CMD_READ_ARTICLECODE");
break;
case 0x0006:
p_printf(YELLOW, (char *)"CMD_START_SINGLE_MEAS");
break;
case 0xD100:
p_printf(YELLOW, (char *)"CMD_GET_FW_LEVEL");
break;
default:
p_printf(YELLOW, (char *)"COMMAND_UNKNOWN");
break;
}
}
/**************************************************
* @brief Display the clock stretch info for debug
**************************************************/
void SCD30::DispClockStretch() {
TWI.DispClockStretch();
}
/*******************************************************
* @brief Sends a command along with arguments and CRC
*
* @param command : SCD30 command
* @param arguments : command arugments to add
*
* @return true if OK, false in case of error
********************************************************/
bool SCD30::sendCommand(uint16_t command, uint16_t arguments) {
return(sendCommand(command, arguments, 5));
}
/**********************************************************
* @brief Sends just a command, no arguments, no CRC
*
* @param command : SCD30 command
*
* @return true if OK, false in case of error
**********************************************************/
bool SCD30::sendCommand(uint16_t command) {
return(sendCommand(command, 0x0, 2));
}
/*******************************************************
* Sends a command to SCD30
* @brief Sends a command to
*
* @param command : SCD30 command
* @param arguments : command arugments to add
* @param len : if > 2 arguments and CRC are added
* else only the command is sent
*
* @return true if OK, false in case of error
********************************************************/
bool SCD30::sendCommand(uint16_t command, uint16_t arguments, uint8_t len)
{
uint8_t buff[5];
int retry = 3, x;
Wstatus result;
/* set slave address for SCD30 */
TWI.setSlave(settings.I2C_Address);
buff[0] = (command >> 8); //MSB
buff[1] = (command & 0xFF); //LSB
/* add arguments and CRC */
if (len > 2)
{
buff[2] = (arguments >> 8); //MSB
buff[3] = (arguments & 0xFF); //LSB
buff[4] = computeCRC8(&buff[2], 2); // Calc CRC on the arguments only, not the command
}
if (SCD_DEBUG > 0)
{
p_printf(YELLOW, (char *) "sending to I2C address 0x%x, ",settings.I2C_Address);
debug_cmd(command);
p_printf(YELLOW, (char *) "\n\tbytes: ");
for (x = 0; x < len ;x++)
p_printf(YELLOW, (char *)" 0x%02x",buff[x]);
printf("\n");
}
while (1)
{
// perform a write of data
result = TWI.i2c_write((char *) buff, len);
// if error, perform retry (if not exceeded)
if (result != I2C_OK)
{
if (SCD_DEBUG > 1) printf(" send retrying %d\n", result);
if (retry-- > 0) continue;
}
switch(result)
{
case I2C_OK:
return(true);
case I2C_SDA_NACK :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "write NACK error\n");
return(false);
case I2C_SCL_CLKSTR :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "write Clock stretch error\n");
return(false);
case I2C_SDA_DATA :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "write not all data has been sent\n");
return(false);
default :
if (SCD_DEBUG > 1) p_printf(RED, (char *) "Unkown error during writing\n");
return(false);
}
}
}
/************************************************************************