Permalink
Browse files

Fix #131 and add example for daisy with hardset

  • Loading branch information...
1 parent a2b4d73 commit af9f08642ec17c69ca33e89519f75788be92e488 @aj-ptw aj-ptw committed Jan 4, 2017
View
@@ -3,6 +3,7 @@
### New Features
* New simulator option `simulatorDaisyModuleCanBeAttached` - Boolean, deafults to true, allows the simulation of the a hot swapped daisy board or simulates a misinformed module.
* New `EventEmitter` - `hardSet` - for when the module detects the board is not configured as the options for the module intended and tries to save itself. i.e. when the `daisy` option is `true` and a soft reset message is parsed and the module determines that a daisy was not detected, the module will emit `hardSet` then send an attach daisy command to recover. Either `error` will be emitted if unable to attach or `ready` will be emitted if success.
+* Add example for streaming with `daisy` and `hardSet`.
### Breaking changes
* `.setInfoForBoardType()` changed to `.overrideInfoForBoardType()` to elevate it's dangerous nature.
@@ -0,0 +1,93 @@
+/**
+ * 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({
+ boardType: 'daisy',
+ debug: debug,
+ hardSet: true,
+ verbose: verbose
+});
+
+ourBoard.on('error', (err) => {
+ console.log(err);
+});
+
+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.once('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 16: -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
+}));
@@ -0,0 +1,18 @@
+{
+ "name": "get-streaming-daisy",
+ "version": "1.0.0",
+ "description": "Get streaming with hard set daisy example",
+ "main": "getStreaming.js",
+ "scripts": {
+ "start": "node getStreamingDaisy.js",
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "keywords": [
+ "get"
+ ],
+ "author": "AJ Keller",
+ "license": "MIT",
+ "dependencies": {
+ "openbci": "^1.5.0"
+ }
+}
View
@@ -1088,10 +1088,6 @@ function OpenBCIFactory () {
return new Promise((resolve, reject) => {
const eotFunc = (data) => {
switch (data.slice(0, data.length - k.OBCIParseEOT.length).toString()) {
- case k.OBCIChannelMaxNumber8NoDaisyToRemove:
- this.overrideInfoForBoardType(k.OBCIBoardDefault);
- resolve('no daisy to remove');
- break;
case k.OBCIChannelMaxNumber8SuccessDaisyRemoved:
this.overrideInfoForBoardType(k.OBCIBoardDefault);
resolve('daisy removed');
@@ -1108,8 +1104,10 @@ function OpenBCIFactory () {
this.overrideInfoForBoardType(k.OBCIBoardDefault);
reject('unable to attach daisy');
break;
+ case k.OBCIChannelMaxNumber8NoDaisyToRemove:
default:
- reject(Error('invalid return, board may not be configured correctly.'));
+ this.overrideInfoForBoardType(k.OBCIBoardDefault);
+ resolve('no daisy to remove');
break;
}
};
@@ -756,6 +756,104 @@ describe('openbci-sdk', function () {
done();
}
});
+ it('daisy attached in soft reset, daisy requested by user in options, module is successful', function (done) {
+ if (ourBoard.isSimulating()) {
+ // Turn hardSet on
+ ourBoard.options.hardSet = true;
+ // Set the options to daisy boardType
+ ourBoard.options.boardType = k.OBCIBoardDaisy;
+ // The simulator does have a daisy
+ ourBoard.options.simulatorDaisyModuleAttached = true;
+ // The simulator is able to attach daisy
+ ourBoard.options.simulatorDaisyModuleCanBeAttached = true;
+ const failTestWithErr = (err) => {
+ ourBoard.options.hardSet = false;
+ ourBoard.disconnect().then(() => { // call disconnect
+ done(err);
+ }).catch(() => {
+ done(err);
+ });
+ };
+ var hardSetFuncFailure = () => {
+ ourBoard.removeListener('ready', readyFuncSuccess);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ failTestWithErr('should not hardSet');
+ };
+ var errorFuncTestFailure = () => {
+ ourBoard.removeListener('ready', readyFuncSuccess);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ failTestWithErr('should not error');
+ };
+ var readyFuncSuccess = () => {
+ ourBoard.removeListener('error', errorFuncTestFailure);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ // Verify the module is still default
+ expect(ourBoard.getBoardType()).to.equal(k.OBCIBoardDaisy);
+ ourBoard.options.hardSet = false;
+ ourBoard.disconnect().then(() => { // call disconnect
+ done();
+ }).catch(() => {
+ done();
+ });
+ };
+
+ ourBoard.once('error', errorFuncTestFailure);
+ ourBoard.once('ready', readyFuncSuccess);
+ ourBoard.once('hardSet', hardSetFuncFailure);
+ ourBoard.connect(masterPortName).catch(err => done(err));
+ } else {
+ done();
+ }
+ });
+ it('no daisy attached in soft reset, default requested by user in options, module is successful', function (done) {
+ if (ourBoard.isSimulating()) {
+ // Turn hardSet on
+ ourBoard.options.hardSet = true;
+ // Set the options to default boardType
+ ourBoard.options.boardType = k.OBCIBoardDefault;
+ // The simulator does not have a daisy
+ ourBoard.options.simulatorDaisyModuleAttached = false;
+ // The simulator is able to attach daisy
+ ourBoard.options.simulatorDaisyModuleCanBeAttached = false;
+ const failTestWithErr = (err) => {
+ ourBoard.options.hardSet = false;
+ ourBoard.disconnect().then(() => { // call disconnect
+ done(err);
+ }).catch(() => {
+ done(err);
+ });
+ };
+ var hardSetFuncFailure = () => {
+ ourBoard.removeListener('ready', readyFuncSuccess);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ failTestWithErr('should not hard set');
+ };
+ var errorFuncTestFailure = () => {
+ ourBoard.removeListener('ready', readyFuncSuccess);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ failTestWithErr('should not emit error');
+ };
+ var readyFuncSuccess = () => {
+ ourBoard.removeListener('error', errorFuncTestFailure);
+ ourBoard.removeListener('hardSet', hardSetFuncFailure);
+ // Verify the module is still default
+ expect(ourBoard.getBoardType()).to.equal(k.OBCIBoardDefault);
+ ourBoard.options.hardSet = false;
+ ourBoard.disconnect().then(() => { // call disconnect
+ done();
+ }).catch(() => {
+ done();
+ });
+ };
+
+ ourBoard.once('error', errorFuncTestFailure);
+ ourBoard.once('ready', readyFuncSuccess);
+ ourBoard.once('hardSet', hardSetFuncFailure);
+ ourBoard.connect(masterPortName).catch(err => done(err));
+ } else {
+ done();
+ }
+ });
});
describe('#connected', function () {
beforeEach(function (done) {

0 comments on commit af9f086

Please sign in to comment.