Skip to content

Commit c2c75fe

Browse files
author
AJ Keller
authored
Merge pull request #129 from aj-ptw/add-exp-debug
Add debug and get streaming examples
2 parents b9d0a46 + 13d4f57 commit c2c75fe

File tree

6 files changed

+247
-1
lines changed

6 files changed

+247
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The purpose of this module is to **get connected** and **start streaming** as fa
3838
npm install openbci
3939
```
4040
### <a name="tldr"></a> TL;DR:
41-
Get connected and start streaming right now
41+
Get connected and [start streaming right now with the example code](examples/getStreaming/getStreaming.js).
4242

4343
```js
4444
var OpenBCIBoard = require('openbci').OpenBCIBoard;
@@ -118,6 +118,15 @@ ourBoard.connect(k.OBCISimulatorPortName) // This will set `simulate` to true
118118
});
119119
```
120120

121+
To debug, it's amazing, do:
122+
123+
```js
124+
var OpenBCIBoard = require('openbci').OpenBCIBoard;
125+
var ourBoard = new OpenBCIBoard({
126+
simulate: true
127+
});
128+
```
129+
ps: go [checkout out the example](examples/debug/debug.js) to do it right now!
121130

122131
'ready' event
123132
------------

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.4.2
2+
3+
### New examples
4+
* Add example of debug
5+
* Add example of get streaming
6+
17
# 1.4.1
28

39
### Bug Fixes

examples/debug/debug.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* This is an example of debugging the board. Thanks to Karl @baffo32
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+
13+
var stream = true;
14+
var debug = true; // Pretty print any bytes in and out... it's amazing...
15+
var verbose = true; // Adds verbosity to functions
16+
var OpenBCIBoard = require('openbci').OpenBCIBoard;
17+
18+
var ourBoard = new OpenBCIBoard({
19+
debug: debug,
20+
verbose: verbose
21+
});
22+
23+
ourBoard.autoFindOpenBCIBoard().then(portName => {
24+
if (portName) {
25+
/**
26+
* Connect to the board with portName
27+
* Only works if one board is plugged in
28+
* i.e. ourBoard.connect(portName).....
29+
*/
30+
// Call to connect
31+
ourBoard.connect(portName).then(() => {
32+
console.log(`connected`);
33+
})
34+
.catch(err => {
35+
console.log(`connect: ${err}`);
36+
});
37+
} else {
38+
/** Unable to auto find OpenBCI board */
39+
console.log('Unable to auto find OpenBCI board');
40+
}
41+
});
42+
43+
/**
44+
* The board is ready to start streaming after the ready function is fired.
45+
*/
46+
var readyFunc = () => {
47+
// Get the sample rate after 'ready'
48+
sampleRate = ourBoard.sampleRate();
49+
if (stream) {
50+
ourBoard.streamStart()
51+
.catch(err => {
52+
console.log(`stream start: ${err}`);
53+
});
54+
}
55+
};
56+
57+
var sampleFunc = sample => {
58+
/**
59+
* Checkout the README.md for all other API functions.
60+
* We support every feature.
61+
* */
62+
};
63+
64+
// Subscribe to your functions
65+
ourBoard.on('ready', readyFunc);
66+
ourBoard.on('sample', sampleFunc);
67+
68+
69+
function exitHandler (options, err) {
70+
if (options.cleanup) {
71+
if (verbose) console.log('clean');
72+
/** Do additional clean up here */
73+
}
74+
if (err) console.log(err.stack);
75+
if (options.exit) {
76+
if (verbose) console.log('exit');
77+
if (stream) {
78+
ourBoard.streamStop().catch(console.log);
79+
}
80+
ourBoard.disconnect().catch(console.log);
81+
}
82+
}
83+
84+
if (process.platform === "win32") {
85+
const rl = require("readline").createInterface({
86+
input: process.stdin,
87+
output: process.stdout
88+
});
89+
90+
rl.on("SIGINT", function () {
91+
process.emit("SIGINT");
92+
});
93+
}
94+
95+
// do something when app is closing
96+
process.on('exit', exitHandler.bind(null, {
97+
cleanup: true
98+
}));
99+
100+
// catches ctrl+c event
101+
process.on('SIGINT', exitHandler.bind(null, {
102+
exit: true
103+
}));
104+
105+
// catches uncaught exceptions
106+
process.on('uncaughtException', exitHandler.bind(null, {
107+
exit: true
108+
}));

examples/debug/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "debug",
3+
"version": "1.0.0",
4+
"description": "Debug example",
5+
"main": "debug.js",
6+
"scripts": {
7+
"start": "node debug.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [
11+
"debug"
12+
],
13+
"author": "AJ Keller",
14+
"license": "MIT",
15+
"dependencies": {
16+
"openbci": "^1.4.1"
17+
}
18+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
21+
ourBoard.autoFindOpenBCIBoard().then(portName => {
22+
if (portName) {
23+
/**
24+
* Connect to the board with portName
25+
* Only works if one board is plugged in
26+
* i.e. ourBoard.connect(portName).....
27+
*/
28+
ourBoard.connect(portName) // Port name is a serial port name, see `.listPorts()`
29+
.then(() => {
30+
ourBoard.on('ready',() => {
31+
ourBoard.streamStart();
32+
ourBoard.on('sample',(sample) => {
33+
/** Work with sample */
34+
for (var i = 0; i < ourBoard.numberOfChannels(); i++) {
35+
console.log("Channel " + (i + 1) + ": " + sample.channelData[i].toFixed(8) + " Volts.");
36+
// prints to the console
37+
// "Channel 1: 0.00001987 Volts."
38+
// "Channel 2: 0.00002255 Volts."
39+
// ...
40+
// "Channel 8: -0.00001875 Volts."
41+
}
42+
});
43+
});
44+
});
45+
} else {
46+
/** Unable to auto find OpenBCI board */
47+
console.log('Unable to auto find OpenBCI board');
48+
}
49+
});
50+
51+
function exitHandler (options, err) {
52+
if (options.cleanup) {
53+
if (verbose) console.log('clean');
54+
/** Do additional clean up here */
55+
}
56+
if (err) console.log(err.stack);
57+
if (options.exit) {
58+
if (verbose) console.log('exit');
59+
ourBoard.disconnect().catch(console.log);
60+
}
61+
}
62+
63+
if (process.platform === "win32") {
64+
const rl = require("readline").createInterface({
65+
input: process.stdin,
66+
output: process.stdout
67+
});
68+
69+
rl.on("SIGINT", function () {
70+
process.emit("SIGINT");
71+
});
72+
}
73+
74+
// do something when app is closing
75+
process.on('exit', exitHandler.bind(null, {
76+
cleanup: true
77+
}));
78+
79+
// catches ctrl+c event
80+
process.on('SIGINT', exitHandler.bind(null, {
81+
exit: true
82+
}));
83+
84+
// catches uncaught exceptions
85+
process.on('uncaughtException', exitHandler.bind(null, {
86+
exit: true
87+
}));

examples/getStreaming/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "get-streaming",
3+
"version": "1.0.0",
4+
"description": "Get streaming example",
5+
"main": "getStreaming.js",
6+
"scripts": {
7+
"start": "node getStreaming.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [
11+
"get"
12+
],
13+
"author": "AJ Keller",
14+
"license": "MIT",
15+
"dependencies": {
16+
"openbci": "^1.4.1"
17+
}
18+
}

0 commit comments

Comments
 (0)