Parcourir la source

Refactoring changes to led module, including

- flatten some helper functions
- break others out into multiple functions
- simplify pattern flow control
- explicitly declare as module
- automatically init
- naming
etisab il y a 10 ans
Parent
commit
39429afa8a
2 fichiers modifiés avec 106 ajouts et 102 suppressions
  1. 103 101
      rebuild/led.lua
  2. 3 1
      rebuild/main.lua

+ 103 - 101
rebuild/led.lua

@@ -1,45 +1,49 @@
 --pin definitions, should we move it out to another module?
-local led_pin = 1 --GPIO 5  
-local override_pin = 2 --GPIO 4
+-- local led_pin = 1 --GPIO 5
+local led_pin = 2 --nodeMCU LED
+local override_pin = 4 --GPIO 4
 
 --pattern definitions
-local STOPPED = 0
-local HEARTBEAT = 1
-local FADE_IN = 2
-local FADE_OUT = 3
-local TRIPLE_BLINK = 4
+local patterns = {
+  STOPPED = 0,
+  HEARTBEAT = 1,
+  FADEIN = 2,
+  FADEOUT = 3,
+  TRIPLEBLINK = 4
+}
 
 --private constants
-local heartbeat_times = {40, 200, 40, 900} --format is alternating on and off times in ms
-local triple_blink_times = {100, 20, 100, 20, 100, 20, 100} --format is alternating off and on times in ms
-local timer_id = nil --"constant"
+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
 
 --state variables
 local current_pattern
 local pattern_queue = {}
-local heartbeat_index = 1
-local triple_blink_index = 1
-
---function declarations
-local init = nil
-local stop = nil
-local do_pattern = nil
-local fade_in_update = nil
-local fade_out_update = nil
-local heartbeat_update = nil
-local triple_blink_update = nil
-local end_pattern = nil
-local override_enable = nil
-local override_disable = nil
-local invalid_timer = nil
-
------------------------public functions------------------------------
+local HEARTBEAT_index = 1
+local TRIPLEBLINK_index = 1
+
+--dependencies
+local TIMER = TIMERS.led
+local debug_message = debug_message
+local gpio = gpio
+local pwm = pwm
+local tmr = tmr
+local table = table
+
+local print = print
+module(...)
+
+function testcall()
+  do_pattern(patterns.FADEOUT)
+end
+
 function init(timer)
+  debug_message('led.timer')
   --handle timer, take exclusive control
-  timer_id = timer
-  tmr.unregister(timer_id)
+  TIMER = timer
+  tmr.unregister(TIMER)
 
   --init pins
   gpio.mode(override_pin, gpio.OUTPUT)
@@ -52,68 +56,82 @@ end
 
 function stop()
   if(invalid_timer()) then
-    print "Error, call led.init(timer_id) before calling any member functions"
+    print "Error, call led.init(TIMER) before calling any member functions"
     return
   end
-  tmr.unregister(timer_id)
+  tmr.unregister(TIMER)
+
   --deassert control
-  override_disable()
-  --stop pwm
-  --pwm.stop(led_pin)
-  current_pattern = STOPPED
+  gpio.write(override_pin, gpio.LOW)
+
+  current_pattern = patterns.STOPPED
   pattern_queue = {}
 end
 
 function do_pattern(pattern)
-  if(invalid_timer()) then
-    print "Error, call led.init(timer_id) before calling any member functions"
-    return
-  end
-
-  --TODO: CONFIRM VALID PATTERN
-  --if(not is_valid_pattern(pattern))
+  debug_message('led.do_pattern: ' .. pattern)
 
   if(pattern == current_pattern) then
     --do nothing
     return
   end
 
-  if not (current_pattern == STOPPED) then
+  if not (current_pattern == patterns.STOPPED) then
     --put pattern in queue
     table.insert(pattern_queue, pattern)
   else
+
+    debug_message('current, next: ' .. current_pattern .. ', ' .. pattern)
     --start pattern!
     current_pattern = pattern
-    override_enable()
-
-    --if train could be optimized with else's but this is more readable
-    if(current_pattern == FADE_IN) then
-      pwm.setduty(led_pin, 0)
-      pwm.start(led_pin)
-      fade_in_update()
-    end
-    if(current_pattern == FADE_OUT) then
-      pwm.setduty(led_pin, led_max_brightness)
-      pwm.start(led_pin)
-      fade_out_update()
-    end
-    if(current_pattern == HEARTBEAT) then
-      pwm.setduty(led_pin, 0)
-      pwm.start(led_pin)
-      heartbeat_index = 1
-      heartbeat_update()
-    end
-    if(current_pattern == TRIPLE_BLINK) then
-      pwm.setduty(led_pin, led_max_brightness)
-      pwm.start(led_pin)
-      triple_blink_index = 1
-      triple_blink_update()
-    end
+  
+    -- override_enable()
+    gpio.write(override_pin, gpio.HIGH)
+
+    pattern_funcs[current_pattern]()
   end
 end
 
+-- do patterns --
+function do_FADEIN()
+  debug_message('led.do_FADEIN')
+  pwm.setduty(led_pin, 0)
+  pwm.start(led_pin)
+  FADEIN_update()
+end
+
+function do_FADEOUT()
+  debug_message('led.do_FADEOUT')
+  pwm.setduty(led_pin, led_max_brightness)
+  pwm.start(led_pin)
+  FADEOUT_update()
+end
+
+function do_HEARTBEAT()
+  debug_message('led.do_HEARTBEAT')
+  pwm.setduty(led_pin, 0)
+  pwm.start(led_pin)
+  HEARTBEAT_index = 1
+  HEARTBEAT_update()
+end
+
+function do_TRIPLEBLINK()
+  debug_message('led.do_TRIPLEBLINK')
+  pwm.setduty(led_pin, led_max_brightness)
+  pwm.start(led_pin)
+  TRIPLEBLINK_index = 1
+  TRIPLEBLINK_update()
+end
+
+pattern_funcs = {
+  do_HEARTBEAT,
+  do_FADEIN,
+  do_FADEOUT,
+  do_TRIPLEBLINK
+}
+
 -----------------------private functions-------------------------
-function fade_in_update()
+function FADEIN_update()
   local current_brightness = pwm.getduty(led_pin)
   current_brightness = current_brightness + 1
   if(current_brightness > led_max_brightness) then
@@ -121,13 +139,13 @@ function fade_in_update()
   end
   pwm.setduty(led_pin, current_brightness)
   if current_brightness < led_max_brightness then
-    tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_in_update)
+    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, FADEIN_update)
   else
     end_pattern()
   end
 end
 
-function fade_out_update()
+function FADEOUT_update()
   local current_brightness = pwm.getduty(led_pin)
   current_brightness = current_brightness - 1
   if(current_brightness < 0) then
@@ -135,31 +153,31 @@ function fade_out_update()
   end
   pwm.setduty(led_pin, current_brightness)
   if current_brightness > 0 then
-    tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_out_update)
+    tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, FADEOUT_update)
   else
     end_pattern()
   end
 end
 
-function heartbeat_update()
+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_id, 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
+  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
   end
   --pattern does not end
 end
 
-function triple_blink_update()
+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_id, triple_blink_times[triple_blink_index], tmr.ALARM_SINGLE, triple_blink_update)
-  triple_blink_index = triple_blink_index + 1
-  if triple_blink_index > table.getn(triple_blink_times) then
+  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()
   end
 end
@@ -170,30 +188,14 @@ function end_pattern()
   else
     p = pattern_queue[1]
     table.remove(pattern_queue, 1)
-    current_pattern = STOPPED
-    tmr.unregister(timer_id)
+    current_pattern = patterns.STOPPED
+    tmr.unregister(TIMER)
     do_pattern(p)
   end
 end
 
-function override_enable()
-  gpio.write(override_pin, gpio.HIGH)
-end
-
-function override_disable()
-  gpio.write(override_pin, gpio.LOW)
-end
-
 function invalid_timer()
-  return (timer_id == nil or timer_id > 6)
+  return (TIMER == nil or TIMER > 6)
 end
 
-return {
-  init = init,
-  stop = stop,
-  do_pattern = do_pattern,
-  HEARTBEAT = HEARTBEAT,
-  FADE_IN = FADE_IN,
-  FADE_OUT = FADE_OUT,
-  TRIPLE_BLINK = TRIPLE_BLINK,
-}
+init(TIMER)

+ 3 - 1
rebuild/main.lua

@@ -1,10 +1,12 @@
 TIMERS = {
   interrupt = 0,
-  setup_timeout = 1
+  setup_timeout = 1,
+  led = 3
 }
 
 local yo = require('yo')
 local server = require('server')
+local led = require('led')
 local read = require('read')
 local button_pin = 6