Avatar for user156756

user156756

Member since Oct 2023 • Last active Feb 2024
  • 2 conversations
  • 8 comments

Most recent activity

    • 6 comments
    • 264 views
  • in Puck.js, Pixl.js and MDBT42
    Avatar for user156756

    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

  • in Puck.js, Pixl.js and MDBT42
    Avatar for user156756

    this works with press and long_press. I can see the events hapenning under BTHome, device (Puck). I had to add var buttonState; at the top.

    will try double_press next.

    Thanks.

  • in Bangle.js
    Avatar for user156756

    Still available if anyone interested.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for user156756

    PS In an automation the device action can be single/double/long etc - but other than a single press nothing is recognized. Thanks.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for user156756

    Coming back to this for a bit. Great to see a BTHome library. Quick and easy device/sensors into HA.
    Works great with your example using a singlebutton press. I can see the event on the Home Assistant log.

    One thing I can't figure out is how to set up long press and double press events. This sends through both a double and a single press when I hit the puck button once

    ...
    function updateAdvertising() {
      NRF.setAdvertising(require("BTHome").get­Advertisement([
        {
          type : "battery",
          v : E.getBattery()
        },
        {
          type : "temperature",
          v : E.getTemperature()
        },
        {
          type: "button_event",
          v: buttonState ? "press" : "none"
        },
        {
          type: "button_event",
          v: buttonState ? "double_press" : "none"
        },
      ]), { name : "Puck" });
    ...
    

    Feeling stupid - Thanks

  • in Bangle.js
    Avatar for user156756

    Thanks for the offer - but think I'll go the easier route ie stick to the UK.

  • in Bangle.js
    Avatar for user156756

    Life moves on and different projects take priority... so selling my Bangle2.

    Factory reset with latest firmware as of today. Excellent condition, buttons all work, battery charges - all lovely. In its original box, with original charging cable and an extra charging cable I picked up.

    Asking £45. Will pay for Royal mail 2nd class shipping in the UK.
    Please respond via DM.

    Have enjoyed this one - now onto other adventures in tiny things.

  • in Bangle.js
    Avatar for user156756

    Getting back to this with all the Home Assistant updates and saw that BTHome now has button events. Works lovely

    var SWBtn = require("https://raw.githubusercontent.c­om/muet/EspruinoDocs/master/modules/SWBu­tton.js");
    var buttonState = 0;
    var btnData = {};
    var idleTimeout;
    if (!Puck.bleAdvert) Puck.bleAdvert = {};
    
    // these get created with a puck button press
    var mySWBtn = new SWBtn(function(k){
      if (k === "S"  ) { // single button press
        buttonState = !buttonState;
        btnData.state = buttonState;
        btnData.btn = 0x01;
        btnData.press = + 0; 
      }
      else if (k === "L" ) { // long button press
        btnData.press = 1; 
        btnData.btn = 0x04;
      }
      else if (k === "SS") { // double short press
        btnData.press = 2; 
        btnData.btn = 0x02;
      }
      updateBTHome(btnData);
    });
    
    function initBTHome() {
      tempF = [(E.getTemperature()*9/5)+32];
      Puck.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information
                  bit 0: "Encryption flag"
                  bit 1-4: "Reserved for future use"
                  bit 5-7: "BTHome Version" */
    
                  0x01, // Battery, 8 bit
                  E.getBattery(),
    
                  0x02, // Temperature, 16 bit
                  tempF&255,tempF>>8,                            
    
                  0x3A, // button event none
                  0x00,
      ];
      NRF.setAdvertising(Puck.bleAdvert);
    }
    
    setInterval(function() {
      initBTHome();
    //}, 1*60*1000); // 1 min
    }, 10000); // 10 sec
    
    function updateBTHome(btnData) {  
      Puck.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information
                  bit 0: "Encryption flag"
                  bit 1-4: "Reserved for future use"
                  bit 5-7: "BTHome Version" */
    
                  0x09, // count1 button press, 1 bit
                  btnData.press,
    
                  0x0F, // binary button state, uint8 (1 byte)
                  btnData.state,
    
                  0x3A, // button event
                  btnData.btn,
                                
                                
      ];
      NRF.setAdvertising(Puck.bleAdvert);
    
      if (idleTimeout) clearTimeout(idleTimeout);
      idleTimeout = setTimeout(function() {
        idleTimeout = undefined;
        Puck.bleAdvert[0xFCD2][2] = 0;
        NRF.setAdvertising(Puck.bleAdvert);
      },2500);
    
    }
    
Actions