Slight optimization in Temperature usermod.

This commit is contained in:
Blaz Kristan 2021-12-19 12:05:28 +01:00
parent 59216a9ae1
commit e43cdc6674
2 changed files with 27 additions and 30 deletions

View File

@ -54,43 +54,40 @@ class UsermodTemperature : public Usermod {
//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013 //Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
float readDallas() { float readDallas() {
byte i;
byte data[9]; byte data[9];
int16_t result; // raw data from sensor int16_t result; // raw data from sensor
if (!oneWire->reset()) return -127.0f; // send reset command and fail fast float retVal = -127.0f;
oneWire->skip(); // skip ROM if (oneWire->reset()) { // if reset() fails there are no OneWire devices
oneWire->write(0xBE); // read (temperature) from EEPROM oneWire->skip(); // skip ROM
for (i=0; i < 9; i++) data[i] = oneWire->read(); // first 2 bytes contain temperature oneWire->write(0xBE); // read (temperature) from EEPROM
if (OneWire::crc8(data,8) != data[8]) DEBUG_PRINTLN(F("CRC error reading temperature.")); delayMicroseconds(250);
// for (i=2; i < 9; i++) oneWire->read(); // read unused bytes for (byte i=0; i < 9; i++) data[i] = oneWire->read(); // first 2 bytes contain temperature
// result = (data[1]<<4) | (data[0]>>4); // we only need whole part, we will add fraction when returning if (OneWire::crc8(data,8) != data[8]) DEBUG_PRINTLN(F("CRC error reading temperature."));
// if (data[1]&0x80) result |= 0xFF00; // fix negative value switch(sensorFound) {
oneWire->reset(); case 0x10: // DS18S20 has 9-bit precision
oneWire->skip(); // skip ROM result = (data[1] << 8) | data[0];
oneWire->write(0x44,parasite); // request new temperature reading (without parasite power) retVal = float(result) * 0.5f;
// return (float)result + ((data[0]&0x0008) ? 0.5f : 0.0f); break;
switch(sensorFound) { case 0x22: // DS18B20
case 0x10: // DS18S20 has 9-bit precision case 0x28: // DS1822
//result = (((data[1] << 8) | (data[0] & 0xFE)) << 3) | ((0x10 - data[6]) & 0x0F); //achieving greater than 9-bit precision case 0x3B: // DS1825
//return result * 0.0625f - 0.25f; case 0x42: // DS28EA00
result = (data[1] << 8) | data[0]; result = (data[1]<<4) | (data[0]>>4); // we only need whole part, we will add fraction when returning
return float(result) * 0.5f; if (data[1] & 0x80) result |= 0xF000; // fix negative value
case 0x22: // DS18B20 retVal = float(result) + ((data[0] & 0x08) ? 0.5f : 0.0f);
case 0x28: // DS1822 break;
case 0x3B: // DS1825 }
case 0x42: // DS28EA00
result = (data[1]<<4) | (data[0]>>4); // we only need whole part, we will add fraction when returning
if (data[1] & 0x80) result |= 0xF000; // fix negative value
return float(result) + ((data[0] & 0x08) ? 0.5f : 0.0f);
} }
return -127.0f; return retVal;
} }
void requestTemperatures() { void requestTemperatures() {
readDallas(); DEBUG_PRINTLN(F("Requesting temperature."));
oneWire->reset();
oneWire->skip(); // skip ROM
oneWire->write(0x44,parasite); // request new temperature reading (TODO: parasite would need special handling)
lastTemperaturesRequest = millis(); lastTemperaturesRequest = millis();
waitingForConversion = true; waitingForConversion = true;
DEBUG_PRINTLN(F("Requested temperature."));
} }
void readTemperature() { void readTemperature() {

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2112181 #define VERSION 2112191
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG