|
| 1 | +/** |
| 2 | +* This is an example of time syncing every second for one minute. Used with a |
| 3 | +* real board. |
| 4 | +* To run: |
| 5 | +* change directory to this file `cd examples/timeSync` |
| 6 | +* do `npm install` |
| 7 | +* then `npm start` |
| 8 | +*/ |
| 9 | + |
| 10 | +var OpenBCIBoard = require('openbci').OpenBCIBoard; |
| 11 | + |
| 12 | +var ourBoard = new OpenBCIBoard({}); |
| 13 | + |
| 14 | +const resyncPeriodMin = 5; // re sync every five minutes |
| 15 | +const secondsInMinute = 60; |
| 16 | +var sampleRate = 250; // Default to 250, ALWAYS verify with a call to `.sampleRate()` after 'ready' event! |
| 17 | +var timeSyncPossible = false; |
| 18 | + |
| 19 | +ourBoard.autoFindOpenBCIBoard().then(portName => { |
| 20 | + if(portName) { |
| 21 | + /** |
| 22 | + * Connect to the board with portName |
| 23 | + * i.e. ourBoard.connect(portName)..... |
| 24 | + */ |
| 25 | + // Call to connect |
| 26 | + ourBoard.connect(portName).then(() => { |
| 27 | + console.log(`connected`); |
| 28 | + |
| 29 | + }) |
| 30 | + .catch(err => { |
| 31 | + console.log(`connect: ${err}`); |
| 32 | + }); |
| 33 | + } else { |
| 34 | + /**Unable to auto find OpenBCI board*/ |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +var readyFunc = () => { |
| 39 | + // Get the sample rate after 'ready' |
| 40 | + sampleRate = ourBoard.sampleRate(); |
| 41 | + // Find out if you can even time sync, you must be using v2 and this is only accurate after a `.softReset()` call which is called internally on `.connect()`. We parse the `.softReset()` response for the presence of firmware version 2 properties. |
| 42 | + timeSyncPossible = ourBoard.usingVersionTwoFirmware(); |
| 43 | + |
| 44 | + if (timeSyncPossible) { |
| 45 | + ourBoard.streamStart() |
| 46 | + .catch(err => { |
| 47 | + console.log(`stream start: ${err}`); |
| 48 | + }); |
| 49 | + } else { |
| 50 | + killFunc(); |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +var killFunc = () => { |
| 56 | + ourBoard.disconnect() |
| 57 | + .then(() => { |
| 58 | + process.kill(); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +var sampleFunc = sample => { |
| 63 | + // Resynchronize every every second |
| 64 | + if (sample._count % (sampleRate * 1) === 0) { |
| 65 | + ourBoard.syncClocksFull() |
| 66 | + .then(syncObj => { |
| 67 | + // Sync was successful |
| 68 | + if (syncObj.valid) { |
| 69 | + // Log the object to check it out! |
| 70 | + console.log(`timeOffset`,syncObj.timeOffsetMaster); |
| 71 | + } else { |
| 72 | + // Retry it |
| 73 | + console.log(`Was not able to sync... retry!`); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + if (sample.timeStamp) { // true after the first successful sync |
| 79 | + if (sample.timeStamp < 10 * 60 * 60 * 1000) { // Less than 10 hours |
| 80 | + console.log(`Bad time sync ${sample.timeStamp}`); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Stop after one minute |
| 85 | + if (sample._count > sampleRate * 60) { |
| 86 | + killFunc(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +// Subscribe to your functions |
| 92 | +ourBoard.on('ready',readyFunc); |
| 93 | +ourBoard.on('sample',sampleFunc); |
0 commit comments