Skip to content

Commit

Permalink
Improved Memory usage by utilizing bytes properly (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Winlin <winlin@vip.126.com>
  • Loading branch information
DVSProductions and winlinvip committed Jan 5, 2021
1 parent e66c1c5 commit d06f831
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -31,4 +31,6 @@
# Debug files
*.dSYM/

# Visual Studio cache/options directory
.vs/
.idea
67 changes: 32 additions & 35 deletions SimpleDHT.cpp
Expand Up @@ -48,7 +48,7 @@ int SimpleDHT::setPinInputMode(uint8_t mode) {
return SimpleDHTErrSuccess;
}

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

if (pin == -1) {
Expand All @@ -62,17 +62,17 @@ int SimpleDHT::read(byte* ptemperature, byte* phumidity, byte pdata[40]) {
}

if (ptemperature) {
*ptemperature = (byte)(int)temperature;
*ptemperature = static_cast<byte>(static_cast<int>(temperature));
}

if (phumidity) {
*phumidity = (byte)(int)humidity;
*phumidity = static_cast<byte>(static_cast<int>(humidity));
}

return ret;
}

int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) {
int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[5]) {
setPin(pin);
return read(ptemperature, phumidity, pdata);
}
Expand Down Expand Up @@ -124,25 +124,24 @@ long SimpleDHT::levelTime(byte level, int firstWait, int interval) {
return time;
}

byte SimpleDHT::bits2byte(byte data[8]) {
byte v = 0;
for (int i = 0; i < 8; i++) {
v += data[i] << (7 - i);
}
return v;
//https://stackoverflow.com/a/2602885/4203189
byte SimpleDHT::reverse(byte b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}

int SimpleDHT::parse(byte data[40], short* ptemperature, short* phumidity) {
short humidity = bits2byte(data);
short humidity2 = bits2byte(data + 8);
short temperature = bits2byte(data + 16);
short temperature2 = bits2byte(data + 24);
byte check = bits2byte(data + 32);
byte expect = (byte)humidity + (byte)humidity2 + (byte)temperature + (byte)temperature2;
int SimpleDHT::parse(byte data[5], short* ptemperature, short* phumidity) {
short humidity = reverse(data[0]);
short humidity2 = reverse(data[1]);
short temperature = reverse(data[2]);
short temperature2 = reverse(data[3]);
byte check = reverse(data[4]);
byte expect = static_cast<byte>(humidity) + static_cast<byte>(humidity2) + static_cast<byte>(temperature) + static_cast<byte>(temperature2);
if (check != expect) {
return SimpleDHTErrDataChecksum;
}

*ptemperature = temperature<<8 | temperature2;
*phumidity = humidity<<8 | humidity2;

Expand All @@ -155,26 +154,25 @@ SimpleDHT11::SimpleDHT11() {
SimpleDHT11::SimpleDHT11(int pin) : SimpleDHT (pin) {
}

int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[5]) {
int ret = SimpleDHTErrSuccess;

if (pin == -1) {
return SimpleDHTErrNoPin;
}

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

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

if (pdata) {
memcpy(pdata, data, 40);
memcpy(pdata, data, 5);
}
if (ptemperature) {
*ptemperature = (int)(temperature>>8);
Expand All @@ -191,14 +189,14 @@ int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
return ret;
}

int SimpleDHT11::read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT11::read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) {
setPin(pin);
return read2(ptemperature, phumidity, pdata);
}

int SimpleDHT11::sample(byte data[40]) {
int SimpleDHT11::sample(byte data[5]) {
// empty output data.
memset(data, 0, 40);
memset(data, 0, 5);

// According to protocol: [1] https://akizukidenshi.com/download/ds/aosong/DHT11.pdf
// notify DHT11 to start:
Expand Down Expand Up @@ -250,7 +248,7 @@ int SimpleDHT11::sample(byte data[40]) {
if (t < 11) { // specs say: 20us
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
}
data[ j ] = (t > 40 ? 1 : 0); // specs: 26-28us -> 0, 70us -> 1
bitWrite(data[j / 8], j % 8, (t > 40 ? 1 : 0)); // specs: 26-28us -> 0, 70us -> 1
}

// DHT11 EOF:
Expand All @@ -259,7 +257,6 @@ int SimpleDHT11::sample(byte data[40]) {
if (t < 24) { // specs say: 50us
return simpleDHTCombileError(t, SimpleDHTErrDataEOF);
}

return SimpleDHTErrSuccess;
}

Expand All @@ -269,14 +266,14 @@ SimpleDHT22::SimpleDHT22() {
SimpleDHT22::SimpleDHT22(int pin) : SimpleDHT (pin) {
}

int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[5]) {
int ret = SimpleDHTErrSuccess;

if (pin == -1) {
return SimpleDHTErrNoPin;
}

byte data[40] = {0};
byte data[5] = {0};
if ((ret = sample(data)) != SimpleDHTErrSuccess) {
return ret;
}
Expand All @@ -288,7 +285,7 @@ int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
}

if (pdata) {
memcpy(pdata, data, 40);
memcpy(pdata, data, 5);
}
if (ptemperature) {
*ptemperature = (float)((temperature & 0x8000 ? -1 : 1) * (temperature & 0x7FFF)) / 10.0;
Expand All @@ -300,14 +297,14 @@ int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
return ret;
}

int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) {
setPin(pin);
return read2(ptemperature, phumidity, pdata);
}

int SimpleDHT22::sample(byte data[40]) {
int SimpleDHT22::sample(byte data[5]) {
// empty output data.
memset(data, 0, 40);
memset(data, 0, 5);

// According to protocol: http://akizukidenshi.com/download/ds/aosong/AM2302.pdf
// notify DHT22 to start:
Expand All @@ -322,7 +319,7 @@ int SimpleDHT22::sample(byte data[40]) {
// @see https://github.com/winlinvip/SimpleDHT/pull/5
digitalWrite(pin, HIGH);
pinMode(pin, this->pinInputMode);
delayMicroseconds(40);
delayMicroseconds(5);

// DHT22 starting:
// 1. T(rel), PULL LOW 80us(75-85us).
Expand Down Expand Up @@ -350,7 +347,7 @@ int SimpleDHT22::sample(byte data[40]) {
if (t < 11) { // specs say: 26us
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
}
data[ j ] = (t > 40 ? 1 : 0); // specs: 22-30us -> 0, 70us -> 1
bitWrite(data[j / 8], j % 8, (t > 40 ? 1 : 0)); // specs: 22-30us -> 0, 70us -> 1
}

// DHT22 EOF:
Expand Down
28 changes: 14 additions & 14 deletions SimpleDHT.h
Expand Up @@ -95,12 +95,12 @@ class SimpleDHT {
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 1s(DHT11) or 2s(DHT22).
// @return SimpleDHTErrSuccess is success; otherwise, failed.
virtual int read(byte* ptemperature, byte* phumidity, byte pdata[40]);
virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
virtual int read(byte* ptemperature, byte* phumidity, byte pdata[5]);
virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[5]);
// To get a more accurate data.
// @remark it's available for dht22. for dht11, it's the same of read().
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]) = 0;
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) = 0;
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]) = 0;
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) = 0;
protected:
// For only AVR - methods returning low level conf. of the pin
#ifdef __AVR
Expand All @@ -117,17 +117,17 @@ class SimpleDHT {
// @param interval time interval between consecutive state checks.
// @return measured time (microseconds). -1 if timeout.
virtual long levelTime(byte level, int firstWait = 10, int interval = 6);
// @data the bits of a byte.
// @data reverses a byte with reversed data
// @remark please use simple_dht11_read().
virtual byte bits2byte(byte data[8]);
virtual byte reverse(byte data);
// read temperature and humidity from dht11.
// @param data a byte[40] to read bits to 5bytes.
// @return 0 success; otherwise, error.
// @remark please use simple_dht11_read().
virtual int sample(byte data[40]) = 0;
virtual int sample(byte data[5]) = 0;
// parse the 40bits data to temperature and humidity.
// @remark please use simple_dht11_read().
virtual int parse(byte data[40], short* ptemperature, short* phumidity);
virtual int parse(byte data[5], short* ptemperature, short* phumidity);
};

/*
Expand All @@ -152,10 +152,10 @@ class SimpleDHT11 : public SimpleDHT {
SimpleDHT11();
SimpleDHT11(int pin);
public:
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
protected:
virtual int sample(byte data[40]);
virtual int sample(byte data[5]);
};

/*
Expand All @@ -180,10 +180,10 @@ class SimpleDHT22 : public SimpleDHT {
SimpleDHT22();
SimpleDHT22(int pin);
public:
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
protected:
virtual int sample(byte data[40]);
virtual int sample(byte data[5]);
};

#endif
11 changes: 5 additions & 6 deletions examples/DHT11WithRawBits/DHT11WithRawBits.ino
Expand Up @@ -19,7 +19,7 @@ void loop() {
// read with raw sample data.
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
byte data[5] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Expand All @@ -28,11 +28,10 @@ void loop() {
}

Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
for (int i = 0; i < 5; i++) {
for(int n=0;n<8;n++)
Serial.print(bitRead(data[i],n));
Serial.print(' ');
}
Serial.println("");

Expand Down
9 changes: 4 additions & 5 deletions examples/DHT22WithRawBits/DHT22WithRawBits.ino
Expand Up @@ -21,7 +21,7 @@ void loop() {
// if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
float temperature = 0;
float humidity = 0;
byte data[40] = {0};
byte data[5] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Expand All @@ -30,11 +30,10 @@ void loop() {
}

Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
for (int i = 0; i < 5; i++) {
for (int n = 0; n < 8; n++)
Serial.print(bitRead(data[i], n));
Serial.print(' ');
}
}
Serial.println("");

Expand Down

0 comments on commit d06f831

Please sign in to comment.