You are reading a single comment by @user156756 and its replies. Click here to read the full conversation.
  • Rather than try to do the time calculations I am trying to use the SWButton.js module.
    This seems to work. Have I got it correct?

    var buttonState;
    var slowTimeout; //< After 60s we revert to slow advertising
    var btnData = {};
    var SWBtn = require("https://raw.githubusercontent.c­om/muet/EspruinoDocs/master/modules/SWBu­tton.js");
    // these get created with a puck button press
    var mySWBtn = new SWBtn(function(k){
      if (k === "S"  ) { // single button press
        btnData.btn = "press";
      }
      else if (k === "L" ) { // long button press
        btnData.btn = "long_press";
      }
      else if (k === "SS") { // double short press
        btnData.btn = "double_press";
      } 
      updateBTHome(btnData);
    });
    
    
    function updateBTHome(btnData) {
      buttonState = btnData.btn;
      updateAdvertising(buttonState);
    }
    
    // Update the data we're advertising here
    function updateAdvertising(buttonState) {
      NRF.setAdvertising(require("BTHome").get­Advertisement([
        {
          type : "battery",
          v : E.getBattery()
        },
        {
          type : "temperature",
          v : E.getTemperature()
        },
        {
          type: "button_event",
          v: buttonState
        },
      ]), {
        name : "Sensor",
        interval: (buttonState!="none")?20:2000, // fast when we have a button press, slow otherwise
        });
      /* After 60s, call updateAdvertising again to update battery/temp and to ensure we're advertising slowly */
      if (slowTimeout) clearTimeout(slowTimeout);
      slowTimeout = setTimeout(function() {
        slowTimeout = undefined;
        updateAdvertising("none" /* no button pressed */);
      }, 60000);
    }
    
    // Update advertising now
    updateAdvertising("none");
    // Enable highest power advertising (4 on nRF52, 8 on nRF52840)
    NRF.setTxPower(4);
    

    Seems quite fast

    Thanks

About

Avatar for user156756 @user156756 started