From 13d4f570035796ca45503747f90be255a3c6eb96 Mon Sep 17 00:00:00 2001 From: AJ Keller Date: Fri, 2 Dec 2016 00:20:42 -0500 Subject: [PATCH] Add debug and get streaming examples --- README.md | 11 ++- changelog.md | 6 ++ examples/debug/debug.js | 108 ++++++++++++++++++++++++++ examples/debug/package.json | 18 +++++ examples/getStreaming/getStreaming.js | 87 +++++++++++++++++++++ examples/getStreaming/package.json | 18 +++++ 6 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 examples/debug/debug.js create mode 100644 examples/debug/package.json create mode 100644 examples/getStreaming/getStreaming.js create mode 100644 examples/getStreaming/package.json diff --git a/README.md b/README.md index 76335ee..48513c1 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The purpose of this module is to **get connected** and **start streaming** as fa npm install openbci ``` ### TL;DR: -Get connected and start streaming right now +Get connected and [start streaming right now with the example code](examples/getStreaming/getStreaming.js). ```js var OpenBCIBoard = require('openbci').OpenBCIBoard; @@ -118,6 +118,15 @@ ourBoard.connect(k.OBCISimulatorPortName) // This will set `simulate` to true }); ``` +To debug, it's amazing, do: + +```js +var OpenBCIBoard = require('openbci').OpenBCIBoard; +var ourBoard = new OpenBCIBoard({ + simulate: true +}); +``` +ps: go [checkout out the example](examples/debug/debug.js) to do it right now! 'ready' event ------------ diff --git a/changelog.md b/changelog.md index 757d6a8..86972b8 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +# 1.4.2 + +### New examples +* Add example of debug +* Add example of get streaming + # 1.4.1 ### Bug Fixes diff --git a/examples/debug/debug.js b/examples/debug/debug.js new file mode 100644 index 0000000..846c510 --- /dev/null +++ b/examples/debug/debug.js @@ -0,0 +1,108 @@ +/** + * This is an example of debugging the board. Thanks to Karl @baffo32 + * On windows you should run with PowerShell not git bash. + * Install + * [nodejs](https://nodejs.org/en/) + * + * To run: + * change directory to this file `cd examples/debug` + * do `npm install` + * then `npm start` + */ + +var stream = true; +var debug = true; // Pretty print any bytes in and out... it's amazing... +var verbose = true; // Adds verbosity to functions +var OpenBCIBoard = require('openbci').OpenBCIBoard; + +var ourBoard = new OpenBCIBoard({ + debug: debug, + verbose: verbose +}); + +ourBoard.autoFindOpenBCIBoard().then(portName => { + if (portName) { + /** + * Connect to the board with portName + * Only works if one board is plugged in + * i.e. ourBoard.connect(portName)..... + */ + // Call to connect + ourBoard.connect(portName).then(() => { + console.log(`connected`); + }) + .catch(err => { + console.log(`connect: ${err}`); + }); + } else { + /** Unable to auto find OpenBCI board */ + console.log('Unable to auto find OpenBCI board'); + } +}); + +/** + * The board is ready to start streaming after the ready function is fired. + */ +var readyFunc = () => { + // Get the sample rate after 'ready' + sampleRate = ourBoard.sampleRate(); + if (stream) { + ourBoard.streamStart() + .catch(err => { + console.log(`stream start: ${err}`); + }); + } +}; + +var sampleFunc = sample => { + /** + * Checkout the README.md for all other API functions. + * We support every feature. + * */ +}; + +// Subscribe to your functions +ourBoard.on('ready', readyFunc); +ourBoard.on('sample', sampleFunc); + + +function exitHandler (options, err) { + if (options.cleanup) { + if (verbose) console.log('clean'); + /** Do additional clean up here */ + } + if (err) console.log(err.stack); + if (options.exit) { + if (verbose) console.log('exit'); + if (stream) { + ourBoard.streamStop().catch(console.log); + } + ourBoard.disconnect().catch(console.log); + } +} + +if (process.platform === "win32") { + const rl = require("readline").createInterface({ + input: process.stdin, + output: process.stdout + }); + + rl.on("SIGINT", function () { + process.emit("SIGINT"); + }); +} + +// do something when app is closing +process.on('exit', exitHandler.bind(null, { + cleanup: true +})); + +// catches ctrl+c event +process.on('SIGINT', exitHandler.bind(null, { + exit: true +})); + +// catches uncaught exceptions +process.on('uncaughtException', exitHandler.bind(null, { + exit: true +})); \ No newline at end of file diff --git a/examples/debug/package.json b/examples/debug/package.json new file mode 100644 index 0000000..3260ca9 --- /dev/null +++ b/examples/debug/package.json @@ -0,0 +1,18 @@ +{ + "name": "debug", + "version": "1.0.0", + "description": "Debug example", + "main": "debug.js", + "scripts": { + "start": "node debug.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "debug" + ], + "author": "AJ Keller", + "license": "MIT", + "dependencies": { + "openbci": "^1.4.1" + } +} diff --git a/examples/getStreaming/getStreaming.js b/examples/getStreaming/getStreaming.js new file mode 100644 index 0000000..b565327 --- /dev/null +++ b/examples/getStreaming/getStreaming.js @@ -0,0 +1,87 @@ +/** + * This is an example from the readme.md + * On windows you should run with PowerShell not git bash. + * Install + * [nodejs](https://nodejs.org/en/) + * + * To run: + * change directory to this file `cd examples/debug` + * do `npm install` + * then `npm start` + */ +var debug = false; // Pretty print any bytes in and out... it's amazing... +var verbose = true; // Adds verbosity to functions + +var OpenBCIBoard = require('openbci').OpenBCIBoard; +var ourBoard = new OpenBCIBoard({ + debug: debug, + verbose: verbose +}); + +ourBoard.autoFindOpenBCIBoard().then(portName => { + if (portName) { + /** + * Connect to the board with portName + * Only works if one board is plugged in + * i.e. ourBoard.connect(portName)..... + */ + ourBoard.connect(portName) // Port name is a serial port name, see `.listPorts()` + .then(() => { + ourBoard.on('ready',() => { + ourBoard.streamStart(); + ourBoard.on('sample',(sample) => { + /** Work with sample */ + for (var i = 0; i < ourBoard.numberOfChannels(); i++) { + console.log("Channel " + (i + 1) + ": " + sample.channelData[i].toFixed(8) + " Volts."); + // prints to the console + // "Channel 1: 0.00001987 Volts." + // "Channel 2: 0.00002255 Volts." + // ... + // "Channel 8: -0.00001875 Volts." + } + }); + }); + }); + } else { + /** Unable to auto find OpenBCI board */ + console.log('Unable to auto find OpenBCI board'); + } +}); + +function exitHandler (options, err) { + if (options.cleanup) { + if (verbose) console.log('clean'); + /** Do additional clean up here */ + } + if (err) console.log(err.stack); + if (options.exit) { + if (verbose) console.log('exit'); + ourBoard.disconnect().catch(console.log); + } +} + +if (process.platform === "win32") { + const rl = require("readline").createInterface({ + input: process.stdin, + output: process.stdout + }); + + rl.on("SIGINT", function () { + process.emit("SIGINT"); + }); +} + +// do something when app is closing +process.on('exit', exitHandler.bind(null, { + cleanup: true +})); + +// catches ctrl+c event +process.on('SIGINT', exitHandler.bind(null, { + exit: true +})); + +// catches uncaught exceptions +process.on('uncaughtException', exitHandler.bind(null, { + exit: true +})); \ No newline at end of file diff --git a/examples/getStreaming/package.json b/examples/getStreaming/package.json new file mode 100644 index 0000000..b3c56a1 --- /dev/null +++ b/examples/getStreaming/package.json @@ -0,0 +1,18 @@ +{ + "name": "get-streaming", + "version": "1.0.0", + "description": "Get streaming example", + "main": "getStreaming.js", + "scripts": { + "start": "node getStreaming.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "get" + ], + "author": "AJ Keller", + "license": "MIT", + "dependencies": { + "openbci": "^1.4.1" + } +}