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.

Number of "Pulses" Detection
#6
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
Reply


Messages In This Thread
Number of "Pulses" Detection - by p_xatzi - 04.10.2021, 11:06
RE: Number of "Pulses" Detection - by Daniel - 04.10.2021, 11:17
RE: Number of "Pulses" Detection - by p_xatzi - 04.10.2021, 14:15
RE: Number of "Pulses" Detection - by p_xatzi - 25.10.2021, 11:12
RE: Number of "Pulses" Detection - by admin - 25.10.2021, 11:35
RE: Number of "Pulses" Detection - by admin - 26.10.2021, 08:18
RE: Number of "Pulses" Detection - by p_xatzi - 26.10.2021, 09:31
RE: Number of "Pulses" Detection - by admin - 26.10.2021, 09:39
RE: Number of "Pulses" Detection - by p_xatzi - 31.10.2021, 22:15
RE: Number of "Pulses" Detection - by admin - 01.11.2021, 07:32
RE: Number of "Pulses" Detection - by p_xatzi - 20.04.2022, 07:22
RE: Number of "Pulses" Detection - by admin - 20.04.2022, 07:36
RE: Number of "Pulses" Detection - by p_xatzi - 20.04.2022, 08:23

Forum Jump: