From 925991715f2472d66ce73f5e636d72cfa40d9db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Thu, 16 Mar 2017 23:10:04 +0100 Subject: [PATCH] Fix float version, auto-detect int/float, OO tmr (#1866) --- lua_modules/ds18b20/ds18b20.lua | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lua_modules/ds18b20/ds18b20.lua b/lua_modules/ds18b20/ds18b20.lua index 90c21510..7f192346 100644 --- a/lua_modules/ds18b20/ds18b20.lua +++ b/lua_modules/ds18b20/ds18b20.lua @@ -25,7 +25,7 @@ return({ if s.parasite == 1 then break end -- parasite sensor blocks bus during conversion end end - tmr.alarm(tmr.create(), 750, tmr.ALARM_SINGLE, function() self:readout() end) + tmr.create():alarm(750, tmr.ALARM_SINGLE, function() self:readout() end) end, readTemp = function(self, cb, lpin) @@ -83,25 +83,28 @@ return({ t = t * 5000 -- DS18S20, 1 fractional bit end - -- integer version - local sgn = t<0 and -1 or 1 - local tA = sgn*t - local tH=tA/10000 - local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0) + if 1/2 == 0 then + -- integer version + local sgn = t<0 and -1 or 1 + local tA = sgn*t + local tH=tA/10000 + local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0) - if tH and (tH~=85) then - self.temp[s.addr]=(sgn<0 and "-" or "")..tH.."."..tL - print(encoder.toHex(s.addr),(sgn<0 and "-" or "")..tH.."."..tL) - s.status = 2 + if tH and (tH~=85) then + self.temp[s.addr]=(sgn<0 and "-" or "")..tH.."."..tL + print(encoder.toHex(s.addr),(sgn<0 and "-" or "")..tH.."."..tL) + s.status = 2 + end + -- end integer version + else + -- float version + if t and (math.floor(t/10000)~=85) then + self.temp[s.addr]=t/10000 + print(encoder.toHex(s.addr), t) + s.status = 2 + end + -- end float version end - -- end integer version - -- -- float version - -- if t and (math.floor(t/10000)~=85) then - -- self.temp[s.addr]=t - -- print(encoder.toHex(s.addr), t) - -- s.status = 2 - -- end - -- -- end float version end next = next or s.status == 0 end