]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
Initial support for color palettes
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 7 Nov 2017 23:30:51 +0000 (18:30 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 8 Nov 2017 07:21:52 +0000 (02:21 -0500)
See examples of usage in draw-snake2 and draw-together.
At present, there is no support in the touch module for this,
only via the message parser.

23 files changed:
README.rst
draw-butterfly.lua
draw-candle.lua
draw-devilish.lua
draw-fill.lua
draw-flower.lua
draw-happy.lua
draw-heart.lua
draw-heartbeat.lua
draw-kiss.lua
draw-laserchase.lua
draw-nom.lua
draw-oo.lua
draw-shifty.lua
draw-shifty2.lua
draw-snake.lua
draw-snake2.lua
draw-staryeyes.lua
draw-together.lua
draw-xx.lua
init2.lua
lamp-remote.lua
lamp-touch.lua

index 91f5490f0289306e269e706e6fded1fda292f769..6a3864c4f5f326bf6ab1992e313da427ff320c58 100644 (file)
@@ -117,8 +117,9 @@ expected to *return a function* which takes, in order,
 * a ws2812 framebuffer object into which all drawing should happen.  This
   framebuffer should be closed over in any timer callbacks.
 
-* a color parameter, expressed as three arguments 0-255: green, red, blue
-  (as per ws2812 byte ordering).
+* A color palette array.  Each entry is a string of three, where the bytes
+  form the green, red, and blue color values (0-255), as per ws2812 byte
+  ordering.  Most drawings will simply use entry 1.
 
 ``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
index f28b8d6dacceba1ea8deb48c7f70c5340bf0cabe..b11009402493b63398fc60e34c2418ee239fca8c 100644 (file)
@@ -1,6 +1,6 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
-  local c = string.char(g,r,b)
+  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)
@@ -27,4 +27,4 @@ return function(t,fb,g,r,b)
     ix = (ix == 4 and 1) or ix + 1
     dodraw()
     end)
- end
\ No newline at end of file
+ end
index 37eb65ce5691630a6057e91f9efdce549c41d07d..26fdecd085f7441a066b47c212cc0520e7c330c5 100644 (file)
@@ -7,13 +7,17 @@
 -- 24 o W W W W W W o
 --
 
-return function(t,fb,g,r,b)
+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 = p[1]:byte(1)
+  local r = p[1]:byte(2)
+  local b = p[1]:byte(3)
+
   local cmax = math.max(r,g,b)
 
   -- off channels stay off, on channels stay on, just minimally dim
index 77921a10912af7c39e57f52766a5d0b3b4d297aa..b156bd2c60ffa8d8c9f99664ae8c022c71b30418 100644 (file)
@@ -2,9 +2,9 @@
 -- x o o x o o x o
 -- x o o o o x x o
 -- o x o o x o o x
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
-  local c = string.char(g,r,b)
+  local c = p[1]
   fb:set( 2,c) fb:set( 5,c) fb:set( 8,c)
   fb:set( 9,c) fb:set(12,c) fb:set(15,c)
   fb:set(17,c) fb:set(22,c) fb:set(23,c)
index 27eee3008a81175aa9b57ca592e276f9a7d9271c..3dd5d62c8376111acb89265fa5d7758535b3062a 100644 (file)
@@ -1 +1 @@
-return function(t,fb,g,r,b) fb:fill(g,r,b) end
+return function(t,fb,p) fb:fill(p[1]:byte(1),p[1]:byte(2),p[1]:byte(3)) end
index f0823fe95da8b2cf30d13577c863a46308a59848..e9c65be80d38ae4cb212feec8c40e9722ea0b9cf 100644 (file)
@@ -5,9 +5,12 @@
 -- 24 o o l o f f f o
 --
 
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
-  local c = string.char(g,r,b)
+  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))
 
   for i,v in ipairs({17,18,19,20}) do fb:set(v,0xf,0,0) end -- stem (s)
index 8e4178704504142a2be97799d5b8c02a72fd83ca..963479dbb72eb510e5d4b29a9040f836932b808f 100644 (file)
@@ -5,8 +5,8 @@
 -- 16 o o o o o o o o
 -- 24 o o o x x o o o
 --
-return function(t,fb,g,r,b)
-  local c = string.char(g,r,b)
+return function(t,fb,p)
+  local c = p[1]
   local i,v
   fb:fill(0,0,0)
   for i,v in ipairs({2,7,9,11,14,16,28,29}) do fb:set(v,c) end
index 39dd90e81434c4811033b268a09e83e08f631058..c71183a7343ebeaa97ad0bcfa0e177ad8a16a48f 100644 (file)
@@ -1,6 +1,6 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
-  local c = string.char(g,r,b)
+  local c = p[1]
                fb:set( 3,c)              fb:set( 5,c)
   fb:set(10,c) fb:set(11,c) fb:set(12,c) fb:set(13,c) fb:set(14,c)
                fb:set(19,c) fb:set(20,c) fb:set(21,c)
index 218baafb0c67067c9c2829fc0498aa3c706b6a5c..50a50983b38e55d58d67e79c003e5105e268ecb7 100644 (file)
@@ -1,5 +1,5 @@
-return function(t,fb,g,r,b)
-  local c = string.char(g,r,b)
+return function(t,fb,p)
+  local c = p[1]
   local ft = { [1] = function() fb:set(11,c)                      fb:set(13,c)     end, -- side chambers
                [2] = function() fb:set(11,0,0,0) fb:set(20,c)     fb:set(13,0,0,0) end, -- bottom chamber
                [3] = function()                  fb:set(20,0,0,0)                  end, -- empty
index 230df77ec85bb27343ca23f81f886547581172fd..8cde5a55dc909875766b9529c3ce915cb49312bb 100644 (file)
@@ -2,10 +2,10 @@
 -- 0 x 0 - - x - -
 -- 1 0 1 x x - - -
 -- - - - - - x - -
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
   local ix = 1
-  local c = string.char(g,r,b)
+  local c = p[1]
   fb:set( 2,c) fb:set( 9,c) fb:set(10,c)
   fb:set(11,c) fb:set(14,c) fb:set(18,c) fb:set(20,c)
   fb:set(21,c) fb:set(30,c)
index 51e2805766c6c72c04171efaf92c9145e91024c5..3ae593fa0e9392beade05739e123a3511c0c0f0f 100644 (file)
@@ -1,6 +1,6 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   local ix = 1
-  local c = string.char(g,r,b)
+  local c = p[1]
   fb:fill(0,0,0)
   t:register(50,tmr.ALARM_AUTO,function()
     fb:fade(2); fb:set(ix,c); dodraw()
index 49c5a7b7902a333c61832a93bee095a2b7427b71..17bc5e858efb25352f1693d82db827b816854ed9 100644 (file)
@@ -1,7 +1,7 @@
 --
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   local ix = 2 -- since we start effectively in state 1...
-  local c = string.char(g,r,b)
+  local c = p[1]
   local z = string.char(0,0,0)
   local ft = {   -- open mouth
                [1] = function() fb:set(16,z) fb:set(24,z) fb:set(9,c) fb:set(17,c) fb:set(2,c) fb:set(26,c) end
index d0d6c53d4cae491a4e1a47e1dd14301ab4eeea74..7941753aa1f938515d85da7c102b1fd203508b7d 100644 (file)
@@ -1,6 +1,6 @@
- return function(t,fb,g,r,b)
+ return function(t,fb,p)
    fb:fill(0,0,0)
-   local c = string.char(g,r,b)
+   local c = p[1]
    fb:set( 2,c) fb:set( 3,c) fb:set( 6,c) fb:set( 7,c)
    fb:set( 9,c) fb:set(12,c) fb:set(13,c) fb:set(16,c)
    fb:set(17,c) fb:set(20,c) fb:set(21,c) fb:set(24,c)
index 6e4f1f0f3f040299a1900dc80467488ff2190327..f9823e52db978ba6f570e95a5ed5e43bde6c3d15 100644 (file)
@@ -1,7 +1,7 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
   local ix = 0
-  local c = string.char(g,r,b)
+  local c = p[1]
   fb:set( 2,c) fb:set( 3,c) fb:set( 6,c) fb:set( 7,c)
   fb:set( 9,c) fb:set(12,c) fb:set(13,c) fb:set(16,c)
   fb:set(17,c) fb:set(19,c) fb:set(20,c) fb:set(21,c)
index 30f39cf9e819e9e1321b5eefe4f8c3d37998444f..30a8d2a2e59bf4d38eaa8b7bfba41d173f44e553 100644 (file)
@@ -1,7 +1,7 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
     fb:fill(0,0,0)
     local ix = 0
-    local c = string.char(g,r,b)
+    local c = p[1]
     local x = string.char(0,0,0)
     fb:set( 3,c) fb:set( 8,c) fb:set(10,c) fb:set(15,c)
     fb:set(17,c) fb:set(18,c) fb:set(22,c) fb:set(23,c)
index c4a191e93e612e2596154436c668c2c123b2cbc4..0d5d31f28b6bdfcbcf877ca0316b0bc34416a5fa 100644 (file)
@@ -1,6 +1,6 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   local ix = 0
-  local c = string.char(g,r,b)
+  local c = p[1]
   fb:fill(0,0,0)
   fb:set(25,c) fb:set(26,c) fb:set(19,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)
index 796e2fec78bde377f34bb3803f90e44de0009877..1914ba2b7e9057c73acea9dd74e2f719b0eb72d9 100644 (file)
@@ -1,14 +1,18 @@
 -- a more curious snake with occasional red "heart" nearby
 --
+-- Palette: snake, heart
+--
+--
 --    1 2 3 4 5 6 7 8
 -- 00 o o o o o h x h
 -- 08 o o o o o e x o
 -- 16 w w w w o o x o
 -- 24 w w w w x x x o
 --
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   local ix = 2 -- since we start effectively in state 1...
-  local c = string.char(g,r,b)
+  local c = p[1]
+  local h = p[2] or string.char(0,15,0) -- heart defaults red
   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
@@ -19,7 +23,7 @@ return function(t,fb,g,r,b)
                  -- look forward
              , [3] = function() fb:set(6,z) fb:set(8,c) end
                  -- heart on (like what we see)
-             , [4] = function() fb:set(14,0,15,0) end
+             , [4] = function() fb:set(14,h) end
                  -- slither 1
              , [5] = function() fb:set(20,c) fb:set(28,z) end
                  -- slither 2
index a5443be1f5f8825e707cd55c829b40487a4e7bc2..ab0962ab8eae380424d3c33bbd281053ca7b7a84 100644 (file)
@@ -1,5 +1,8 @@
-return function(t,fb,g,r,b)
-  local c = string.char(g,r,b)
+return function(t,fb,p)
+  local c = p[1]
+  local g = p[1]:byte(1)
+  local r = p[1]:byte(2)
+  local b = p[1]:byte(3)
   local cmax = math.max(r,g,b)
   local i,v
   
@@ -15,7 +18,7 @@ return function(t,fb,g,r,b)
     for i,v in ipairs{...} do fb:set(v,ag,ar,ab) end
   end
 
-  local function drawEq() for i,v in ipairs({3,6}) do fb:set(v,g,r,b) end end
+  local function drawEq() for i,v in ipairs({3,6}) do fb:set(v,c) end end
   local function drawTwinkleEyes() drawbiased(3,6) end
   local ft = { [0] = drawEq, drawEq, drawTwinkleEyes }
   fb:fill(0,0,0)
index 385078feb4f07b98fceb70b4f8944453f91b3ade..810713d030ee17629ca979d8e10da52b3a898004 100644 (file)
@@ -1,6 +1,8 @@
 -- Two hands animate together, heartbeat, then repeat at offset (to average
 -- out the symmetry breaking)
-
+--
+-- Palette: together, left, right
+--
 --    1 2 3 4 5 6 7 8    --    1 2 3 4 5 6 7 8   --    1 2 3 4 5 6 7 8
 -- 00 o 1 o o o o 1 o    -- 00 o o 2 o o 2 o o   -- 00 o o 3 o 3 o o o
 -- 08 1 o o o o o o 1    -- 08 o 2 o o o o 2 o   -- 08 o 3 o 3 o 3 o o
 --                                                 (or shifted right one)
 
 
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   local k,v
-  local c = string.char(g,r,b)
+  local c = p[1]
+  local c2 = p[2] or c
+  local c3 = p[3] or c
   local offset = 0
   local ft = {
     -- animate together
-    function() fb:fill(0,0,0) for k,v in ipairs({2,7,9,16,18,23,27,30}) do fb:set(v,c) end end,
+    function() fb:fill(0,0,0)
+               for k,v in ipairs({2,9,18,27})  do fb:set(v,c2) end -- left
+               for k,v in ipairs({7,16,23,30}) do fb:set(v,c3) end -- right
+               end,
     function() end,
-    function() fb:fill(0,0,0) for k,v in ipairs({3,6,10,15,19,22,28,29}) do fb:set(v,c) end end,
+    function() fb:fill(0,0,0)
+               for k,v in ipairs({3,10,19,28}) do fb:set(v,c2) end -- left
+               for k,v in ipairs({6,15,22,29}) do fb:set(v,c3) end -- right
+               end,
     function() end,
+    function() fb:fill(0,0,0)
+               for k,v in ipairs({3,10,19}) do fb:set(v+offset,c2) end -- left
+               for k,v in ipairs({5,14,21}) do fb:set(v+offset,c3) end -- right
+               for k,v in ipairs({12,28})   do fb:set(v+offset,c)  end -- both
+               end,
+    -- all the both color
     function() fb:fill(0,0,0) for k,v in ipairs({3,5,10,12,14,19,21,28}) do fb:set(v+offset,c) end end,
-    function() end,
     -- beat
     function() fb:set(11+offset,c)     fb:set(13+offset,c) end,
     function() fb:set(11+offset,0,0,0) fb:set(13+offset,0,0,0) fb:set(20+offset,c) end,
index 87ff53fc44f846156385b463a3bb30e23b67e089..4bf40de399b898d391ef4560865eb60fb2eed3de 100644 (file)
@@ -1,6 +1,6 @@
-return function(t,fb,g,r,b)
+return function(t,fb,p)
   fb:fill(0,0,0)
-  local c = string.char(g,r,b)
+  local c = p[1]
   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)
index 046eba8bb134df796c980fdc7934d5344844be29..a4a871a05c32752ad1a768c3a2cf3b99e5d18f75 100644 (file)
--- a/init2.lua
+++ b/init2.lua
@@ -13,7 +13,7 @@ remoteqtmrs = {}
 isTouch = false
 pendRemoteMsg = nil
 
-local function drawfailsafe(t,fb,g,r,b) fb:fill(g,r,b) end
+local function drawfailsafe(t,fb,p) fb:fill(p[1]:byte(1),p[1]:byte(2),p[1]:byte(3)) end
 function loaddrawfn(name)
   local f = loadfile (string.format("draw-%s.lc",name))
   local fn = f and f()
index c3f3da776c85b0ca736578939684ca6f67b711c1..7de7b0f3d1882688b64852a38c4e3e5c4785bcae 100644 (file)
@@ -12,7 +12,7 @@ local vt = {
   ['end'] = function() return true end,
 
   -- in means to queue up a timer pointing at a label (which must already be known)
-  ['in'] = function(s,ls)
+  ['in'] = function(s,ls,p)
       local d,l = s:match("^(%d+)%s+(.*)")
       if d and ls[l] then
         s = ls[l]
@@ -22,21 +22,31 @@ local vt = {
         remoteqtmrs[tn] = t
         t:alarm(d,tmr.ALARM_SINGLE,function()
           remoteqtmrs[tn] = nil
-          intloop(s,ls)
+          intloop(s,ls,p)
         end)
       end
     end,
 
+  ['color'] = function(s,_,p)
+      local ix,r,g,b = s:match("^(%w+)%s+(%x+)%s+(%x+)%s+(%x+)%s*$")
+      ix = tonumber(ix)
+      if ix then
+        g = (tonumber(g,16)) or 1; r = (tonumber(r,16)) or 1; b = (tonumber(b,16)) or 1
+        p[ix] = string.char(g,r,b)
+      end
+    end,
+
   -- draw is the real reason we're here
-  ['draw'] = function(s)
+  ['draw'] = function(s,_,p)
     -- engage a drawing function and post a time event to pop the fifo
     -- on the next tick (for, e.g., delay's use).  This is done on a
     -- callback to prevent deep stacks.
     local m,r,g,b = s:match("^(%w+)%s+(%x+)%s+(%x+)%s+(%x+)%s*$")
     if m then
       g = (tonumber(g,16)) or 0; r = (tonumber(r,16)) or 0; b = (tonumber(b,16)) or 0
+      p[1] = string.char(g,r,b)
       remotetmr:unregister()
-      loaddrawfn(m)(remotetmr,remotefb,g,r,b)
+      loaddrawfn(m)(remotetmr,remotefb,p)
       remotetmr:start()
       dodraw()
     end
@@ -44,10 +54,10 @@ local vt = {
 }
 
 -- loop over all ;-delimited statements in the message and fire them off.
-function intloop(msg,labels)
+function intloop(msg,labels,palette)
   local c,as
   for c,as in msg:gmatch("(%w*)%s*([^;]*);%s*") do
-    if c and vt[c] then if vt[c](as,labels) then break end end
+    if c and vt[c] then if vt[c](as,labels,palette) then break end end
   end
 end
 
@@ -71,5 +81,5 @@ return function(msg)
     end
   end
 
-  intloop(msg,labels)
+  intloop(msg,labels,{})
 end
index 2564a5a2cd408eec53b9730be03e8d3ab73686dd..715a43f825a666f9a881527be8d3064a7e5b005d 100644 (file)
@@ -182,7 +182,7 @@ local function ontouch()
   if ledfb == touchfb then
     touchtmr:unregister()
     touchlastfn = touchfns[touchfnix]
-    loaddrawfn(touchlastfn)(touchtmr,touchfb,touchcolorvec(touchcolor)); dodraw()
+    loaddrawfn(touchlastfn)(touchtmr,touchfb,{string.char(touchcolorvec(touchcolor))}); dodraw()
     touchtmr:start()
   end
 end