diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea869cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.idea +.idea/vcs.xml diff --git a/README.md b/README.md index f73c389..15fd59c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ -# brainwaves-quickstart -A quickstart template for receiving and and passing data from OpenBCI to the browser +# Brainwaves Quickstart + +A quickstart template for receiving and passing data from OpenBCI to the browser + +### Step 1 + +Install the dependencies + +``` +npm install +``` + +### Step 2 + +Simply run the app: + +``` +node brainwaves +``` + +Or in simulated more: + +``` +node brainwaves simulate +``` + +### Step 3 + +Have fun + diff --git a/brainwaves.js b/brainwaves.js new file mode 100644 index 0000000..b5e1829 --- /dev/null +++ b/brainwaves.js @@ -0,0 +1,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); +}); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..45136f4 --- /dev/null +++ b/index.html @@ -0,0 +1,27 @@ + + +
+