Parcourir la source

Strange callback behavior solved.

The `callback` returned by `pattern` is self-referential by design.
However, the callback function itself was not defined in the scope of `pattern`, so it did not become an upvalue.
As a result, the callback of the current pattern would change with each call of `pattern`, thrashing the old one.
One unexplained behavior though is why that call returned nil instead of simply transitioning to the next pattern without respecing the Q. Working on it...
etisab il y a 10 ans
Parent
commit
a1fb80a471
1 fichiers modifiés avec 17 ajouts et 6 suppressions
  1. 17 6
      rebuild/led.lua

+ 17 - 6
rebuild/led.lua

@@ -30,12 +30,10 @@ end
 
 -- params must contain duty and interval
 function pattern(params, trans_func)
-  local params = params 
+  local params = params
+  local callback = nil
 
-  debug_message('pattern')
-  for k,v in pairs(params) do print(k,v) end
-
-  function callback()
+  callback = function()
     if params and params.duty ~= nil then
       pwm.setduty(LED_PIN, params.duty)
       tmr.alarm(TIMER, params.interval, tmr.ALARM_SINGLE, callback)
@@ -50,7 +48,6 @@ end
 
 function enqueue(pattern)
   debug_message('enqueue')
-  for k,v in pairs(Q) do print(k,v) end
   table.insert(Q, pattern)
   -- debug_message('tmr.state ' .. tostring(tmr.state(TIMER)))
   -- if not tmr.state(TIMER) then
@@ -78,8 +75,22 @@ end
 
 
 
+function q_blink()
+  print('q_blink')
 
+  local params = {interval=1000, duty=0}
+  local transition = function(p)
+    if params.duty == 0 then
+      params.duty = MAX_DUTY
+    else
+      params.duty = 0
+    end
 
+    return params
+  end
+
+  enqueue(pattern(params, transition))
+end
 
 -- built-in conveniences specific to Yo Button
 function q_fade_in()