From a569fab83efa8b7539f47196ed6f31e9e7356e8c Mon Sep 17 00:00:00 2001 From: Agne Gediminskaite Date: Tue, 9 Jan 2018 20:23:12 +0100 Subject: [PATCH 1/2] Impedance test fix for simulator with daisy module --- .../impedanceTestDaisy/impedanceTestDaisy.js | 75 +++++++++++++++++++ examples/impedanceTestDaisy/package.json | 17 +++++ openBCICyton.js | 16 ++-- 3 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 examples/impedanceTestDaisy/impedanceTestDaisy.js create mode 100644 examples/impedanceTestDaisy/package.json diff --git a/examples/impedanceTestDaisy/impedanceTestDaisy.js b/examples/impedanceTestDaisy/impedanceTestDaisy.js new file mode 100644 index 0000000..20ddb25 --- /dev/null +++ b/examples/impedanceTestDaisy/impedanceTestDaisy.js @@ -0,0 +1,75 @@ +/** + * 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` + */ +const debug = false; // Pretty print any bytes in and out... it's amazing... +const verbose = true; // Adds verbosity to functions + +const Cyton = require('../../openBCICyton'); +var ourBoard = new Cyton({ + boardType: 'daisy', + debug: debug, + hardSet: true, + verbose: verbose, + simulate: true, + simulatorDaisyModuleAttached: true, + simulatorSampleRate: 125 +}); + +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.impedanceTestAllChannels(); + ourBoard.on('impedanceArray', impedanceArray => { + console.log(impedanceArray); + ourBoard.impedanceTestAllChannels(); + }); + }); + }); + } 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'); + ourBoard.removeAllListeners(); + /** Do additional clean up here */ + } + if (err) console.log(err.stack); + if (options.exit) { + if (verbose) console.log('exit'); + ourBoard.disconnect().catch(console.log); + } +} + +// do something when app is closing +process.on('exit', exitHandler.bind(null, { + cleanup: true +})); + +// catches uncaught exceptions +process.on('uncaughtException', exitHandler.bind(null, { + exit: true +})); diff --git a/examples/impedanceTestDaisy/package.json b/examples/impedanceTestDaisy/package.json new file mode 100644 index 0000000..14a3ab4 --- /dev/null +++ b/examples/impedanceTestDaisy/package.json @@ -0,0 +1,17 @@ +{ + "name": "impedance-test-daisy", + "version": "1.0.0", + "description": "Impedance test function with daisy example", + "main": "impedanceTestDaisy.js", + "scripts": { + "start": "node impedanceTestDaisy.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "get" + ], + "license": "MIT", + "dependencies": { + "openbci": "^2.0.0" + } +} diff --git a/openBCICyton.js b/openBCICyton.js index b65ad1a..5bf7b4a 100644 --- a/openBCICyton.js +++ b/openBCICyton.js @@ -1396,7 +1396,7 @@ Cyton.prototype.impedanceTestAllChannels = function () { let upperLimit = k.OBCINumberOfChannelsCyton; /* istanbul ignore if */ - if (this.options.daisy) { + if (this.options.boardType == k.OBCIBoardDaisy) { upperLimit = k.OBCINumberOfChannelsDaisy; } @@ -2165,13 +2165,18 @@ Cyton.prototype._finalizeNewSample = function (sampleObject) { this.badPackets++; this.emit(k.OBCIEmitterDroppedPacket, [this.previousSampleNumber + 1]); } else if (this.impedanceTest.active) { - this._processImpedanceTest(sampleObject); + if (_.eq(this.getBoardType(), k.OBCIBoardDaisy)) { + this._finalizeNewSampleForDaisy(sampleObject, true); + } else { + this._processImpedanceTest(sampleObject); + this.emit(k.OBCIEmitterSample, sampleObject); + } } else { // With the daisy board attached, lower channels (1-8) come in packets with odd sample numbers and upper // channels (9-16) come in packets with even sample numbers if (_.eq(this.getBoardType(), k.OBCIBoardDaisy)) { // Send the sample for downstream sample compaction - this._finalizeNewSampleForDaisy(sampleObject); + this._finalizeNewSampleForDaisy(sampleObject, false); } else { this.emit(k.OBCIEmitterSample, sampleObject); } @@ -2188,7 +2193,7 @@ Cyton.prototype._finalizeNewSample = function (sampleObject) { * @private * @author AJ Keller (@pushtheworldllc) */ -Cyton.prototype._finalizeNewSampleForDaisy = function (sampleObject) { +Cyton.prototype._finalizeNewSampleForDaisy = function (sampleObject, impedance) { if (obciUtils.isOdd(sampleObject.sampleNumber)) { // Check for the skipped packet condition if (this._lowerChannelsSampleObject) { @@ -2203,7 +2208,8 @@ Cyton.prototype._finalizeNewSampleForDaisy = function (sampleObject) { let mergedSample = obciUtils.makeDaisySampleObject(this._lowerChannelsSampleObject, sampleObject); // Set the _lowerChannelsSampleObject object to null this._lowerChannelsSampleObject = null; - // Emite the new merged sample + // Emite the new merged sample and if relevant process impedance + if(impedance) this._processImpedanceTest(mergedSample); this.emit('sample', mergedSample); } else { // Missed the odd packet, i.e. two evens in a row From 4b30d9bd7efb8e51d068f0477e8287f8e191c440 Mon Sep 17 00:00:00 2001 From: AJ Keller Date: Fri, 19 Jan 2018 11:38:57 -0500 Subject: [PATCH 2/2] FIX: Linting issues --- openBCICyton.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openBCICyton.js b/openBCICyton.js index 5bf7b4a..8effd2b 100644 --- a/openBCICyton.js +++ b/openBCICyton.js @@ -1396,7 +1396,7 @@ Cyton.prototype.impedanceTestAllChannels = function () { let upperLimit = k.OBCINumberOfChannelsCyton; /* istanbul ignore if */ - if (this.options.boardType == k.OBCIBoardDaisy) { + if (this.options.boardType === k.OBCIBoardDaisy) { upperLimit = k.OBCINumberOfChannelsDaisy; } @@ -2209,7 +2209,7 @@ Cyton.prototype._finalizeNewSampleForDaisy = function (sampleObject, impedance) // Set the _lowerChannelsSampleObject object to null this._lowerChannelsSampleObject = null; // Emite the new merged sample and if relevant process impedance - if(impedance) this._processImpedanceTest(mergedSample); + if (impedance) this._processImpedanceTest(mergedSample); this.emit('sample', mergedSample); } else { // Missed the odd packet, i.e. two evens in a row