-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFRobot_SGP40.cpp
165 lines (140 loc) · 3.78 KB
/
DFRobot_SGP40.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
/**
*@file DFRobot_SGP40.cpp
*@brief Define the DFRobot_SGP40 class infrastructure, the implementation of the underlying methods
*@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
*@SKU SEN0392
*@licence The MIT License (MIT)
*@author [yangfeng]<[email protected]>
*@version V1.0
*@date 2020-12-18
*@url https://github.com/DFRobot/DFRobot_SGP40
*/
#include <SoftI2C.h>
#include "DFRobot_SGP40.h"
#include "src/sgp_40/sensirion_arch_config.h"
#include "src/sgp_40/sensirion_voc_algorithm.h"
DFRobot_SGP40::DFRobot_SGP40(SoftI2C *pWire):
_pWire(pWire),_temperatureC(25)
{
_deviceAddr = DFRobot_SGP40_ICC_ADDR;
_relativeHumidity = 50.0;
_temperatureC=25.0;
}
// >>>>> 改为不while的预热方式 >>>>>
long timestamp;
uint32_t duration;
bool DFRobot_SGP40::begin(uint32_t d)
{
_pWire->begin();
VocAlgorithm_init(&_vocaAgorithmParams);
duration = d;
timestamp = millis();
return true;
}
bool DFRobot_SGP40::warmUp(){
if(millis()-timestamp<duration){
getVoclndex();
return false;
}
return sgp40MeasureTest();
}
// <<<<< 改为不while的预热方式 <<<<<
uint8_t DFRobot_SGP40::checkCrc(uint8_t data1,uint8_t data2)
{
uint8_t crc = 0xFF;
uint8_t data[2];
data[0]=data1;
data[1]=data2;
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;
}
void DFRobot_SGP40::setRhT(float relativeHumidity, float temperatureC)
{
_relativeHumidity = relativeHumidity;
_temperatureC = temperatureC;
dataTransform();
write(_rhTemData,6);
}
void DFRobot_SGP40::dataTransform(void)
{
uint16_t RH = (uint16_t)((_relativeHumidity*65535)/100+0.5);
uint16_t TemC = (uint16_t)((_temperatureC+45)*(65535/175)+0.5);
_rhTemData[INDEX_MEASURE_RAW_H]=CMD_MEASURE_RAW_H;
_rhTemData[INDEX_MEASURE_RAW_L]=CMD_MEASURE_RAW_L;
_rhTemData[INDEX_RH_H]=RH>>8;
_rhTemData[INDEX_RH_L]=RH&0x00FF;
_rhTemData[INDEX_RH_CHECK_CRC]=checkCrc(_rhTemData[INDEX_RH_H],_rhTemData[INDEX_RH_L]);
_rhTemData[INDEX_TEM_H]=TemC>>8;
_rhTemData[INDEX_TEM_L]=TemC&0x00FF;
_rhTemData[INDEX_TEM_CHECK_CRC]=checkCrc(_rhTemData[INDEX_TEM_H],_rhTemData[INDEX_TEM_L]);
}
void DFRobot_SGP40::write(uint8_t* cmd,uint8_t len)
{
_pWire->beginTransmission(_deviceAddr);
for(uint8_t i=0;i<len;i++){
_pWire->write(cmd[i]);
}
_pWire->endTransmission();
}
uint16_t DFRobot_SGP40::readRawData()
{
uint8_t data[3]={0,0,0};
uint16_t value=0;
_pWire->requestFrom(_deviceAddr,(uint8_t)3);
for(uint8_t i=0;i<3;i++){
data[i]=_pWire->read();
}
value=(data[0]<<8)|data[1];
return value;
}
uint16_t DFRobot_SGP40::getVoclndex(void)
{
uint8_t data[3]={0,0,0};
int32_t value;
int32_t vocIndex=0;
dataTransform();
_pWire->beginTransmission(_deviceAddr);
for(int i=0;i<8;i++){
_pWire->write(_rhTemData[i]);
}
_pWire->endTransmission();
delay(DURATION_READ_RAW_VOC);
_pWire->requestFrom(_deviceAddr,(uint8_t)3);
for(uint8_t i=0;i<3;i++){
data[i]=_pWire->read();
}
value=(data[0]<<8)|data[1];
VocAlgorithm_process(&_vocaAgorithmParams, value, &vocIndex);
return vocIndex;
}
void DFRobot_SGP40::spg40HeaterOff()
{
uint8_t testCommand[CMD_HEATER_OFF_SIZE]={CMD_HEATER_OFF_H,CMD_HEATER_OFF_L};
write(testCommand,CMD_HEATER_OFF_SIZE);
}
bool DFRobot_SGP40::sgp40MeasureTest()
{
uint8_t testCommand[CMD_MEASURE_TEST_SIZE]={CMD_MEASURE_TEST_H,CMD_MEASURE_TEST_L};
write(testCommand,CMD_MEASURE_TEST_SIZE);
delay(DURATION_WAIT_MEASURE_TEST);
if(readRawData()==TEST_OK){
return true;
}
return false;
}
void DFRobot_SGP40::softReset()
{
uint8_t testCommand[CMD_SOFT_RESET_SIZE]={CMD_SOFT_RESET_H,CMD_SOFT_RESET_L};
write(testCommand,CMD_SOFT_RESET_SIZE);
_relativeHumidity = 50;
_temperatureC = 25;
}