Scrolling info on LCD

8 replies [Last post]
aivo
Offline
Tallinn, Estonia
Joined: 2 Mar 2011

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

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
Great Script

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Excellent work

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Could not help myself

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 !

aivo
Offline
Tallinn, Estonia
Joined: 2 Mar 2011
How it works on 279.21

Hi Brett,

It does need output.state.1 and id=* otherwise not coming to lcd; plus scrollPos = 1 is better ;)

Also for some reason I do not understand following happens: first two lines come out (print(scrollMsg)) basically at same time.  I.e. from showmap() in init and the one from timer.  When commenting out one in init first line from timer comes out I would say before 1sec passes though timer is 10sec (this isn't by design?), next intervals are ok.
For my unit the scrolling message also remains on "No data available".

Also when starting plugboard from command line one will notice cursor advancing periodically (or space printed without linefeed) - if this is some debug info maybe?

Regards,
Aivo

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Yup a few minor error

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

aivo
Offline
Tallinn, Estonia
Joined: 2 Mar 2011
Thank you

Good, it already did light up some bulbs!  Though one has to stay at a level with his scripts one is comfortable troubleshooting ;)  Too many dimensions or lua arrays etc. at this point of lua learning for me .. might make the joy to go away.

Otherwise, joy and understanding one can make things work yourself are important motivators for audience of your hah forum, dont you agree?  I might be wrong too of course.

Best regards,
Aivo

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I'm learning too

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Timer firing not by design

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

Hardware Info