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.com/muet/EspruinoDocs/master/modules/SWButton.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").getAdvertisement([
{
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);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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?
Seems quite fast
Thanks