grandfatherclock: implement settings menu

master
June Bennison 2025-02-10 00:47:37 +11:00
parent 717d263159
commit 53f4415675
2 changed files with 111 additions and 25 deletions

View File

@ -0,0 +1,83 @@
(function(back) {
const configFile = "grandfatherclock.json";
let config = Object.assign({
twelve_hour: true,
swap_meridian: false,
hour_attention_buzz_ms: 1000,
hour_count_buzz_ms: 250,
fraction_count_buzz_ms: 250,
fractions_of_hour: 4, // 4 = 15min intervals, 6 = 10min intervals
wait_ms: 500,
meridian_buzz_ms: 50,
meridian_buzz_wait_ms: 300
}, require('Storage').readJSON("grandfatherclock.json", true) || {});
let writeConfig = function() {
require('Storage').writeJSON(configFile, config);
};
E.showMenu({
"": {"title" : "Grandfather Clock"},
"< Back": () => back(),
"12 / 24 hour": {
value: config.twelve_hour,
format: v => v ? "12" : "24",
onchange: v => {
config.twelve_hour = v;
writeConfig();
}
},"Swap meridian": {
value: config.swap_meridian,
format: v => v ? "Yes" : "No",
onchange: v => {
config.swap_meridian = v;
writeConfig();
}
},"Hr attn. buzz length (ms)": {
value: config.hour_attention_buzz_ms,
onchange: v => {
config.hour_attention_buzz_ms = v;
writeConfig();
}
},"Hr count buzz (ms)": {
value: config.hour_count_buzz_ms,
onchange: v => {
config.hour_count_buzz_ms = v;
writeConfig();
}
},"Frac. count buzz (ms)": {
value: config.fraction_count_buzz_ms,
onchange: v => {
config.fraction_count_buzz_ms = v;
writeConfig();
}
},"Fracs. of hour": {
value: config.fractions_of_hour,
onchange: v => {
config.fractions_of_hour = v;
writeConfig();
}
},"Count wait (ms)": {
value: config.wait_ms,
onchange: v => {
config.wait_ms = v;
writeConfig();
}
},"Meridian buzz (ms)": {
value: config.meridian_buzz_ms,
onchange: v => {
config.meridian_buzz_ms = v;
writeConfig();
}
},"Meridian wait (ms)": {
value: config.meridian_buzz_wait_ms,
onchange: v => {
config.meridian_buzz_wait_ms = v;
writeConfig();
}
}
});
})

View File

@ -2,36 +2,39 @@
WIDGETS["grandfatherclock"] = { WIDGETS["grandfatherclock"] = {
area: "tr", area: "tr",
width: 16,
draw: function() { draw: function() {
g.reset(); g.reset();
g.drawImage(atob("EBiDASSTJJISSSSZJJJCSSTJ///ISSZP///5CTJ/////ITJ/////ITJ/+B//ITJ/+B//ITJ//+P/ITJ/////ISZP///5CSRJ///ICSQJJJJACSYBJJIBCSYABgABCSYABgABCSYAJAABCSYANgABCSYBtgABCSYNtsABCSYBtgABCSYAMAABCSYAAAABCSZJJJJJCQ=="), this.x, this.y); g.drawImage(atob("EBiDASSTJJISSSSZJJJCSSTJ///ISSZP///5CTJ/////ITJ/////ITJ/+B//ITJ/+B//ITJ//+P/ITJ/////ISZP///5CSRJ///ICSQJJJJACSYBJJIBCSYABgABCSYABgABCSYAJAABCSYANgABCSYBtgABCSYNtsABCSYBtgABCSYAMAABCSYAAAABCSZJJJJJCQ=="), this.x, this.y);
} }
}; };
// todo: all of these configurable through app settings menu. // sensible defaults
const twelve_hour = true; let config = Object.assign({
const swap_meridian = false; twelve_hour: true,
const hour_attention_buzz_ms = 1000; swap_meridian: false,
const hour_count_buzz_ms = 250; hour_attention_buzz_ms: 1000,
const fraction_count_buzz_ms = 250; hour_count_buzz_ms: 250,
const fractions_of_hour = 4; // 4 = 15min intervals, 6 = 10min intervals fraction_count_buzz_ms: 250,
const wait_ms = 500; fractions_of_hour: 4, // 4 = 15min intervals, 6 = 10min intervals
const meridian_buzz_ms = 50; wait_ms: 500,
const meridian_buzz_wait_ms = 300; meridian_buzz_ms: 50,
meridian_buzz_wait_ms: 300
}, require('Storage').readJSON("grandfatherclock.json", true) || {}); // or, load the app settings file.
let date; let date;
let fractionMs = 3600000 / fractions_of_hour; let fractionMs = 3600000 / config.fractions_of_hour;
let chime = function () { let chime = function () {
date = new Date(); date = new Date();
let hourFrac = Math.floor(date.getMinutes() / (60 / fractions_of_hour)); let hourFrac = Math.floor(date.getMinutes() / (60 / config.fractions_of_hour));
if (hourFrac == 0) { // if it's an o'clock hour if (hourFrac == 0) { // if it's an o'clock hour
let chimeHour = (twelve_hour ? date.getHours() % 12 : date.getHours()); let chimeHour = (config.twelve_hour ? date.getHours() % 12 : date.getHours());
if (chimeHour == 0) (twelve_hour ? chimeHour += 12 : chimeHour += 24); if (chimeHour == 0) (config.twelve_hour ? chimeHour += 12 : chimeHour += 24);
Bangle.buzz(hour_attention_buzz_ms).then(() => { // initial buzz Bangle.buzz(config.hour_attention_buzz_ms).then(() => { // initial buzz
setTimeout(hourChime, wait_ms, chimeHour); // wait a period before doing the first chime setTimeout(hourChime, config.wait_ms, chimeHour); // wait a period before doing the first chime
}); });
} else { // if it's a fraction of an hour } else { // if it's a fraction of an hour
fractionChime(hourFrac); fractionChime(hourFrac);
@ -42,27 +45,27 @@
let hourChime = function (hoursLeftToChime) { let hourChime = function (hoursLeftToChime) {
hoursLeftToChime--; hoursLeftToChime--;
Bangle.buzz(hour_count_buzz_ms).then(() => { // recursive. buzz and wait to do the next buzz. Bangle.buzz(config.hour_count_buzz_ms).then(() => { // recursive. buzz and wait to do the next buzz.
if (hoursLeftToChime > 0) { if (hoursLeftToChime > 0) {
setTimeout(hourChime, wait_ms, hoursLeftToChime); setTimeout(hourChime, config.wait_ms, hoursLeftToChime);
} else if (twelve_hour) { // once finished with the hour count } else if (config.twelve_hour) { // once finished with the hour count
setTimeout(meridianChime, wait_ms, (date.getHours() >= 12)); // if in twelve hour mode, queue up the meridian chime. setTimeout(meridianChime, config.wait_ms, (date.getHours() >= 12)); // if in twelve hour mode, queue up the meridian chime.
} }
}); });
}; };
let fractionChime = function (fractionsLeftToChime) { let fractionChime = function (fractionsLeftToChime) {
fractionsLeftToChime--; fractionsLeftToChime--;
Bangle.buzz(fraction_count_buzz_ms).then(() => { // recursive. buzz and wait to do the next buzz. Bangle.buzz(config.fraction_count_buzz_ms).then(() => { // recursive. buzz and wait to do the next buzz.
if (fractionsLeftToChime > 0) setTimeout(fractionChime, wait_ms, fractionsLeftToChime); if (fractionsLeftToChime > 0) setTimeout(fractionChime, config.wait_ms, fractionsLeftToChime);
}); });
}; };
let meridianChime = function (meridian) { let meridianChime = function (meridian) {
if ((swap_meridian ? !meridian : meridian)) { // default: if PM if ((config.swap_meridian ? !meridian : meridian)) { // default: if PM
Bangle.buzz(meridian_buzz_ms).then(setTimeout(Bangle.buzz, meridian_buzz_wait_ms, meridian_buzz_ms)); // buzz once, wait, buzz again. Bangle.buzz(config.meridian_buzz_ms).then(setTimeout(Bangle.buzz, config.meridian_buzz_wait_ms, config.meridian_buzz_ms)); // buzz once, wait, buzz again.
} else { // default: if AM } else { // default: if AM
Bangle.buzz(meridian_buzz_ms); // buzz once. Bangle.buzz(config.meridian_buzz_ms); // buzz once.
} }
}; };