widbt: Added configuration option

master
Hank 2022-07-01 23:01:48 +02:00
parent c99a0de9a9
commit d83396036b
4 changed files with 139 additions and 37 deletions

View File

@ -10,3 +10,4 @@
0.11: Avoid too many notifications. Change disconnected colour to red.
0.12: Prevent repeated execution of `draw()` from the current app.
0.13: Added "connection restored" notification. Fixed restoring of the watchface.
0.14: Added configuration option

View File

@ -1,13 +1,17 @@
{
"id": "widbt_notify",
"name": "Bluetooth Widget with Notification",
"version": "0.13",
"version": "0.14",
"description": "Show the current Bluetooth connection status in the top right of the clock and vibrate when disconnected.",
"icon": "widget.png",
"type": "widget",
"tags": "widget,bluetooth",
"supports": ["BANGLEJS","BANGLEJS2"],
"storage": [
{"name":"widbt_notify.wid.js","url":"widget.js"}
{"name":"widbt_notify.wid.js","url":"widget.js"},
{"name":"widbt_notify.settings.js","url":"settings.js"}
],
"data": [
{"name":"widbt_notify.json"}
]
}

View File

@ -0,0 +1,62 @@
(function(back) {
var FILE = "widbt_notify.json";
var settings = Object.assign({
secondsOnUnlock: false,
}, require('Storage').readJSON(FILE, true) || {});
function writeSettings() {
require('Storage').writeJSON(FILE, settings);
}
// Helper method which uses int-based menu item for set of string values
function stringItems(startvalue, writer, values) {
return {
value: (startvalue === undefined ? 0 : values.indexOf(startvalue)),
format: v => values[v],
min: 0,
max: values.length - 1,
wrap: true,
step: 1,
onchange: v => {
writer(values[v]);
writeSettings();
}
};
}
// Helper method which breaks string set settings down to local settings object
function stringInSettings(name, values) {
return stringItems(settings[name], v => settings[name] = v, values);
}
var mainmenu = {
"": {
"title": "Bluetooth Widget WN"
},
"< Back": () => back(),
"Show Widget": {
value: (settings.showWidget !== undefined ? settings.showWidget : true),
onchange: v => {
settings.showWidget = v;
writeSettings();
}
},
"Buzz on Connect": {
value: (settings.buzzOnConnect !== undefined ? settings.buzzOnConnect : true),
onchange: v => {
settings.buzzOnConnect = v;
writeSettings();
}
},
"Buzz on loss": {
value: (settings.buzzOnLoss !== undefined ? settings.buzzOnLoss : true),
onchange: v => {
settings.buzzOnLoss = v;
writeSettings();
}
}
};
E.showMenu(mainmenu);
});

View File

@ -2,7 +2,38 @@ WIDGETS.bluetooth_notify = {
area: "tr",
width: 15,
warningEnabled: 1,
// ------------ Settings -------- very lame - need to improve
readshowWidget: function() {
var showWidget;
const SETTINGSFILE = "widbt_notify.json";
function def (value, def) {return value !== undefined ? value : def;}
var settings = require('Storage').readJSON(SETTINGSFILE, true) || {};
showWidget = def(settings.showWidget, true);
return showWidget;
},
readBuzzOnConnect: function() {
var buzzOnConnect;
const SETTINGSFILE = "widbt_notify.json";
function def (value, def) {return value !== undefined ? value : def;}
var settings = require('Storage').readJSON(SETTINGSFILE, true) || {};
buzzOnConnect = def(settings.buzzOnConnect, true);
return buzzOnConnect;
},
readBuzzOnLoss: function() {
var buzzOnLoss;
const SETTINGSFILE = "widbt_notify.json";
function def (value, def) {return value !== undefined ? value : def;}
var settings = require('Storage').readJSON(SETTINGSFILE, true) || {};
buzzOnLoss = def(settings.buzzOnLoss, true);
return buzzOnLoss;
},
// ------------ Settings --------
draw: function() {
if (WIDGETS.bluetooth_notify.readshowWidget()){
g.reset();
if (NRF.getSecurityStatus().connected) {
g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f"));
@ -11,6 +42,7 @@ WIDGETS.bluetooth_notify = {
g.setColor("#f00"); // red is easier to distinguish from blue
}
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
}
},
redrawCurrentApp: function(){
@ -25,34 +57,37 @@ WIDGETS.bluetooth_notify = {
},
connect: function() {
if(WIDGETS.bluetooth_notify.warningEnabled == 1){
E.showMessage(/*LANG*/'Connection\n restored.', 'Bluetooth');
E.showMessage(/*LANG*/'Connection\nrestored.', 'Bluetooth');
setTimeout(()=>{WIDGETS.bluetooth_notify.redrawCurrentApp();}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'.
WIDGETS.bluetooth_notify.warningEnabled = 0;
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 30000); // don't buzz for the next 30 seconds.
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 300); // don't buzz for the next 30 seconds.
var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet;
if(!quiet){
Bangle.buzz(700, 1); // buzz on connection loss
if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnConnect()){
Bangle.buzz(700, 1); // buzz on connection resume
}
}
WIDGETS.bluetooth_notify.draw();
},
disconnect: function() {
if(WIDGETS.bluetooth_notify.warningEnabled == 1){
E.showMessage(/*LANG*/'Connection\nlost.', 'Bluetooth');
E.showMessage(/*LANG*/ 'Connection\nlost.', 'Bluetooth');
setTimeout(()=>{WIDGETS.bluetooth_notify.redrawCurrentApp();}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'.
WIDGETS.bluetooth_notify.warningEnabled = 0;
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 30000); // don't buzz for the next 30 seconds.
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 300); // don't buzz for the next 30 seconds.
var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet;
if(!quiet){
if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnLoss()){
Bangle.buzz(700, 1); // buzz on connection loss
}
}
WIDGETS.bluetooth_notify.draw();
}
};