Skip to content

Commit d9fdf06

Browse files
committed
Fix #25, Combine error and t into error code.
1 parent b9167d7 commit d9fdf06

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

SimpleDHT.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ int SimpleDHT11::sample(byte data[40]) {
215215
// 1. PULL LOW 80us
216216
// 2. PULL HIGH 80us
217217
long t = levelTime(LOW); // 1.
218-
if (t < 36) { // specs [2]: 80us
219-
return SimpleDHTErrStartLow;
218+
if (t < 30) { // specs [2]: 80us
219+
return simpleDHTCombileError(t, SimpleDHTErrStartLow);
220220
}
221221

222222
t = levelTime(HIGH); // 2.
223-
if (t < 68) { // specs [2]: 80us
224-
return SimpleDHTErrStartHigh;
223+
if (t < 50) { // specs [2]: 80us
224+
return simpleDHTCombileError(t, SimpleDHTErrStartHigh);
225225
}
226226

227227
// DHT11 data transmite:
@@ -232,13 +232,13 @@ int SimpleDHT11::sample(byte data[40]) {
232232
for (int j = 0; j < 40; j++) {
233233
t = levelTime(LOW); // 1.
234234
if (t < 24) { // specs says: 50us
235-
return SimpleDHTErrDataLow;
235+
return simpleDHTCombileError(t, SimpleDHTErrDataLow);
236236
}
237237

238238
// read a bit
239239
t = levelTime(HIGH); // 2.
240240
if (t < 11) { // specs say: 20us
241-
return SimpleDHTErrDataRead;
241+
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
242242
}
243243
data[ j ] = (t > 40 ? 1 : 0); // specs: 26-28us -> 0, 70us -> 1
244244
}
@@ -247,7 +247,7 @@ int SimpleDHT11::sample(byte data[40]) {
247247
// 1. PULL LOW 50us.
248248
t = levelTime(LOW); // 1.
249249
if (t < 24) { // specs say: 50us
250-
return SimpleDHTErrDataEOF;
250+
return simpleDHTCombileError(t, SimpleDHTErrDataEOF);
251251
}
252252

253253
return SimpleDHTErrSuccess;
@@ -319,10 +319,10 @@ int SimpleDHT22::sample(byte data[40]) {
319319
// 2. T(reh), PULL HIGH 80us(75-85us).
320320
long t = 0;
321321
if ((t = levelTime(LOW)) < 30) {
322-
return SimpleDHTErrStartLow;
322+
return simpleDHTCombileError(t, SimpleDHTErrStartLow);
323323
}
324324
if ((t = levelTime(HIGH)) < 50) {
325-
return SimpleDHTErrStartHigh;
325+
return simpleDHTCombileError(t, SimpleDHTErrStartHigh);
326326
}
327327

328328
// DHT11 data transmite:
@@ -332,13 +332,13 @@ int SimpleDHT22::sample(byte data[40]) {
332332
for (int j = 0; j < 40; j++) {
333333
t = levelTime(LOW); // 1.
334334
if (t < 24) { // specs says: 50us
335-
return SimpleDHTErrDataLow;
335+
return simpleDHTCombileError(t, SimpleDHTErrDataLow);
336336
}
337337

338338
// read a bit
339339
t = levelTime(HIGH); // 2.
340340
if (t < 11) { // specs say: 26us
341-
return SimpleDHTErrDataRead;
341+
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
342342
}
343343
data[ j ] = (t > 40 ? 1 : 0); // specs: 22-30us -> 0, 70us -> 1
344344
}
@@ -347,7 +347,7 @@ int SimpleDHT22::sample(byte data[40]) {
347347
// 1. T(en), PULL LOW 50us(45-55us).
348348
t = levelTime(LOW);
349349
if (t < 24) {
350-
return SimpleDHTErrDataEOF;
350+
return simpleDHTCombileError(t, SimpleDHTErrDataEOF);
351351
}
352352

353353
return SimpleDHTErrSuccess;

SimpleDHT.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,31 @@
2727

2828
#include <Arduino.h>
2929

30+
// High 8bits are time duration.
31+
// Low 8bits are error code.
32+
// For example, 0x0310 means t=0x03 and code=0x10,
33+
// which is start low signal(0x10) error.
34+
// @see https://github.com/winlinvip/SimpleDHT/issues/25
35+
#define simpleDHTCombileError(t, err) ((t << 8) & 0xff00) | (err & 0x00ff)
36+
3037
// Success.
3138
#define SimpleDHTErrSuccess 0
3239
// Error to wait for start low signal.
33-
#define SimpleDHTErrStartLow 100
40+
#define SimpleDHTErrStartLow 0x10
3441
// Error to wait for start high signal.
35-
#define SimpleDHTErrStartHigh 101
42+
#define SimpleDHTErrStartHigh 0x11
3643
// Error to wait for data start low signal.
37-
#define SimpleDHTErrDataLow 102
44+
#define SimpleDHTErrDataLow 0x12
3845
// Error to wait for data read signal.
39-
#define SimpleDHTErrDataRead 103
46+
#define SimpleDHTErrDataRead 0x13
4047
// Error to wait for data EOF signal.
41-
#define SimpleDHTErrDataEOF 104
48+
#define SimpleDHTErrDataEOF 0x14
4249
// Error to validate the checksum.
43-
#define SimpleDHTErrDataChecksum 105
50+
#define SimpleDHTErrDataChecksum 0x15
4451
// Error when temperature and humidity are zero, it shouldn't happen.
45-
#define SimpleDHTErrZeroSamples 106
52+
#define SimpleDHTErrZeroSamples 0x16
4653
// Error when pin is not initialized.
47-
#define SimpleDHTErrNoPin 107
54+
#define SimpleDHTErrNoPin 0x17
4855

4956
class SimpleDHT {
5057
protected:

0 commit comments

Comments
 (0)