add settings for move and sit time

master
v1nc 2020-05-07 20:03:11 +02:00
parent dbf57058d0
commit c091db0d68
2 changed files with 66 additions and 2 deletions

View File

@ -1,9 +1,25 @@
const storage = require("Storage");
const SETTINGS_FILE = 'getup.settings.json';
function setting(key) {
const DEFAULTS = {
'sitTime' : 20,
'moveTime' : 1
};
if (!settings) { loadSettings(); }
return (key in settings) ? settings[key] : DEFAULTS[key];
}
let settings;
function loadSettings() {
settings = storage.readJSON(SETTINGS_FILE, 1) || {};
}
function remind() { function remind() {
Bangle.buzz(1000,1); Bangle.buzz(1000,1);
g.clear(); g.clear();
g.setColor(0xF800); g.setColor(0xF800);
g.drawString("MOVE!", g.getWidth()/2, g.getHeight()/2); g.drawString("MOVE!", g.getWidth()/2, g.getHeight()/2);
setTimeout(print_message,60000); setTimeout(print_message,moveTime * 60000);
} }
function print_message(){ function print_message(){
@ -18,5 +34,5 @@ g.setFontAlign(0,0);
g.flip(); g.flip();
print_message(); print_message();
setInterval(remind,1200000); setInterval(remind,settings.sitTime * 60000);

48
apps/getup/settings.js Normal file
View File

@ -0,0 +1,48 @@
// This file should contain exactly one function, which shows the app's settings
/**
* @param {function} back Use back() to return to settings menu
*/
(function(back) {
const SETTINGS_FILE = 'getup.settings.json';
// initialize with default settings...
let s = {
'sitTime' : 20
'moveTime' : 1
};
// ...and overwrite them with any saved values
// This way saved values are preserved if a new version adds more settings
const storage = require('Storage');
const saved = storage.readJSON(SETTINGS_FILE, 1) || {};
for (const key in saved) {
s[key] = saved[key];
}
// creates a function to safe a specific setting, e.g. save('color')(1)
function save(key) {
return function (value) {
s[key] = value;
storage.write(SETTINGS_FILE, s);
};
}
const menu = {
'': { 'title': 'Get Up' },
'< Back': back,
'Sit time (min)': {
value: s.sitTime,
min: 0,
max: 10000,
step: 1,
onchange: save('sitTime'),
},
'Move time (min)': {
value: s.moveTime,
min: 0,
max: 5000,
step: 1,
onchange: save('moveTime'),
},
};
E.showMenu(menu);
});