Skip to content

Commit

Permalink
Merge pull request #129 from aj-ptw/add-exp-debug
Browse files Browse the repository at this point in the history
Add debug and get streaming examples
  • Loading branch information
AJ Keller authored Dec 2, 2016
2 parents b9d0a46 + 13d4f57 commit c2c75fe
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The purpose of this module is to **get connected** and **start streaming** as fa
npm install openbci
```
### <a name="tldr"></a> 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;
Expand Down Expand Up @@ -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
------------
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.2

### New examples
* Add example of debug
* Add example of get streaming

# 1.4.1

### Bug Fixes
Expand Down
108 changes: 108 additions & 0 deletions examples/debug/debug.js
Original file line number Diff line number Diff line change
@@ -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
}));
18 changes: 18 additions & 0 deletions examples/debug/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
87 changes: 87 additions & 0 deletions examples/getStreaming/getStreaming.js
Original file line number Diff line number Diff line change
@@ -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
}));
18 changes: 18 additions & 0 deletions examples/getStreaming/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit c2c75fe

Please sign in to comment.