]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_lamp/commitdiff
first pass implementation of allowing the lamp to specify multiple colors for animations
authorRachael <srbennett@gmail.com>
Tue, 20 Feb 2018 06:51:35 +0000 (22:51 -0800)
committerRachael <srbennett@gmail.com>
Tue, 20 Feb 2018 06:51:35 +0000 (22:51 -0800)
init2.lua
lamp-touch.lua

index b24ccac10a79b20aa11d93debc0d515fbcfeb5fc..1ae366fe544428e7832fc2363cf09be840e4a782 100644 (file)
--- a/init2.lua
+++ b/init2.lua
@@ -83,9 +83,37 @@ nwfnet.onmqtt["lamp"] = function(c,t,m)
   if t and m and t:find("^lamp/[^/]+/out") then remotemsg(m) end
 end
 
+function transformcolors(color)
+  local g = color[1]
+  local r = color[2]
+  local b = color[3]
+  return r,g,b
+end
+
 -- TODO: messages to specific lamps?  Multiple brokers?
-function lamp_announce(fn,g,r,b)
-  mqc:publish(mqttBcastPfx,string.format("draw %s %x %x %x;",fn,r,g,b),1,1)
+function lamp_announce(fn,colors)
+  print("trying to announce ", colors)
+  if #colors > 1 then
+    if colors then
+      for k,v in pairs(colors[1]) do print(k,v) end
+    else
+      print("nil colors")
+    end
+    -- TODO more than 2 colors?
+    local r,g,b = transformcolors(colors[1]);
+    local r2,g2,b2 = transformcolors(colors[2])
+    mqc:publish(mqttBcastPfx,string.format("color 2 %x %x %x; draw %s %x %x %x;",r2,g2,b2,fn,r,g,b),1,1)
+  else
+    print("one color")
+    if colors then
+      for k,v in pairs(colors[1]) do print(k,v) end
+    else
+      print("nil colors")
+    end
+    local r,g,b = transformcolors(colors[1])
+    print(r,g,b)
+    mqc:publish(mqttBcastPfx,string.format("draw %s %x %x %x;",fn,r,g,b),1,1)
+  end
 end
 
 -- mqtt setup
index 856316ef2386e1a2f77c93bf5cb2dcbd68d28d28..942864685cdc2306a56d2a0e8150e1c989c12ada 100644 (file)
@@ -80,7 +80,8 @@ local function touchcolorvec(c)
   return g,r,b
 end
 
-local colors      = { string.char(touchcolorvec(touchcolor)) }
+local colors      = { [1] = string.char(touchcolorvec(touchcolor)) }
+local networkcolors = {[1] = {touchcolorvec(touchcolor)} }
 local colorindex = 1;
 
 local function onblackdebounce() touch_db_blackout = nil end
@@ -101,7 +102,7 @@ local function ontouchdone()
       cap:mr(0x72,set30) -- link
     end)
 
-    lamp_announce(touchfns[touchfnix],touchcolorvec(touchcolor))
+    lamp_announce(touchfns[touchfnix],networkcolors)
   end
 
   isTouch = false
@@ -144,7 +145,8 @@ local function ontouch()
   -- left side back button: reset colors and dimming.
   if bit.isset(down,7) then
     dimfactor = 0;
-    colors = { string.char(touchcolorvec(touchcolor)) }
+    colors = { [1] = string.char(touchcolorvec(touchcolor)) }
+    networkcolors = { [1] = {touchcolorvec(touchcolor)}}
     colorindex = 1;
     -- Don't claim the image, just dim whatever is currently on the screen.
     dodraw()
@@ -170,7 +172,9 @@ local function ontouch()
      if     touchcolor >= 48 then touchcolor = touchcolor - 48
      elseif touchcolor < 0  then touchcolor = touchcolor + 48
      end
+     print(colors, "colorindex is while changing color", colorindex);
      colors[colorindex] = string.char(touchcolorvec(touchcolor))
+     networkcolors[colorindex] = {touchcolorvec(touchcolor)}
     end
 
     -- front middle: mode select (rate-limited, not exactly debounced)
@@ -198,13 +202,15 @@ local function ontouch()
     dodraw()
   end
 
-  -- XXX left side middle button
+  -- XXX left side middle button; change colors!
   if bit.isset(down, 6) then
-    if colorindex < ncolors then
+    print("ncolors is ", ncolors);
+    if ncolors and colorindex < ncolors then
       colorindex = colorindex + 1;
     else
       colorindex = 1;
     end
+        print("should increment color", colorindex);
   end
 
   -- XXX front left
@@ -213,6 +219,7 @@ local function ontouch()
 
   -- draw if we've claimed it!
   if (ledfb == touchfb) and not didChangeFn and didChangeColor and cccb ~= nil then
+    print("colors only");
     -- all we did was change the color(s); inform the existing animation
     cccb()
   elseif didChangeFn or didChangeColor then
@@ -221,9 +228,13 @@ local function ontouch()
     touchlastfn = touchfns[touchfnix]
     print(touchlastfn);
     local drawinfo = loaddrawfn(touchlastfn)(touchtmr,touchfb,colors)
-    print(drawinfo);
+    print(touchlasstfn, "trying drawinfo", drawinfo);
+    if drawinfo then
+      for k,v in pairs(drawinfo) do print(k,v) end
+    end
     cccb = drawinfo and drawinfo['cccb']
     ncolors = drawinfo and drawinfo['ncolors'] or 1
+    print(ncolors);
 
     dodraw()
     touchtmr:start()