-- Never change these unless the board changes red = 7 green = 5 pins = {toggle_red=red, toggle_green=green} lights = {} lights[green] = false lights[red] = false function calc_duty_cycle(duty_cycle_factor) if duty_cycle_factor <= 1 then return 1023 else return 1023 / duty_cycle_factor end end function is_table(v) local is_it_a_table, _ = pcall(function() return v["is_table"] end) return is_it_a_table end function show_pair(key, value) -- TODO handle case of array and then call back to show_list if is_table(value) then return "("..tostring(key) .. " . " .. tostring(show_table(value)) .. ")" else return "("..tostring(key) .. " . " .. tostring(value) .. ")" end end function show_table(t) result = "" for key, value in pairs(t) do result = result .. show_pair(key, value) end return "(" .. result .. ")" end function show_list(ts) if is_table(12) or (not is_table({green=12})) then return "" end result = "" for _, t in ipairs(ts) do if is_table(t) then result = result .. show_table(t) else result = result .. tostring(t) end end return "(" .. result .. ")" end function make_timer(params) -- interval is in milliseconds -- pin is the gpio number -- params {pin=pin, frequency=frequency, duty_cycle=duty_cycle, step=step, interval=interval, delay=delay} local duty_cycle_t = params["duty_cycle"] local direction = params["step"] local delay = 0 print("timer created!") print(duty_cycle_t) local timer = tmr.create() timer:register(params["interval"], tmr.ALARM_AUTO, function() if delay > 0 then delay = delay - 1 return end if (direction > 0 and ((duty_cycle_t + direction) >= 1023)) then delay = params["delay"] direction = -math.abs(direction) end if ((direction <= 0) and ((duty_cycle_t + direction) < 0)) then delay = params["delay"] direction = math.abs(direction) end if delay > 0 then return end duty_cycle_t = duty_cycle_t + direction -- if this is running then it's turned on lights[params["pin"]] = true pwm.setduty(params["pin"], duty_cycle_t) end) return timer end -- pwm.setup(pin, frequency, duty cycle) -- max duty cycle is 1023, min 1 pwm.setup(green, 500, 1023) pwm.setup(red, 500, 1023) pwm.start(green) pwm.start(red) timers = {red=false, green=false} -- cron job which checks if auto mode is enabled -- if auto mode is enabled, it cycles through various timers -- runs the current timer for one period (cron job runs once every period of time) -- then stops and unregisters that timer, and moves on to the next, etc -- if auto mode is disabled, it does nothing -- UI allows you to configure auto mode period maybe -- UI could also allow to configure the specific settings but this might be too much work auto_mode_enabled = true function make_timer_params(pin, duty_cycle, interval, frequency, step, delay) duty_cycle = duty_cycle or 1023 interval = interval or 20 step = step or 1 frequency = frequency or 500 delay = delay or 200 return {pin=pin, frequency=frequency, duty_cycle=duty_cycle, step=step, interval=interval, delay=delay} end function unregister_timers() if timers[green] then timers[green]:unregister() end if timers[red] then timers[red]:unregister() end duty_cycle = 1023 pwm.setduty(red, duty_cycle) pwm.setduty(green, duty_cycle) end timer_states = { {green=make_timer_params(green, 1022, 200, 1000, 1021, 5), red=make_timer_params(red, 1022, 200, 1000, -1021, 5)}, {green=make_timer_params(green, 1023, 5), red=make_timer_params(red, 1, 5)}, --{green=make_timer_params(green, 1022, 100, 1000, -1021, 5), red=make_timer_params(red, 1022, 100, 1000, 1021, 5)}, --{green=make_timer_params(green, 1023, 5, 1000), red=make_timer_params(red, 1, 5, 1000)}, --{green=make_timer_params(green, 1023, 5), red=make_timer_params(red, 1023, 5)}, --{green=make_timer_params(green, 1, 5), red=make_timer_params(red, 1023, 5)}, --{green=make_timer_params(green, 1), red=make_timer_params(red, 1023)}, } function start_auto_mode() -- this is enabled by default local current_state = 1 local number_of_states = table.getn(timer_states) print("Making cronjob, number of states = " .. number_of_states) cron.reset() unregister_timers() cron.schedule("*/1 * * * *", function(e) if auto_mode_enabled then print("Auto mode enabled, switching modes, current mode = " .. current_state) unregister_timers() -- TODO iter8 thru them instead timers[green] = make_timer(timer_states[current_state]["green"]) timers[red] = make_timer(timer_states[current_state]["red"]) timers[green]:start() timers[red]:start() current_state = (current_state % number_of_states) + 1 end end) end function turn_light_on(pin, duty_cycle) if not lights[pin] and not auto_mode_enabled then lights[pin] = true pwm.setduty(pin, duty_cycle) end end function turn_light_off(pin) if lights[pin] and not auto_mode_enabled then pwm.setduty(pin, 0) lights[pin] = false end end function toggle_light(pin) if auto_mode_enabled then return end print("Toggling " .. tostring(pin)) local duty_cycle = calc_duty_cycle(1) print("duty_cycle = ".. duty_cycle) if lights[pin] then turn_light_off(pin) else turn_light_on(pin, duty_cycle) end end print("Booted up") function hex_to_char(x) return string.char(tonumber(x, 16)) end function get_time() local t = tmr.time() local hours = t/3600 local seconds_leftover = t % 3600 return tostring(hours) .. " hours, " .. tostring(minutes_leftover) end function urldecode(url) if url == nil then return end url = url:gsub("+", " ") url = url:gsub("%%(%x%x)", hex_to_char) return url end function extract_formdata(s) local cgi = {} for name, value in string.gmatch(s, "([^&=]+)=([^&=]+)") do cgi[name] = value end return cgi end function get_info(group) local info = node.info(group) local result = "
" .. tostring(group) .. " | |
---|---|
" .. tostring(key) .. " | " .. tostring(value) .. " |