From eff427c996bd43a7418a4cf8cc6ea03378c48ac4 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Tue, 11 Oct 2016 18:16:37 -0400 Subject: [PATCH] Experimental timer/animation support in lamp --- init2.lua | 20 +++++++++++++------- lamp-draw.lua | 17 ++++++++++++++--- lamp-remote.lua | 7 ++++--- lamp-touch.lua | 13 ++++++++----- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/init2.lua b/init2.lua index 1471f0c..d3fe857 100644 --- 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") diff --git a/lamp-draw.lua b/lamp-draw.lua index 9cb22ea..9926444 100644 --- a/lamp-draw.lua +++ b/lamp-draw.lua @@ -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 } diff --git a/lamp-remote.lua b/lamp-remote.lua index 97b1af2..048a739 100644 --- a/lamp-remote.lua +++ b/lamp-remote.lua @@ -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 diff --git a/lamp-touch.lua b/lamp-touch.lua index a39c09b..ef0bc47 100644 --- a/lamp-touch.lua +++ b/lamp-touch.lua @@ -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 -- 2.50.1