15.11.2023, 14:26
Create user library named timers:
Resident script:
Code:
return function(timers)
if not client then
grp.sender = 'tm'
function set(timer)
grp.checkwrite(timer.output, timer.state == 'on')
timer.state = nil
timer.ticks = nil
end
client = require('localbus').new(0.1)
client:sethandler('groupwrite', function(event)
local timer = timers[ event.dst ]
if timer and event.sender ~= grp.sender then
local value = tonumber(event.datahex, 16) or 0
local state = value > 0 and 'on' or 'off'
if timer.state ~= state then
timer.state = state
local ticks = timer['delay' .. state]
if ticks > 0 then
timer.ticks = ticks
else
set(timer)
end
end
end
end)
end
client:loop(1)
for _, timer in pairs(timers) do
if timer.ticks then
timer.ticks = timer.ticks - 1
if timer.ticks == 0 then
set(timer)
end
end
end
end
Resident script:
Code:
if not timers then
timers = {
['1/1/1'] = {
output = '1/1/2', -- binary output
delayon = 10, -- on delay time (seconds)
delayoff = 0, -- off delay time (seconds)
}
}
end
require('user.timers')(timers)