Logic Machine Forum
Single/double press detection from www.logicmachine - 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: Single/double press detection from www.logicmachine (/showthread.php?tid=5355)



Single/double press detection from www.logicmachine - Borek - 11.04.2024

Hello everyone, I used the script from www LogicMachine/Documentation and examples - Single/double press detection. I followed the instructions and the script did not work. After restarting LogicMachine it worked, but after editing it stopped working and restarting LogicMachine didn't help either. Has anyone encountered something similar or any ideas? Thanks Borek


RE: Single/double press detection from www.logicmachine - admin - 11.04.2024

Check that the resident script sleep time is set to 0.


RE: Single/double press detection from www.logicmachine - Borek - 11.04.2024

(11.04.2024, 10:44)admin Wrote: Check that the resident script sleep time is set to 0.

Yes, I have...


RE: Single/double press detection from www.logicmachine - admin - 11.04.2024

Post your script. What exactly have you changed?


RE: Single/double press detection from www.logicmachine - Borek - 11.04.2024

(11.04.2024, 14:45)admin Wrote: Post your script. What exactly have you changed?

-- maximum time between presses
presstimeout = 0.5

mapping = {
  ['9/0/0'] = {
    -- single press
    single_address = '9/1/0',
    single_value = FALSE,
    -- double press
    double_address = '9/2/0',
    double_value = TRUE,
    -- multi (3+) press (optional)
    multi_address = '9/0/9',
    multi_value = 33,
  },
  ['9/0/1'] = {
    -- single press
    single_address = '1/3/40',
    single_value = TRUE,
    -- double press
    double_address = '1/3/41',
    double_value = TRUE,
  },
}

require('copas')

function write(map, key)
  local address = map[ key .. '_address']
  local value = map[ key .. '_value']

  if address and value ~= nil then
    grp.write(address, value)
    return true
  end
end

function timerthread(addr, map)
  local counter = 0
  local timeout = -1 -- suspend thread

  while true do
    copas.sleep(timeout)

    if map.counter == counter then
      if timeout == presstimeout then
        if counter == 1 then
          write(map, 'single')
        elseif counter == 2 or not write(map, 'multi') then
          write(map, 'double')
        end

        timeout = -1
        counter = 0
        map.counter = 0
      end
    else
      timeout = presstimeout
      counter = map.counter
    end
  end
end

for addr, map in pairs(mapping) do
  map.counter = 0
  map.thread = copas.addthread(timerthread, addr, map)
end

function eventhandler(event)
  local map = mapping[ event.dst ]

  if map then
    map.counter = map.counter + 1
    copas.wakeup(map.thread)
  end
end

lb = require('localbus').new(1)
lbConfusedethandler('groupwrite', eventhandler)

copas.addserver(lb.sock, function()
  lb.sock = copas.wrap(lb.sock)

  while true do
    lbConfusedtep()
  end
end, 1)

copas.loop()



ONLY: adress and value...


RE: Single/double press detection from www.logicmachine - admin - 11.04.2024

true/false must be in lowercase


RE: Single/double press detection from www.logicmachine - Borek - 11.04.2024

(11.04.2024, 15:10)admin Wrote: true/false must be in lowercase

Thank you very much, I forget that LUA has to be lowercase.....Sad((