You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi - I'd be pretty sure your issue really is that the data is being sent too quickly.

    In newer Espruino builds, after a few minutes of inactivity it drops down to a lower speed connection to save battery power - which means (IIRC) about 200ms connection intervals, so 400ms to send a single 'tap' over HID - and because you send on press and release you'd have to keep the button held for at least 0.5 sec each time.

    You can force the connection interval to stay fast with NRF.setConnectionInterval(7.5) but it doesn't really solve the underlying problem.

    Because of the way you're checking for a button press there's no 'debouncing', so you could quite easily end up sending two taps as well (It'll also burn through the battery because you're waking Puck.js up 200 times a second).

    Can you try what's used in the examples on http://www.espruino.com/BLE+Keyboard and use setWatch instead as it's more power efficient and handles debouncing? It's easy to extend it to work with button press and release to get what you want:

    function btnPressed(e) {
      currentButtonState = e.state;
    }
    setWatch(btnPressed, BTN, {edge:"both",repeat:true,debounce:50});
    

    Then to handle sending multiple HID events that might get queued up, you probably want something like:

    var queue = [];
    var busy = false;
    
    function sendTap(key) {
      if (busy) return queue.push(key);
      busy = true;
      kb.tap(key, 0, function(){
        busy = false;
        if (queue.length) sendTap(queue.shift());
      });
    }
    

    So now, if sendTap gets called multiple times it'll just queue up keypressed and send them when it's able to

About

Avatar for Gordon @Gordon started