-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhrm.js
51 lines (40 loc) · 1.42 KB
/
hrm.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
var ant = require('ant-plus');
var _io = null;
var exports = module.exports = {};
exports.start = function(io) {
_io = io;
// Initialize the antenna and the heart rate sensor
var antenna = new ant.GarminStick3;
var hrm = new ant.HeartRateSensor(antenna);
if (!antenna.open()) {
console.log('ERROR: ANT+ antenna not found!');
_io.emit('hrm antenna', false);
return;
}
// When we get heart rate data, emit it
hrm.on('hbdata', function(data) {
_io.emit('hrm beat', data.ComputedHeartRate);
});
// We care if the heart rate monitor somehow disconnects
hrm.on('attached', function() {
console.log('Heart Rate Monitor attached.');
_io.emit('hrm attached', true);
});
hrm.on('detached', function() {
console.log('Heart Rate Monitor detached.');
_io.emit('hrm attached', false);
});
// Don't attached the heart rate monitor until we know the antenna is up
antenna.on('startup', function(){
console.log('Starting up ANT+ antenna...');
console.log('Max channels:', antenna.maxChannels);
_io.emit('hrm antenna', true);
console.log('Attaching to heart rate monitor...');
hrm.attach(0, 0);
});
// Warn the page if the antenna goes down
antenna.on('shutdown', function() {
console.log('Shutting down ANT+ antenna...');
_io.emit('hrm antenna', false);
});
};