|
|
@@ -1,195 +1,94 @@
|
|
|
--- 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 IS_FADE_IN = false
|
|
|
+
|
|
|
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.FADEIN)
|
|
|
- do_pattern(patterns.FADEOUT)
|
|
|
-end
|
|
|
-
|
|
|
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)
|
|
|
-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
|
|
|
- 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
|
|
|
-
|
|
|
+ gpio.mode(OVERRIDE_PIN, gpio.OUTPUT)
|
|
|
+ pwm.setup(LED_PIN, 500, 0)
|
|
|
+ pwm.start(LED_PIN)
|
|
|
+ pwm.stop(LED_PIN)
|
|
|
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
|
|
|
-
|
|
|
- --looping timer instead of alarm_single, can deregister on nil
|
|
|
- transition()
|
|
|
-end
|
|
|
+function pattern(start_duty, )
|
|
|
|
|
|
-function FADEIN_update()
|
|
|
- local current_brightness = pwm.getduty(led_pin)
|
|
|
+function fade_in()
|
|
|
+ IS_FADE_IN = true
|
|
|
+ local current_brightness = pwm.getduty(LED_PIN)
|
|
|
current_brightness = current_brightness + 1
|
|
|
- if(current_brightness > led_max_brightness) then
|
|
|
- current_brightness = led_max_brightness
|
|
|
+ if(current_brightness > MAX_BRIGHTNESS) then
|
|
|
+ current_brightness = MAX_BRIGHTNESS
|
|
|
end
|
|
|
- pwm.setduty(led_pin, current_brightness)
|
|
|
+ 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
|
|
|
+ tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
|
|
|
else
|
|
|
- end_pattern()
|
|
|
+ IS_FADE_IN = false
|
|
|
end
|
|
|
end
|
|
|
|
|
|
-function FADEOUT_update()
|
|
|
- local current_brightness = pwm.getduty(led_pin)
|
|
|
+function fade_out()
|
|
|
+ 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)
|
|
|
+ pwm.setduty(LED_PIN, current_brightness)
|
|
|
|
|
|
if current_brightness > 0 then
|
|
|
- tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, FADEOUT_update)
|
|
|
- else
|
|
|
- end_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
|
|
|
+ tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
|
|
|
end
|
|
|
- --pattern does not end
|
|
|
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()
|
|
|
+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)
|
|
|
+
|
|
|
+ --endless
|
|
|
+ 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
|
|
|
end
|
|
|
|
|
|
-next_pattern_funcs = {
|
|
|
- HEARTBEAT_update,
|
|
|
- FADEIN_update,
|
|
|
- FADEOUT_update,
|
|
|
- TRIPLEBLINK_update
|
|
|
-}
|
|
|
-
|
|
|
-function end_pattern()
|
|
|
- if table.getn(pattern_queue) == 0 then
|
|
|
- stop()
|
|
|
+function triple_blink()
|
|
|
+ local intervals = {100, 20, 100, 20, 100, 20, 100} --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)
|
|
|
else
|
|
|
- p = pattern_queue[1]
|
|
|
- table.remove(pattern_queue, 1)
|
|
|
- current_pattern = patterns.STOPPED
|
|
|
- tmr.unregister(TIMER)
|
|
|
- do_pattern(p)
|
|
|
+ tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
+function is_fade_in() return IS_FADE_IN end
|
|
|
+
|
|
|
init(TIMER)
|