Skip to content

Commit

Permalink
Fix #8, #7, use macro to replace class static field. 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 28, 2017
1 parent 0ca48d0 commit 71197ea
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 53 deletions.
44 changes: 18 additions & 26 deletions SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ SOFTWARE.

#include "SimpleDHT.h"

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 SimpleDHT::confirm(int pin, int us, byte level) {
// wait one more count to ensure.
int cnt = us / 10 + 1;
Expand All @@ -48,7 +40,7 @@ int SimpleDHT::confirm(int pin, int us, byte level) {
if (!ok) {
return -1;
}
return ErrSuccess;
return SimpleDHTErrSuccess;
}

byte SimpleDHT::bits2byte(byte data[8]) {
Expand All @@ -67,24 +59,24 @@ int SimpleDHT::parse(byte data[40], byte* ptemperature, byte* phumidity) {
byte check = bits2byte(data + 32);
byte expect = humidity + humidity2 + temperature + temperature2;
if (check != expect) {
return ErrDataChecksum;
return SimpleDHTErrDataChecksum;
}
*ptemperature = temperature;
*phumidity = humidity;
return ErrSuccess;
return SimpleDHTErrSuccess;
}

int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) {
int ret = ErrSuccess;
int ret = SimpleDHTErrSuccess;

byte data[40] = {0};
if ((ret = sample(pin, data)) != ErrSuccess) {
if ((ret = sample(pin, data)) != SimpleDHTErrSuccess) {
return ret;
}

byte temperature = 0;
byte humidity = 0;
if ((ret = parse(data, &temperature, &humidity)) != ErrSuccess) {
if ((ret = parse(data, &temperature, &humidity)) != SimpleDHTErrSuccess) {
return ret;
}

Expand Down Expand Up @@ -120,10 +112,10 @@ int SimpleDHT11::sample(int pin, byte data[40]) {
// 1. PULL LOW 80us
// 2. PULL HIGH 80us
if (confirm(pin, 80, LOW)) {
return ErrStartLow;
return SimpleDHTErrStartLow;
}
if (confirm(pin, 80, HIGH)) {
return ErrStartHigh;
return SimpleDHTErrStartHigh;
}

// DHT11 data transmite:
Expand All @@ -132,7 +124,7 @@ int SimpleDHT11::sample(int pin, byte data[40]) {
// 3. PULL HIGH 70us, bit(1)
for (int j = 0; j < 40; j++) {
if (confirm(pin, 50, LOW)) {
return ErrDataLow;
return SimpleDHTErrDataLow;
}

// read a bit, should never call method,
Expand All @@ -148,18 +140,18 @@ int SimpleDHT11::sample(int pin, byte data[40]) {
delayMicroseconds(10);
}
if (!ok) {
return ErrDataRead;
return SimpleDHTErrDataRead;
}
data[j] = (tick > 3? 1:0);
}

// DHT11 EOF:
// 1. PULL LOW 50us.
if (confirm(pin, 50, LOW)) {
return ErrDataEOF;
return SimpleDHTErrDataEOF;
}

return ErrSuccess;
return SimpleDHTErrSuccess;
}

int SimpleDHT22::sample(int pin, byte data[40]) {
Expand All @@ -181,10 +173,10 @@ int SimpleDHT22::sample(int pin, byte data[40]) {
// 1. PULL LOW 80us
// 2. PULL HIGH 80us
if (confirm(pin, 80, LOW)) {
return ErrStartLow;
return SimpleDHTErrStartLow;
}
if (confirm(pin, 80, HIGH)) {
return ErrStartHigh;
return SimpleDHTErrStartHigh;
}

// DHT11 data transmite:
Expand All @@ -193,7 +185,7 @@ int SimpleDHT22::sample(int pin, byte data[40]) {
// 3. PULL HIGH 70us, bit(1)
for (int j = 0; j < 40; j++) {
if (confirm(pin, 50, LOW)) {
return ErrDataLow;
return SimpleDHTErrDataLow;
}

// read a bit, should never call method,
Expand All @@ -209,17 +201,17 @@ int SimpleDHT22::sample(int pin, byte data[40]) {
delayMicroseconds(10);
}
if (!ok) {
return ErrDataRead;
return SimpleDHTErrDataRead;
}
data[j] = (tick > 3? 1:0);
}

// DHT11 EOF:
// 1. PULL LOW 50us.
if (confirm(pin, 50, LOW)) {
return ErrDataEOF;
return SimpleDHTErrDataEOF;
}

return ErrSuccess;
return SimpleDHTErrSuccess;
}

33 changes: 16 additions & 17 deletions SimpleDHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@

#include <Arduino.h>

// Success.
#define SimpleDHTErrSuccess 0
// Error to wait for start low signal.
#define SimpleDHTErrStartLow 100
// Error to wait for start high signal.
#define SimpleDHTErrStartHigh 101
// Error to wait for data start low signal.
#define SimpleDHTErrDataLow 102
// Error to wait for data read signal.
#define SimpleDHTErrDataRead 103
// Error to wait for data EOF signal.
#define SimpleDHTErrDataEOF 104
// Error to validate the checksum.
#define SimpleDHTErrDataChecksum 105

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;
public:
// to read from dht11.
// @param pin the DHT11 pin.
// @param ptemperature output, NULL to igore.
// @param phumidity output, NULL to ignore.
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 1s.
// @return SimpleDHT11::ErrSuccess is success; otherwise, failed.
// @return SimpleDHTErrSuccess is success; otherwise, failed.
int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
protected:
// confirm the OUTPUT is level in us,
Expand All @@ -76,7 +76,6 @@ class SimpleDHT {
int parse(byte data[40], byte* ptemperature, byte* phumidity);
};


/*
Simple DHT11
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 = SimpleDHT::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
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 = SimpleDHT::ErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHT::ErrSuccess) {
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(err);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/DHT22Default/DHT22Default.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 = SimpleDHT::ErrSuccess;
if ((err = dht22.read(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
int err = SimpleDHTErrSuccess;
if ((err = dht22.read(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(err);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/TwoSensorsDefault/TwoSensorsDefault.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void loop() {
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHT::ErrSuccess;
if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Communication error with Sensor 1, err="); Serial.print(err);
return;
}
Expand All @@ -47,7 +47,7 @@ void loop() {

byte temperature2 = 0;
byte humidity2 = 0;
if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) {
if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Communication error with Sensor 2, err="); Serial.print(err);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SimpleDHT
version=1.0.2
version=1.0.4
author=Winlin <winlin@vip.126.com>
maintainer=Winlin <winlin@vip.126.com>
sentence=Arduino Temp & Humidity Sensors for DHT11 and DHT22.
Expand Down

0 comments on commit 71197ea

Please sign in to comment.