/
main.ts
528 lines (488 loc) · 13.9 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* Global Imports
*/
const storage = require('Storage');
const locale = require('locale');
import * as watchMaker from 'watchmaker-js';
const version = '0.1';
// Instance of the debug function that collects debug data on a defined tick
let debugFn;
// Instance of the moods app, initialised below;
let moodsApp;
/**
* Get debug information for the app per tick
* @param tickTime
* @return {function(): void}
*/
function debugInfo(tickTime?: number) {
const interval = setInterval(() => {
const memory = process.memory();
const pc = Math.round((memory.usage * 100) / memory.total);
const debugInfo = [Bangle.dbg(), memory, pc, storage.getFree(), process.version];
moodsApp.setState('debugInfo', debugInfo);
}, tickTime || 5000);
return () => clearInterval(interval);
}
/**
* Draw a heart for the HRM monitor page with current heart rate and number
* of collected rates
* @param queue
* @param startX
* @param startY
*/
function drawHeart(queue, startX, startY) {
if (queue.length > 0) {
const heartImage = moodsApp.getImage('heart');
const item = `${queue[queue.length - 1]}`;
g.drawImage(heartImage, startX, startY, { scale: 2 });
g.setColor(255, 255, 255);
g.setFontVector(item.length === 3 ? 15 : 18);
g.drawString(`${item}`, startX + 17, startY + 20);
g.setFontVector(15);
g.drawString(`${queue.length}`, startX + 50, startY + 20);
}
}
/**
* Set up the LCD Listener, if the user has the setting to close the app on LCD
* close this will check if the user is on the home screen and closes the app
* If the user is on another page it won't close
*/
function setupLCDListener(app) {
let timeout;
// When the LCD Powers down on the menu page, close the app
Bangle.on('lcdPower', (on) => {
const lcdOff = app.getSettings('lcdOffClose');
if (lcdOff) {
if (on && timeout) {
clearTimeout(timeout);
} else if (!on && app.currentRoute === 'home') {
timeout = setTimeout(() => load(), 20000);
}
}
});
}
/**
* Set up the GPS listener, if the user has it toggled on then when we get a fix set
* the last location
*/
function setupGPSListener(app, cb) {
Bangle.on('GPS', (gps) => {
cb && cb(gps);
gps.fix && app.setState('currentLocation', gps);
});
}
/**
* Set up a heart rate monitor collection, this collects a value that is above the
* confidence level of the HRM device
* @param app
* @param cb
*/
function setupHMRListener(app, cb) {
Bangle.on('HRM', (hrm) => {
const confidenceLevel = app.getSettings('confidenceLevel');
const hrmQueue = app.getState('hrmQueue');
const isValid = hrm.confidence >= confidenceLevel;
if (hrm.confidence >= confidenceLevel) {
hrmQueue.push(hrm.bpm);
app.setState('hrmQueue', hrmQueue);
}
cb(hrmQueue, isValid);
});
}
/**
* Function called when showing intro
* @param step
* @param steps
*/
function showIntro(step: number, steps?: string[]) {
steps = steps || watchMaker.convertStringToPage(moodsApp.getText('text.help'));
if (step < 0 || !steps[step]) {
moodsApp.goTo('home');
} else {
watchMaker
.createSelectPage(
'Welcome to Moods',
steps[step],
{
Next: step + 1,
Exit: 0,
},
0,
true
)
.then((nextStep) => (!nextStep ? showIntro(-1, steps) : showIntro(nextStep, steps)));
}
}
/**
* The main menu view, this first disables any active devices to off, which allows us to use it
* as the last step or exit view for all other views
*/
function viewMainMenu() {
toggleLocationCollection(false);
toggleHMR(false);
const lastMood = moodsApp.getState('lastMood');
const moodText = lastMood ? `${locale.time(new Date(lastMood.time), 1)} - ${lastMood.mood}` : '';
const menu = {};
menu['Set Mood'] = () => moodsApp.goTo('addMood');
menu['Recent History'] = () => moodsApp.goTo('showHistory');
menu['Settings'] = () => moodsApp.goTo('settings');
menu['Help'] = () => moodsApp.goTo('help', 0);
if (moodsApp.getState('globalDebug')) {
menu['Debug'] = () => moodsApp.goTo('debug');
}
watchMaker.createMenu('Moods', moodText, menu, undefined, () => {
if (debugFn) debugFn();
load();
});
}
/**
* Create a queue for collecting Heart Rate data of a certain confidence level
* @param seconds
* @return {Promise<unknown>}
*/
function createHRMQueue(seconds) {
moodsApp.setState('hrmQueue', []);
const countdown = watchMaker.createCountdown(
seconds / 1000,
(left) => {
E.showMessage(`Collecting\nKeep Still...\nCancel - BTN2\n${left} seconds left`, 'Heart Rate');
LED2.write(1);
},
5
);
setWatch(() => countdown.cancel(), BTN2);
toggleHMR(true);
return countdown.start.then((cancelled) => {
toggleHMR(false);
return cancelled ? [] : moodsApp.getState('hrmQueue');
});
}
function toggleLocationCollection(on) {
Bangle.setGPSPower(on ? 1 : 0);
}
function toggleHMR(on) {
Bangle.setHRMPower(on ? 1 : 0);
}
/**
*
* @param mood
* @param level
*/
function requestHeartRate(mood, level) {
watchMaker
.createSelectPage(
'Your Mood Data',
'Do you want to collect an average heart rate? Select 30s, 60s or skip this time',
{
'30s': 30000,
'60s': 60000,
Skip: 0,
}
)
.then((seconds) => {
if (seconds === 0) {
writeMood(mood, level, 0);
} else {
createHRMQueue(seconds).then((results) => {
const average = Math.floor(results.reduce((a, b) => a + b, 0) / results.length);
writeMood(mood, level, average);
moodsApp.setState('hrmQueue', null);
});
}
});
}
/**
* Prompt the user for a more detailed mood selection
* @param forMood
*/
function promptMoodList(forMood) {
const emotions = moodsApp.getText(`emotions.${forMood}`);
if (!emotions) {
E.showAlert(`No values for ${forMood}`).then(() => moodsApp.goTo('home'));
return;
}
watchMaker
.createScrollPrompt(
`${forMood} Mood`,
'Select a more detailed emotion - use BTN1 and BTN3 to scroll',
emotions.reduce((a, k) => (a[k] = k) && a, {})
)
.then((k) => moodsApp.goTo('setMoodLevel', k));
}
/**
* Prompt the user for a general mood, this allows us to load the sub-menu of more detailed mood desriptions
*/
function promptMoodGroup() {
const collectLocation = moodsApp.getSettings('collectLocation');
if (collectLocation) {
toggleLocationCollection(true);
}
const emotions = moodsApp.getText('emotions');
E.showMessage('', 'Select An Emotion');
setTimeout(() => {
const menuButtons = Object.keys(emotions).reduce(
(buttons, key) => (buttons[key] = () => moodsApp.goTo('setMoodType', key)) && buttons,
{}
);
watchMaker.createMenu('Select Emotion', undefined, menuButtons, undefined, () => moodsApp.goTo('home'));
}, 3000);
}
/**
* Get the intensity of feeling from the user, this is on a scale of 1-5.
*
* Next ->
* - If setting `collectHRM` is true -> requestHRM
* - If setting `collectHRM` is false -> writeMood
*
* @param mood
*/
function promptMoodLevel(mood) {
watchMaker
.createSelectPage(
'Mood Intensity',
'How intensely are you feeling this mood?',
{
'5': 5,
'4': 4,
'3': 3,
'2': 2,
'1': 1,
},
2
)
.then((level) =>
moodsApp.getSettings('collectHRM') ? moodsApp.goTo('requestHRM', mood, level) : writeMood(mood, level, 0)
);
}
function showLastMoods(index, lastIndex, moods) {
const maxHistory = moodsApp.getSettings('maxHistory');
moodsApp.getData('data', maxHistory).then((moods) => {
if (moods.length === 0) {
return watchMaker
.createAlert('No Data', 'No data has been collected for Moods')
.then(() => moodsApp.goTo('home'));
}
let btnCount = 1;
const buttons = {};
if (moods.length > 1 && index !== 0) {
buttons['<'] = index - 1;
btnCount++;
}
buttons['Exit'] = -1;
if (moods.length > 1 && index !== moods.length - 1) {
buttons['>'] = index + 1;
btnCount++;
}
let selected;
switch (btnCount) {
case 3:
selected = index > lastIndex ? 2 : 0;
break;
case 2:
selected = index === 0 ? 1 : 0;
break;
default:
selected = 0;
}
const mood = moods[index];
watchMaker
.createSelectPage(
`${locale.date(new Date(mood.time), 1)}: ${locale.time(new Date(mood.time), 1)}`,
[
`Mood: ${mood.mood}`,
`Level: ${mood.level}`,
mood.bpmRate ? `Heart rate ${mood.bpmRate}bpm` : `No Heart Rate`,
mood.location !== null ? `With Location` : `No Location`,
].join('\n'),
buttons,
selected,
true
)
.then((i) => (i === -1 ? moodsApp.goTo('home') : moodsApp.goTo('showHistory', i, index, moods)));
});
}
/**
* Show the application settings menu
*/
function showSettings() {
const currentSettings = moodsApp.getSettings();
watchMaker.createMenu(
'Mood Settings',
undefined,
{
'Log Location': {
value: currentSettings.collectLocation,
format: (v) => (v ? 'On' : 'Off'),
onchange: (v) => (currentSettings.collectLocation = v),
},
'Log HRM': {
value: currentSettings.collectHRM,
format: (v) => (v ? 'On' : 'Off'),
onchange: (v) => (currentSettings.collectHRM = v),
},
'Buzz On HRM': {
value: currentSettings.buzzOnHRM,
format: (v) => (v ? 'On' : 'Off'),
onchange: (v) => (currentSettings.buzzOnHRM = v),
},
'HRM Min Conf': {
value: currentSettings.confidenceLevel,
min: 0,
max: 100,
step: 5,
onchange: (v) => (currentSettings.confidenceLevel = v),
},
'Max History': {
value: currentSettings.maxHistory,
min: 0,
max: 50,
step: 5,
onchange: (v) => (currentSettings.maxHistory = v),
},
'LCD Off Close': {
value: currentSettings.lcdOffClose,
format: (v) => (v ? 'On' : 'Off'),
onchange: (v) => (currentSettings.lcdOffClose = v),
},
},
() => {
if (!moodsApp.saveSettings(currentSettings)) {
E.showAlert('Unable to save settings').then(() => {
moodsApp.goTo('home');
});
} else {
moodsApp.goTo('home');
}
}
);
}
/**
* Write the mood to the moods file
* @param mood
* @param level
* @param rate
*/
function writeMood(mood, level, rate) {
toggleLocationCollection(false);
const currentLocation = moodsApp.getState('currentLocation');
const time = Math.round(Date.now());
const result = {
time: time,
mood: mood,
level: level,
bpmRate: rate,
location:
currentLocation && currentLocation.fix
? {
lat: currentLocation.lat,
lon: currentLocation.lon,
alt: currentLocation.alt,
}
: null,
};
moodsApp.getData('data').then((moods) => {
moods.unshift(result);
moodsApp.saveData('data', moods).then(() => {
moodsApp.setState('lastMood', result);
moodsApp.goTo('home');
});
});
}
/**
* Show the application debug menu which shows current memory usage
*/
function showDebug() {
const debugInfo = moodsApp.getState('debugInfo');
if (!debugInfo) {
watchMaker.createAlert('No Debug Info', 'No Debug info available, try again in a few seconds').then(() => {
moodsApp.goTo('home');
});
return;
}
const header = `Debug Info (${version})`;
watchMaker.createMenu(
header,
undefined,
{
'Mem % Used': { value: `${debugInfo[2]}` },
'Mem Usage': { value: `${debugInfo[1].usage}` },
'Mem Total': { value: `${debugInfo[1].total}` },
'Mem Free': { value: `${debugInfo[1].free}` },
Blocksize: { value: `${debugInfo[1].blocksize}` },
'Space Free': { value: `${debugInfo[3]}` },
'Espurino v.': { value: `${debugInfo[4]}` },
Dump: () => {
watchMaker.createMessage('Moods Debug', 'Dumping variables to console').then(() => {
E.dumpVariables();
moodsApp.goTo('debug');
});
},
Refresh: () => moodsApp.goTo('debug'),
},
() => moodsApp.goTo('home')
);
}
/**
* Initialise the application
*/
moodsApp = watchMaker.lug('moods', {
routes: {
home: { view: () => viewMainMenu() },
help: { view: (step) => showIntro(step) },
addMood: { view: () => promptMoodGroup() },
setMoodType: { view: (mood) => promptMoodList(mood) },
setMoodLevel: { view: (mood) => promptMoodLevel(mood) },
showHistory: {
view: (index, lastIndex, moods) => showLastMoods(index || 0, lastIndex, moods),
},
requestHRM: { view: (mood, level) => requestHeartRate(mood, level) },
settings: { view: () => showSettings() },
debug: { view: () => showDebug() },
},
init: (app) => {
// Fast access images
app.loadImage('heart');
// Set up global listeners
setupLCDListener(app);
setupGPSListener(app, () => LED1.write(1));
setupHMRListener(app, (currentQueue, buzz) => {
drawHeart(currentQueue, 80, 160);
if (buzz) {
app.getSettings('buzzOnHRM') && Bangle.buzz(200, 1);
}
});
// Setup global debug
const globalDebug = app.getGlobalSettings('log');
if (globalDebug) {
app.setState('globalDebug', true);
debugFn = debugInfo();
}
// Check for settings, if not there create defaults
let settings = app.getSettings();
if (!settings) {
app.saveSettings({
collectLocation: false,
collectHRM: true,
buzzOnHRM: true,
confidenceLevel: 65,
lcdOffClose: true,
maxHistory: 10,
});
app.setState('firstTime', true);
}
app.getData('data', 1).then((result) => {
app.setState('lastMood', result[0]);
});
},
/**
* Once the app is initialised, do the first transition
* @param app
*/
first: (app) => {
if (app.getState('firstTime')) {
app.goTo('help', 0);
} else {
app.goTo('home');
}
},
});