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
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
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
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
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)
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
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
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
dodraw()
end)
+
+ return { ['ncolors'] = 2 }
end
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
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
--
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
-- 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
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
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