Change address printing to Hex from Base64. (#1840)

This commit is contained in:
Jonathan Karras 2017-03-06 23:06:50 -07:00 committed by Marcel Stör
parent fcc91967f9
commit 4acabab1cc
2 changed files with 19 additions and 20 deletions

View File

@ -7,7 +7,7 @@ pin = 3 -- gpio0 = 3, gpio2 = 4
function readout(temp) function readout(temp)
for addr, temp in pairs(temp) do for addr, temp in pairs(temp) do
-- print(string.format("Sensor %s: %s 'C", addr, temp)) -- print(string.format("Sensor %s: %s 'C", addr, temp))
print(string.format("Sensor %s: %s 'C", encoder.toBase64(addr), temp)) -- readable address with base64 encoding is preferred when encoder module is available print(string.format("Sensor %s: %s °C", encoder.toHex(addr), temp)) -- readable address with base64 encoding is preferred when encoder module is available
end end
-- Module can be released when it is no longer needed -- Module can be released when it is no longer needed
@ -21,6 +21,6 @@ if t.sens then
print("Total number of DS18B20 sensors: "..table.getn(t.sens)) print("Total number of DS18B20 sensors: "..table.getn(t.sens))
for i, s in ipairs(t.sens) do for i, s in ipairs(t.sens) do
-- print(string.format(" sensor #%d address: %s%s", i, s.addr, s.parasite == 1 and " (parasite)" or "")) -- print(string.format(" sensor #%d address: %s%s", i, s.addr, s.parasite == 1 and " (parasite)" or ""))
print(string.format(" sensor #%d address: %s%s", i, encoder.toBase64(s.addr), s.parasite == 1 and " (parasite)" or "")) -- readable address with base64 encoding is preferred when encoder module is available print(string.format(" sensor #%d address: %s%s", i, encoder.toHex(s.addr), s.parasite == 1 and " (parasite)" or "")) -- readable address with base64 encoding is preferred when encoder module is available
end end
end end

View File

@ -12,12 +12,12 @@ return({
pin=3, pin=3,
sens={}, sens={},
temp={}, temp={},
conversion = function(self) conversion = function(self)
local pin = self.pin local pin = self.pin
for i,s in ipairs(self.sens) do for i,s in ipairs(self.sens) do
if s.status == 0 then if s.status == 0 then
print("starting conversion:", encoder.toBase64(s.addr), s.parasite == 1 and "parasite" or " ") print("starting conversion:", encoder.toHex(s.addr), s.parasite == 1 and "parasite" or " ")
ow.reset(pin) ow.reset(pin)
ow.select(pin, s.addr) -- select the sensor ow.select(pin, s.addr) -- select the sensor
ow.write(pin, 0x44, 1) -- and start conversion ow.write(pin, 0x44, 1) -- and start conversion
@ -27,14 +27,14 @@ return({
end end
tmr.alarm(tmr.create(), 750, tmr.ALARM_SINGLE, function() self:readout() end) tmr.alarm(tmr.create(), 750, tmr.ALARM_SINGLE, function() self:readout() end)
end, end,
readTemp = function(self, cb, lpin) readTemp = function(self, cb, lpin)
local pin = self.pin local pin = self.pin
self.cb = cb self.cb = cb
self.temp={} self.temp={}
if lpin then pin = lpin end if lpin then pin = lpin end
ow.setup(pin) ow.setup(pin)
self.sens={} self.sens={}
ow.reset_search(pin) ow.reset_search(pin)
-- ow.target_search(pin,0x28) -- ow.target_search(pin,0x28)
@ -50,25 +50,25 @@ return({
ow.write(pin, 0xB4, 1) -- Read Power Supply [B4h] ow.write(pin, 0xB4, 1) -- Read Power Supply [B4h]
local parasite = (ow.read(pin)==0 and 1 or 0) local parasite = (ow.read(pin)==0 and 1 or 0)
table.insert(self.sens,{addr=addr, parasite=parasite, status=0}) table.insert(self.sens,{addr=addr, parasite=parasite, status=0})
print("contact: ", encoder.toBase64(addr), parasite == 1 and "parasite" or " ") print("contact: ", encoder.toHex(addr), parasite == 1 and "parasite" or " ")
end end
addr = ow.search(pin) addr = ow.search(pin)
tmr.wdclr() tmr.wdclr()
end end
-- place powered sensors first -- place powered sensors first
table.sort(self.sens, function(a,b) return a.parasite<b.parasite end) table.sort(self.sens, function(a,b) return a.parasite<b.parasite end)
node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end) node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end)
end, end,
readout=function(self) readout=function(self)
local pin = self.pin local pin = self.pin
local next = false local next = false
if not self.sens then return 0 end if not self.sens then return 0 end
for i,s in ipairs(self.sens) do for i,s in ipairs(self.sens) do
-- print(encoder.toBase64(s.addr), s.status) -- print(encoder.toHex(s.addr), s.status)
if s.status == 1 then if s.status == 1 then
ow.reset(pin) ow.reset(pin)
ow.select(pin, s.addr) -- select the sensor ow.select(pin, s.addr) -- select the sensor
@ -82,38 +82,37 @@ return({
else else
t = t * 5000 -- DS18S20, 1 fractional bit t = t * 5000 -- DS18S20, 1 fractional bit
end end
-- integer version -- integer version
local sgn = t<0 and -1 or 1 local sgn = t<0 and -1 or 1
local tA = sgn*t local tA = sgn*t
local tH=tA/10000 local tH=tA/10000
local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0) local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0)
if tH and (tH~=85) then if tH and (tH~=85) then
self.temp[s.addr]=(sgn<0 and "-" or "")..tH.."."..tL self.temp[s.addr]=(sgn<0 and "-" or "")..tH.."."..tL
print(encoder.toBase64(s.addr),(sgn<0 and "-" or "")..tH.."."..tL) print(encoder.toHex(s.addr),(sgn<0 and "-" or "")..tH.."."..tL)
s.status = 2 s.status = 2
end end
-- end integer version -- end integer version
-- -- float version -- -- float version
-- if t and (math.floor(t/10000)~=85) then -- if t and (math.floor(t/10000)~=85) then
-- self.temp[s.addr]=t -- self.temp[s.addr]=t
-- print(encoder.toBase64(s.addr), t) -- print(encoder.toHex(s.addr), t)
-- s.status = 2 -- s.status = 2
-- end -- end
-- -- end float version -- -- end float version
end end
next = next or s.status == 0 next = next or s.status == 0
end end
if next then if next then
node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end) node.task.post(node.task.MEDIUM_PRIORITY, function() self:conversion() end)
else else
self.sens = nil self.sens = nil
if self.cb then if self.cb then
node.task.post(node.task.MEDIUM_PRIORITY, function() self.cb(self.temp) end) node.task.post(node.task.MEDIUM_PRIORITY, function() self.cb(self.temp) end)
end end
end end
end end
}) })