|
| 1 | +/** |
| 2 | + * This is an example from the readme.md |
| 3 | + * On windows you should run with PowerShell not git bash. |
| 4 | + * Install |
| 5 | + * [nodejs](https://nodejs.org/en/) |
| 6 | + * |
| 7 | + * To run: |
| 8 | + * change directory to this file `cd examples/debug` |
| 9 | + * do `npm install` |
| 10 | + * then `npm start` |
| 11 | + */ |
| 12 | +var debug = false; // Pretty print any bytes in and out... it's amazing... |
| 13 | +var verbose = true; // Adds verbosity to functions |
| 14 | + |
| 15 | +var OpenBCIBoard = require('openbci').OpenBCIBoard; |
| 16 | +var ourBoard = new OpenBCIBoard({ |
| 17 | + debug: debug, |
| 18 | + verbose: verbose |
| 19 | +}); |
| 20 | +var k = require('openbci').OpenBCIConstants; |
| 21 | + |
| 22 | +let startedImpedance = false; |
| 23 | +let iBuffer = []; |
| 24 | +let count = 0; |
| 25 | +const window = 50; |
| 26 | + |
| 27 | +ourBoard.autoFindOpenBCIBoard().then(portName => { |
| 28 | + if (portName) { |
| 29 | + /** |
| 30 | + * Connect to the board with portName |
| 31 | + * Only works if one board is plugged in |
| 32 | + * i.e. ourBoard.connect(portName)..... |
| 33 | + */ |
| 34 | + ourBoard.connect(portName) // Port name is a serial port name, see `.listPorts()` |
| 35 | + .then(() => { |
| 36 | + ourBoard.on('ready', () => { |
| 37 | + ourBoard.streamStart(); |
| 38 | + ourBoard.on('sample', (sample) => { |
| 39 | + if (startedImpedance === false) { |
| 40 | + startedImpedance = true; |
| 41 | + k.getImpedanceSetter(1, false, true) |
| 42 | + .then((commands) => { |
| 43 | + return ourBoard.write(commands); |
| 44 | + }) |
| 45 | + .then(() => { |
| 46 | + console.log('wrote commands to board'); |
| 47 | + }) |
| 48 | + .catch((err) => { |
| 49 | + console.log('errr', err); |
| 50 | + }); |
| 51 | + } |
| 52 | + /** Work with sample */ |
| 53 | + const chan1ValInVolts = sample.channelData[0]; |
| 54 | + |
| 55 | + // const impedance = chan1ValInVolts / k.OBCILeadOffDriveInAmps; |
| 56 | + |
| 57 | + // console.log(`impedance:\t${impedance} kOhms`); |
| 58 | + iBuffer.push(chan1ValInVolts); |
| 59 | + count++; |
| 60 | + if (count >= window) { |
| 61 | + let max = 0.0; // sumSquared |
| 62 | + for (let i = 0; i < window; i++) { |
| 63 | + if (iBuffer[i] > max) { |
| 64 | + max = iBuffer[i]; |
| 65 | + } |
| 66 | + // sumSquared += iBuffer[i] * iBuffer[i]; |
| 67 | + } |
| 68 | + let min = 0.0; |
| 69 | + for (let i = 0; i < window; i++) { |
| 70 | + if (iBuffer[i] < min) { |
| 71 | + min = iBuffer[i]; |
| 72 | + } |
| 73 | + // sumSquared += iBuffer[i] * iBuffer[i]; |
| 74 | + } |
| 75 | + const vP2P = max - min; // peak to peak |
| 76 | + |
| 77 | + console.log(`impedance: \t${vP2P / 2 / k.OBCILeadOffDriveInAmps}`); |
| 78 | + // console.log(`impedance: ${vRms/k.OBCILeadOffDriveInAmps}`); |
| 79 | + |
| 80 | + // const mean_squared = sumSquared / window; |
| 81 | + // const root_mean_squared = Math.sqrt(mean_squared); |
| 82 | + // console.log(`impedance: ${root_mean_squared/k.OBCILeadOffDriveInAmps}`); |
| 83 | + |
| 84 | + count = 0; |
| 85 | + iBuffer = []; |
| 86 | + } |
| 87 | + // console.log(`uV:\t${chan1ValInVolts/(10*6)}\nimpedance:\t${impedance}`); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + } else { |
| 92 | + /** Unable to auto find OpenBCI board */ |
| 93 | + console.log('Unable to auto find OpenBCI board'); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +function exitHandler (options, err) { |
| 98 | + if (options.cleanup) { |
| 99 | + if (verbose) console.log('clean'); |
| 100 | + /** Do additional clean up here */ |
| 101 | + ourBoard.disconnect().catch(console.log); |
| 102 | + ourBoard.removeAllListeners(); |
| 103 | + } |
| 104 | + if (err) console.log(err.stack); |
| 105 | + if (options.exit) { |
| 106 | + if (verbose) console.log('exit'); |
| 107 | + ourBoard.streamStop() |
| 108 | + .then(() => { |
| 109 | + process.exit(0); |
| 110 | + }) |
| 111 | + .catch((err) => { |
| 112 | + console.log(err); |
| 113 | + process.exit(0); |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +if (process.platform === 'win32') { |
| 119 | + const rl = require('readline').createInterface({ |
| 120 | + input: process.stdin, |
| 121 | + output: process.stdout |
| 122 | + }); |
| 123 | + |
| 124 | + rl.on('SIGINT', function () { |
| 125 | + process.emit('SIGINT'); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +// do something when app is closing |
| 130 | +process.on('exit', exitHandler.bind(null, { |
| 131 | + cleanup: true |
| 132 | +})); |
| 133 | + |
| 134 | +// catches ctrl+c event |
| 135 | +process.on('SIGINT', exitHandler.bind(null, { |
| 136 | + exit: true |
| 137 | +})); |
| 138 | + |
| 139 | +// catches uncaught exceptions |
| 140 | +process.on('uncaughtException', exitHandler.bind(null, { |
| 141 | + exit: true |
| 142 | +})); |
0 commit comments