-
Notifications
You must be signed in to change notification settings - Fork 2
/
scd30.cpp
executable file
·877 lines (731 loc) · 27.9 KB
/
scd30.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
/*******************************************************************
*
* 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 : created 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
* - added option to output temperature in Fahrenheit instead of celsius
*
* Version 3.0 : October 2018
* - added dewpoint and heatindex
*
* Version 3.0.1 : November 2018
* - fixed humidity type in printf
*
* version 3.0.2: January 2019
* - changed to use p_printf in do_output to fix an issue with providing
* output as a sub-program to python.
*
* Version 3.1.0 : August 2020
* Changes based on Datasheet May 2020
* - added functions : getForceRecalibration, getMeasurementInterval,
* getTemperatureOffset, getAltitudeCompensation, getFirmwareLevel
*
* Resources / dependencies:
* BCM2835 library (http://www.airspayce.com/mikem/bcm2835/)
* twowire library (https://github.com/paulvha/twowire)
*
* The SCD30 monitor can be extended with a DYLOS 1700 monitor.
* For this "DYLOS" needs to be set. The best way is to use the makefile
*
* To create a build with only the SCD30 monitor type:
* make
*
* To create a build with the SCD30 and DYLOS monitor type:
* make BUILD=DYLOS
*
* *****************************************************************
* 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"
/* global constructor */
SCD30 MySensor;
char progname[20];
#ifdef DYLOS // DYLOS monitor option
/* indicate these are C-programs and not to be linked */
extern "C" {
void close_dylos();
int read_dylos (char * buf, int len, int wait, int verbose);
int open_dylos(char * device, int verbose);
}
typedef struct dylos
{
char port[MAXBUF]; // connected port (like /dev/ttyUSB0)
bool include; // true = include
uint16_t value_pm10; // measured value PM10 DC1700
uint16_t value_pm1; // Measured value PM1 DC1700
} dylos;
#endif
typedef struct scd_par
{
/* option SCD30 parameters */
bool stop_cm; // stop continuous measurement
bool perform_single; // perform a single measurement
uint16_t interval; // sample interval. 2 <> 1800 seconds
int16_t frc; // SCD30 forced recalibration 400 <>2000 ppm
int16_t temp_offset; // Temperature offset. 0 <> 25C
int16_t altitude; // altitude in meters -1520 <> 3040 meter
int16_t pressure; // pressure in Mbar 700 <> 1200 mbar
bool asc; // true to perform Automatic Self calibration
// added 3.1 / August 2020
bool g_measurement; // get measurement interval
bool g_FRC; // get Forced recalibration factor
bool g_temp_offset; // get current temperature offset
bool g_altitude; // get altitude compensation
bool d_deviceinfo; // display firmware
/* option program variables */
uint16_t loop_count; // number of measurement
uint16_t loop_delay; // loop delay in between measurements
bool timestamp; // include timestamp in output
bool tempCel; // show temperature in Celsius or Fahrenheit
bool heatindex; // add heatindex in output
bool dewpoint; // add dewpoint in output
int verbose; // verbose level
#ifdef DYLOS // DYLOS monitor option
/* include Dylos info */
struct dylos dylos;
#endif
} scd_par;
/*********************************************************************
* @brief close hardware and program correctly
**********************************************************************/
void closeout()
{
/* reset pins in Raspberry Pi */
MySensor.close();
#ifdef DYLOS // DYLOS monitor option
/* close dylos */
close_dylos();
#endif
exit(EXIT_SUCCESS);
}
/*********************************************************************
* @brief catch signals to close out correctly
* @param sig_num : signal that was raised
*
**********************************************************************/
void signal_handler(int sig_num)
{
switch(sig_num)
{
case SIGINT:
case SIGKILL:
case SIGABRT:
case SIGTERM:
default:
#ifdef DYLOS // DYLOS monitor option
printf("\nStopping SCD30 & Dylos monitor\n");
#else
printf("\nStopping SCD30 monitor\n");
#endif
closeout();
break;
}
}
/*****************************************
* @brief setup signals
*****************************************/
void set_signals()
{
struct sigaction act;
memset(&act, 0x0,sizeof(act));
act.sa_handler = &signal_handler;
sigemptyset(&act.sa_mask);
sigaction(SIGTERM,&act, NULL);
sigaction(SIGINT,&act, NULL);
sigaction(SIGABRT,&act, NULL);
sigaction(SIGSEGV,&act, NULL);
sigaction(SIGKILL,&act, NULL);
}
/*********************************************
* @brief generate timestamp
*
* @param buf : returned the timestamp
*********************************************/
void get_time_stamp(char * buf)
{
time_t ltime;
struct tm *tm ;
ltime = time(NULL);
tm = localtime(<ime);
static const char wday_name[][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char mon_name[][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
sprintf(buf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
wday_name[tm->tm_wday], mon_name[tm->tm_mon],
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
1900 + tm->tm_year);
}
/************************************************
* @brief initialise the variables
* @param scd : pointer to SCD30 parameters
************************************************/
void init_variables(struct scd_par *scd)
{
/* option SCD30 parameters */
scd->asc = true; // set Automatic Self Calibration (ASC)
scd->stop_cm = false; // NOT stop continuous measurement
scd->perform_single = false; // NOT perform a single measurement
scd->interval = 2; // sample interval. 2 <> 1800 seconds
scd->frc = -1; // SCD30 forced recalibration 400 <>2000 ppm
scd->temp_offset = -1; // Temperature offset. 0 <> 25C
scd->altitude = -1; // altitude in meters -1520 <> 3040 meter
scd->pressure = -1; // pressure in Mbar 700 <> 1200 mbar
// added 3.1 / August 2020
scd->g_measurement = false; // get measurement interval
scd->g_FRC = false; // get Forced recalibration factor
scd->g_temp_offset = false; // get current temperature offset
scd->g_altitude = false; // get altitude compensation
scd->d_deviceinfo = false; // display firmware
/* option program variables */
scd->loop_count = 10; // number of measurement
scd->loop_delay = 5; // loop delay in between measurements
scd->timestamp = false; // NOT include timestamp in output
scd->dewpoint = false; // NOT include dewpoint in output
scd->heatindex = false; // NOT include heatindex in output
scd->tempCel = true; // display temperarure in Celsius
scd->verbose = 0; // No verbose level
#ifdef DYLOS // DYLOS monitor option
/* Dylos values */
scd->dylos.include = false;
scd->dylos.value_pm1 = 0;
scd->dylos.value_pm10 = 0;
#endif
}
/**********************************************************
* @brief Display different correction settings
* @param scd : pointer to SCD30 parameters
*********************************************************/
void get_value(struct scd_par *scd)
{
uint16_t val;
if (scd->g_measurement) {
if (MySensor.getSettingValue(COMMAND_SET_MEASUREMENT_INTERVAL, &val))
p_printf(GREEN,(char *)"Measurement interval %d\n", val);
else
p_printf(RED,(char *)"Could not read Measurement interval\n");
}
if (scd->g_FRC){
if (MySensor.getSettingValue(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, &val))
p_printf(GREEN,(char *)"Forced calibration factor %d\n", val);
else
p_printf(RED,(char *)"Could not read forced calibration factor\n");
}
if (scd->g_temp_offset){
if (MySensor.getSettingValue(COMMAND_SET_TEMPERATURE_OFFSET, &val))
p_printf(GREEN,(char *)"Temperature offset %d or %d *C\n", val, val / 100);
else
p_printf(RED,(char *)"Could not read temperature offset\n");
}
if (scd->g_altitude){
if (MySensor.getSettingValue(COMMAND_SET_ALTITUDE_COMPENSATION, &val))
p_printf(GREEN,(char *)"Altitude compensation %d\n", val);
else
p_printf(RED,(char *)"Could not read altitude compensation \n");
}
}
/**********************************************************
* @brief initialise the Raspberry PI and SCD30 / Dylos hardware
* @param scd : pointer to SCD30 parameters
*********************************************************/
void init_hw(struct scd_par *scd)
{
/* Dylos monitor MUST be started as ROOT (/dev/tty* permission) */
#ifndef DYLOS
/* hard_I2C requires root permission */
if (MySensor.settings.I2C_interface == hard_I2C)
#endif
{
if (geteuid() != 0)
{
p_printf(RED,(char *)"You must be super user\n");
exit(EXIT_FAILURE);
}
}
/* progress & debug messages tell driver */
MySensor.setDebug(scd->verbose);
/* 3.1 only obtain the values */
if (scd->g_measurement || scd->g_FRC || scd->g_temp_offset || scd->g_altitude)
{
/* start hardware */
if (! MySensor.begin(0, 0))
{
p_printf(RED,(char *) "Error during init I2C\n");
exit(-1);
}
get_value(scd);
closeout();
}
/* start hardware and SCD30 */
if (! MySensor.begin(scd->asc, scd->interval))
{
p_printf(RED,(char *) "Error during init I2C\n");
exit(-1);
}
/* frc, altitude, pressure */
if (scd->altitude != -1)
{
if (scd->verbose) printf("setting altitude to %d\n", scd->altitude);
if(MySensor.setAltitudeCompensation(scd->altitude) == false) closeout();
}
/* pressure will overrule altitude */
if (scd->pressure != -1)
{
if (scd->verbose) printf("setting pressure to %d\n", scd->pressure);
if(MySensor.setAmbientPressure(scd->pressure) == false)
{
p_printf (RED, (char *) "Error during setting pressure\n");
closeout();
}
}
/* will overrule ASC */
if (scd->frc != -1)
{
if (scd->verbose) printf("setting forced recalibration to %d\n", scd->frc);
if(MySensor.setForceRecalibration(scd->frc) == false)
{
p_printf (RED, (char *) "Error during setting FRC\n");
closeout();
}
}
/* only impacts the temperature and humidity reading. NOT the CO2 */
if (scd->temp_offset != -1)
{
if (scd->verbose) printf("setting temperature offset to %d\n", scd->temp_offset);
if(MySensor.setTemperatureOffset(scd->temp_offset) == false)
{
p_printf (RED, (char *) "Error during setting Temperature offset\n");
closeout();
}
}
#ifdef DYLOS // DYLOS monitor option
/* init Dylos DC1700 port */
if (scd->dylos.include)
{
if(scd->verbose) p_printf (YELLOW, (char *) "initialize Dylos\n");
if (open_dylos(scd->dylos.port, scd->verbose) != 0) closeout();
}
#endif
}
#ifdef DYLOS // DYLOS monitor option
/*****************************************************************
* @brief Try to read from Dylos DC1700 monitor
*
* @param scd : pointer to SCD30 parameters and Dylos values
****************************************************************/
bool do_dylos(struct scd_par *scd)
{
char buf[MAXBUF], t_buf[MAXBUF];
int ret, i, offset =0 ;
/* if no Dylos device specified */
if ( ! scd->dylos.include) return(false);
if(scd->verbose > 0 ) printf("\nReading Dylos data ");
/* reset values */
scd->dylos.value_pm1 = scd->dylos.value_pm10 = 0;
/* try to read from Dylos and wait max 2 seconds */
ret = read_dylos(buf, MAXBUF, 2, scd->verbose);
/* if data received : parse it */
for(i = 0; i < ret; i++)
{
/* if last byte on line */
if (buf[i] == 0xa)
{
/* terminate & get PM10 */
t_buf[offset] = 0x0;
scd->dylos.value_pm10 = (uint16_t)strtod(t_buf, NULL);
// break
i = ret;
}
/* skip carriage return and any carbage below 'space' */
else if (buf[i] != 0xd && buf[i] > 0x1f)
{
t_buf[offset] = buf[i];
/* get PM1 */
if (t_buf[offset] == ',')
{
t_buf[offset] = 0x0;
scd->dylos.value_pm1 = (uint16_t)strtod(t_buf, NULL);
offset=0;
}
else
offset++;
}
}
return(true);
}
#endif
/*****************************************************************
* @brief output the results
*
* @param scd : pointer to SCD30 parameters
****************************************************************/
void do_output(struct scd_par *scd)
{
char buf[30],t;
float index, dew, temp, hum;
uint16_t co2;
if (scd->timestamp)
{
get_time_stamp(buf);
printf("%s: ",buf);
}
co2 = (uint16_t) MySensor.getCO2();
hum = MySensor.getHumidity();
if (scd->tempCel) // Celsius
{
temp = MySensor.getTemperature();
index = MySensor.computeHeatIndex(temp, hum, false);
dew = MySensor.calc_dewpoint(temp, hum, false);
t= 'C';
}
else // Fahrenheit
{
temp = MySensor.getTemperatureF();
index = MySensor.computeHeatIndex(temp, hum, true);
dew = MySensor.calc_dewpoint(temp, hum, true);
t= 'F';
}
p_printf(WHITE, (char *) "CO2: %4d PPM\tHumidity: %3.2f %%RH Temperature: %3.2f *%c ",co2, hum,temp,t);
if (scd->heatindex) p_printf(WHITE, (char *) "heatindex: %3.2f *%c ", index, t);
if (scd->dewpoint) p_printf(WHITE, (char *) "dew-point: %3.2f *%c ", dew, t);
#ifdef DYLOS
if (do_dylos(scd))
p_printf(WHITE, (char *)" DYLOS: PM1 %4d PPM PM10 %4d PPM",scd->dylos.value_pm1, scd->dylos.value_pm10 );
#endif
p_printf(WHITE, (char *) "\n");
/* display debug information on highest verbose level */
if(scd->verbose == 2) MySensor.DispClockStretch();
}
/*****************************************************************
* @brief Here the main of the program
* @param scd : pointer to SCD30 parameters
****************************************************************/
void main_loop(struct scd_par *scd)
{
char buf[(SCD30_SERIAL_NUM_WORDS * 2) + 1]; // 3.1
int loop_set, reset_retry = RESET_RETRY;
bool first=true;
uint16_t val;
// include device information
if (scd->d_deviceinfo)
{
/* get the serial number (check that communication works) */
if(MySensor.getSerialNumber(buf) )
p_printf(YELLOW, (char *) "Serialnumber\t%s\n", buf);
else
p_printf (RED, (char *) "Error during getting serial number\n");
/* 3.1 add firmware level */
if (MySensor.getSettingValue(CMD_GET_FW_LEVEL, &val))
p_printf(YELLOW,(char *)"Firmware level\t%d.%d\n", val>>8 & 0xff, val & 0xff);
else
p_printf(RED,(char *)"Could not read firmware level\n");
}
/* single measurement requested */
if (scd->perform_single == true)
{
p_printf(GREEN,(char *) "Starting single SCD30 measurement:\n");
if (MySensor.StartSingleMeasurement() == false)
{
p_printf (RED, (char *) "Can not perform single measurement\n");
closeout();
}
do_output(scd);
return;
}
p_printf(GREEN,(char *) "Starting SCD30 measurement:\n");
/* check for endless loop */
if (scd->loop_count > 0 ) loop_set = scd->loop_count;
else loop_set = 1;
/* loop requested */
while (loop_set > 0)
{
if(MySensor.dataAvailable() == true)
{
reset_retry = RESET_RETRY;
do_output(scd);
}
else
{
if (reset_retry-- == 0)
{
p_printf (RED, (char *) "Retry count exceeded. perform softreset\n");
MySensor.SoftReset();
reset_retry = RESET_RETRY;
first = true;
}
else
{
/* Prevent message when previous mode of the SCD30 was
* STOP continuous measurement. It needs 4 seconds
* at least for the first results in that case */
if (first) first = false;
else printf("no data available\n");
}
}
/* delay for seconds */
sleep(scd->loop_delay);
/* check for endless loop */
if (scd->loop_count > 0) loop_set--;
}
}
/*********************************************************************
* @brief usage information
* @param scd : pointer to SCD30 parameters
**********************************************************************/
void usage(struct scd_par *scd)
{
printf( "%s [options] (version %d.%d) \n\n"
"SCD30 settings: \n"
"-a set Automatic Self Calibration (ASC) (default)\n"
"-n set NO ASC\n"
"-i # measurement interval period SCD30 (default %d)\n"
"-f # set forced recalibration value (No default)\n"
"-m # set current altitude in meters (No default)\n"
"-o # set temperature offset in *C (No default)\n"
"-p # set ambient pressure mbar (No default)\n\n"
"-k stop continuous measurement (No default)\n"
"-c set for continuous measurement (default)\n"
"-S perform single measurement (No default)\n"
"-b Only display measurement interval\n"
"-r Only display forced recalibration factor\n"
"-e Only display temperature offset\n"
"\nprogram settings\n"
"-B Do not display output in color\n"
"-l # number of measurements (0 = endless) (default %d)\n"
"-w # waittime (seconds) between measurements (default %d)\n"
"-v # verbose/ debug level (0 - 2) (default %d)\n"
"-t add timestamp to output (default no stamp)\n"
"-j add device info to output\n"
"-x add dew-point to output\n"
"-u add heat-index to output\n"
"-F show temperature in Fahrenheit\n"
#ifdef DYLOS
"\nDylos DC1700: \n"
"-D port Enable Dylos input from port (No default)\n"
#endif
"\nI2C settings: \n"
"-H use hardware I2C (default:soft_I2C)\n"
"-q # set I2C speed (default is %dkhz)\n"
"-s # set SDA GPIO for soft_I2C (default GPIO %d)\n"
"-d # set SCL GPIO for soft_I2C (default GPIO %d)\n"
"-P set internal pullup resistor on SDA/SCL (default not set)\n"
,progname, VERSIONMAJOR, VERSIONMINOR, scd->interval, scd->loop_count, scd->loop_delay, scd->verbose,
SCD30_SPEED, DEF_SDA, DEF_SCL);
}
/*********************************************************************
* Parse parameter input
* @param scd : pointer to SCD30 parameters
*********************************************************************/
void parse_cmdline(int opt, char *option, struct scd_par *scd)
{
switch (opt) {
case 'a': // set Automatic Self Calibration (ASC)
scd->asc = true;
break;
case 'b': // get measurement interval
scd->g_measurement = true;
break;
case 'r': // get forced recalibration factor
scd->g_FRC = true;
break;
case 'e': // get temperature offset
scd->g_temp_offset = true;
break;
case 'g': // get altitude compensation
scd->g_altitude = true;
break;
case 'j': // read firmware level
scd->d_deviceinfo = true;
break;
case 'n': // set NO Automatic Self Calibration (ASC)
scd->asc = false;
break;
case 'm': // altitude in meters
scd->altitude = (int16_t) strtod(option, NULL);
// 700 mbar ~ 3040M altitude, 1200mbar ~ -1520
if (scd->altitude < -1520 || scd->altitude > 3040)
{
p_printf (RED, (char *) "Incorrect altitude. Must be between -1520 and 3040 meter\n");
exit(EXIT_FAILURE);
}
if (scd->pressure != -1)
{
p_printf (RED, (char *) "Either set altitude or pressure\n");
exit(EXIT_FAILURE);
}
break;
case 'p': // pressure in Mbar
scd->pressure = (int16_t) strtod(option, NULL);
// setting to zero will de-activate
if (scd->pressure != 0)
{
// must be between 700 and 1200 mbar
if (scd->pressure < 700 || scd->pressure > 1200)
{
p_printf (RED, (char *) "Incorrect pressure. Must be between 700 and 1200 mbar\n");
exit(EXIT_FAILURE);
}
}
if (scd->altitude != -1)
{
p_printf (RED, (char *) "Either set altitude or pressure\n");
exit(EXIT_FAILURE);
}
break;
case 'i': // SCD30 interval
scd->interval = (uint16_t) strtod(option, NULL);
// must be between 2 and 1800 seconds
if (scd->interval < 2 || scd->interval > 1800)
{
p_printf (RED, (char *) "Incorrect interval %d. Must be between 2 and 1800 seconds\n", scd->interval);
exit(EXIT_FAILURE);
}
break;
case 'o': // temperature offset
scd->temp_offset = (uint16_t) strtod(option, NULL);
// must be between 0 and 25 degrees
if (scd->temp_offset < 0 || scd->temp_offset > 25)
{
p_printf (RED, (char *) "Incorrect temperature offset %d. Must be between 0 and 25C degrees\n",scd->temp_offset );
exit(EXIT_FAILURE);
}
break;
case 'f': // SCD30 forced recalibration value
scd->frc = (uint16_t) strtod(option, NULL);
scd->asc = false;
// must be between 400 and 2000 ppm
if (scd->frc < 400 || scd->frc > 2000)
{
p_printf (RED, (char *) "Incorrect recalibration value (FRC) %d. Must be between 400 and 2000 ppm\n", scd->frc);
exit(EXIT_FAILURE);
}
break;
case 'S': // perform single measurement (NO break !)
scd->perform_single = true;
case 'k': // stop continuous measurement
scd->interval = 0; // set for NO interval to stop in MySensor.begin()
break;
case 'B': // set for no color output
NoColor = true;
break;
case 'l': // loop count
scd->loop_count = (uint16_t) strtod(option, NULL);
break;
case 'w': // loop delay in between measurements
scd->loop_delay = (uint16_t) strtod(option, NULL);
break;
case 't': // Add timestamp to output
scd->timestamp = true;
break;
case 'F': // show temperature in Fahrenheit
scd->tempCel = false;
break;
case 'v': // set verbose / debug level
scd->verbose = (int) strtod(option, NULL);
// must be between 0 and 2
if (scd->verbose < 0 || scd->verbose > 2)
{
p_printf (RED, (char *) "Incorrect verbose/debug. Must be 0,1, 2 \n");
exit(EXIT_FAILURE);
}
break;
case 'u': // add heatindex to output
scd->heatindex = true;
break;
case 'x': // add dewpoint to output
scd->dewpoint = true;
break;
case 'H': // i2C interface
MySensor.settings.I2C_interface = hard_I2C;
break;
case 'P': // enable internal BCM2835 pullup resistor
MySensor.settings.pullup = true;
break;
case 'q': // i2C Speed
MySensor.settings.baudrate = (uint32_t) strtod(option, NULL);
if (MySensor.settings.baudrate < 1 || MySensor.settings.baudrate > 400)
{
p_printf(RED,(char *) "Invalid i2C speed option %dKhz\n",MySensor.settings.baudrate );
exit(EXIT_FAILURE);
}
break;
case 'd': // change default SCL line for soft_I2C
MySensor.settings.scl = (int)strtod(option, NULL);
if (MySensor.settings.scl < 2 || MySensor.settings.scl == 4 ||
MySensor.settings.scl > 27 || MySensor.settings.sda == MySensor.settings.scl)
{
p_printf(RED,(char *) "invalid GPIO for SCL : %d\n",MySensor.settings.scl);
exit(EXIT_FAILURE);
}
break;
case 's': // change default SDA line for soft_I2C
MySensor.settings.sda = (int)strtod(option, NULL);
if (MySensor.settings.sda < 2 || MySensor.settings.sda == 4 ||
MySensor.settings.sda > 27 || MySensor.settings.sda == MySensor.settings.scl)
{
p_printf(RED,(char *) "Invalid GPIO for SDA : %d\n", MySensor.settings.sda);
exit(EXIT_FAILURE);
}
break;
case 'D': // include Dylos read
#ifdef DYLOS
strncpy(scd->dylos.port, option, MAXBUF);
scd->dylos.include = true;
#else
p_printf(RED, (char *) "Dylos is not supported in this build\n");
#endif
break;
case 'h': // help (No break)
default: /* '?' */
usage(scd);
exit(EXIT_FAILURE);
}
}
/***********************
* program starts here
**********************/
int main(int argc, char *argv[])
{
int opt;
struct scd_par scd; // parameters
/* set signals */
set_signals();
/* save name for (potential) usage display */
strncpy(progname,argv[0],20);
/* set the initial values */
init_variables(&scd);
/* parse commandline */
while ((opt = getopt(argc, argv, "abregjni:f:m:o:p:kcSBl:v:w:tHs:d:q:PD:hFxu")) != -1)
{
parse_cmdline(opt, optarg, &scd);
}
/* initialise hardware */
init_hw(&scd);
/* main loop to read SCD30 results */
main_loop(&scd);
closeout();
}