-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from aj-ptw/add-exp-debug
Add debug and get streaming examples
- Loading branch information
Showing
6 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |