From 267f7ca980bec3107cf97a3b2a3d151cf4b16f13 Mon Sep 17 00:00:00 2001 From: orgua Date: Wed, 12 Jul 2017 00:04:01 +0200 Subject: [PATCH 1/3] add unittest for datasheet examples --- main.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.cpp b/main.cpp index 7f63a46..41634eb 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ // #include #include +#include using namespace std; @@ -40,6 +41,11 @@ constexpr int8_t operator "" _i8(const unsigned long long int value) return static_cast(value); } +constexpr int16_t operator "" _i16(const unsigned long long int value) +{ + return static_cast(value); +} + constexpr int32_t operator "" _i32(const unsigned long long int value) { return static_cast(value); @@ -183,6 +189,30 @@ int main() } } + { + // DS18B20 & DS1822 Datasheet examples, both same + const std::array temp_degC { 125.0f, 85.0f, 25.0625f, 10.125f, 0.5f, 0.0f, -0.5f, -10.125f, -25.0625f, -55.0f }; + const std::array temp_raw { 0x07D0, 0x0550, 0x0191, 0x00A2, 0x0008, 0x0000, 0xFFF8_i16, 0xFF5E_i16, 0xFE6F_i16, 0xFC90_i16 }; + + for (size_t index {0}; index < temp_degC.size(); ++index) + { + ds18B20.setTemperature(temp_degC[index]); + test_eq(ds18B20.getTemperatureRaw(), temp_raw[index], "DS18B22 datasheet test" + to_string(index)); + } + } + + { + // DS18S20 Datasheet examples + const std::array temp_degC { 85.0f, 25.0f, 0.5f, 0.0f, -0.5f, -25.0f, -55.0f }; + const std::array temp_raw { 0x00AA, 0x0032, 0x0001, 0x0000, 0xFFFF_i16, 0xFFCE_i16, 0xFF92_i16 }; + + for (size_t index {0}; index < temp_degC.size(); ++index) + { + ds18S20.setTemperature(temp_degC[index]); + test_eq(ds18S20.getTemperatureRaw(), temp_raw[index], "DS18S22 datasheet test" + to_string(index)); + } + } + // TODO: look through src code, for now just converted ino-examples { From 15c405a05f1ecec34c7b4a4331b94188975315da Mon Sep 17 00:00:00 2001 From: orgua Date: Wed, 12 Jul 2017 00:04:57 +0200 Subject: [PATCH 2/3] make ds18b20 more testable and bugfix problem with degC<=0 --- src/DS18B20.cpp | 26 ++++++++++---------------- src/DS18B20.h | 5 +++-- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/DS18B20.cpp b/src/DS18B20.cpp index 5d45e74..c1bb29a 100644 --- a/src/DS18B20.cpp +++ b/src/DS18B20.cpp @@ -83,25 +83,24 @@ void DS18B20::setTemperatureRaw(const int16_t value_raw) if (ds18s20_mode) { value /= 8; // deg*16/8 = deg*2 ... - if (value > 0) + if (value >= 0) { value &= 0x00FF; // upper byte is signum (0) } else { - value = -value; value |= 0xFF00; // upper byte is signum (1) } } else { // normal 18b20, uses always 12bit mode! also 9,10,11,12 bit possible bitPosition seems to stay the same - if (value > 0) + if (value >= 0) { value &= 0x07FF; // upper 5 bits are signum - } else + } + else { - value = -value; value |= 0xF800; } } @@ -116,26 +115,21 @@ void DS18B20::setTemperatureRaw(const int16_t value_raw) int DS18B20::getTemperature(void) const { - int16_t value = (scratchpad[1] << 8) | scratchpad[0]; + int16_t value = getTemperatureRaw(); if (ds18s20_mode) { - if ((scratchpad[1] & 0xF0) != 0) - { - value &= 0x00FF; - value = -value; - } value /= 2; } else { - if ((scratchpad[1] & 0xF0) != 0) - { - value &= 0x07FF; - value = -value; - } value /= 16; } return value; } + +int16_t DS18B20::getTemperatureRaw() const +{ + return static_cast((scratchpad[1] << 8) | scratchpad[0]); +} diff --git a/src/DS18B20.h b/src/DS18B20.h index dd44975..e183a38 100644 --- a/src/DS18B20.h +++ b/src/DS18B20.h @@ -30,9 +30,10 @@ class DS18B20 : public OneWireItem void setTemperature(float value_degC); // -55 to +125 degC void setTemperature(int8_t value_degC); // -55 to +125 degC - int getTemperature(void) const; + int getTemperature() const; - void setTemperatureRaw(int16_t value_raw); + void setTemperatureRaw(int16_t value_raw); + int16_t getTemperatureRaw() const; }; From 9924e9715e502fdbad6bada96bb3c82145868bef Mon Sep 17 00:00:00 2001 From: orgua Date: Wed, 12 Jul 2017 00:05:06 +0200 Subject: [PATCH 3/3] bump version --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index ef7c4c8..bea21a9 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=OneWireHub -version=2.1.1 +version=2.1.2 author=Ingmar Splitt, orgua, MarkusLange, Shagrat2 maintainer=orgua sentence=OneWire slave device emulator with support for up to 32 simultaneous 1wire devices.