]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
drawings: return information structure
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 3 Jan 2018 22:09:36 +0000 (17:09 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 3 Jan 2018 22:09:36 +0000 (17:09 -0500)
README.rst
draw-butterfly.lua
draw-candle.lua
draw-flower.lua
draw-heartbeat.lua
draw-heartstream.lua
draw-sketchheart.lua
draw-snake2.lua
draw-together.lua

index 7eba3da968cfa8bd299925efd014ca50b8437fde..6b5cb704ff87b09b9cb29fcf19ec33d288aee397 100644 (file)
@@ -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
index b11009402493b63398fc60e34c2418ee239fca8c..88e8d031b0999586e0f9e6bfe82fe6c68a7ab47b 100644 (file)
@@ -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
index 26fdecd085f7441a066b47c212cc0520e7c330c5..50faa6bd54319e8add9cc38d06fd73f160485354 100644 (file)
@@ -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
index e9c65be80d38ae4cb212feec8c40e9722ea0b9cf..009e56334c016b4b0c80203fbdddf400403a5d85 100644 (file)
@@ -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
index 845fd7ac49d6b30c497a9ea76325643372f78c3c..448ad1c15b37367d2db1e89b400aa0766a9386b7 100644 (file)
@@ -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
index d972719b3bea7f54a8aae4ace1db4362d370894c..08c287540b74f5e342462ea5fc97e72a79d78c16 100644 (file)
@@ -51,4 +51,6 @@ return function(t,fb,p)
 
     dodraw()
   end)
+
+  return { ['ncolors'] = 2 }
 end
index ada28270e69587eb9f23ba47f4744745e30595ca..a23c12a3080e2d5dfa0f7d1f878405a222358da1 100644 (file)
 
 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
index 1914ba2b7e9057c73acea9dd74e2f719b0eb72d9..a732053412e729150fcb304f6894c44ac1cb6ab8 100644 (file)
@@ -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
index 810713d030ee17629ca979d8e10da52b3a898004..deece701f2c173b7193bfa0b2b1b66afad4a409f 100644 (file)
@@ -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