Logic Machine Forum
Activation after holding a button - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Activation after holding a button (/showthread.php?tid=5748)



Activation after holding a button - Nir70 - 20.11.2024

Is there a way that I press a button for 3-4 seconds only then it sends a command "0" or "1"? thanks Rolleyes


RE: Activation after holding a button - Daniel - 20.11.2024

https://kb.logicmachine.net/scripting/short-long-press/


RE: Activation after holding a button - Nir70 - 20.11.2024

thank you Hello, I only need to send a value of 0 (OFF) when pressed for 3-4 seconds (a short press does nothing). What should I refer to in the script? thanks


RE: Activation after holding a button - Daniel - 20.11.2024

Try this (not tested)
Code:
if not client then
  timeout = 1 -- long press in seconds

  mapping = {
    ['32/1/1'] = { short = '32/1/2', long = '32/1/3' },
    ['32/1/4'] = { short = '32/1/5', long = '32/1/6' },
  }

  timerstep = timeout / 4

  function eventhandler(event)
    local object = mapping[ event.dst ]
    if not object then
      return
    end

    local value = busdatatype.decode(event.datahex, dt.bool)
    if value then
      object.timer = timeout
    elseif object.timer then
      object.timer = nil
     -- grp.write(object.short, not grp.getvalue(object.short), dt.bool)
    end
  end

  client = require('localbus').new(1)
  clientfd = socket.fdmaskset(client:getfd(), 'r')
  client:sethandler('groupwrite', eventhandler)

  timer = require('timerfd').new(timerstep)
  timerfd = socket.fdmaskset(timer:getfd(), 'r')
end

res, clientstat, timerstat = socket.selectfds(10, clientfd, timerfd)

if clientstat then
  client:step()
end

if timerstat then
  timer:read()

  for addr, object in pairs(mapping) do
    if object.timer then
      object.timer = object.timer - timerstep

      if object.timer <= 0 then
        object.timer = nil
        grp.write(object.long, false, dt.bool)
      end
    end
  end
end