Skip to content

Commit

Permalink
Merge pull request #1 from hasenradball/improve_databuffer
Browse files Browse the repository at this point in the history
Improve databuffer
  • Loading branch information
hasenradball authored Nov 29, 2023
2 parents 24bc817 + 29dbd96 commit c112097
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AM2302-Sensor",
"version": "1.1.1",
"version": "1.2.0",
"repository":
{
"type": "git",
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=AM2302-Sensor
version=1.1.1
version=1.2.0
author=Frank Häfele <[email protected]>
maintainer=Frank Häfele <[email protected]>
sentence=This library read temperature and humidity from the AM2302 (aka DHT22) senor.
Expand Down
52 changes: 23 additions & 29 deletions src/AM2302-Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,15 @@ int8_t AM2302::AM2302_Sensor::read() {
// ==== Read Sensor Data ====
// *****************************
// ==> START of time critical code <==
int8_t data_buffer[40U];
// read 40 bits from sensor:
// read 40 bits from sensor into the buffer:
// ==> HIGH state is 70 µs
// ==> LOW state is 28 µs
//for (uint8_t i = 0; i < 40U; ++i) {
if (read_sensor_data(data_buffer, 40U) == AM2302_ERROR_TIMEOUT) {
uint8_t _data[5U] = {0};
if (read_sensor_data(_data, 5U) == AM2302_ERROR_TIMEOUT) {
return AM2302_ERROR_TIMEOUT;
}
// ==> END of time critical code <==
uint8_t count{0};
int8_t val;
for (uint8_t i = 0; i < 5U; ++i) {
for (uint8_t j = 0; j < 8U; ++j) {
_data[i] <<= 1;
val = data_buffer[count++];
_data[i] |= val;
}
}

// check checksum
_checksum_ok = (_data[4] == ( (_data[0] + _data[1] + _data[2] + _data[3]) & 0xFF) );

Expand Down Expand Up @@ -131,26 +122,29 @@ int8_t AM2302::AM2302_Sensor::await_state(uint8_t state) {
* @param size of buffer => 40
* @return int8_t
*/
int8_t AM2302::AM2302_Sensor::read_sensor_data(int8_t *buffer, uint8_t size) {
int8_t AM2302::AM2302_Sensor::read_sensor_data(uint8_t *buffer, uint8_t size) {
for (uint8_t i = 0; i < size; ++i) {
uint8_t wait_counter{0}, state_counter{0};
// count wait for state time
while ( !digitalRead(_pin) ) {
++wait_counter;
delayMicroseconds(1.0);
if (wait_counter >= READ_TIMEOUT) {
return AM2302_ERROR_TIMEOUT;
for (uint8_t bit = 0; bit < 8U; ++bit) {
uint8_t wait_counter{0}, state_counter{0};
// count wait for state time
while ( !digitalRead(_pin) ) {
++wait_counter;
delayMicroseconds(1.0);
if (wait_counter >= READ_TIMEOUT) {
return AM2302_ERROR_TIMEOUT;
}
}
}
// count state time
while ( digitalRead(_pin) ) {
++state_counter;
delayMicroseconds(1U);
if (state_counter >= READ_TIMEOUT) {
return AM2302_ERROR_TIMEOUT;
// count state time
while ( digitalRead(_pin) ) {
++state_counter;
delayMicroseconds(1U);
if (state_counter >= READ_TIMEOUT) {
return AM2302_ERROR_TIMEOUT;
}
}
buffer[i] <<= 1;
buffer[i] |= (state_counter > wait_counter);
}
buffer[i] = (state_counter > wait_counter);
}
return AM2302_READ_OK;
}
Expand Down
3 changes: 1 addition & 2 deletions src/AM2302-Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ namespace AM2302 {


private:
uint8_t _data[5];
uint16_t _hum {0};
int16_t _temp {0};
uint8_t _pin;
bool _checksum_ok {false};
int8_t await_state(uint8_t state);
int8_t read_sensor_data(int8_t *buffer, uint8_t const size);
int8_t read_sensor_data(uint8_t *buffer, uint8_t const size);
};
}

Expand Down

0 comments on commit c112097

Please sign in to comment.