Skip to content

Commit 623cbf2

Browse files
author
AJ Keller
committed
Saving for the night
1 parent 4d26f61 commit 623cbf2

File tree

4 files changed

+378
-42
lines changed

4 files changed

+378
-42
lines changed

openBCIBoard.js

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

openBCISimulator.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function OpenBCISimulatorFactory() {
7373
this.buffer = new Buffer(500);
7474
// Numbers
7575
this.channelNumber = 1;
76+
this.hostChannelNumber = this.channelNumber;
7677
this.pollTime = 80;
7778
this.sampleNumber = -1; // So the first sample is 0
7879
// Objects
@@ -260,6 +261,7 @@ function OpenBCISimulatorFactory() {
260261
if (!this.options.boardFailure) {
261262
if (dataBuffer[2] < k.OBCIRadioChannelMax) {
262263
this.channelNumber = dataBuffer[2];
264+
this.hostChannelNumber = this.channelNumber;
263265
this._printSuccess();
264266
this.emit('data', new Buffer(`Channel Number ${this.channelNumber}`));
265267
this.emit('data', new Buffer([this.channelNumber]));
@@ -274,6 +276,26 @@ function OpenBCISimulatorFactory() {
274276
}
275277
}
276278
break;
279+
case k.OBCIRadioCmdChannelSetOverride:
280+
if (this.options.firmwareVersion === k.OBCIFirmwareV2) {
281+
if (dataBuffer[2] < k.OBCIRadioChannelMax) {
282+
if (dataBuffer[2] === this.channelNumber) {
283+
this.options.boardFailure = false;
284+
} else {
285+
this.options.boardFailure = true;
286+
}
287+
this.hostChannelNumber = dataBuffer[2];
288+
this._printSuccess();
289+
this.emit('data', new Buffer(`Host override - Channel Number ${this.hostChannelNumber}`));
290+
this.emit('data', new Buffer([this.hostChannelNumber]));
291+
this._printEOT();
292+
} else {
293+
this._printFailure();
294+
this.emit('data', new Buffer("Verify channel number is less than 25"));
295+
this._printEOT();
296+
}
297+
}
298+
break;
277299
case k.OBCIRadioCmdPollTimeGet:
278300
if (this.options.firmwareVersion === k.OBCIFirmwareV2) {
279301
if (!this.options.boardFailure) {
@@ -303,14 +325,14 @@ function OpenBCISimulatorFactory() {
303325
if (this.options.firmwareVersion === k.OBCIFirmwareV2) {
304326
this._printSuccess();
305327
this.emit('data', new Buffer("Switch your baud rate to 115200"));
306-
this._printEOT();
328+
this.emit('data', new Buffer([0x24,0x24,0x24,0xFF])); // The board really does this
307329
}
308330
break;
309331
case k.OBCIRadioCmdBaudRateSetFast:
310332
if (this.options.firmwareVersion === k.OBCIFirmwareV2) {
311333
this._printSuccess();
312334
this.emit('data', new Buffer("Switch your baud rate to 230400"));
313-
this._printEOT();
335+
this.emit('data', new Buffer([0x24,0x24,0x24,0xFF])); // The board really does this
314336
}
315337
break;
316338
case k.OBCIRadioCmdSystemStatus:

0 commit comments

Comments
 (0)