Merge pull request #2134 from storm64/widbt_notify

[widbt_notify] Simplify code, add some options
pull/2144/head
Gordon Williams 2022-09-22 08:41:44 +01:00 committed by GitHub
commit 2abad628ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 153 deletions

View File

@ -11,4 +11,5 @@
0.12: Prevent repeated execution of `draw()` from the current app. 0.12: Prevent repeated execution of `draw()` from the current app.
0.13: Added "connection restored" notification. Fixed restoring of the watchface. 0.13: Added "connection restored" notification. Fixed restoring of the watchface.
0.14: Added configuration option 0.14: Added configuration option
0.15: Added option to hide widget when connected 0.15: Added option to hide widget when connected
0.16: Simplify code, add option to disable displaying a message

View File

@ -1,8 +1,8 @@
{ {
"id": "widbt_notify", "id": "widbt_notify",
"name": "Bluetooth Widget with Notification", "name": "Bluetooth Widget with Notification",
"version": "0.15", "version": "0.16",
"description": "Show the current Bluetooth connection status in the top right of the watch. Optional buzz and/or and hide if disconnected", "description": "Show the current Bluetooth connection status with some optional features: show message, buzz on connect/loss, hide always/if connected.",
"icon": "widget.png", "icon": "widget.png",
"type": "widget", "type": "widget",
"tags": "widget,bluetooth", "tags": "widget,bluetooth",

View File

@ -1,69 +1,57 @@
(function(back) { (function(back) {
var FILE = "widbt_notify.json";
var filename = "widbt_notify.json";
// set Storage and load settings
var storage = require("Storage");
var settings = Object.assign({ var settings = Object.assign({
secondsOnUnlock: false, showWidget: true,
}, require('Storage').readJSON(FILE, true) || {}); buzzOnConnect: true,
buzzOnLoss: true,
hideConnected: true,
showMessage: true,
nextBuzz: 30000
}, storage.readJSON(filename, true) || {});
function writeSettings() { // setup boolean menu entries
require('Storage').writeJSON(FILE, settings); function boolEntry(key) {
}
// Helper method which uses int-based menu item for set of string values
function stringItems(startvalue, writer, values) {
return { return {
value: (startvalue === undefined ? 0 : values.indexOf(startvalue)), value: settings[key],
format: v => values[v],
min: 0,
max: values.length - 1,
wrap: true,
step: 1,
onchange: v => { onchange: v => {
writer(values[v]); // change the value of key
writeSettings(); settings[key] = v;
// write to storage
storage.writeJSON(filename, settings);
} }
}; };
} }
// Helper method which breaks string set settings down to local settings object // setup menu
function stringInSettings(name, values) { var menu = {
return stringItems(settings[name], v => settings[name] = v, values);
}
var mainmenu = {
"": { "": {
"title": "Bluetooth Widget WN" "title": "Bluetooth Widget WN"
}, },
"< Back": () => back(), "< Back": () => back(),
"Show Widget": { "Show Widget": boolEntry("showWidget"),
value: (settings.showWidget !== undefined ? settings.showWidget : true), "Buzz on connect": boolEntry("buzzOnConnect"),
"Buzz on loss": boolEntry("buzzOnLoss"),
"Hide connected": boolEntry("hideConnected"),
"Show Message": boolEntry("showMessage"),
"Next Buzz": {
value: settings.nextBuzz,
step: 1000,
min: 1000,
max: 120000,
wrap: true,
format: v => (v / 1000) + "s",
onchange: v => { onchange: v => {
settings.showWidget = v; settings.nextBuzz = v;
writeSettings(); storage.writeJSON(filename, settings);
} }
}, }
"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();
}
},
"Hide connected": {
value: (settings.hideConnected !== undefined ? settings.hideConnected : false),
onchange: v => {
settings.hideConnected = v;
writeSettings();
}
}
}; };
E.showMenu(mainmenu); // draw main menu
E.showMenu(menu);
}); })

View File

@ -1,110 +1,90 @@
WIDGETS.bluetooth_notify = { (function() {
// load settings
var settings = Object.assign({
showWidget: true,
buzzOnConnect: true,
buzzOnLoss: true,
hideConnected: true,
showMessage: true,
nextBuzz: 30000
}, require("Storage").readJSON("widbt_notify.json", true) || {});
// setup widget with to hide if connected and option set
var widWidth = settings.hideConnected && NRF.getSecurityStatus().connected ? 0 : 15;
// write widget with loaded settings
WIDGETS.bluetooth_notify = Object.assign(settings, {
// set area and width
area: "tr", area: "tr",
width: 15, width: widWidth,
// setup warning status
warningEnabled: 1, 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;
},
readHideConnected: function() {
var hideConnected;
const SETTINGSFILE = "widbt_notify.json";
function def (value, def) {return value !== undefined ? value : def;}
var settings = require('Storage').readJSON(SETTINGSFILE, true) || {};
hideConnected = def(settings.hideConnected, true);
return hideConnected;
},
// ------------ Settings --------
draw: function() { draw: function() {
if (WIDGETS.bluetooth_notify.readshowWidget()){ if (this.showWidget) {
g.reset(); g.reset();
if (NRF.getSecurityStatus().connected) { if (NRF.getSecurityStatus().connected) {
if (!WIDGETS.bluetooth_notify.readHideConnected()) { if (!this.hideConnected) {
g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f")); g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f"));
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
} }
} else { } else {
// g.setColor(g.theme.dark ? "#666" : "#999"); // g.setColor(g.theme.dark ? "#666" : "#999");
g.setColor("#f00"); // red is easier to distinguish from blue g.setColor("#f00"); // red is easier to distinguish from blue
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y); g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
}
} }
}
}, },
redrawCurrentApp: function(){ redrawCurrentApp: function() {
if(typeof(draw)=='function'){ if (typeof(draw) == 'function') {
g.clear(); g.clear();
draw(); draw();
Bangle.loadWidgets(); Bangle.loadWidgets();
Bangle.drawWidgets(); Bangle.drawWidgets();
}else{ } else {
load(); // fallback. This might reset some variables load(); // fallback. This might reset some variables
}
},
onNRF: function(connect) {
// setup widget with and reload widgets to show/hide if hideConnected is enabled
if (this.hideConnected) {
this.width = connect ? 0 : 15; // ensures correct redraw
Bangle.drawWidgets();
} else {
// redraw widget
this.draw();
}
if (this.warningEnabled) {
if (this.showMessage) {
E.showMessage( /*LANG*/ 'Connection\n' + (connect ? /*LANG*/ 'restored.' : /*LANG*/ 'lost.'), 'Bluetooth');
setTimeout(() => {
WIDGETS.bluetooth_notify.redrawCurrentApp();
}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'.
} }
},
connect: function() {
if(WIDGETS.bluetooth_notify.warningEnabled == 1){ this.warningEnabled = 0;
E.showMessage(/*LANG*/'Connection\nrestored.', 'Bluetooth'); setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', this.nextBuzz); // don't buzz for the next X seconds.
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.
var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet;
if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnConnect()){
Bangle.buzz(700, 1); // buzz on connection resume
}
}
WIDGETS.bluetooth_notify.draw();
}, var quiet = (require('Storage').readJSON('setting.json', 1) || {}).quiet;
if (!quiet && (connect ? this.buzzOnConnect : this.buzzOnLoss)) {
disconnect: function() { Bangle.buzz(700, 1); // buzz on connection resume or loss
if(WIDGETS.bluetooth_notify.warningEnabled == 1){ }
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.
var quiet = (require('Storage').readJSON('setting.json',1)||{}).quiet;
if(!quiet && WIDGETS.bluetooth_notify.readBuzzOnLoss()){
Bangle.buzz(700, 1); // buzz on connection loss
}
}
WIDGETS.bluetooth_notify.draw();
} }
};
NRF.on('connect', WIDGETS.bluetooth_notify.connect); });
NRF.on('disconnect', WIDGETS.bluetooth_notify.disconnect);
// clear variables
settings = undefined;
widWidth = undefined;
// setup bluetooth connection events
NRF.on('connect', (addr) => WIDGETS.bluetooth_notify.onNRF(addr));
NRF.on('disconnect', () => WIDGETS.bluetooth_notify.onNRF());
})()