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+ } ) ) ;
0 commit comments