From 5a629e429752c5f99d1233cb133bdf614fb7d7c0 Mon Sep 17 00:00:00 2001 From: sza2 Date: Sat, 14 Feb 2015 10:48:41 +0100 Subject: [PATCH 1/2] Fix for negative values --- lua_modules/ds18b20/ds18b20.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua_modules/ds18b20/ds18b20.lua b/lua_modules/ds18b20/ds18b20.lua index c31addcf..32c0f4c0 100644 --- a/lua_modules/ds18b20/ds18b20.lua +++ b/lua_modules/ds18b20/ds18b20.lua @@ -3,6 +3,7 @@ -- NODEMCU TEAM -- LICENCE: http://opensource.org/licenses/MIT -- Vowstar +-- 2015/02/14 sza2 Fix for negative values -------------------------------------------------------------------------------- -- Set module name as parameter of require @@ -96,12 +97,16 @@ function readNumber(addr, unit) crc = ow.crc8(string.sub(data,1,8)) -- print("CRC="..crc) if (crc == data:byte(9)) then + t = (data:byte(1) + data:byte(2) * 256) + if (t > 32767) then + t = -(65536 - t) + end if(unit == nil or unit == C) then - t = (data:byte(1) + data:byte(2) * 256) * 625 + t = t * 625 elseif(unit == F) then - t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000 + t = t * 1125 + 320000 elseif(unit == K) then - t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500 + t = t * 625 + 2731500 else return nil end From 67ab2d018e5d012e7d3867f190873a1437cee58d Mon Sep 17 00:00:00 2001 From: Tamas Szabo Date: Sat, 21 Feb 2015 17:05:16 +0100 Subject: [PATCH 2/2] Update ds18b20.lua --- lua_modules/ds18b20/ds18b20.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua_modules/ds18b20/ds18b20.lua b/lua_modules/ds18b20/ds18b20.lua index 32c0f4c0..138c10da 100644 --- a/lua_modules/ds18b20/ds18b20.lua +++ b/lua_modules/ds18b20/ds18b20.lua @@ -99,7 +99,7 @@ function readNumber(addr, unit) if (crc == data:byte(9)) then t = (data:byte(1) + data:byte(2) * 256) if (t > 32767) then - t = -(65536 - t) + t = t - 65536 end if(unit == nil or unit == C) then t = t * 625