Scrolling info on LCD
Hello,
My wish was to make more use of LCD and show some data of interest on it. In particular I wanted to see CurrenCost and 1wire data, of course anything else "known" to HAH may be added.
Hopefully useful for someone else too, either in whole or partially ;)
Enjoy,
Aivo
runlcdApplet.lua, new plugboard format (some formatting - like tabs - is lost here, hopefully still well readable)
Edit: changed self:stop() to self:delete() on advice from Brett, no leakeage needed :)
require("xap")
module (...,package.seeall)
info = {version="1.0", description="Scrolling 1wire and CurrentCost data on lcd"}
lcdlen = 16
period = 2 -- scroll/shift speed in seconds
shift = 2 -- scroll/shift step in symbols
tarray = { } -- 1wire temperature data
parray = { } -- CC house/0 sensor data, phases 1 to 3
fill = "-" -- Space does not work, gets trimmed
function init()
ft = xap.Filter()
ft:add("xap-header", "source", "dbzoo.livebox.Controller:1wire.*")
-- ft:add("xap-header", "class", "xAPBSC.info")
ft:callback(catch1wire)
fp = xap.Filter()
fp:add("xap-header", "source", "dbzoo.livebox.CurrentCost:ch.*")
-- fp:add("xap-header", "class", "xAPBSC.info")
fp:callback(catchCurrentCost)
send2lcd("Scrolling LCD ..")
xap.Timer(repeatlcd, 10):start()
end
function catch1wire()
local source = xap.getValue("xap-header","source")
local id = tonumber(source:sub(-1))
tarray[id] = xap.getValue("input.state","displaytext")
end
function catchCurrentCost()
local source = xap.getValue("xap-header","source")
local id = tonumber(source:sub(-1))
parray[id] = "P"..id.." "..xap.getValue("input.state","text")
end
-- Refresh infostring from t + p arrays and start new runlcd cycle
function repeatlcd(self)
local arraylen = table.getn(tarray) + table.getn(parray)
if arraylen > 0 then
infostring = string.rep(fill, lcdlen) -- global
for i,value in ipairs(tarray) do infostring = infostring.." "..value.." "..fill end
for i,value in ipairs(parray) do infostring = infostring.." "..value.." "..fill end
infostring = infostring..string.rep(fill, lcdlen - 1)
-- print(infostring)
infostringlen = string.len(infostring) -- global
startpos = 1 -- global
endpos = lcdlen -- global
xap.Timer(runlcd, period):start()
self:delete()
end
end
-- Show lcdlen/16 wide window of infostring on lcd
function runlcd(self)
lcdstring = string.sub(infostring, startpos, endpos)
-- print(infostringlen.."."..startpos.."."..endpos.."'"..lcdstring.."'")
send2lcd(lcdstring)
endpos = endpos + shift
if endpos > infostringlen then
xap.Timer(repeatlcd, period):start()
self:delete()
else
startpos = startpos + shift
end
end
function send2lcd(lcdstr)
xap.sendShort(string.format([[xap-header
{
target=dbzoo.livebox.Controller:lcd
class=xAPBSC.cmd
}
output.state.1
{
id=*
state=on
text=%s
}]], lcdstr))
end
Its great to see Lua being put to some serious work. Good stuff. You might want to change self:stop() to self:delete() otherwise all these timers you are creating and then stopping will slowly leak memory.
Brett
I thought I would have a go at collecting data to scroll over the LCD although as my HAH hardware is down I could not fully test - its look roughly ok thou.
Its in the 279.21 beta as it required a minor change to xap library to enable user data to be supplied to a filter callback an oversight on my behalf. I'll update doc for this.
See /etc_ro_fs/plugboard/samples/tickerApplet.lua - Let me know if its truely broken !
I did say I had only done basic testing. Thanks for those corrections. :)
The message should change once you toggle some relays etc,, try adding your own fitlers/functions to display data you want.
Mess around the program and get it working then send it back - It was more to show you some more advanced ways of doing things.
Not sure what that print is coming from... I don't see anything obvious check your applets perhaps?
Brett
Before the HAH project I'd never use LUA myself so I'm learning too - perhaps I just learn faster, i did read a few book first.
Your scripts are a good inspiration for idea's to code up and see how flexible this language is. So far I'm really happy with what it can do, its a great way to add extensions and capabilities as you've discovering.
Brett
The Timer() was firing when it was first started instead of after its timeout interval - this was a bug. Gary found out the hard way; he had a autoreboot LUA script that ran as soon as his livebox came up . Needless to say he locked himself out in a continuous reboot cycle after he upgraded to 279.21.
I've fixed this is 279.22 and added another test case to make sure it does not regress.
Why did this happen? I removed all the LUA/C library wrappers and rewrote the entire xap library in pure LUA saving about 200k of flash space in the process and allowing the system to be more flexible.
Brett
Aivo,
This is a great script. It really demonstrates what can be done with the Lua engine that Brett is now incorporating into the HAH.
Well done, and keep them coming,
Karl