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)
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
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
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
-- 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")
-- 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)
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
}
--- 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
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
--- 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)
local function claimfb()
if ledfb_claimed == 0 then
+ remotetmr:stop()
ledfb_claimed = 1
ledfb = touchfb
end
-- 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
-- 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