This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Single/double press detection from www.logicmachine
#1
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
Reply
#2
Check that the resident script sleep time is set to 0.
Reply
#3
(11.04.2024, 10:44)admin Wrote: Check that the resident script sleep time is set to 0.

Yes, I have...
Reply
#4
Post your script. What exactly have you changed?
Reply
#5
(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...
Reply
#6
true/false must be in lowercase
Reply
#7
(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((
Reply


Forum Jump: