From 03f2f306ebc97568f591e893286bc18015c420ca Mon Sep 17 00:00:00 2001 From: Izik Dubnov Date: Sat, 25 Feb 2017 22:18:09 +0200 Subject: [PATCH] Split DS18B20 reading into 2 stages for performance * Waiting for conversation takes ~750ms which is ~4-5K main loop iterations in my setup Signed-off-by: Izik Dubnov --- code/espurna/ds18b20.ino | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index 3f7031f0..8f262fc7 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -26,6 +26,8 @@ double getDSTemperature() { void dsSetup() { ds18b20.begin(); + ds18b20.setWaitForConversion(false); + apiRegister("/api/temperature", "temperature", [](char * buffer, size_t len) { dtostrf(_dsTemperature, len-1, 1, buffer); }); @@ -35,13 +37,28 @@ void dsLoop() { // Check if we should read new data static unsigned long last_update = 0; + static bool requested = false; if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) { + if (!requested) { + ds18b20.requestTemperatures(); + requested = true; + + /* Requesting takes time, + * so data will probably not be available in this round */ + return; + } + + /* Check if requested data is already available */ + if (!ds18b20.isConversionComplete()) { + return; + } + + requested = false; last_update = millis(); unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt(); // Read sensor data - ds18b20.requestTemperatures(); double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0); // Check if readings are valid