-
Notifications
You must be signed in to change notification settings - Fork 0
/
heart.js
59 lines (53 loc) · 1.15 KB
/
heart.js
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
import { me } from 'appbit';
import { display } from 'display';
import { HeartRateSensor } from 'heart-rate';
import { user } from 'user-profile';
let hrm, watchID, hrmCallback;
let lastReading = 0;
let heartRate;
export default function onHeartUpdate(callback) {
if (me.permissions.granted('access_heart_rate') && me.permissions.granted('access_user_profile')) {
hrmCallback = callback;
hrm = new HeartRateSensor();
setupEvents();
start();
lastReading = hrm.timestamp;
} else {
callback({
bpm: '???'
});
}
}
function getReading() {
if (hrm.timestamp === lastReading) {
heartRate = '--';
} else {
heartRate = hrm.heartRate || '--';
}
lastReading = hrm.timestamp;
hrmCallback({
bpm: heartRate,
zone: user.heartRateZone(hrm.heartRate || 0),
});
}
function setupEvents() {
display.addEventListener('change', function() {
if (display.on) {
start();
} else {
stop();
}
});
}
function start() {
if (!watchID) {
hrm.start();
getReading();
watchID = setInterval(getReading, 1000);
}
}
function stop() {
hrm.stop();
clearInterval(watchID);
watchID = null;
}