浏览代码

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 10 年之前
父节点
当前提交
a1fb80a471
共有 1 个文件被更改,包括 17 次插入6 次删除
  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()