From c2c176ff47d624152593364f8092c8a37b3b3fd9 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Mon, 21 Nov 2016 16:40:47 -0500 Subject: [PATCH] lamp: support sequences of drawings This changes the protocol to necessitate a ';' at the end of each animation. The directives are "[delay] [drawing] [r] [g] [b] ;", with delay decimal milliseconds to wait after the previous command, r/g/b hex 0-FF and drawing as before. e.g. '0 fill 0 3 0 ; 5000 heart 7 0 7 ;'. --- init2.lua | 2 +- lamp-remote.lua | 36 ++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/init2.lua b/init2.lua index 338d0ce..7278d24 100644 --- a/init2.lua +++ b/init2.lua @@ -58,7 +58,7 @@ mqtt_revert = nil nwfnet.onmqtt["lamp"] = function(c,t,m) if t and m and t:find("^lamp/[^/]+/out") then dofile("lamp-remote.lc")(m) end end -- TODO: messages to specific lamps? Multiple brokers? -function lamp_announce(fn,g,r,b) mqc:publish(mqttBcastPfx,string.format("0 %s %x %x %x",fn,r,g,b),1,1) end +function lamp_announce(fn,g,r,b) mqc:publish(mqttBcastPfx,string.format("0 %s %x %x %x ;",fn,r,g,b),1,1) end -- mqtt setup local mqtt_beat_cancel diff --git a/lamp-remote.lua b/lamp-remote.lua index 14a0a91..8ebfcbf 100644 --- a/lamp-remote.lua +++ b/lamp-remote.lua @@ -1,27 +1,23 @@ --- GLOBAL: tq, remotefb, leddefault, doremotedraw, mqtt_revert, remotetmr, loaddrawfn - -local function ledrevert(ix) - if ix < 3 then - remotetmr:unregister() - remotefb:fade(2) doremotedraw() - tq:queue(500,function() ledrevert(ix+1) end) - else leddefault(remotefb,0,16,16) end - dodraw() -end +-- GLOBAL: tq, remotefb, doremotedraw, remotetmr, loaddrawfn, remotetqh return function(msg) - if mqtt_revert then tq:dequeue(mqtt_revert) end + if remotetqh then tq:dequeue(remotetqh) end - local ix, _, d, m, r, g, b = msg:find("^(%d+)%s+(%w+)%s+(%x+)%s+(%x+)%s+(%x+)%s*$") - if ix then - g = tonumber(g,16); r = tonumber(r,16); b = tonumber(b,16) + local fifo = (require "fifo")() + local function fdq() if not fifo:dequeue(function(k) k() end) then remotetqh = nil end end - remotetmr:unregister() - loaddrawfn(m)(remotetmr,remotefb,g,r,b); doremotedraw() + local d,m,r,g,b - -- if there's a duration set, register a timer to reset the display to the default - local dn = tonumber(d) - if dn and dn > 0 then tq:queue(math.min(dn,6870947),ledrevert,0) end + for d,m,r,g,b in msg:gmatch("(%d+)%s+(%w+)%s+(%x+)%s+(%x+)%s+(%x+)%s*;") do + g = tonumber(g,16); r = tonumber(r,16); b = tonumber(b,16); d = tonumber(d) + if d and d > 0 then + fifo:queue(function() remotetqh = tq:queue(d,fdq) end) + end + fifo:queue(function() + remotetmr:unregister() + loaddrawfn(m)(remotetmr,remotefb,g,r,b); doremotedraw() + remotetqh = tq:queue(1,fdq) -- run on callback to avoid stack problems + end) end - + fdq() -- start party end -- 2.50.1