-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
90 lines (81 loc) · 2.25 KB
/
index.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
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
// Requires
var util = require('util');
var EventEmitter = require('events').EventEmitter;
// Constructor function
function PulseSensor (hardware, callback) {
var self = this;
// Set properties
self.ready = false;
self.hardware = hardware; // Hardware should be a specific pin for PulseSensor
self.pollRate = 10; // Interval to read sensor
self.arraySize = 3; // // BPM is calculated based on the average of an array of time differences. Increasing the array size smooths the data.
self.array = [];
self.BPM = 0; // Starts at 0, samples
console.log('Collecting samples...');
// Emit a time at each beat
var high = false;
setInterval(function () {
if(self.readRaw() == 1) {
if(!high) {
high = true;
var time = new Date(milliseconds);
// TODO: add filtering
self.emit('beat', time);
}
} else {
high = false;
}
}, pollRate);
// Collect for BPM
var oldTime = new Date(milliseconds);
self.on('beat', function (time) {
var diff = time - oldTime
// Check that the data is reasonable
if (diff > 40 && diff < 5000) {
self.array.push(time - oldTime);
}
oldTime = time;
if(self.array.length > self.arraySize) {
self.array.shift();
var sum = 0;
for (var i = 0; i < self.array.length; i++) {
sum += self.array[i];
}
var avg = sum/self.array.length;
self.BPM = 60000/avg;
if (!self.ready) {
if (self.BPM > 0) {
self.ready = true;
// Emit the ready event
setImmediate(function emitReady() {
self.emit('ready', self);
if(callback) {
callback(null, self);
}
});
}
}
}
});
}
// Inherit event emission
util.inherits(PulseSensor, EventEmitter);
// Functions
// Read the state of the pin
PulseSensor.prototype.readRaw = function (callback) {
if(callback) {
callback(this.hardware.read());
}
return this.hardware.read();
};
// Standard Tessel use function
function use (hardware, callback) {
if (!hardware) {
// Set default configuration
hardware = require('tessel').port['GPIO'].pin['A1'];
}
return new PulseSensor(hardware, callback);
}
// Exports
exports.PulseSensor = PulseSensor;
exports.use = use;