26.10.2021, 08:18
Resident script with sleep time set to 0. You only need to edit the config table. You will need to restart the script via disable/enable after each config edit. In the config table each key is a group address which sends 1 on button press and 0 on button release. Value for each key is another table with the button configuration. This way you only need a single script for all buttons with multiple press functionality.
Code:
if not client then
config = {
['0/0/1'] = {
timeout = 0.5,
longpress = 1,
press = function(ptype, count)
local light1 = '1/1/1'
local light2 = '1/1/2'
if ptype == 'short' then
if count == 1 then
grp.write(light1, not grp.getvalue(light1))
else
grp.write(light2, not grp.getvalue(light2))
end
else
grp.write(light1, false)
grp.write(light2, false)
end
end
},
['1/1/3'] = {
timeout = 0.5,
press = function(ptype, count)
log('press 1/1/3', ptype, count)
end
}
}
function groupwrite(event)
local cfg = config[ event.dst ]
if cfg then
cfg.pressed = tonumber(event.datahex, 16) ~= 0
if cfg.pressed then
cfg.timer = 0
cfg.count = (cfg.count or 0) + 1
end
end
end
function press(cfg, ptype, count)
if type(cfg.press) == 'function' then
cfg.press(ptype, count)
end
cfg.pressed = false
cfg.timer = nil
cfg.count = 0
end
client = require('localbus').new(0.1)
client:sethandler('groupwrite', groupwrite)
end
ts, tu = os.microtime()
client:loop(0.2)
diff = math.max(0, os.udifftime(ts, tu))
for addr, cfg in pairs(config) do
if cfg.timer then
cfg.timer = cfg.timer + diff
if cfg.pressed then
if cfg.longpress and cfg.timer >= cfg.longpress then
press(cfg, 'long')
end
elseif cfg.timer >= (cfg.timeout or 1) then
press(cfg, 'short', cfg.count)
end
end
end