rs485-lpc/lpc_serial_lux.lua

50 lines
1.1 KiB
Lua
Raw Normal View History

local tv = get_trigger_variable(1)
if tv == nil then return end
local lux = tv.integer
if lux == nil then return end
if lux < 0 then lux = 0 end
if lux > 200000 then lux = 200000 end
if lux_target == nil then lux_target = 500 end
local TARGET_LUX = lux_target
local DEADBAND = 50
local MIN_LEVEL = 0.05
local MAX_LEVEL = 1.0
local STEP_MIN = 0.02
local STEP_MAX = 0.20
local GAIN = 0.3
local ok, grp = pcall(get_group, 0)
if not ok or grp == nil then
ok, grp = pcall(get_group, 'All Fixtures')
end
if not ok or grp == nil then return end
local err = TARGET_LUX - lux
if err > -DEADBAND and err < DEADBAND then return end
local lv = grp.master_intensity_level
local cur
if lv ~= nil and lv.range ~= nil and lv.range > 0 then
cur = lv.integer / lv.range
else
cur = lux_cur_level or 0.5
end
local step = math.abs(err) / TARGET_LUX * GAIN
if step < STEP_MIN then step = STEP_MIN end
if step > STEP_MAX then step = STEP_MAX end
local new = cur
if err > 0 then
new = cur + step
else
new = cur - step
end
if new < MIN_LEVEL then new = MIN_LEVEL end
if new > MAX_LEVEL then new = MAX_LEVEL end
lux_cur_level = new
grp:set_master_intensity(new, 1.0)