Skip to content

Instantly share code, notes, and snippets.

@jptrsn
Last active January 19, 2024 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jptrsn/fecd7040138f285782192ac01df01fbc to your computer and use it in GitHub Desktop.
Save jptrsn/fecd7040138f285782192ac01df01fbc to your computer and use it in GitHub Desktop.
espruino swbutton
// Add the following module URL to the "communications" section of your IDE settings
// https://raw.githubusercontent.com/muet/EspruinoDocs/master/modules
const SWBtn = require("SWButton");
const advertisingInterval = 60000;
const eventTimeout = 2500;
let buttonState = "none";
let led = 2;
function blinkLeds(c,d,optCallback) {
setLeds(c);
setTimeout(function() {
setLeds(0);
if (optCallback) {
optCallback();
}
}, d || eventTimeout);
}
function setLeds(color) {
digitalWrite([LED3,LED2,LED1], color);
}
function updateAdvertising() {
NRF.setAdvertising(require("BTHome").getAdvertisement([
{
type : "battery",
v : E.getBattery()
},
{
type : "temperature",
v : E.getTemperature()
},
{
type: "button_event",
v: buttonState
},
]), { name : "Puck" });
if (buttonState !== "none") {
setTimeout(updateAdvertising, eventTimeout);
buttonState = "none";
}
}
const softBtn = new SWBtn((k) => {
console.log('button event', k);
switch (k) {
case "S":
led = 2;
buttonState = "press";
break;
case "SS":
led = 3;
buttonState = "double_press";
break;
case "SSS":
led = 4;
buttonState = "triple_press";
break;
case "L":
led = 5;
buttonState = "long_press";
break;
case "LL":
led = 6;
buttonState = "long_double_press";
break;
case "LLL":
led = 7;
buttonState = "long_triple_press";
break;
default:
led = 1;
buttonState = "none";
}
if (buttonState !== "none") {
updateAdvertising();
}
blinkLeds(led);
});
// Update advertising now
updateAdvertising();
// Update advertising every advertising interval
setInterval(updateAdvertising, advertisingInterval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment