forked from openbci-archive/OpenBCI_NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenBCISimulator.js
121 lines (90 loc) · 3.44 KB
/
openBCISimulator.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
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
'use strict';
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var stream = require('stream');
var openBCISample = require('./openBCISample');
var k = openBCISample.k;
function OpenBCISimulatorFactory() {
var factory = this;
var _options = {
samplerate: 250,
daisy: false,
verbose: false
};
function OpenBCISimulator(portName, options) {
options = (typeof options !== 'function') && options || {};
var opts = {};
stream.Stream.call(this);
/** Configuring Options */
opts.sampleRate = options.sampleRate || options.samplerate || _options.samplerate;
opts.daisy = options.daisy || _options.daisy;
opts.verbose = options.verbose || _options.verbose;
this.options = opts;
// Bools
this.connected = false;
// Buffers
this.buffer = new Buffer(500);
// Numbers
this.sampleNumber = -1; // So the first sample is 0
// Strings
this.portName = portName || k.OBCISimulatorPortName;
// Call 'open'
setTimeout(() => {
console.log('Port name: ' + portName);
if (portName === k.OBCISimulatorPortName) {
this.emit('open');
this.connected = true;
} else {
var err = new Error('Serialport not open.');
this.emit('error',err);
}
}, 200);
}
// This allows us to use the emitter class freely outside of the module
util.inherits(OpenBCISimulator, stream.Stream);
OpenBCISimulator.prototype.flush = function() {
this.buffer.fill(0);
//if (this.options.verbose) console.log('flushed');
};
OpenBCISimulator.prototype.write = function(data,callback) {
if (data[0] === k.OBCIStreamStart) {
if (!this.stream) this._startStream();
} else if (data[0] === k.OBCIStreamStop) {
if (this.stream) clearInterval(this.stream); // Stops the stream
} else if (data[0] === k.OBCIMiscSoftReset) {
if (this.stream) clearInterval(this.stream);
this.emit('data', new Buffer('OpenBCI Board Simulator\nPush The World V-0.2\n$$$'));
}
/** Handle Callback */
if (this.connected) {
callback(null,'Success!');
}
};
OpenBCISimulator.prototype.drain = function(callback) {
callback();
//if (this.options.verbose) console.log('drain');
};
OpenBCISimulator.prototype.close = function(callback) {
if (this.connected) {
this.emit('close');
}
this.connected = false;
//if (this.options.verbose) console.log('close');
callback();
};
OpenBCISimulator.prototype._startStream = function() {
var intervalInMS = 1000 / this.options.sampleRate;
if (intervalInMS < 2) intervalInMS = 2;
var generateSample = openBCISample.randomSample(k.OBCINumberOfChannelsDefault, k.OBCISampleRate250);
var getNewPacket = sampNumber => {
return openBCISample.convertSampleToPacket(generateSample(sampNumber));
};
this.stream = setInterval(() => {
this.emit('data', getNewPacket(this.sampleNumber));
this.sampleNumber++;
}, intervalInMS);
};
factory.OpenBCISimulator = OpenBCISimulator;
}
util.inherits(OpenBCISimulatorFactory, EventEmitter);
module.exports = new OpenBCISimulatorFactory();