Skip to content

Commit 2c5a41e

Browse files
committed
Add error codes
1 parent 4f64a07 commit 2c5a41e

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

src/hackair.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void hackAIR::readData(hackAirData &data) {
6868
// If package is valid
6969
if (receiveflag > 0) {
7070
// Set the error flag
71-
data.error = 0;
71+
data.error = H_NO_ERROR;
7272

7373
// Set data
7474
data.pm25 = ((_buff[8] << 8) + _buff[9]);
@@ -80,7 +80,7 @@ void hackAIR::readData(hackAirData &data) {
8080
return;
8181
} else {
8282
// Set the error flag and exit
83-
data.error = 1;
83+
data.error = H_ERROR_SENSOR;
8484
return;
8585
}
8686
}
@@ -103,7 +103,7 @@ void hackAIR::readData(hackAirData &data) {
103103

104104
if ((receiveSum & 0xFF) == _buff[8]) {
105105
// Set the error flag
106-
data.error = 0;
106+
data.error = H_NO_ERROR;
107107

108108
// Set data
109109
data.pm25 = ((_buff[3] << 8) + _buff[2]) / 10.0f;
@@ -112,7 +112,7 @@ void hackAIR::readData(hackAirData &data) {
112112
return;
113113
} else {
114114
// Invalid package, set the error flag
115-
data.error = 1;
115+
data.error = H_ERROR_SENSOR;
116116
return;
117117
}
118118
}
@@ -126,7 +126,7 @@ void hackAIR::readData(hackAirData &data) {
126126

127127
int ratio = _pulseDuration / 20000.0;
128128
data.pm10 = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // From manual
129-
data.error = 0;
129+
data.error = H_NO_ERROR;
130130
return;
131131
}
132132

@@ -144,7 +144,7 @@ void hackAIR::readAverageData(hackAirData &data, uint8_t n) {
144144
// 1Hz sampling rate
145145
readData(data);
146146

147-
if (data.error == 0) {
147+
if (data.error == H_NO_ERROR) {
148148
sum_pm25 += data.pm25;
149149
sum_pm10 += data.pm10;
150150

@@ -156,16 +156,16 @@ void hackAIR::readAverageData(hackAirData &data, uint8_t n) {
156156
data.pm10 = sum_pm10 / successes;
157157

158158
if (successes != n) {
159-
data.error = 1;
159+
data.error = H_ERROR_SENSOR_ONCE;
160160
} else {
161-
data.error = 0;
161+
data.error = H_NO_ERROR;
162162
}
163163
}
164164

165165
void hackAIR::clearData(hackAirData &data) {
166166
data.pm25 = 0;
167167
data.pm10 = 0;
168-
data.error = 0;
168+
data.error = H_NO_ERROR;
169169
data.tamper = 0;
170170
data.battery = 0;
171171
}

src/hackair.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#endif
4141

4242
#include "Arduino.h"
43+
#include "hackair_errors.h"
4344
#include <SoftwareSerial.h>
4445

4546
/**

src/hackair_errors.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* hackAIR Arduino Library
3+
* Copyright © 2016-2017 hackAir Consortium
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/**
20+
* @file Error codes
21+
* @author Thanasis Georgiou
22+
*/
23+
24+
/** No error, everything is fine */
25+
#define H_NO_ERROR 0
26+
27+
/** There was an error reading from the air quality sensor */
28+
#define H_ERROR_SENSOR (1 << 0)
29+
30+
/**
31+
* There was an error reading from the sensor at least once
32+
* but the reading should be correct
33+
*/
34+
#define H_ERROR_SENSOR_ONCE (1 << 1)
35+
36+
/** There was an error reading from the humidity sensor */
37+
#define H_ERROR_HUMIDITY (1 << 2)
38+
39+
/** Something else went wrong */
40+
#define H_ERROR_UNKNOWN (1 << 3)

0 commit comments

Comments
 (0)