]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
lamp: support sequences of drawings
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 21 Nov 2016 21:40:47 +0000 (16:40 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 21 Nov 2016 21:40:47 +0000 (16:40 -0500)
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
lamp-remote.lua

index 338d0ceacac574f6d2250b8f024fc3a2926f2835..7278d24654abfdd753ade2cbebeb2c40fa658ade 100644 (file)
--- 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
index 14a0a9160469f7d9ef899726cc0406193f4be4d5..8ebfcbfff11f0d06adbfe34f90d40fda4e0de83a 100644 (file)
@@ -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