-
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.
- Loading branch information
AJ Keller
committed
Sep 29, 2016
1 parent
2528d56
commit fe2abac
Showing
8 changed files
with
126 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
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,19 @@ | ||
{ | ||
"name": "timesync", | ||
"version": "1.0.0", | ||
"description": "Time sync example", | ||
"main": "timeSync.js", | ||
"scripts": { | ||
"start": "node timeSync.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"time", | ||
"sync" | ||
], | ||
"author": "AJ Keller", | ||
"license": "MIT", | ||
"dependencies": { | ||
"openbci": "^1.3.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,93 @@ | ||
/** | ||
* This is an example of time syncing every second for one minute. Used with a | ||
* real board. | ||
* To run: | ||
* change directory to this file `cd examples/timeSync` | ||
* do `npm install` | ||
* then `npm start` | ||
*/ | ||
|
||
var OpenBCIBoard = require('openbci').OpenBCIBoard; | ||
|
||
var ourBoard = new OpenBCIBoard({}); | ||
|
||
const resyncPeriodMin = 5; // re sync every five minutes | ||
const secondsInMinute = 60; | ||
var sampleRate = 250; // Default to 250, ALWAYS verify with a call to `.sampleRate()` after 'ready' event! | ||
var timeSyncPossible = false; | ||
|
||
ourBoard.autoFindOpenBCIBoard().then(portName => { | ||
if(portName) { | ||
/** | ||
* Connect to the board with portName | ||
* 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*/ | ||
} | ||
}); | ||
|
||
var readyFunc = () => { | ||
// Get the sample rate after 'ready' | ||
sampleRate = ourBoard.sampleRate(); | ||
// 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. | ||
timeSyncPossible = ourBoard.usingVersionTwoFirmware(); | ||
|
||
if (timeSyncPossible) { | ||
ourBoard.streamStart() | ||
.catch(err => { | ||
console.log(`stream start: ${err}`); | ||
}); | ||
} else { | ||
killFunc(); | ||
} | ||
|
||
} | ||
|
||
var killFunc = () => { | ||
ourBoard.disconnect() | ||
.then(() => { | ||
process.kill(); | ||
}); | ||
} | ||
|
||
var sampleFunc = sample => { | ||
// Resynchronize every every second | ||
if (sample._count % (sampleRate * 1) === 0) { | ||
ourBoard.syncClocksFull() | ||
.then(syncObj => { | ||
// Sync was successful | ||
if (syncObj.valid) { | ||
// Log the object to check it out! | ||
console.log(`timeOffset`,syncObj.timeOffsetMaster); | ||
} else { | ||
// Retry it | ||
console.log(`Was not able to sync... retry!`); | ||
} | ||
}); | ||
} | ||
|
||
if (sample.timeStamp) { // true after the first successful sync | ||
if (sample.timeStamp < 10 * 60 * 60 * 1000) { // Less than 10 hours | ||
console.log(`Bad time sync ${sample.timeStamp}`); | ||
} | ||
} | ||
|
||
// Stop after one minute | ||
if (sample._count > sampleRate * 60) { | ||
killFunc(); | ||
} | ||
|
||
} | ||
|
||
// Subscribe to your functions | ||
ourBoard.on('ready',readyFunc); | ||
ourBoard.on('sample',sampleFunc); |
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
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