forked from casper-sv/ZE07_CO_Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZE07CO_Sensor.cpp
128 lines (113 loc) · 3.13 KB
/
ZE07CO_Sensor.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
/*
ZE07CO_Sensor.cpp - ZE07-CO_Sensor library
Developed by Valentin - 2018/01/03
This library 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 2.1 of the License, or (at your option) any later version.
This library 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.
Version 1.0: 03 Jan 2018 by Valentin
- Updated to build against Arduino 1.0.6
- Made accessors inline in the header so they can be optimized away
*/
#include "ZE07CO_Sensor.h"
ZE07CO_Sensor::ZE07CO_Sensor(HardwareSerial* Serial) //read the uart signal by hardware uart,such as D0
{
mySerial = Serial;
receivedFlag = 0;
}
ZE07CO_Sensor::ZE07CO_Sensor(SoftwareSerial* Serial) //read the uart signal by software uart,such as D10
{
mySerial = Serial;
receivedFlag = 0;
}
ZE07CO_Sensor:: ZE07CO_Sensor(int pin,float ref)//read the analog signal by analog input pin ,such as A2; ref:voltage on AREF pin
{
_sensorPin = pin;
_ref = ref; //for arduino uno ,the ref should be 5.0V(Typical)
}
byte ZE07CO_Sensor::checkSum(byte array[],byte length)
{
byte sum = 0;
for(int i = 1; i < length-1; i ++)
{
sum += array[i];
}
sum = (~sum) + 1;
return sum;
}
void ZE07CO_Sensor::boucle()
{
_status = STATUS_WAITING;
if (mySerial->available()){
uint8_t ch = mySerial->read();
switch (_index){
case 0:
if (ch != 0xFF){
return;
}
receivedCommandStack[_index]=ch;
break;
case 1:
if (ch != 0x04){
_index = 0;
return;
}
receivedCommandStack[_index]=ch;
break;
case 2:
if (ch != 0x03){
_index = 0;
return;
}
receivedCommandStack[_index]=ch;
break;
default:
if (_index==8){
_status = STATUS_OK;
}
receivedCommandStack[_index]=ch;
break;
}
_index++;
}
}
boolean ZE07CO_Sensor::available(uint16_t timeout) //new data was recevied
{
if (timeout > 0){
uint32_t start = millis();
do{
boucle();
if (_status == STATUS_OK) break;
} while (millis() - start < timeout);
} else {
boucle();
}
if ((receivedCommandStack[MAXLENGTH-1]==checkSum(receivedCommandStack,MAXLENGTH)) && (_status == STATUS_OK)){
receivedFlag = 1; //new data received
return receivedFlag;
} else {
receivedFlag = 0; //data loss or error
return receivedFlag;
}
}
float ZE07CO_Sensor::uartReadPPM()
{
receivedFlag = 0;
float ppm = (unsigned int)receivedCommandStack[4]<<8 | receivedCommandStack[5]; // bit 4: ppm high 8-bit; bit 5: ppm low 8-bit
ppm = ppm / 10.0;
return ppm;
}
float ZE07CO_Sensor::dacReadPPM()
{
float analogVoltage = analogRead(_sensorPin) / 1024.0 * _ref;
float ppm = (3.125 * analogVoltage - 1.25) * 100; //linear relationship(0.4V for 0 ppm and 2V for 500ppm)
if(ppm<0)
ppm=0;
else if(ppm>500)
ppm = 500;
return ppm;
}