forked from DFRobot/DFRobot_OzoneSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFRobot_OzoneSensor.cpp
99 lines (91 loc) · 2.54 KB
/
DFRobot_OzoneSensor.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
/*!
* @file DFRobot_OzoneSensor.cpp
* @brief Define the basic structure of the DFRobot_OzoneSensor class, the implementation of the basic methods
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [ZhixinLiu]([email protected])
* @version V1.0
* @date 2019-07-13
* @url https://github.com/DFRobot/DFRobot_OzoneSensor
*/
#include "DFRobot_OzoneSensor.h"
DFRobot_OzoneSensor::DFRobot_OzoneSensor(TwoWire & wire):
_wire(wire)
{}
DFRobot_OzoneSensor::~DFRobot_OzoneSensor(){}
bool DFRobot_OzoneSensor::begin(uint8_t addr)
{
this->_addr = addr;
_wire.begin();
_wire.beginTransmission(_addr);
if(_wire.endTransmission() == 0){
delay(100);
return true;
}
delay(100);
return false;
}
void DFRobot_OzoneSensor::i2cWrite(uint8_t reg , uint8_t pData)
{
_wire.beginTransmission(_addr);
_wire.write(reg);
_wire.write(pData);
_wire.endTransmission();
}
int16_t DFRobot_OzoneSensor::i2cReadOzoneData(uint8_t reg)
{
uint8_t i = 0;
uint8_t rxbuf[10]={0};
_wire.beginTransmission(_addr);
_wire.write(reg);
_wire.endTransmission();
delay(100);
_wire.requestFrom(_addr, (uint8_t)2);
while (_wire.available())
rxbuf[i++] = _wire.read();
return ((int16_t)rxbuf[0] << 8) + rxbuf[1];
}
void DFRobot_OzoneSensor::setModes(uint8_t mode)
{
if(mode == MEASURE_MODE_AUTOMATIC){
i2cWrite(MODE_REGISTER , MEASURE_MODE_AUTOMATIC);
delay(100);
_M_Flag = 0;
}else if(mode == MEASURE_MODE_PASSIVE){
i2cWrite(MODE_REGISTER , MEASURE_MODE_PASSIVE);
delay(100);
_M_Flag = 1;
}else {
return;
}
}
int16_t DFRobot_OzoneSensor::readOzoneData(uint8_t collectNum)
{
static uint8_t i = 0, j = 0;
if (collectNum > 0) {
for(j = collectNum - 1; j > 0; j--){
ozoneData[j] = ozoneData[j-1];
}
if(_M_Flag == 0){
i2cWrite(SET_PASSIVE_REGISTER , AUTO_READ_DATA);
delay(100);
ozoneData[0] = i2cReadOzoneData(AUTO_DATA_HIGE_REGISTER);
}else if(_M_Flag == 1){
i2cWrite(SET_PASSIVE_REGISTER , PASSIVE_READ_DATA); delay(100); // read passive data in passive mode, first request once, then read the data
ozoneData[0] = i2cReadOzoneData(PASS_DATA_HIGE_REGISTER);
}
if(i < collectNum) i++;
return getAverageNum(ozoneData, i);
}else if(collectNum <= 0 || collectNum > 100){
return -1;
}
return 0;
}
int DFRobot_OzoneSensor::getAverageNum(int bArray[], int iFilterLen)
{
unsigned long bTemp = 0;
for(uint16_t i = 0; i < iFilterLen; i++) {
bTemp += bArray[i];
}
return bTemp / iFilterLen;
}