-
Notifications
You must be signed in to change notification settings - Fork 1
/
svm30.cpp
1059 lines (875 loc) · 27.7 KB
/
svm30.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
/**
* SVM30 Library Header file
*
* Copyright (c) September 2019, Paul van Haastrecht
*
* SVM30 is a sensor from Sensirion AG.
* All rights reserved.
*
* Development environment specifics:
* Arduino IDE 1.9
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
* Version 1.0 / September 2019 / paulvha
* - Initial version
*
* Version 1.1 / October 2019 / paulvha
* - added dewPoint and heatindex
* - added Temperature selection (Fahrenheit / Celsius)
* - updated examples
* - added example 5
*
* Version 1.2 / August 2020 / paulvha
* it seems that older product version(level 9) of the SGP30 /SVM30 fail to read raw data.
* added support to exclude reading the raw data.
*
* - added raw boolean (default true) to include(true) / exclude (false) raw data
* - added read-delay setting based on the kind of command request.
* - added support for inceptive baseline of the SGP30 (requires level 34 at least)
*
* Version 1.3 / October 2020 / paulvha
* - Added support for Artemis / Apollo3 boards
* - fixed some typo's
* - added example8 (display including a LCD screen)
*
*********************************************************************
*/
#include "svm30.h"
const char * SVM30_VERSION = VERSION;
/**
* @brief constructor and initialize variables
*/
SVM30::SVM30(void) {
_Send_BUF_Length = 0;
_Receive_BUF_Length = 0;
_SVM30_Debug = false;
_started = false;
_wait = DEFAULT_WAIT; // wait time after sending command
_SelectTemp = true; // default to celsius
}
/**
* @brief start SGP30 (if not started already)
*
* @return
* true on success else false
*/
bool SVM30::StartSGP30() {
if (! _started) {
PrepSendBuffer(SGP30_ADDRESS, SGP30_Init_Air_Quality);
// send request (no response expected)
if (SendToSVM() != ERR_OK) {
if (_SVM30_Debug) printf("Error during requesting init Air Quality\n");
return(false);
}
_started = true;
}
return(true);
}
/**
* @brief Manual assigment I2C communication port added 1.2
*
* @param port : I2C communication channel to be used
*
* User must have preform the wirePort.begin() in the sketch.
*/
bool SVM30::begin(TwoWire *wirePort)
{
_i2cPort = wirePort; // Grab which port the user wants us to use
if (! reset(SGP30)) return(false);
// start SGP30
return(StartSGP30());
return(true);
}
/**
* @brief : Initialize the communication
*
* @return :
* true on success else false
*/
bool SVM30::begin() {
Wire.begin();
_i2cPort = &Wire;
if (! reset(SGP30)) return(false);
// start SGP30
return(StartSGP30());
}
/**
* @brief : Enable or disable the printing of debug messages.
*
* @param act :
* false : no debug messages
* true : debug messages
*/
void SVM30::EnableDebugging(bool act) {
_SVM30_Debug = act;
}
/**
* @brief : return driver version.
*
* @return : driver information string
*/
const char * SVM30::GetDriverVersion() {
return (SVM30_VERSION);
}
/**
* @brief : Set temperature.
*
* @param act : true is Celsius, false is Fahrenheit
*
*/
void SVM30::SetTempCelsius(bool act) {
_SelectTemp = act;
}
/**
* @brief : reset SGP30 or SHTC1
*
* @param device : I2C address of device
*
* @return :
* true on success else false
*/
bool SVM30::reset(uint8_t device) {
if (device == SGP30_ADDRESS){
PrepSendBuffer(RESET_ADDRESS, RESET_CMD);
if (_SVM30_Debug) printf("WARNING: reset ALL devices on I2C\n");
}
else if (device == SHTC1_ADDRESS)
PrepSendBuffer(SHTC1_ADDRESS, SHTC1_Reset);
else
return(false);
// send Request to sensor(s)
if (SendToSVM() != ERR_OK) {
// on the SGP30 this could be normal, so ignore
if (device == SHTC1_ADDRESS) {
if (_SVM30_Debug) printf("Error during reset\n");
return(false);
}
}
// give time to settle reset // WAS 500
delay(1000);
_started = false;
return(true);
}
/**
* @brief calculate dew point
*
* Using both Rothfusz and Steadman's equations
* http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
*/
void SVM30::computeHeatIndex(struct svm_values *v) {
float hi, temperature;
/* if Celsius turn to Fahrenheit */
if (_SelectTemp) temperature = (v->temperature/1000 * 1.8) + 32;
else temperature = v->temperature /1000;
float percentHumidity = v->humidity/1000;
/* 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 */
if (_SelectTemp) v->heat_index = (hi - 32) * 0.55555;
else v->heat_index = hi;
}
/**
* @brief calculate dew point
*
* using the Augst-Roche-Magnus Approximation.
*
*/
void SVM30::calc_dewpoint(struct svm_values *v) {
float H;
float temp = (float) v->temperature /1000;
float hum = (float) v->humidity/1000;
/* if Fahrenheit turn to Celsius */
if (! _SelectTemp) temp = (temp - 32) * 0.55555;
/* calculate */
H = log(hum/100) + ((17.625 * temp) / (243.12 + temp));
v->dew_point = 243.04 * H / (17.625 - H);
/* if Fahrenheit was input, convert */
if (! _SelectTemp) v->dew_point = (v->dew_point * 1.8) + 32;
}
/**
* @brief : read feature set from SGP30
*
* @param buf : buffer to store feature set number (min 2)
*
* @return :
* true on success else false
*/
bool SVM30::GetFeatureSet(char *buf) {
PrepSendBuffer(SGP30_ADDRESS, SGP30_Get_Feature_Set);
// send Request and read from sensor
if (RequestFromSVM(2) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading Feature set\n");
return(false);
}
// copy feature set
buf[0] = _Receive_BUF[0];
buf[1] = _Receive_BUF[1];
return(true);
}
/**
* @brief : Measure test on SGP30
*
* @return :
* true on success else false
*/
bool SVM30::MeasureTest() {
bool restart = false;
if (_started) {
if (! reset(SGP30)) return(false);
restart = true;
}
PrepSendBuffer(SGP30_ADDRESS, SGP30_Measure_Test);
// set for longer wait time
_wait = MEASURE_WAIT;
// send Request and read from sensor
if (RequestFromSVM(2) != ERR_OK) {
if (_SVM30_Debug) printf("Error during measurement test\n");
return(false);
}
// set for default wait time
_wait = DEFAULT_WAIT;
// check the return code
if (_Receive_BUF[0] != (SGP30_TestOK >> 8 & 0xff) || _Receive_BUF[1] != (SGP30_TestOK & 0xff) ) {
if (_SVM30_Debug) printf("Error in measurement test return code\n");
return(false);
}
if (restart) return(StartSGP30());
return(true);
}
/**
* @brief : get BOTH baselines (TVOC and CO2)
*
* @param : pointer to baseline to store results
*
* @return :
* true on success else false
*/
bool SVM30::GetBaseLines(uint32_t *baseline) {
uint16_t base;
// first get TVOC
if ( ! GetBaseLine(&base, true)) return(false);
*baseline = base << 16;
// get CO2
if ( ! GetBaseLine(&base, false)) return(false);
*baseline = *baseline | base;
return(true);
}
/**
* @brief read baseline from SGP30
*
* @param baseline : store baseline
*
* @param tvoc :
* true get tvoc baseline
* false get CO2eq baseline
*
* source : datasheet
* The sensor responds with 2 data bytes (MSB first) and 1 CRC byte
* for each of the two values in the order CO 2 eq and TVOC.
*
* REMARK: The order is TVOC first then CO2 (seen in sample code)
*
* @return :
* true on success else false
*/
bool SVM30::GetBaseLine(uint16_t *baseline , bool tvoc) {
PrepSendBuffer(SGP30_ADDRESS, SGP30_Get_Baseline);
// send Request and read from sensor
if (RequestFromSVM(4) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading baseline\n");
return(false);
}
// copy baseline
if (tvoc) *baseline = byte_to_uint16(0);
else *baseline = byte_to_uint16(2);
return(true);
}
/**
* @brief : get Inceptivebaseline (impact TVOC only)
*
* See datasheet May 2020 SGP30
*
* !!! REQUIRES LEVEL 34 FEATURE SET !!!
* @param *baseline
* Store the baseline
*
* @return :
* true on success else false
*/
bool SVM30::GetInceptiveBaseLine_TVOC(uint16_t *baseline) {
PrepSendBuffer(SGP30_ADDRESS, SGP30_Get_tvoc_inceptive_baseline);
// send Request and read from sensor
if (RequestFromSVM(2) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading Inceptivebaseline\n");
return(false);
}
// copy baseline
*baseline = byte_to_uint16(0);
return(true);
}
/**
* @brief : set Inceptivebaseline (impact TVOC only)
*
* See datasheet May 2020 SGP30
*
* !!! REQUIRES LEVEL 34 FEATURE SET !!!
* @param baseline
* Store the baseline to set (previous obtained with GetInceptiveBaseLine_TVOC
*
* @return :
* true on success else false
*/
bool SVM30::SetInceptiveBaseLine_TVOC(uint16_t baseline) {
char data[2];
if (baseline == 0x0) {
if (_SVM30_Debug) printf("Error during setting Inceptivebaseline. Baseline can NOT be zero\n");
return(false);
}
data[0] = (baseline >> 8) & 0xff;// MSB
data[1] = baseline & 0xff; // LSB
PrepSendBuffer(SGP30_ADDRESS, SGP30_Set_tvoc_inceptive_baseline, data, 2);
// send Request to sensor
if (SendToSVM() != ERR_OK) {
if (_SVM30_Debug) printf("Error during setting Inceptivebaseline\n");
return(false);
}
return(true);
}
/**
* @brief : set BOTH baselines (TVOC and CO2)
*
* @param baseline
*
* @return :
* true on success else false
*/
bool SVM30::SetBaseLines(uint32_t baseline) {
uint16_t base;
// first set CO2
base = baseline & 0xffff;
if ( ! SetBaseLine(base, false)) return(false);
// set TVOC
base = (baseline >> 16) & 0xffff;
if ( ! SetBaseLine(base, true)) return(false);
return(true);
}
/**
* @brief set baseline on SGP30
*
* @param baseline: baseline to set
*
* @param tvoc :
* true set tvoc baseline
* false set CO2eq baseline
*
* source datasheet:
* After a power-up or soft reset, the baseline of the baseline compensation
* algorithm can be restored by sending first an “Init_air_quality” command followed
* by a “Set_baseline” command with the two baseline values as parameters
* in the order as (TVOC, CO 2 eq)
*
* WARNING: The datasheet is NOT completly correct, as a CRC has to
* be added after each baseline.
*
* @return :
* true on success else false
*/
bool SVM30::SetBaseLine(uint16_t baseline, bool tvoc) {
char data[4];
uint16_t base, len;
/* Setting a baseline of 0x0000 on CO2, will result in CO2
* being set the same as TVOC. Setting TVOC to 0x0000 is ignored
* by the SGP30
*
* Hence a baseline of 0x0000 will be treated as an error */
if (baseline == 0x0) {
if (_SVM30_Debug) printf("Error during setting baseline. Baseline can NOT be zero\n");
return(false);
}
if (tvoc) {
data[0] = (baseline >> 8) & 0xff;// MSB update TVOC
data[1] = baseline & 0xff; // LSB
len = 2;
}
else { // CO2
// first read current baseline TVOC
if ( !GetBaseLine_TVOC(&base)) return(false);
data[0] = (base >> 8) & 0xff; // MSB keep TVOC
data[1] = base & 0xff; // LSB
data[2] = (baseline >> 8) & 0xff;// MSB update Co2
data[3] = baseline & 0xff; // LSB
len = 4;
}
PrepSendBuffer(SGP30_ADDRESS, SGP30_Set_Baseline, data, len);
// send Request to sensor
if (SendToSVM() != ERR_OK) {
if (_SVM30_Debug) printf("Error during setting baseline\n");
return(false);
}
return(true);
}
/**
* @brief set humidity on SGP30
*
* @param humidity: absolute humidity value to set
*
* @return :
* true on success else false
*/
bool SVM30::SetHumidity(float humidity) {
char data[2];
if (humidity > 256000 || humidity < 0) {
if (_SVM30_Debug) printf("Invalid humidity\n");
return (false);
}
// convert to 8.8 fixed point
uint16_t ConvHum = ConvAbsolute(humidity);
data[0] = (ConvHum >> 8) & 0xff; //MSB
data[1] = ConvHum & 0xff; //LSB
PrepSendBuffer(SGP30_ADDRESS, SGP30_Set_Humidity, data, 2);
// send Request to sensor
if (SendToSVM() != ERR_OK) {
if (_SVM30_Debug) printf("Error during setting humidity\n");
return(false);
}
return(true);
}
/**
* @brief : read ID number from SGP30 or SHTC1
*
* @param device : I2C address of device (SGP30 or SHTC1)
* @param buf : buffer to store ID (SGP30 : 3 words, SHTC1 : 1 word)
*
* @return :
* true on success else false
*/
bool SVM30::GetId(uint8_t device, uint16_t *buf) {
uint8_t i = 0, j = 0, len;
if (device == SGP30_ADDRESS) {
/* The get serial ID command returns 3 words (6 bytes) and
* every word (2 bytes) is followed by an 8-bit CRC checksum.
* Together the 3 words constitute a unique serial ID with a length of 48 bits.*/
len = 6;
PrepSendBuffer(SGP30_ADDRESS, SGP30_Read_ID);
}
else if (device == SHTC1_ADDRESS) {
/* After the SHTC1 has acknowledged the proper reception of
* the command, the master can send an I2C read header and
* the SHTC1 will submit the 16-bit ID followed by 8 bits of CRC.
* REMARK : only bit 5:0 are valid for SHTC1 ID (source: datasheet)*/
len = 2;
PrepSendBuffer(SHTC1_ADDRESS, SHTC1_Read_ID);
}
else
return(false);
// send Request and read from sensor
if (RequestFromSVM(len)){
if (_SVM30_Debug) printf("Error during get ID\n");
return(false);
}
// copy received ID number
for (i = 0; i < len; i += 2) buf[j++] = byte_to_uint16(i);
return(true);
}
/**
* @brief : check if SVM30 sensors are available (read ID)
*
* @return :
* true on success else false
*/
bool SVM30::probe() {
uint16_t buf[3]; // SGP30 has 3 words, SHTC1 has 1 word
if (GetId(SGP30_ADDRESS, buf) != true){
if (_SVM30_Debug) printf("Error during probe SGP30 at address 0x%x\n", SGP30_ADDRESS );
return(false);
}
if (GetId(SHTC1_ADDRESS, buf) != true) {
if (_SVM30_Debug) printf("Error during probe SHTC1 at address 0x%x\n", SHTC1_ADDRESS);
return(false);
}
return(true);
}
/**
* @brief : Trigger a read on the SGP30
*
* The on-chip baseline compensation algorithm has been optimized
* for 1HZ sampling rate. The sensor shows best performance when
* used with this sampling rate.
*
* Sample rate to be implemented in the sketch (see examples)
*
* @return :
* true on success else false
*/
bool SVM30::TriggerSGP30() {
// SGP30 measurement started already?
if ( ! _started)
if (! StartSGP30()) return(false);
// get TVOC and CO2 equivalent data
PrepSendBuffer(SGP30_ADDRESS, SGP30_Measure_Air_Quality);
// send Request and read from sensor
if (RequestFromSVM(4) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading TVOC and CO2\n");
return(false);
}
return(true);
}
/**
* @brief : read all measurement values from the sensor
* calculate others and store in structure
* @param v: pointer to structure to store
* @param raw: if true it will try to read the raw values from the SGP30 (update 1.2)
*
* It seems that older version of the SGP30 do not support reading raw, hence the "raw" -option
* has been added as an option to exclude. By default it will read to stay backward compatible
*
* @return :
* true on success else false
*/
bool SVM30::GetValues(struct svm_values *v, bool raw) {
memset(v,0x0,sizeof(struct svm_values));
/** data from SGP30 */
if (TriggerSGP30() == false) return(false);
v->CO2eq = byte_to_uint16(0);
v->TVOC = byte_to_uint16(2);
if (raw){
// get raw H2 signal and Ethanol signal
PrepSendBuffer(SGP30_ADDRESS, SGP30_Measure_Raw_Signals);
// send Request and read from sensor
if (RequestFromSVM(4) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading Raw signals\n");
return(false);
}
v->H2_signal = byte_to_uint16(0);
v->Ethanol_signal = byte_to_uint16(2);
}
else {
v->H2_signal = 0;
v->Ethanol_signal = 0;
}
/** data from SHTC1 */
PrepSendBuffer(SHTC1_ADDRESS, SHTC1_Read_Temp_First);
// send Request and read from sensor
if (RequestFromSVM(4) != ERR_OK) {
if (_SVM30_Debug) printf("Error during reading SHTC1\n");
return(false);
}
// get the raw values from the SHTC
v->r_temperature = byte_to_uint16(0);
v->r_humidity = byte_to_uint16(2);
/** different calculations */
// convert to useable temperature and humidity
shtc1_conv(&v->temperature, &v->humidity, v->r_temperature, v->r_humidity);
// calculate absolute humidity
calc_absolute_humidity(v);
// calculate heat Index
computeHeatIndex(v);
// calculate dew_point
calc_dewpoint(v);
return(true);
}
/**
* @brief : translate 2 bytes to uint16
* @param x : offset in _Receive_BUF
*
* assumed is MSB first, LSB second byte
*
* @return : uint16_t number
*/
uint16_t SVM30::byte_to_uint16(int x) {
uint16_t val;
val = _Receive_BUF[x] << 8 | _Receive_BUF[x+1];
return val;
}
/**
* @brief : Fill buffer to send over I2C communication
* @param device : I2C address to use (either SGP30 or SHTC1)
* @param cmd: I2C commmand for device
* @param param : additional parameters to add
* @param len : length of parameters to add (zero if none)
*
*/
void SVM30::PrepSendBuffer(uint8_t device, uint16_t cmd, char *param, uint8_t len) {
uint8_t i = 0, j, c;
_I2C_address = device;
// add command
_Send_BUF[i++] = cmd >> 8 & 0xff; //0 MSB
_Send_BUF[i++] = cmd & 0xff; //1 LSB
// additional parameters to add?
if (len != 0) {
for (j = 0, c = 0 ; j < len; j++) {
// add bytes
_Send_BUF[i++] = param[j];
// add CRC after each 2 bytes
if(++c == 2){
_Send_BUF[i] = CalcCrC(&_Send_BUF[i - 2]);
i++;
c = 0;
}
}
}
// 1.2 : the delay is now depending on the Measurement Commands typical
// timing as defined in the datasheet table 13
switch(cmd) {
case SGP30_Measure_Test:
ReadDelay = 220;
break;
case SGP30_Measure_Raw_Signals:
ReadDelay = 25;
break;
default:
ReadDelay = 10; // default 10 ms
break;
}
_Send_BUF_Length = i;
}
/**
* @brief : send a prepared command (with PrepsendBuffer())
*
* @return :
* Ok ERR_OK
* else error
*/
uint8_t SVM30::SendToSVM() {
if (_Send_BUF_Length == 0) return(ERR_DATALENGTH);
if (_SVM30_Debug) {
printf("Sending to 0x%x: ",_I2C_address );
for(byte i = 0; i < _Send_BUF_Length; i++)
printf("0x%02X ", _Send_BUF[i]);
}
_i2cPort->beginTransmission(_I2C_address);
_i2cPort->write(_Send_BUF, _Send_BUF_Length);
if( _i2cPort->endTransmission() != 0) return ERR_PROTOCOL;
_Send_BUF_Length = 0;
// give time to react on request
delay(_wait);
return(ERR_OK);
}
/**
* @brief : sent command/request and read from SVM30 sensor
* @param cnt: number of data bytes to get
*
* @return :
* OK ERR_OK
* else error
*/
uint8_t SVM30::RequestFromSVM(uint8_t cnt) {
uint8_t ret;
#if defined STABIILITY // see SVM30.h
uint8_t retry = RETR_CNT;
RETRY:
#endif
// sent Request
ret = SendToSVM();
if (ret != ERR_OK) {
if (_SVM30_Debug) printf("Can not sent request\n");
return(ret);
}
// read from Sensor
ret = ReadFromSVM(cnt);
if (ret != ERR_OK) {
#if defined STABIILITY // see SVM30.h
// Optional retry mechanism to improve stability
if (--retry > 0) {
// printf("R\ N");
if (_I2C_address == SGP30_ADDRESS) reset(SGP30);
else reset(SHTC1);
goto RETRY;
}
#endif
if (_SVM30_Debug) printf("Error during reading. Errorcode: 0x%02X\n", ret);
}
if (_SVM30_Debug){
printf(", Received: ");
for(byte i = 0; i < _Receive_BUF_Length; i++) printf("0x%02X ",_Receive_BUF[i]);
printf("length: %d\n",_Receive_BUF_Length);
}
return(ret);
}
/**
* @brief : receive from Sensor
* @param count : number of data bytes to read
*
* @return :
* OK ERR_OK
* else error
*/
uint8_t SVM30::ReadFromSVM(uint8_t count) {
uint8_t data[3];
uint8_t i, j;
j = i = _Receive_BUF_Length = 0;
// 2 data bytes + crc
_i2cPort->requestFrom(_I2C_address, uint8_t (count / 2 * 3));
// 1.2 : the delay is now depending on the Measurement Commands typical
// timing as defined in the datasheet table 13
delay(ReadDelay);
while (_i2cPort->available()) {
data[i++] = _i2cPort->read();
// 2 bytes data, 1 CRC
if( i == 3) {
if (data[2] != CalcCrC(&data[0])){
if (_SVM30_Debug){
printf("I2C CRC error: Expected 0x%02X, calculated 0x%02X\n",data[2] & 0xff,CalcCrC(&data[0]) &0xff);
}
return(ERR_PROTOCOL);
}
_Receive_BUF[_Receive_BUF_Length++] = data[0];
_Receive_BUF[_Receive_BUF_Length++] = data[1];
i = 0;
if (_Receive_BUF_Length >= count) break;
}
}
if (i != 0) {
if (_SVM30_Debug) printf("Error: Data counter %d\n",i);
while (j < i) _Receive_BUF[_Receive_BUF_Length++] = data[j++];
}
if (_Receive_BUF_Length == 0) {
if (_SVM30_Debug) printf("Error: Received NO bytes\n");
return(ERR_PROTOCOL);
}
if (_Receive_BUF_Length == count) return(ERR_OK);
if (_SVM30_Debug)
printf("Error: Expected bytes : %d, Received bytes %d\n", count,_Receive_BUF_Length);
return(ERR_DATALENGTH);
}
/**
* @brief : calculate CRC for I2c comms
* @param data : 2 databytes to calculate the CRC from
*
* Source : datasheet SPS30
*
* return CRC
*/
uint8_t SVM30::CalcCrC(uint8_t *data) {
uint8_t crc = 0xFF;
for(int i = 0; i < 2; i++) {
crc ^= data[i];
for(uint8_t bit = 8; bit > 0; --bit) {
if(crc & 0x80) {
crc = (crc << 1) ^ 0x31u;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
/**
* @brief : calculate the absolute humidity from relative humidity [%RH *1000]
* and temperature [mC]
*/
void SVM30::calc_absolute_humidity(struct svm_values *v) {
float Temp = (float) v->temperature /1000;
float Hum = (float) v->humidity/1000;
// if temperature is Fahrenheit turn to Celsius
if (! _SelectTemp) Temp = (Temp - 32) * 0.55555;
if (Hum == 0) return;
v->absolute_hum = (6.112 * pow(2.71828,((17.67 * Temp)/(Temp + 243.5))) * Hum * 2.1674) / (273.15 + Temp);
}
/**
* Convert relative humidity [%RH] and temperature [C] to
* absolute humidity [g/m^3] that can be used as input for
* the SetHumidity() call.
*
* source: datasheet
* The 2 data bytes represent humidity values as a fixed-point 8.8bit
* number with a minimum value of 0x0001 (=1/256 g/m 3 ) and a maximum
* value of 0xFFFF (255 g/m 3 + 255/256 g/m 3 ).
* For instance, sending a value of 0x0F80 corresponds to a humidity
* value of 15.50 g/m3 (15 g/m3 + 128/256 g/m 3 ).
*/
uint16_t SVM30::ConvAbsolute(float AbsoluteHumidity) {
uint16_t val;
int val1;