@@ -251,11 +251,11 @@ function OpenBCIFactory() {
251251
252252 if ( this . options . verbose ) console . log ( 'Serial port connected' ) ;
253253
254- boardSerial . on ( 'data' , ( data ) => {
254+ boardSerial . on ( 'data' , data => {
255255 this . _processBytes ( data ) ;
256256 } ) ;
257257 this . connected = true ;
258- boardSerial . on ( 'open' , ( ) => {
258+ boardSerial . once ( 'open' , ( ) => {
259259 var timeoutLength = this . options . simulate ? 50 : 300 ;
260260 if ( this . options . verbose ) console . log ( 'Serial port open' ) ;
261261 setTimeout ( ( ) => {
@@ -271,12 +271,12 @@ function OpenBCIFactory() {
271271
272272 } , timeoutLength + 250 ) ;
273273 } ) ;
274- boardSerial . on ( 'close' , ( ) => {
274+ boardSerial . once ( 'close' , ( ) => {
275275 if ( this . options . verbose ) console . log ( 'Serial Port Closed' ) ;
276276 this . emit ( 'close' )
277277 } ) ;
278278 /* istanbul ignore next */
279- boardSerial . on ( 'error' , ( err ) => {
279+ boardSerial . once ( 'error' , ( err ) => {
280280 if ( this . options . verbose ) console . log ( 'Serial Port Error' ) ;
281281 this . emit ( 'error' , err ) ;
282282 } ) ;
@@ -545,7 +545,7 @@ function OpenBCIFactory() {
545545 * exist and thus this method will reject. If the board is using firmware 2+ then this function should resolve.
546546 * **Note**: This functionality requires OpenBCI Firmware Version 2.0
547547 * @since 1.0.0
548- * @returns {Number } - The new channel number.
548+ * @returns {Promise } - Resolves with the new channel number, rejects with err .
549549 * @author AJ Keller (@pushtheworldllc)
550550 */
551551 OpenBCIBoard . prototype . radioChannelSet = function ( channelNumber ) {
@@ -567,6 +567,7 @@ function OpenBCIFactory() {
567567
568568 // Subscribe to the EOT event
569569 this . once ( 'eot' , data => {
570+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
570571 // Remove the timeout!
571572 clearTimeout ( badCommsTimeout ) ;
572573 badCommsTimeout = null ;
@@ -585,6 +586,53 @@ function OpenBCIFactory() {
585586 } ) ;
586587 } ;
587588
589+ /**
590+ * @description Used to query the OpenBCI system for its radio channel number. The function will reject if not
591+ * connected to the serial port of the dongle. Further the function should reject if currently streaming.
592+ * Lastly and more important, if the board is not running the new firmware then this functionality does not
593+ * exist and thus this method will reject. If the board is using firmware 2+ then this function should resolve.
594+ * **Note**: This functionality requires OpenBCI Firmware Version 2.0
595+ * @since 1.0.0
596+ * @returns {Promise } - Resolves with the new channel number, rejects with err.
597+ * @author AJ Keller (@pushtheworldllc)
598+ */
599+ OpenBCIBoard . prototype . radioChannelSetHostOverride = function ( channelNumber ) {
600+ var badCommsTimeout ;
601+ return new Promise ( ( resolve , reject ) => {
602+ if ( ! this . connected ) return reject ( "Must be connected to Dongle. Pro tip: Call .connect()" ) ;
603+ if ( this . streaming ) return reject ( "Don't query for the radio while streaming" ) ;
604+ if ( channelNumber === undefined || channelNumber === null ) return reject ( "Must input a new channel number to switch too!" ) ;
605+ if ( ! k . isNumber ( channelNumber ) ) return reject ( "Must input type Number" ) ;
606+ if ( channelNumber > k . OBCIRadioChannelMax ) return reject ( `New channel number must be less than ${ k . OBCIRadioChannelMax } ` ) ;
607+ if ( channelNumber < k . OBCIRadioChannelMin ) return reject ( `New channel number must be greater than ${ k . OBCIRadioChannelMin } ` ) ;
608+
609+ // Set a timeout. Since poll times can be max of 255 seconds, we should set that as our timeout. This is
610+ // important if the module was connected, not streaming and using the old firmware
611+ badCommsTimeout = setTimeout ( ( ) => {
612+ reject ( "Please make sure your dongle is using firmware v2" ) ;
613+ } , 1000 ) ;
614+
615+ // Subscribe to the EOT event
616+ this . once ( 'eot' , data => {
617+ if ( this . options . verbose ) console . log ( `${ data . toString ( ) } ` ) ;
618+ // Remove the timeout!
619+ clearTimeout ( badCommsTimeout ) ;
620+ badCommsTimeout = null ;
621+
622+ if ( openBCISample . isSuccessInBuffer ( data ) ) {
623+ resolve ( data [ data . length - 4 ] ) ;
624+ } else {
625+ reject ( `Error [radioChannelSet]: ${ data } ` ) ; // The channel number is in the first byte
626+ }
627+ } ) ;
628+
629+ this . curParsingMode = k . OBCIParsingEOT ;
630+
631+ // Send the radio channel query command
632+ this . _writeAndDrain ( new Buffer ( [ k . OBCIRadioKey , k . OBCIRadioCmdChannelSetOverride , channelNumber ] ) ) ;
633+ } ) ;
634+ } ;
635+
588636 /**
589637 * @description Used to query the OpenBCI system for it's radio channel number. The function will reject if not
590638 * connected to the serial port of the dongle. Further the function should reject if currently streaming.
@@ -613,6 +661,7 @@ function OpenBCIFactory() {
613661
614662 // Subscribe to the EOT event
615663 this . once ( 'eot' , data => {
664+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
616665 // Remove the timeout!
617666 clearTimeout ( badCommsTimeout ) ;
618667 badCommsTimeout = null ;
@@ -659,13 +708,13 @@ function OpenBCIFactory() {
659708
660709 // Subscribe to the EOT event
661710 this . once ( 'eot' , data => {
711+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
662712 // Remove the timeout!
663713 clearTimeout ( badCommsTimeout ) ;
664714 badCommsTimeout = null ;
665715
666716 if ( openBCISample . isSuccessInBuffer ( data ) ) {
667- var pollTime = data [ data . length - 3 ] ;
668- console . log ( `pollTime is ${ pollTime } ` ) ;
717+ var pollTime = data [ data . length - 4 ] ;
669718 resolve ( pollTime ) ;
670719 } else {
671720 reject ( `Error [radioPollTimeGet]: ${ data } ` ) ; // The channel number is in the first byte
@@ -688,7 +737,7 @@ function OpenBCIFactory() {
688737 * function should resolve.
689738 * **Note**: This functionality requires OpenBCI Firmware Version 2.0
690739 * @since 1.0.0
691- * @returns {Promise } - With a {Buffer} that contains the success message.
740+ * @returns {Promise } - Resolves with new poll time, rejects with error message.
692741 * @author AJ Keller (@pushtheworldllc)
693742 */
694743 OpenBCIBoard . prototype . radioPollTimeSet = function ( pollTime ) {
@@ -710,12 +759,13 @@ function OpenBCIFactory() {
710759
711760 // Subscribe to the EOT event
712761 this . once ( 'eot' , data => {
762+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
713763 // Remove the timeout!
714764 clearTimeout ( badCommsTimeout ) ;
715765 badCommsTimeout = null ;
716766
717767 if ( openBCISample . isSuccessInBuffer ( data ) ) {
718- resolve ( data ) ;
768+ resolve ( data [ data . length - 4 ] ) ; // Ditch the eot $$$
719769 } else {
720770 reject ( `Error [radioPollTimeSet]: ${ data } ` ) ; // The channel number is in the first byte
721771 }
@@ -759,33 +809,34 @@ function OpenBCIFactory() {
759809
760810 // Subscribe to the EOT event
761811 this . once ( 'eot' , data => {
812+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
762813 // Remove the timeout!
763814 clearTimeout ( badCommsTimeout ) ;
764815 badCommsTimeout = null ;
765- var newBaudRateBuf = data . slice ( data . length - 9 , data . length - 3 ) ;
816+ var eotBuf = new Buffer ( '$$$' ) ;
817+ var newBaudRateBuf ;
818+ for ( var i = data . length ; i > 3 ; i -- ) {
819+ if ( bufferEqual ( data . slice ( i - 3 , i ) , eotBuf ) ) {
820+ newBaudRateBuf = data . slice ( i - 9 , i - 3 ) ;
821+ break ;
822+ }
823+ }
766824 var newBaudRateNum = Number ( newBaudRateBuf . toString ( ) ) ;
767825 if ( newBaudRateNum !== k . OBCIRadioBaudRateDefault && newBaudRateNum !== k . OBCIRadioBaudRateFast ) {
768826 return reject ( "Error parse mismatch, restart your system!" ) ;
769827 }
770828 if ( openBCISample . isSuccessInBuffer ( data ) ) {
771829 // Change the sample rate here
772830 if ( this . options . simulate === false ) {
773- if ( this . serial . isOpen ( ) ) {
774- this . serial . close ( ( ) => {
775- this . serial = new serialPort . SerialPort ( this . portName , {
776- baudRate : newBaudRateNum
777- } , ( err ) => {
778- if ( err ) {
779- this . connected = false ;
780- reject ( err ) ;
781- } else {
782- this . connected = true ;
783- this . options . baudRate = newBaudRateNum ;
784- resolve ( newBaudRateNum ) ;
785- }
786- } ) ;
787- } ) ;
788- }
831+ this . serial . update ( {
832+ baudRate : newBaudRateNum
833+ } , err => {
834+ if ( err ) {
835+ reject ( err ) ;
836+ } else {
837+ resolve ( newBaudRateNum ) ;
838+ }
839+ } ) ;
789840 } else {
790841 resolve ( newBaudRateNum ) ;
791842 }
@@ -835,6 +886,8 @@ function OpenBCIFactory() {
835886 clearTimeout ( badCommsTimeout ) ;
836887 badCommsTimeout = null ;
837888
889+ if ( this . options . verbose ) console . log ( data . toString ( ) ) ;
890+
838891 if ( openBCISample . isSuccessInBuffer ( data ) ) {
839892 resolve ( true ) ;
840893 } else {
0 commit comments