diff --git a/apps/chimer/icon.txt b/apps/chimer/icon.txt new file mode 100644 index 000000000..c1750ff20 --- /dev/null +++ b/apps/chimer/icon.txt @@ -0,0 +1,2 @@ + +widget.png: https://icons8.com/icon/15715/plus-1-hour \ No newline at end of file diff --git a/apps/chimer/metadata.json b/apps/chimer/metadata.json new file mode 100644 index 000000000..4f021c866 --- /dev/null +++ b/apps/chimer/metadata.json @@ -0,0 +1,15 @@ +{ + "id": "chimer", + "name": "Chimer", + "version": "0.01", + "description": "A fork of Hour Chime. Buzz or beep on every 60, 30 or 15 minutes.", + "icon": "widget.png", + "type": "widget", + "tags": "widget", + "supports": ["BANGLEJS","BANGLEJS2"], + "storage": [ + {"name":"widchime.wid.js","url":"widget.js"}, + {"name":"widchime.settings.js","url":"settings.js"} + ], + "data": [{"name":"widchime.json"}] +} \ No newline at end of file diff --git a/apps/chimer/settings.js b/apps/chimer/settings.js new file mode 100644 index 000000000..586b9d7ca --- /dev/null +++ b/apps/chimer/settings.js @@ -0,0 +1,54 @@ +/** + * @param {function} back Use back() to return to settings menu + */ + + + +(function(back) { + // default to buzzing + var FILE = "chimer.json" + var settings = {} + const chimes = ["Off", "Buzz", "Beep", "Both"] + const frequency = ["60 min", "30 min", "15 min"] + + + + const menu = { + "": {"title": "Hour Chime"}, + "< Back": back, + "Chime Type": { + value: settings.type, + min: 0, max: 2, // both is just silly + format: v => chimes[v], + onchange: function(v) { + settings.type = v + writeSettings(settings) + }, + }, + "Frequency": { + value: settings.freq, + min: 0, max: 2, + format: v => frequency[v], + onchange : function(v) { + settings.freq = v + writeSettings(settings) + } + } + } + + + var readSettings = () => { + var settings = require("Storage").readJSON(FILE, 1) || { + type: 1, + freq: 0, + }; + return settings; + }; + + var writeSettings = (settings) => { + require("Storage").writeJSON(FILE, settings); + }; + settings = readSettings() + E.showMenu(menu) +}) + diff --git a/apps/chimer/widget.js b/apps/chimer/widget.js new file mode 100644 index 000000000..faf913a20 --- /dev/null +++ b/apps/chimer/widget.js @@ -0,0 +1,49 @@ +(function() { + // 0: off, 1: buzz, 2: beep, 3: both + var readSettings = () => { + var settings = require("Storage").readJSON(FILE, 1) || { + type: 1, + freq: 0, + }; + return settings; + }; + + var settings = readSettings() + + function chime() { + if (settings.type == 0) return; + if (settings.type&1) Bangle.buzz(100); + if (settings.type&2) Bangle.beep(); + } + + let lastHour = (new Date()).getHours(); // don't chime when (re)loaded at a whole hour + function check() { + const now = new Date(), + h = now.getHours(), m = now.getMinutes(), + s = now.getSeconds(), ms = now.getMilliseconds(); + if (settings.freq == 1){ + if (h!==lastHour && m===30) chime(); + lastHour = h; + // check again in 30 minutes + const mLeft = 30-m, sLeft = (mLeft*60)-s, msLeft = (sLeft*1000)-ms; + setTimeout(check, msLeft); + }else if (settings.freq == 2){ + if (h!==lastHour && m===15) chime(); + lastHour = h; + // check again in 15 minutes + const mLeft = 15-m, sLeft = (mLeft*60)-s, msLeft = (sLeft*1000)-ms; + setTimeout(check, msLeft); + }else{ + if (h!==lastHour && m===0) chime(); + lastHour = h; + // check again in 60 minutes + const mLeft = 60-m, sLeft = (mLeft*60)-s, msLeft = (sLeft*1000)-ms; + setTimeout(check, msLeft); + } + + + } + + check(); +}) +(); \ No newline at end of file diff --git a/apps/chimer/widget.png b/apps/chimer/widget.png new file mode 100644 index 000000000..7c3d7eebd Binary files /dev/null and b/apps/chimer/widget.png differ