From ef498de857d38bf944d45ccd281e424dfe69cd5f Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 3 Jan 2018 17:09:36 -0500 Subject: [PATCH] drawings: return information structure --- README.rst | 13 +++++++++ draw-butterfly.lua | 63 ++++++++++++++++++++++++++++---------------- draw-candle.lua | 24 +++++++++++------ draw-flower.lua | 22 +++++++++++----- draw-heartbeat.lua | 16 ++++++++--- draw-heartstream.lua | 2 ++ draw-sketchheart.lua | 13 ++++++--- draw-snake2.lua | 20 ++++++++++---- draw-together.lua | 21 +++++++++++---- 9 files changed, 139 insertions(+), 55 deletions(-) diff --git a/README.rst b/README.rst index 7eba3da..6b5cb70 100644 --- a/README.rst +++ b/README.rst @@ -121,6 +121,19 @@ expected to *return a function* which takes, in order, form the green, red, and blue color values (0-255), as per ws2812 byte ordering. Most drawings will simply use entry 1. +These drawing functions should return a table containing the following keys: + ++-------------+-----------------------------------------------------------+ +| ``ncolors`` | The number of colors taken from the palette, default 1. | ++-------------+-----------------------------------------------------------+ +| ``cccb`` | A callback function for color palette changes; | +| | if ``nil`` (default), no notifications will be given and | +| | the animation will be restarted on such changes. | ++-------------+-----------------------------------------------------------+ + +Returning ``nil`` from the drawing function is equivalent to returning a +table with all defaults (i.e., 1 color, no notification on changes). + ``lamp-touch.lua`` (see below) knows the naming scheme used by ``init2.lua`` and so walks the list of files on the system looking for files whose name matches the Lua regex ``draw-(%w+).lc`` and builds a table of all such diff --git a/draw-butterfly.lua b/draw-butterfly.lua index b110094..88e8d03 100644 --- a/draw-butterfly.lua +++ b/draw-butterfly.lua @@ -1,30 +1,47 @@ return function(t,fb,p) + local c + fb:fill(0,0,0) - local c = p[1] - local ft = { [1] = function() fb:set(2,c) fb:set(9,0,0,0) fb:set(10,c) fb:set(17,0,0,0) fb:set(18,c) fb:set(14,c) fb:set(15,0,0,0) fb:set(22,c) fb:set(23,0,0,0) fb:set(26,c) end, --start flapping - [2] = function() fb:set(2,0,0,0) fb:set(3,c) - fb:set(6,0,0,0) fb:set(27,c) fb:set(5,c) - fb:set(26,0,0,0) fb:set(29,c) fb:set(30,0,0,0) - fb:set(10,0,0,0) fb:set(18,0,0,0) - fb:set(14,0,0,0) fb:set(22,0,0,0) end, -- fully collapsed - [3] = function() fb:set(2,c) fb:set(3,0,0,0) fb:set(27,0,0,0) - fb:set(5,0,0,0) fb:set(6,c) fb:set(29,0,0,0) fb:set(10,c) - fb:set(18,c) fb:set(14,c) fb:set(22,c) fb:set(26,c) - fb:set(30,c) - end, -- half expanded - [4] = function() fb:set(9,c) fb:set(10,0,0,0) fb:set(17,c) - fb:set(18,0,0,0) fb:set(14,0,0,0) fb:set(15,c) - fb:set(22,0,0,0) fb:set(23,c) end -- back to the beginning - } + local z = string.char(0,0,0) + local ft = {[1] = function() --start flapping + fb:set(2,c) + fb:set(9,z) fb:set(10,c) fb:set(14,c) fb:set(15,z) + fb:set(17,z) fb:set(18,c) + fb:set(22,c) fb:set(23,z) fb:set(26,c) + end, + [2] = function() -- fully collapsed + fb:set(2,z) fb:set(3,c) fb:set(5,c) fb:set(6,z) + fb:set(10,z) fb:set(14,z) fb:set(18,z) fb:set(22,z) + fb:set(26,z) fb:set(27,c) fb:set(29,c) fb:set(30,z) + end, + [3] = function() -- half expanded + fb:set(2,c) fb:set(3,z) fb:set(5,z) fb:set(6,c) + fb:set(10,c) + fb:set(14,c) fb:set(18,c) fb:set(22,c) + fb:set(26,c) fb:set(27,z) fb:set(29,z) fb:set(30,c) + end, + [4] = function() -- back to the beginning + fb:set(9,c) fb:set(10,z) fb:set(14,z) fb:set(15,c) + fb:set(17,c) fb:set(18,z) fb:set(22,z) fb:set(23,c) + end + } + + local function reinit() + c = p[1] + + fb:set( 2,c) fb:set( 4,c) fb:set( 6,c) + fb:set( 9,c) fb:set(11,c) fb:set(12,c) fb:set(13,c) fb:set(15,c) + fb:set(17,c) fb:set(19,c) fb:set(20,c) fb:set(21,c) fb:set(23,c) + fb:set(26,c) fb:set(28,c) fb:set(30,c) + end + reinit() - fb:set( 2,c) fb:set( 4,c) fb:set( 6,c) fb:set( 9,c) - fb:set(11,c) fb:set(12,c) fb:set(13,c) fb:set(15,c) - fb:set(17,c) fb:set(19,c) fb:set(20,c) fb:set(21,c) - fb:set(23,c) fb:set(26,c) fb:set(28,c) fb:set(30,c) - local ix = 1 + local ix = 4 t:register(500,tmr.ALARM_AUTO, function() - ft[ix]() ix = (ix == 4 and 1) or ix + 1 + ft[ix]() dodraw() end) - end + + return { ['cccb'] = function() reinit(); for ixp = 1,ix do ft[ixp]() end; dodraw() end } +end diff --git a/draw-candle.lua b/draw-candle.lua index 26fdecd..50faa6b 100644 --- a/draw-candle.lua +++ b/draw-candle.lua @@ -9,16 +9,22 @@ return function(t,fb,p) fb:fill(0,0,0) - -- static base - local i,v - for i,v in ipairs({18,23,26,27,28,29,30,31}) do fb:set(v,2,2,1) end -- whiteish (W) - for i,v in ipairs({10,15,19,20,21,22}) do fb:set(v,1,1,1) end -- dim white (w) + local g, r, b, cmax - local g = p[1]:byte(1) - local r = p[1]:byte(2) - local b = p[1]:byte(3) + local function reinit() + g = p[1]:byte(1) + r = p[1]:byte(2) + b = p[1]:byte(3) + cmax = math.max(r,g,b) - local cmax = math.max(r,g,b) + -- "static" base + local i,v + local w = p[2] or string.char(2,2,1) + for i,v in ipairs({18,23,26,27,28,29,30,31}) do fb:set(v,w) end -- whiteish (W) + local w = p[3] or string.char(1,1,1) + for i,v in ipairs({10,15,19,20,21,22}) do fb:set(v,w) end -- dim white (w) + end + reinit() -- off channels stay off, on channels stay on, just minimally dim local function adjust(val,bias) if val == 0 then return 0 elseif val <= bias then return 1 else return val - bias end end @@ -41,4 +47,6 @@ return function(t,fb,p) local ft = { [0] = draweq, draweq, draweq, drawbb, drawlb, drawrb } t:register(125,tmr.ALARM_AUTO,function() ft[math.random(#ft)]() dodraw() end) draweq() + + return { ['ncolors'] = 3, ['cccb'] = function() reinit() draweq() dodraw() end } end diff --git a/draw-flower.lua b/draw-flower.lua index e9c65be..009e563 100644 --- a/draw-flower.lua +++ b/draw-flower.lua @@ -7,11 +7,17 @@ return function(t,fb,p) fb:fill(0,0,0) - local c = p[1] - local g = c:byte(1) - local r = c:byte(2) - local b = c:byte(3) - local cdim = string.char(math.floor((g+1)/2),math.floor((r+1)/2),math.floor((b+1)/2)) + + local c, g, r, b, cdim + + local function reinit() + c = p[1] + g = c:byte(1) + r = c:byte(2) + b = c:byte(3) + cdim = string.char(math.floor((g+1)/2),math.floor((r+1)/2),math.floor((b+1)/2)) + end + reinit() for i,v in ipairs({17,18,19,20}) do fb:set(v,0xf,0,0) end -- stem (s) for i,v in ipairs({10,27}) do fb:set(v,0x7,0,0) end -- leaf (l) @@ -47,14 +53,16 @@ return function(t,fb,p) end, } - local ix = 1 + local ix = #ft local function cb() - local dly = ft[ix]() ix = (ix == #ft and 1) or ix + 1 + local dly = ft[ix]() dodraw() t:register(dly,tmr.ALARM_SINGLE,cb) t:start() end cb() + + return { ['ncolors'] = 1, ['cccb'] = function() reinit(); for ixp = 1,ix do ft[ixp]() end; dodraw() end } end diff --git a/draw-heartbeat.lua b/draw-heartbeat.lua index 845fd7a..448ad1c 100644 --- a/draw-heartbeat.lua +++ b/draw-heartbeat.lua @@ -1,6 +1,12 @@ return function(t,fb,p) - local c = p[1] - local c2 = p[2] or c + local c, c2 + + local function reinit() + c = p[1] + c2 = p[2] or c + end + reinit() + local z = string.char(0,0,0) local ft = { [1] = function() fb:set(11,c2) fb:set(13,c2) end, -- side chambers [2] = function() fb:set(11,z) fb:set(20,c2) fb:set(13,z) end, -- bottom chamber @@ -15,10 +21,12 @@ return function(t,fb,p) fb:set(19,c) fb:set(21,c) fb:set(28,c) - local ix = 1 + local ix = 4 t:register(250,tmr.ALARM_AUTO,function() - ft[ix]() ix = (ix == 4 and 1) or ix + 1 + ft[ix]() dodraw() end) + + return { ['ncolors'] = 2, ['cccb'] = function() reinit(); local ixp = 1,ix do ft[ixp]() end; dodraw() end } end diff --git a/draw-heartstream.lua b/draw-heartstream.lua index d972719..08c2875 100644 --- a/draw-heartstream.lua +++ b/draw-heartstream.lua @@ -51,4 +51,6 @@ return function(t,fb,p) dodraw() end) + + return { ['ncolors'] = 2 } end diff --git a/draw-sketchheart.lua b/draw-sketchheart.lua index ada2827..a23c12a 100644 --- a/draw-sketchheart.lua +++ b/draw-sketchheart.lua @@ -11,7 +11,13 @@ return function(t,fb,p) local k,v - local c = p[1] + local c + + local function reinit() + c = p[1] + end + reinit() + local offset = 0 local ft = { -- animate together @@ -40,11 +46,12 @@ return function(t,fb,p) function() fb:set(20+offset,0,0,0) ; end, } ft[1](); dodraw() - local ix = 2 + local ix = 1 t:register(350,tmr.ALARM_AUTO,function() - ft[ix]() ix = (ix == #ft and 1) or ix + 1 + ft[ix]() dodraw() end) + return { ['cccb'] = function() reinit(); local ixp=1,ix do ft[ixp]() end; dodraw() end } end diff --git a/draw-snake2.lua b/draw-snake2.lua index 1914ba2..a732053 100644 --- a/draw-snake2.lua +++ b/draw-snake2.lua @@ -11,8 +11,8 @@ -- return function(t,fb,p) local ix = 2 -- since we start effectively in state 1... - local c = p[1] - local h = p[2] or string.char(0,15,0) -- heart defaults red + local c, h + local z = string.char(0,0,0) local ft = { -- flatten out [1] = function() fb:set(25,c) fb:set(27,c) fb:set(17,z) fb:set(19,z) end @@ -33,12 +33,22 @@ return function(t,fb,p) -- slither 4 , [8] = function() fb:set(17,c) fb:set(18,z) fb:set(19,c) fb:set(20,z) fb:set(25,z) fb:set(26,c) fb:set(27,z) fb:set(28,c) end } - fb:fill(0,0,0) - fb:set(25,c) fb:set(26,c) fb:set(27,c) fb:set(28,c) fb:set(29,c) fb:set(30,c) - fb:set(23,c) fb:set(15,c) fb:set(7,c) fb:set(8,c) + + local function reinit() + c = p[1] + h = p[2] or string.char(0,15,0) -- heart defaults red + + fb:fill(0,0,0) + fb:set(25,c) fb:set(26,c) fb:set(27,c) fb:set(28,c) fb:set(29,c) fb:set(30,c) + fb:set(23,c) fb:set(15,c) fb:set(7,c) fb:set(8,c) + end + reinit() + t:register(500,tmr.ALARM_AUTO,function() ft[ix]() ix = (ix == 8 and 1) or ix + 1 dodraw() end) + + return { ['ncolors'] = 2, ['cccb'] = function() reinit(); ix = 4; ft[ix](); dodraw() end } end diff --git a/draw-together.lua b/draw-together.lua index 810713d..deece70 100644 --- a/draw-together.lua +++ b/draw-together.lua @@ -13,9 +13,7 @@ return function(t,fb,p) local k,v - local c = p[1] - local c2 = p[2] or c - local c3 = p[3] or c + local c, c2, c3 local offset = 0 local ft = { -- animate together @@ -45,12 +43,25 @@ return function(t,fb,p) function() fb:set(11+offset,0,0,0) fb:set(13+offset,0,0,0) fb:set(20+offset,c) end, function() fb:set(20+offset,0,0,0) ; offset = 1 - offset end, } + + local function reinit() + c = p[1] + c2 = p[2] or c + c3 = p[3] or c + end + reinit() + ft[1](); dodraw() - local ix = 2 + local ix = 1 t:register(350,tmr.ALARM_AUTO,function() - ft[ix]() ix = (ix == #ft and 1) or ix + 1 + ft[ix]() dodraw() + t:interval(350) -- put it back in case cccb has changed it end) + -- In cccb, set ix=5 so that all colors are on screen; artificially + -- increase animation delay + return { ['ncolors'] = 3, ['cccb'] = function() reinit(); ix = 5; ft[ix]() ; dodraw(); t:interval(800) end } + end -- 2.50.1