25.10.2024, 07:37
Resident script with sleep time set to 0. In this example 0/0/1 is movement input (1 bit) and 0/0/6 is actuator output control (1 bit). Multiple timers can be added to the timers table if needed.
Code:
if not client then
timers = {
['0/0/1'] = {
output = '0/0/6', -- binary output control (0/1)
timeoutinit = 100, -- initial timeout in seconds
timeoutadd = 30, -- additional timeout in seconds
},
}
function timerset(timer, value, ticks)
timer.on = value
timer.ticks = ticks
grp.write(timer.output, value, dt.bool)
end
function timerstart(timer)
if timer.on then
timer.ticks = timer.ticks + timer.timeoutadd
else
timerset(timer, true, timer.timeoutinit)
end
end
grp.sender = 'tm'
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
if value == 1 then
timerstart(timer)
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
timerset(timer, false)
end
end
end