You are reading a single comment by @tako and its replies. Click here to read the full conversation.
  • Here's my current code. Previous versions did not have the "sending" check that this now does. I haven't experienced issues with this version of it.

    console.log(NRF.getAddress());
    console.log("Battery: " + E.getBattery() + " " + Puck.getBatteryPercentage());
    
    var debugText = false;
    
    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report });
    
    var sending = 0;
    
    function catchButton(e) {
    
      var keyDown = e.state === true;
      var keyUp = e.state === false;
      var click = false;
    
      debug_log(keyDown?"key down" : (keyUp? "keyUp" : "broke"));
    
      if(keyUp) {
        triggerCommand();
      }
    }
    
    function triggerCommand() {
      try {
        if(!sending) {
          sending = 1;
          digitalWrite(LED3,1);
          NRF.sendHIDReport([0,0,99,0,0,0,0,0], function() {
            NRF.sendHIDReport([0,0,0,0,0,0,0,0], function() {
              setTimeout(function() {
                console.log("press");
                digitalWrite(LED3,0);
                sending = 0;
              }, 250);
            });
          });
        }
      } catch (e) {
        console.log("caught:" + JSON.stringify(e));
        digitalWrite(LED3,0);
        digitalWrite(LED1,1);
        setTimeout(function() {
          digitalWrite(LED1,0);
          sending = 0;
        }, 250);
      }
    }
    
    
    setWatch(catchButton, BTN, {repeat:true, debounce:50, edge:'both' });
    
    function debug_log(t) {
      if(debugText) console.log(t);
    }
    
About

Avatar for tako @tako started