Skip to content

Commit

Permalink
Merge pull request #8 from dok-net/resp_timeout
Browse files Browse the repository at this point in the history
Employ total response timeout (fix #7)
  • Loading branch information
dok-net authored Dec 11, 2022
2 parents 18079b7 + 0b1f1d4 commit 9639e35
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esp_sds011",
"version": "1.0.0",
"version": "1.0.1",
"description": "ESP8266/ESP32 library for the SDS011 particulate matter sensor",
"keywords": [
"sds011"
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=esp_sds011
version=1.0.0
version=1.0.1
author=Dirk O. Kaar
maintainer=Dirk O. Kaar <[email protected]>
sentence=ESP8266/ESP32 library for the SDS011 particulate matter sensor.
Expand Down
40 changes: 18 additions & 22 deletions src/Sds011.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ bool Sds011::query_data_auto(int& pm25, int& pm10) {
return true;
}

bool Sds011::timeout() {
return _timeout;
}

bool Sds011::crc_ok() {
uint8_t crc = 0;
for (int i = 2; i < 8; ++i) {
Expand Down Expand Up @@ -170,18 +166,15 @@ void Sds011::_send_cmd(enum Command cmd, const uint8_t* data, uint8_t len) {
_out.write(_buf, sizeof(_buf));
}

int Sds011::_read_byte(long unsigned deadline) {
uint32_t start = millis();
while (!_out.available()) {
if (millis() - start < deadline) {
delay(1);
continue;
}
_timeout = true;
return -1;
// _read_byte returns timeout (-1) regardless of pending data
// if _read_response_deadline has expired.
int Sds011::_read_byte() {
for (;;) {
const uint32_t deadlineExpired = millis() - _read_response_start;
if (deadlineExpired >= _read_response_deadline) return -1;
if (_out.available()) break;
delay(1);
}

_timeout = false;
return _out.read();
}

Expand All @@ -193,22 +186,25 @@ void Sds011::_clear_responses() {

bool Sds011::_read_response(enum Command cmd) {
uint8_t i = 0;
const long unsigned deadline = 1000;
int recv;
_read_response_start = millis();
while (i < 3) {
_buf[i] = _read_byte(deadline);
if (timeout()) { break; }
recv = _read_byte();
if (0 > recv) { break; }
_buf[i] = recv;
switch (i++) {
case 0: if (_buf[0] != 0xAA) i = 0; break;
case 1: if (_buf[1] != ((cmd == CMD_QUERY_DATA) ? 0xC0 : 0xC5)) i = 0; break;
case 2: if (cmd != CMD_QUERY_DATA && _buf[2] != cmd) i = 0; break;
}
}
for (i = 3; i < 10; i++) {
if (timeout()) { break; }
_buf[i] = _read_byte(deadline);
if (0 > recv) { break; }
recv = _read_byte();
_buf[i] = recv;
}

bool succ = !timeout() && _buf[9] == 0xAB;
bool succ = !(0 > recv) && _buf[9] == 0xAB;
return succ;
}

Expand Down Expand Up @@ -288,7 +284,7 @@ bool Sds011Async_Base::query_data_auto_async(int n, int* pm25_table, int* pm10_t
query_data_auto_start = millis();
query_data_auto_deadline = (rampup_s - dataAutoCnt) * 1000UL;
onReceive([this](int avail) {
unsigned long deadlineExpired = millis() - query_data_auto_start;
uint32_t deadlineExpired = millis() - query_data_auto_start;
if (deadlineExpired < query_data_auto_deadline) {
_get_out().flush();
return;
Expand Down
6 changes: 3 additions & 3 deletions src/Sds011.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class Sds011 {
bool query_data(int& pm25, int& pm10);
bool query_data(int& pm25, int& pm10, int n);
bool query_data_auto(int& pm25, int& pm10);
bool timeout();
bool crc_ok();

bool filter_data(int n, const int* pm25_table, const int* pm10_table, int& pm25, int& pm10);
Expand All @@ -85,14 +84,15 @@ class Sds011 {
};

void _send_cmd(enum Command cmd, const uint8_t* buf, uint8_t len);
int _read_byte(long unsigned deadline);
int _read_byte();
String _buf_to_string(uint8_t size);
void _clear_responses();
bool _read_response(enum Command cmd);

Stream& _out;
uint8_t _buf[19];
bool _timeout = false;
uint32_t _read_response_start;
static constexpr uint32_t _read_response_deadline = 1010;

unsigned rampup_s = 10;
};
Expand Down

0 comments on commit 9639e35

Please sign in to comment.