This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
brainwaves.js
68 lines (59 loc) · 2.15 KB
/
brainwaves.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
var argv = require('yargs').argv; // Yargs be a node.js library fer hearties tryin' ter parse optstrings.
var OpenBCIBoard = require('openbci-sdk');
var io = require('socket.io')(8080);
// Sockets
io.on('connection', function(socket){
console.log('A user with a brain connected');
});
var board = new OpenBCIBoard.OpenBCIBoard({
verbose: true // This is great for debugging
});
board.autoFindOpenBCIBoard()
.then(onBoardFind)
.catch(function () { // If a board is not found...
// This next part looks for a command line argument called 'simulate'
// This is specially helpful if you don't have a BCI and want to get some simulated data
if (!!(argv._[0] && argv._[0] === 'simulate')) {
board.connect(OpenBCIBoard.OpenBCIConstants.OBCISimulatorPortName)
.then(onBoardConnect);
}
});
// This function will be called when a board is found
function onBoardFind (portName) {
// The serial port's name
if (portName) {
console.log('board found', portName);
board.connect(portName)
.then(onBoardConnect);
}
}
// This function will be called when the board successfully connects
function onBoardConnect () {
board.on('ready', onBoardReady);
}
// This function will be called when the board is ready to stream data
function onBoardReady () {
board.streamStart();
board.on('sample', onSample);
}
// This function will be called every time a "sample" received from the board
// A sample is received every 4 milliseconds (holy batman!)
function onSample (sample) {
// In here we can access 'channelData' from the sample object
// 'channelData' is an array with 8 values, a value for each channel from the BCI, see example below
console.log(sample);
io.emit('brainwave', sample);
}
// This function will be called if the board is disconnected
function disconnectBoard () {
board.streamStop()
.then(function () {
board.disconnect().then(function () {
console.log('board disconnected');
process.exit();
});
});
}
process.on('SIGINT', function () {
setTimeout(disconnectBoard);
});