Browse Source

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 <izikd@izikd.net>
fastled
Izik Dubnov 7 years ago
parent
commit
03f2f306eb
1 changed files with 18 additions and 1 deletions
  1. +18
    -1
      code/espurna/ds18b20.ino

+ 18
- 1
code/espurna/ds18b20.ino View File

@ -26,6 +26,8 @@ double getDSTemperature() {
void dsSetup() { void dsSetup() {
ds18b20.begin(); ds18b20.begin();
ds18b20.setWaitForConversion(false);
apiRegister("/api/temperature", "temperature", [](char * buffer, size_t len) { apiRegister("/api/temperature", "temperature", [](char * buffer, size_t len) {
dtostrf(_dsTemperature, len-1, 1, buffer); dtostrf(_dsTemperature, len-1, 1, buffer);
}); });
@ -35,13 +37,28 @@ void dsLoop() {
// Check if we should read new data // Check if we should read new data
static unsigned long last_update = 0; static unsigned long last_update = 0;
static bool requested = false;
if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) { 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(); last_update = millis();
unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt(); unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
// Read sensor data // Read sensor data
ds18b20.requestTemperatures();
double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0); double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);
// Check if readings are valid // Check if readings are valid


Loading…
Cancel
Save