Ver Fonte

Merge pull request #4 from epukaza/pwm_enable

Pwm enable
etisab há 10 anos atrás
pai
commit
745c667a7f
4 ficheiros alterados com 124 adições e 165 exclusões
  1. 11 3
      rebuild/init.lua
  2. 75 152
      rebuild/led.lua
  3. 21 10
      rebuild/main.lua
  4. 17 0
      rebuild/yo.lua

+ 11 - 3
rebuild/init.lua

@@ -1,4 +1,4 @@
-DEBUG = true
+DEBUG = false
 
 function debug_message(message)
   if DEBUG then
@@ -10,8 +10,16 @@ end
 if DEBUG then
   debug_message('1 second startup delay on timer 0 ...')
   tmr.alarm(0, 1000, 0, function()
-    dofile('main.lua')
+    if file.exists('main.lc') then
+      dofile('main.lc')
+    else
+      dofile('main.lua')
+    end
   end)
 else
-  dofile('main.lua')
+  if file.exists('main.lc') then
+    dofile('main.lc')
+  else
+    dofile('main.lua')
+  end
 end

+ 75 - 152
rebuild/led.lua

@@ -1,195 +1,118 @@
--- local led_pin = 1 --GPIO 5
-local led_pin = 1 --nodeMCU LED
-local override_pin = 4 --GPIO 4
-
-local HEARTBEAT_times = {40, 200, 40, 900} --format is alternating on and off times in ms
-local TRIPLEBLINK_times = {100, 20, 100, 20, 100, 20, 100} --format is alternating off and on times in ms
-local led_pwm_frequency = 500 --units: hz
-local led_max_brightness = 1023
-local current_pattern
-local pattern_queue = {}
-local HEARTBEAT_index = 1
-local TRIPLEBLINK_index = 1
-
---pattern definitions
-local patterns = {
-  STOPPED = 0,
-  HEARTBEAT = 1,
-  FADEIN = 2,
-  FADEOUT = 3,
-  TRIPLEBLINK = 4
-}
-
-local patterns_initial_pwm_duty = {
-  [0] = 0,
-  0,
-  0,
-  led_max_brightness,
-  led_max_brightness
-}
-
-local pattern_intervals = {
-  [0] = 0,
-  HEARTBEAT_times,
-  1,
-  1,
-  TRIPLEBLINK_times
-}
-
---dependencies
+local LED_PIN = 1
+local OVERRIDE_PIN = 4
+local MAX_BRIGHTNESS = 1023
+local HEART_BEAT_IDX = 1
+local TRIPLE_BLINK_IDX = 1
 local TIMER = TIMERS.led
+local Q = {}
+
 local debug_message = debug_message
 local gpio = gpio
 local pwm = pwm
 local tmr = tmr
 local table = table
+local next = next
 
-local print = print
 module(...)
 
-function testcall()
-  do_pattern(patterns.FADEIN)
-  do_pattern(patterns.FADEOUT)
-end
-
-function init()
+local function init()
   debug_message('led.init')
 
   --handle timer, take exclusive control
   tmr.unregister(TIMER)
-  current_pattern = patterns.STOPPED
 
   --init pins
-  gpio.mode(override_pin, gpio.OUTPUT)
-  pwm.setup(led_pin, led_pwm_frequency, 0)
-  pwm.start(led_pin)
-  pwm.stop(led_pin)
+  gpio.mode(OVERRIDE_PIN, gpio.OUTPUT)
+  pwm.setup(LED_PIN, 500, 0)
+  pwm.start(LED_PIN)
+  pwm.stop(LED_PIN)
 end
 
-function stop()
-  tmr.unregister(TIMER)
-  --deassert control
-  gpio.write(override_pin, gpio.LOW)
-  current_pattern = patterns.STOPPED
-  pattern_queue = {}
-end
-
-function do_pattern(next_pattern)
-  debug_message('led.do_pattern: ' .. next_pattern)
-
-  if(next_pattern == current_pattern) then
-    debug_message('led.do_pattern: next same as current')
-    return
+local function enqueue(pattern)
+  return function()
+    table.insert(Q, pattern)
+    if not tmr.state(TIMER) then
+      next_pattern()
+    end
   end
-  if current_pattern ~= patterns.STOPPED then
-    debug_message('led.do_pattern: current not STOPPED')
-    --put next_pattern in queue
-    table.insert(pattern_queue, next_pattern)
-  else
-    debug_message('current, next: ' .. current_pattern .. ', ' .. next_pattern)
-    --start next_pattern!
-    current_pattern = next_pattern
-    -- override_enable()
-    gpio.write(override_pin, gpio.HIGH)
-
-    pattern(
-      patterns_initial_pwm_duty[next_pattern],
-      pattern_intervals[next_pattern],
-      next_pattern_funcs[next_pattern]
-    )
-  end
-
-  --rewrite queue:
-  --when queueing new pattern, if transition (or flag) not nil then do immediately
-  --otherwise enqueue and let end_function or similar get next from queue
-
-  --STOP should instead a pattern like all others, renamed as OFF
-
-  --transition functions transition(t) --> t+1
-  --if t+1 is nil, end pattern
-
 end
 
---TODO: intervals not used (still using global state)
-function pattern(initial_pwm_duty, intervals, transition)
-  debug_message('led.pattern')
-  pwm.setduty(led_pin, initial_pwm_duty)
-  local pwm_duty = initial_pwm_duty
+local function fade_in()
+  local current_brightness = pwm.getduty(LED_PIN)
 
-  --looping timer instead of alarm_single, can deregister on nil
-  transition()
-end
-
-function FADEIN_update()
-  local current_brightness = pwm.getduty(led_pin)
-  current_brightness = current_brightness + 1
-  if(current_brightness > led_max_brightness) then
-    current_brightness = led_max_brightness
-  end
-  pwm.setduty(led_pin, current_brightness)
-
-  if current_brightness < led_max_brightness then
-    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, FADEIN_update)
+  if current_brightness < MAX_BRIGHTNESS then
+    current_brightness = current_brightness + 1
+    pwm.setduty(LED_PIN, current_brightness)
+    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
   else
-    end_pattern()
+    next_pattern()
   end
 end
 
-function FADEOUT_update()
-  local current_brightness = pwm.getduty(led_pin)
-  current_brightness = current_brightness - 1
-  if(current_brightness < 0) then
-    current_brightness = 0
-  end
-  pwm.setduty(led_pin, current_brightness)
+local function fade_out()
+  local current_brightness = pwm.getduty(LED_PIN)
 
   if current_brightness > 0 then
-    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, FADEOUT_update)
+    current_brightness = current_brightness - 1
+    pwm.setduty(LED_PIN, current_brightness)
+    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
   else
-    end_pattern()
+    next_pattern()
   end
 end
 
-function HEARTBEAT_update()
-  local current_brightness = pwm.getduty(led_pin)
-  current_brightness = led_max_brightness - current_brightness
-  pwm.setduty(led_pin, current_brightness)
-  tmr.alarm(TIMER, HEARTBEAT_times[HEARTBEAT_index], tmr.ALARM_SINGLE, HEARTBEAT_update)
-  HEARTBEAT_index = HEARTBEAT_index + 1
-  if HEARTBEAT_index > table.getn(HEARTBEAT_times) then
-    HEARTBEAT_index = 1
+local function heart_beat()
+  local intervals = {40, 200, 40, 900} --alternating, millisec
+  local current_brightness = pwm.getduty(LED_PIN)
+
+  current_brightness = MAX_BRIGHTNESS - current_brightness
+  pwm.setduty(LED_PIN, current_brightness)
+
+  tmr.alarm(TIMER, intervals[HEART_BEAT_IDX], tmr.ALARM_SINGLE, heart_beat)
+
+  HEART_BEAT_IDX = HEART_BEAT_IDX + 1
+  if HEART_BEAT_IDX > table.getn(intervals) then
+    HEART_BEAT_IDX = 1
   end
-  --pattern does not end
+  -- must call next_pattern() yourself
 end
 
-function TRIPLEBLINK_update()
-  local current_brightness = pwm.getduty(led_pin)
-  current_brightness = led_max_brightness - current_brightness
-  pwm.setduty(led_pin, current_brightness)
-  tmr.alarm(TIMER, TRIPLEBLINK_times[TRIPLEBLINK_index], tmr.ALARM_SINGLE, TRIPLEBLINK_update)
-  TRIPLEBLINK_index = TRIPLEBLINK_index + 1
-  if TRIPLEBLINK_index > table.getn(TRIPLEBLINK_times) then
-    end_pattern()
+local function triple_blink()
+  local intervals =  {200, 50, 200, 50, 200, 50, 200} --alternating, millisec
+
+  local current_brightness = pwm.getduty(LED_PIN)
+  current_brightness = MAX_BRIGHTNESS - current_brightness
+  pwm.setduty(LED_PIN, current_brightness)
+  
+  TRIPLE_BLINK_IDX = TRIPLE_BLINK_IDX + 1
+  if TRIPLE_BLINK_IDX > table.getn(intervals) then
+    TRIPLE_BLINK_IDX = 1
+    pwm.setduty(LED_PIN, 0)
+    next_pattern()
+  else
+    tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
   end
 end
 
-next_pattern_funcs = {
-  HEARTBEAT_update,
-  FADEIN_update,
-  FADEOUT_update,
-  TRIPLEBLINK_update
-}
+-- interface --
+q_fade_in = enqueue(fade_in)
+q_fade_out = enqueue(fade_out)
+q_heart_beat = enqueue(heart_beat)
+q_triple_blink = enqueue(triple_blink)
 
-function end_pattern()
-  if table.getn(pattern_queue) == 0 then
-    stop()
+function next_pattern()
+  tmr.unregister(TIMER)
+  if next(Q) then
+    local next_p = table.remove(Q, 1) -- how expensive is this?
+    next_p()
   else
-    p = pattern_queue[1]
-    table.remove(pattern_queue, 1)
-    current_pattern = patterns.STOPPED
-    tmr.unregister(TIMER)
-    do_pattern(p)
   end
 end
 
+function kill()
+  Q = {}
+  tmr.unregister(TIMER)
+  pwm.setduty(LED_PIN, 0)
+end
+
 init(TIMER)

+ 21 - 10
rebuild/main.lua

@@ -4,13 +4,17 @@ TIMERS = {
   led = 3
 }
 
+local button_pin = 6
+local led_pin = 1
 local yo = require('yo')
 local server = require('server')
 local led = require('led')
 local read = require('read')
-local button_pin = 6
 
-function wifi_setup(func, ...)
+function setup_mode(func, ...)
+  led.kill()
+  led.q_heart_beat()
+
   wifi.setmode(wifi.STATIONAP)
   wifi.ap.config({
     ssid = "YoButton-" .. node.chipid(),
@@ -21,34 +25,39 @@ function wifi_setup(func, ...)
   wifi.ap.dhcp.start()
   wifi.sleeptype(wifi.NONE_SLEEP)
 
-  --wifi_setup inactivity timeout: 5 minutes
+  --setup_mode inactivity timeout: 5 minutes
   tmr.alarm(TIMERS.setup_timeout, 60*5*1000, tmr.ALARM_SINGLE, function()
-    wifi_default(function()
-      debug_message('wifi_setup: timeout')
+    default_mode(function()
+      debug_message('setup_mode: timeout')
       return nil  --nil function to use decorator side effects: code smell
     end)
   end)
 
   func(...)
-
 end
 
-function wifi_default(func, ...)
+function default_mode(func, ...)
+  led.kill()
+  led.q_fade_in()
   server.stop()
+
   wifi.setmode(wifi.STATION)
   wifi.sleeptype(wifi.NONE_SLEEP)
 
-  func(...)
+  local success = func(...)
+
+  debug_message(success)
 
   wifi.sleeptype(wifi.MODEM_SLEEP)
 end
 
+
 function handle_short_press()
-  wifi_default(yo.yo, read.yo_recipient(), read.api_key())
+  default_mode(yo.yo, read.yo_recipient(), read.api_key())
 end
 
 function handle_long_press()
-  wifi_setup(server.start)
+  setup_mode(server.start)
 end
 
 function handle_button_flip()
@@ -95,6 +104,8 @@ function debounce(delay, func)
 end
 
 wifi.setmode(wifi.STATION)
+pwm.setduty(led_pin, 0)
+
 handle_short_press = debounce(3000000, handle_short_press) --3 seconds
 gpio.mode(button_pin, gpio.INT, gpio.FLOAT)
 gpio.trig(button_pin, "both", debounce(50000, handle_button_flip)) --50 ms

+ 17 - 0
rebuild/yo.lua

@@ -1,5 +1,6 @@
 local http = http
 local string = string
+local led = require('led')
 local assert = assert
 local type = type
 local debug_message = debug_message
@@ -14,6 +15,7 @@ function yo(yo_user, api_token)
 
   local content_string = "api_token=" .. api_token .. "&username=" .. string.upper(yo_user) .. '\r\n\r\n'
   local content_length = string.len(content_string)
+  local succeeded = false
 
   debug_message('yo.yo: sending Yo')
   http.post(
@@ -24,6 +26,21 @@ function yo(yo_user, api_token)
     function(status_code, response_data)
       debug_message('yo.yo: status code: ' .. status_code)
       debug_message('yo.yo: response data: ' .. (response_data or 'nil'))
+
+      if status_code >= 200 and status_code < 300 then
+        succeeded = true
+      else
+        succeeded = false
+      end
+      yo_sent(succeeded)
     end
   )
 end
+
+function yo_sent(succeeded)
+  if succeeded then
+    led.q_fade_out()
+  else
+    led.q_triple_blink()
+  end
+end