diff --git a/library.json b/library.json index 5668565..14ff038 100644 --- a/library.json +++ b/library.json @@ -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" diff --git a/library.properties b/library.properties index 0aa3400..a0bba82 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=esp_sds011 -version=1.0.0 +version=1.0.1 author=Dirk O. Kaar maintainer=Dirk O. Kaar sentence=ESP8266/ESP32 library for the SDS011 particulate matter sensor. diff --git a/src/Sds011.cpp b/src/Sds011.cpp index 31aa1c2..180f48d 100644 --- a/src/Sds011.cpp +++ b/src/Sds011.cpp @@ -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) { @@ -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(); } @@ -193,10 +186,12 @@ 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; @@ -204,11 +199,12 @@ bool Sds011::_read_response(enum Command cmd) { } } 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; } @@ -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; diff --git a/src/Sds011.h b/src/Sds011.h index e54e0b1..086fad8 100644 --- a/src/Sds011.h +++ b/src/Sds011.h @@ -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); @@ -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; };