]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
Experimental timer/animation support in lamp
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 11 Oct 2016 22:16:37 +0000 (18:16 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 11 Oct 2016 22:16:37 +0000 (18:16 -0400)
init2.lua
lamp-draw.lua
lamp-remote.lua
lamp-touch.lua

index 1471f0c6f51d366db40ed486bc0856f007b5f8cf..d3fe857618ba825f40e4e6b4f2b61e4e4257bdd9 100644 (file)
--- a/init2.lua
+++ b/init2.lua
@@ -3,8 +3,9 @@ local mqttHeartbeat = 600000
 local mqttUser
 
 -- some exported modules for overlay and REPL use
--- XXX timer 5 reserved for the eventual day that we want animations in lamp-draw.lc
-tq = (dofile "tq.lc")(6)            -- timer 6
+remotetmr = tmr.create()
+touchtmr = tmr.create()
+tq = (dofile "tq.lc")(tmr.create())
 nwfnet = require "nwfnet"
 mqc, mqttUser = dofile("nwfmqtt.lc").mkclient("nwfmqtt.conf")
 local mqttBcastPfx = string.format("lamp/%s/out",mqttUser)
@@ -35,7 +36,12 @@ ledfb_claimed = 0 -- 0 : unclaimed, set remote immediately
 
 isblackout = false
 function dodraw() if not isblackout then ws2812.write(ledfb) end end
-function doremotedraw() if ledfb_claimed > 1 then ledfb_claimed = 2 else ledfb = remotefb; dodraw() end end
+function doremotedraw()
+  if ledfb_claimed > 1
+   then ledfb_claimed = 2
+   else touchtmr:unregister(); ledfb = remotefb; remotetmr:start(); dodraw()
+  end
+end
 
 function leddefault(fb,...) fb:fill(0,0,0); local ix; for ix = 25,32 do fb:set(ix,...) end end
 
@@ -59,7 +65,7 @@ nwfnet.onnet["init"] = function(e,c)
   if     e == "mqttdscn" and c == mqc then
     if mqtt_beat_cancel then mqtt_beat_cancel(); mqtt_beat_cancel = nil end
     if not mqtt_reconn_poller then mqtt_reconn() end
-    dofile("lamp-draw.lc").xx(remotefb,0,5,0); doremotedraw()
+    dofile("lamp-draw.lc").xx(remotetmr,remotefb,0,5,0); doremotedraw()
   elseif e == "mqttconn" and c == mqc then
     if mqtt_reconn_poller then tq:dequeue(mqtt_reconn_poller); mqtt_reconn_poller = nil end
     if not mqtt_beat_cancel then mqtt_beat_cancel = dofile("nwfmqtt.lc").heartbeat(mqc,mqttHeartTopic,tq,mqttHeartbeat) end
@@ -75,6 +81,9 @@ nwfnet.onnet["init"] = function(e,c)
   end
 end
 
+-- initialize display
+dofile("lamp-draw.lc").xx(remotetmr,remotefb,0,5,5); dodraw()
+
 -- touch overlay loader
 function ontouch_load() dofile("lamp-touch.lc") end
 
@@ -82,9 +91,6 @@ function ontouch_load() dofile("lamp-touch.lc") end
 -- pin 5 (GPIO14) is cap sensor reset (active low)
 dofile("cap1188-init.lc").init(6,5,ontouch_load)
 
--- initialize display
-dofile("lamp-draw.lc").xx(remotefb,0,5,5); dodraw()
-
 -- initialize network
 dofile("nwfnet-diag.lc")(true)
 dofile("nwfnet-go.lc")
index 9cb22ea0938dab15ea1e30c77ff8684bc538ff0f..9926444abc6383b537f48c63402aa6811d2f9aef 100644 (file)
@@ -1,6 +1,7 @@
 -- a table of functions that take framebuffers and colors
 return {
-  ["heart"] = function(fb,g,r,b)
+  ["heart"] = function(t,fb,g,r,b)
+    t:unregister()
     fb:fill(0,0,0)
     local c = string.char(g,r,b)
                  fb:set( 3,c)              fb:set( 5,c)
@@ -8,13 +9,23 @@ return {
                  fb:set(19,c) fb:set(20,c) fb:set(21,c)
                               fb:set(28,c)
   end,
-  ["fill"] = function(fb,g,r,b) fb:fill(g,r,b) end,
-  ["xx"] = function(fb,g,r,b)
+  ["fill"] = function(t,fb,g,r,b) t:unregister(); fb:fill(g,r,b) end,
+  ["xx"] = function(t,fb,g,r,b)
+    t:unregister()
     fb:fill(0,0,0)
     local c = string.char(g,r,b)
     fb:set( 1,c) fb:set( 4,c) fb:set( 5,c) fb:set( 8,c)
     fb:set(10,c) fb:set(11,c) fb:set(14,c) fb:set(15,c)
     fb:set(18,c) fb:set(19,c) fb:set(22,c) fb:set(23,c)
     fb:set(25,c) fb:set(28,c) fb:set(29,c) fb:set(32,c)
+  end,
+  ["laserchase"] = function(t,fb,g,r,b)
+    local ix = 1
+    local c = string.char(g,r,b)
+    fb:fill(0,0,0)
+    t:register(50,tmr.ALARM_AUTO,function()
+      fb:fade(2); fb:set(ix,c); dodraw()
+      ix = ix + 1; if ix > fb:size() then ix = 1 end
+    end)
   end
 }
index 97b1af27193eac05e816e026a4ba26994e11b361..048a7397d5a58b9dcf85c8318cfa82f3d23d824a 100644 (file)
@@ -1,7 +1,8 @@
--- GLOBAL: tq, remotefb, leddefault, doremotedraw, mqtt_revert
+-- GLOBAL: tq, remotefb, leddefault, doremotedraw, mqtt_revert, remotetmr
 
 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
@@ -15,8 +16,8 @@ return function(m)
   if ix then
     g = tonumber(g,16); r = tonumber(r,16); b = tonumber(b,16)
     local f = loadfile "lamp-draw.lc"
-    local fn = f and type(f) == "table" and f[m]
-    if fn then fn(doremotedraw,remotefb,g,r,b)
+    local fn = f and f()[m]
+    if fn then fn(remotetmr,remotefb,g,r,b); doremotedraw()
      else remotefb:fill(g,r,b); doremotedraw() -- failsafe
     end
     -- if there's a duration set, register a timer to reset the display to the default
index a39c09b8e4fd4003f26494a14f04339be73267ac..ef0bc4704a6520371526e0a755986821dcd4ffb5 100644 (file)
@@ -1,4 +1,4 @@
--- globals referenced: isblackout, dodraw, ledfb, ledfb_claimed, remotefb, lamp_announce, tq
+-- globals referenced: isblackout, dodraw, ledfb, ledfb_claimed, remotefb, remotetmr, lamp_announce, tq
 -- assumptions: gpio.trig(6) is the right thing to do for touch IRQs
 
 local touch_tq = (dofile "tq.lc")(5)
@@ -21,6 +21,7 @@ end
 
 local function claimfb()
   if ledfb_claimed == 0 then
+    remotetmr:stop()
     ledfb_claimed = 1
     ledfb = touchfb
   end
@@ -79,8 +80,10 @@ local function ontouchdone()
   -- leave the ledfb pointing at us; it'll get updated eventually,
   -- unless there was a remote message while we were doing our thing
   -- in which case, display it now
-  if ledfb_claimed == 2 then ledfb = remotefb; dodraw() end
-  ledfb_claimed = 0
+  if ledfb_claimed == 2
+   then ledfb_claimed = 0; remotetmr:start(); doremotedraw()
+   else ledfb_claimed = 0
+  end
 end
 
 -- must not change ledfb to touchfb unless the user interacts with us
@@ -128,8 +131,8 @@ local function ontouch()
 
   -- draw if we've claimed it!
   if ledfb == touchfb then
-    touchfns[touchfnix](touchfb,touchcolorvec(touchcolor))
-    dodraw()
+    touchfns[touchfnix](touchtmr,touchfb,touchcolorvec(touchcolor)); dodraw()
+    touchtmr:start()
   end
 end