Skip to content

Commit

Permalink
Define errors in SimpleDHT
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 3, 2017
1 parent b598538 commit d7dfe91
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 60 deletions.
69 changes: 35 additions & 34 deletions SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,13 @@ SOFTWARE.

#include "SimpleDHT.h"

int SimpleDHT11::ErrSuccess = 0;
int SimpleDHT11::ErrStartLow = 100;
int SimpleDHT11::ErrStartHigh = 101;
int SimpleDHT11::ErrDataLow = 102;
int SimpleDHT11::ErrDataRead = 103;
int SimpleDHT11::ErrDataEOF = 104;
int SimpleDHT11::ErrDataChecksum = 105;

int SimpleDHT11::confirm(int pin, int us, byte level) {
// wait one more count to ensure.
int cnt = us / 10 + 1;

bool ok = false;
for (int i = 0; i < cnt; i++) {
if (digitalRead(pin) != level) {
ok = true;
break;
}
delayMicroseconds(10);
}

if (!ok) {
return -1;
}
return ErrSuccess;
}

byte SimpleDHT11::bits2byte(byte data[8]) {
byte v = 0;
for (int i = 0; i < 8; i++) {
v += data[i] << (7 - i);
}
return v;
}
int SimpleDHT::ErrSuccess = 0;
int SimpleDHT::ErrStartLow = 100;
int SimpleDHT::ErrStartHigh = 101;
int SimpleDHT::ErrDataLow = 102;
int SimpleDHT::ErrDataRead = 103;
int SimpleDHT::ErrDataEOF = 104;
int SimpleDHT::ErrDataChecksum = 105;

int SimpleDHT11::sample(int pin, byte data[40]) {
// empty output data.
Expand Down Expand Up @@ -120,6 +93,33 @@ int SimpleDHT11::sample(int pin, byte data[40]) {
return ErrSuccess;
}

int SimpleDHT11::confirm(int pin, int us, byte level) {
// wait one more count to ensure.
int cnt = us / 10 + 1;

bool ok = false;
for (int i = 0; i < cnt; i++) {
if (digitalRead(pin) != level) {
ok = true;
break;
}
delayMicroseconds(10);
}

if (!ok) {
return -1;
}
return ErrSuccess;
}

byte SimpleDHT11::bits2byte(byte data[8]) {
byte v = 0;
for (int i = 0; i < 8; i++) {
v += data[i] << (7 - i);
}
return v;
}

int SimpleDHT11::parse(byte data[40], byte* ptemperature, byte* phumidity) {
byte humidity = bits2byte(data);
byte humidity2 = bits2byte(data + 8);
Expand Down Expand Up @@ -161,3 +161,4 @@ int SimpleDHT11::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[4

return ret;
}

40 changes: 22 additions & 18 deletions SimpleDHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@

#include <Arduino.h>

class SimpleDHT {
public:
// Success.
static int ErrSuccess;
// Error to wait for start low signal.
static int ErrStartLow;
// Error to wait for start high signal.
static int ErrStartHigh;
// Error to wait for data start low signal.
static int ErrDataLow;
// Error to wait for data read signal.
static int ErrDataRead;
// Error to wait for data EOF signal.
static int ErrDataEOF;
// Error to validate the checksum.
static int ErrDataChecksum;
};


/*
Simple DHT11
Expand All @@ -43,22 +62,7 @@
https://cdn-shop.adafruit.com/datasheets/DHT11-chinese.pdf
*/
class SimpleDHT11 {
public:
// Success.
static int ErrSuccess;
// Error to wait for start low signal.
static int ErrStartLow;
// Error to wait for start high signal.
static int ErrStartHigh;
// Error to wait for data start low signal.
static int ErrDataLow;
// Error to wait for data read signal.
static int ErrDataRead;
// Error to wait for data EOF signal.
static int ErrDataEOF;
// Error to validate the checksum.
static int ErrDataChecksum;
class SimpleDHT11 : public SimpleDHT {
public:
// to read from dht11.
// @param pin the DHT11 pin.
Expand All @@ -68,7 +72,7 @@ class SimpleDHT11 {
// @remark the min delay for this method is 1s.
// @return SimpleDHT11::ErrSuccess is success; otherwise, failed.
int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
private:
protected:
// confirm the OUTPUT is level in us,
// for example, when DHT11 start sample, it will
// 1. PULL LOW 80us, call confirm(pin, 80, LOW)
Expand All @@ -86,7 +90,7 @@ class SimpleDHT11 {
// @param data a byte[40] to read bits to 5bytes.
// @return 0 success; otherwise, error.
// @remark please use simple_dht11_read().
int sample(int pin, byte data[40]);
virtual int sample(int pin, byte data[40]);
// parse the 40bits data to temperature and humidity.
// @remark please use simple_dht11_read().
int parse(byte data[40], byte* ptemperature, byte* phumidity);
Expand Down
4 changes: 2 additions & 2 deletions examples/DHT11Default/DHT11Default.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void loop() {
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHT11::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHT11::ErrSuccess) {
int err = SimpleDHT::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(err);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/DHT11WithRawBits/DHT11WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void loop() {
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
int err = SimpleDHT11::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHT11::ErrSuccess) {
int err = SimpleDHT::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHT::ErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(err);
return;
}
Expand Down
9 changes: 5 additions & 4 deletions examples/TwoSensorsDefault/TwoSensorsDefault.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ void loop() {
// read without samples.
byte temperature = 0;
byte humidity = 0;
if (dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) {
Serial.print("Communication error with Sensor 1");
int err = SimpleDHT::ErrSuccess;
if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
Serial.print("Communication error with Sensor 1, err="); Serial.print(err);
return;
}

Expand All @@ -46,8 +47,8 @@ void loop() {

byte temperature2 = 0;
byte humidity2 = 0;
if (dht11.read(dataPinSensor2, &temperature2, &humidity2, NULL)) {
Serial.print("Communication error with sensor 2");
if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
Serial.print("Communication error with Sensor 2, err="); Serial.print(err);
return;
}

Expand Down

0 comments on commit d7dfe91

Please sign in to comment.