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
#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


Messages In This Thread
RE: Single/double press detection from www.logicmachine - by Borek - 11.04.2024, 15:06

Forum Jump: