sleepphasealarm: add settings file

- Vibrate with configured pattern
- Add setting to defer start of algorithm
pull/1965/head
Erik Andresen 2022-06-11 09:00:47 +02:00
parent 418d14a156
commit c28d85ad1f
6 changed files with 135 additions and 55 deletions

View File

@ -7,3 +7,5 @@
use Layout library and display ETA
0.07: Add check for day of week
0.08: Update to new time_utils module
0.09: Vibrate with configured pattern
Add setting to defer start of algorithm

View File

@ -4,10 +4,19 @@ The alarm must be in the next 24h.
The display shows:
- the current time
- time of the next alarm or timer
- time difference between current time and alarm time (ETA)
- current state of the ESS algorithm, "Sleep" or "Awake", useful for debugging
- The current time.
- Time of the next alarm or timer.
- Time difference between current time and alarm time (ETA).
- Current state of the ESS algorithm, "Sleep" or "Awake", useful for debugging. State can also be "Deferred", see the "Run before alarm"-option.
## Settings
* **Keep alarm enabled**
- Yes: (default) Alert will stay enabled, e.g. for an alarm at 7:00 the clock will buzz at 6:45 (the calculated time from the ESS algorithm) and again at 7:00.
- No: No action at configured alarm time from scheduler.
* **Run before alarm**
- disabled: (default) The ESS algorithm starts immediately when the application starts.
- 1..23: The ESS algorithm starts the configured time before the alarm. E.g. when set to 1h for an alarm at 7:00 the ESS algorithm will start at 6:00. This improves battery life.
## Logging

View File

@ -1,9 +1,18 @@
const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2
const CONFIGFILE = "sleepphasealarm.json";
const Layout = require("Layout");
const locale = require('locale');
const alarms = require("Storage").readJSON("sched.json",1) || [];
const config = require("Storage").readJSON("sleepphasealarm.json",1) || {logs: []};
const config = Object.assign({
logs: [], // array of length 31 with one entry for each day of month
settings: {
startBeforeAlarm: 0, // 0 = start immediately, 1..23 = start 1h..23h before alarm time
disableAlarm: false,
}
}, require("Storage").readJSON(CONFIGFILE,1) || {});
const active = alarms.filter(a=>a.on);
const schedSettings = require("sched").getSettings();
let buzzCount = schedSettings.buzzCount;
let logs = [];
// Sleep/Wake detection with Estimation of Stationary Sleep-segments (ESS):
@ -43,7 +52,8 @@ function calc_ess(acc_magn) {
}
// locate next alarm
var nextAlarm;
var nextAlarmDate;
var nextAlarmConfig;
active.forEach(alarm => {
const now = new Date();
const time = require("time_utils").decodeTime(alarm.t);
@ -52,8 +62,9 @@ active.forEach(alarm => {
dateAlarm.setTime(dateAlarm.getTime() + (24*60*60*1000));
}
if ((alarm.dow >> dateAlarm.getDay()) & 1) { // check valid day of week
if (nextAlarm === undefined || dateAlarm < nextAlarm) {
nextAlarm = dateAlarm;
if (nextAlarmDate === undefined || dateAlarm < nextAlarmDate) {
nextAlarmDate = dateAlarm;
nextAlarmConfig = alarm;
}
}
});
@ -69,8 +80,8 @@ var layout = new Layout({
}, {lazy:true});
function drawApp() {
var alarmHour = nextAlarm.getHours();
var alarmMinute = nextAlarm.getMinutes();
var alarmHour = nextAlarmDate.getHours();
var alarmMinute = nextAlarmDate.getMinutes();
if (alarmHour < 10) alarmHour = "0" + alarmHour;
if (alarmMinute < 10) alarmMinute = "0" + alarmMinute;
layout.alarm_date.label = "Alarm at " + alarmHour + ":" + alarmMinute;
@ -80,7 +91,7 @@ function drawApp() {
if (Bangle.isLCDOn()) {
const now = new Date();
layout.date.label = locale.time(now, BANGLEJS2 && Bangle.isLocked() ? 1 : 0); // hide seconds on bangle 2
const diff = nextAlarm - now;
const diff = nextAlarmDate - now;
const diffHour = Math.floor((diff % 86400000) / 3600000).toString();
const diffMinutes = Math.floor(((diff % 86400000) % 3600000) / 60000).toString();
layout.eta.label = "ETA: -"+ diffHour + ":" + diffMinutes.padStart(2, '0');
@ -92,70 +103,91 @@ function drawApp() {
setInterval(drawTime, 500); // 2Hz
}
var buzzCount = 19;
function buzz() {
if ((require('Storage').readJSON('setting.json',1)||{}).quiet>1) return; // total silence
Bangle.setLCDPower(1);
Bangle.buzz().then(()=>{
if (buzzCount--) {
setTimeout(buzz, 500);
} else {
// back to main after finish
setTimeout(load, 1000);
}
});
Bangle.setLCDPower(1);
require("buzz").pattern(nextAlarmConfig.vibrate || ";");
if (buzzCount--) {
setTimeout(buzz, schedSettings.buzzIntervalMillis);
} else {
// back to main after finish
setTimeout(load, 1000);
}
}
function addLog(time, type) {
logs.push({time: time, type: type});
require("Storage").writeJSON("sleepphasealarm.json", config);
if (logs.length > 1) { // Do not write if there is only one state
require("Storage").writeJSON(CONFIGFILE, config);
}
}
// run
var minAlarm = new Date();
var measure = true;
if (nextAlarm !== undefined) {
config.logs[nextAlarm.getDate()] = []; // overwrite log on each day of month
logs = config.logs[nextAlarm.getDate()];
if (nextAlarmDate !== undefined) {
config.logs[nextAlarmDate.getDate()] = []; // overwrite log on each day of month
logs = config.logs[nextAlarmDate.getDate()];
g.clear();
Bangle.loadWidgets();
Bangle.drawWidgets();
let swest_last;
// minimum alert 30 minutes early
minAlarm.setTime(nextAlarm.getTime() - (30*60*1000));
Bangle.on('accel', (accelData) => { // 12.5Hz
const now = new Date();
const acc = accelData.mag;
const swest = calc_ess(acc);
minAlarm.setTime(nextAlarmDate.getTime() - (30*60*1000));
run = () => {
layout.state.label = "Start";
layout.render();
Bangle.on('accel', (accelData) => { // 12.5Hz
const now = new Date();
const acc = accelData.mag;
const swest = calc_ess(acc);
if (swest !== undefined) {
if (Bangle.isLCDOn()) {
layout.state.label = swest ? "Sleep" : "Awake";
layout.render();
}
// log
if (swest_last != swest) {
if (swest) {
addLog(new Date(now - sleepthresh*13/12.5*1000), "sleep"); // calculate begin of no motion phase, 13 values/second at 12.5Hz
} else {
addLog(now, "awake");
if (swest !== undefined) {
if (Bangle.isLCDOn()) {
layout.state.label = swest ? "Sleep" : "Awake";
layout.render();
}
// log
if (swest_last != swest) {
if (swest) {
addLog(new Date(now - sleepthresh*13/12.5*1000), "sleep"); // calculate begin of no motion phase, 13 values/second at 12.5Hz
} else {
addLog(now, "awake");
}
swest_last = swest;
}
swest_last = swest;
}
}
if (now >= nextAlarm) {
// The alarm widget should handle this one
addLog(now, "alarm");
setTimeout(load, 1000);
} else if (measure && now >= minAlarm && swest === false) {
addLog(now, "alarm");
buzz();
measure = false;
}
});
if (now >= nextAlarmDate) {
// The alarm widget should handle this one
addLog(now, "alarm");
setTimeout(load, 1000);
} else if (measure && now >= minAlarm && swest_last === false) {
addLog(now, "alarm");
buzz();
measure = false;
if (config.settings.disableAlarm) {
// disable alarm for scheduler
nextAlarmConfig.last = now.getDate();
require("Storage").writeJSON("sched.json", alarms);
}
}
});
};
drawApp();
if (config.settings.startBeforeAlarm === 0) {
// Start immediately
run();
} else {
// defer start
layout.state.label = "Deferred";
layout.render();
const diff = nextAlarmDate - Date.now();
let timeout = diff-config.settings.startBeforeAlarm*60*60*1000;
if (timeout < 0) timeout = 0;
setTimeout(run, timeout);
}
} else {
E.showMessage('No Alarm');
setTimeout(load, 1000);

View File

@ -1,7 +1,6 @@
<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.css">
</head>
<body>
<p>Please select a wakeup day:</p>

View File

@ -2,7 +2,7 @@
"id": "sleepphasealarm",
"name": "SleepPhaseAlarm",
"shortName": "SleepPhaseAlarm",
"version": "0.08",
"version": "0.09",
"description": "Uses the accelerometer to estimate sleep and wake states with the principle of Estimation of Stationary Sleep-segments (ESS, see https://ubicomp.eti.uni-siegen.de/home/datasets/ichi14/index.html.en). This app will read the next alarm from the alarm application and will wake you up to 30 minutes early at the best guessed time when you are almost already awake.",
"icon": "app.png",
"tags": "alarm",
@ -11,6 +11,7 @@
"dependencies": {"scheduler":"type"},
"storage": [
{"name":"sleepphasealarm.app.js","url":"app.js"},
{"name":"sleepphasealarm.settings.js","url":"settings.js"},
{"name":"sleepphasealarm.img","url":"app-icon.js","evaluate":true}
],
"data": [{"name":"sleepphasealarm.json","storageFile":true}],

View File

@ -0,0 +1,37 @@
(function(back) {
const CONFIGFILE = "sleepphasealarm.json";
// Load settings
const config = Object.assign({
logs: [], // array of length 31 with one entry for each day of month
settings: {
startBeforeAlarm: 0, // 0 = start immediately, 1..23 = start 1h..23h before alarm time
disableAlarm: false,
}
}, require("Storage").readJSON(CONFIGFILE,1) || {});
function writeSettings() {
require('Storage').writeJSON(CONFIGFILE, config);
}
// Show the menu
E.showMenu({
"" : { "title" : "SleepPhaseAlarm" },
'Keep alarm enabled': {
value: !!config.settings.disableAlarm,
format: v => v?"No":"Yes",
onchange: v => {
config.settings.disableAlarm = v;
writeSettings();
}
}, "< Back" : () => back(),
'Run before alarm': {
format: v => v === 0 ? 'disabled' : v+'h',
value: config.settings.startBeforeAlarm,
min: 0, max: 23,
onchange: v => {
config.settings.startBeforeAlarm = v;
writeSettings();
}
},
});
})