mirror of https://github.com/espruino/BangleApps
[widbt_notify] Simplify code, add some options
parent
674ef0aa13
commit
a1e03d3229
|
@ -12,3 +12,4 @@
|
||||||
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
|
|
@ -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",
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
});
|
})
|
|
@ -1,53 +1,32 @@
|
||||||
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 not 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);
|
||||||
}
|
}
|
||||||
|
@ -59,52 +38,50 @@ WIDGETS.bluetooth_notify = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
connect: function() {
|
onNRF: function(connect) {
|
||||||
|
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'.
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
var quiet = (require('Storage').readJSON('setting.json', 1) || {}).quiet;
|
||||||
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', 30000); // don't buzz for the next 30 seconds.
|
if (!quiet && (connect ? this.buzzOnConnect : this.buzzOnLoss)) {
|
||||||
|
Bangle.buzz(700, 1); // buzz on connection resume or loss
|
||||||
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();
|
// if hideConnected is en- and showMessage disabled, redraw widgets to hide
|
||||||
|
if (this.hideConnected && !this.showMessage) {
|
||||||
},
|
Bangle.drawWidgets();
|
||||||
|
} else {
|
||||||
disconnect: function() {
|
this.draw();
|
||||||
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);
|
// clear variables
|
||||||
NRF.on('disconnect', WIDGETS.bluetooth_notify.disconnect);
|
settings = undefined;
|
||||||
|
widWidth = undefined;
|
||||||
|
|
||||||
|
// setup bluetooth connection events
|
||||||
|
NRF.on('connect', (addr) => WIDGETS.bluetooth_notify.onNRF(addr));
|
||||||
|
NRF.on('disconnect', () => WIDGETS.bluetooth_notify.onNRF());
|
||||||
|
|
||||||
|
})()
|
Loading…
Reference in New Issue