Permalink
Browse files

Added functionality to make channel data (3-byte signed int) and aux …

…data (2-byte signed int) into 32 bit ints from a buffer...

Still need to add support for scaling the channel data
Added sample data
Added a function to make a OpenBCISample, that will be the core data unit for the node...
  • Loading branch information...
1 parent cb87442 commit 210f4b3139c2c3fe1b90a0f6c07a0dc6502407f2 AJ Keller committed Dec 15, 2015
Showing with 205 additions and 0 deletions.
  1. +120 −0 OpenBCISample.js
  2. +85 −0 openBCISerialFormat
View
@@ -0,0 +1,120 @@
+/*
+Author: AJ Keller
+*/
+
+/*
+Require Statements
+*/
+var fs = require("fs");
+
+
+// Fill a buffer with that data
+var buf = function () {
+ const byteSample = '\x45';
+ const byteStart = '\x0A';
+ const byteStop = '\xC0';
+ const chunkDataAux = '\x00\x01';
+ const chunkDataChannel = '\x00\x00\x01';
+
+ // test data in OpenBCI serial format V3
+ var data = byteStart +
+ byteSample +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataChannel +
+ chunkDataAux +
+ chunkDataAux +
+ chunkDataAux +
+ byteStop;
+ return new Buffer(data);
+}
+
+
+function OpenBCISample(dataBuf) {
+ var numberOfBytes = dataBuf.byteLength;
+
+ var numberOfChannels = numberOfBytes - 9;
+
+
+
+ var channelData = function() {
+ var out = {};
+ for(var i = 2; i < numberOfBytes-7; i++) {
+ out[i] = interpret24bitAsInt32(buf.slice(i,i+3));
+ }
+ return out;
+ }
+
+ // var auxData = function() {
+ // var out = {};
+ // for(var i = ; i < numberOfBytes - 1; i++) {
+ // out[i] = dataBuf.readUInt8(i);
+ // }
+ // return out;
+ // }
+
+
+
+ return {
+ startByte : dataBuf.readUInt8(0), //byte
+ sampleNumber : dataBuf.readUInt8(1), //byte
+ channelData : channelData(), // multiple of 3 bytes
+ auxData : "auData", // 6 bytes
+ stopByte : dataBuf.readUInt8(numberOfBytes - 1) //byte
+ };
+}
+
+
+// var openBCISample1 = convertSerialToOpenBCISample(buf);
+
+// console.log(openBCISample1);
+
+
+function interpret24bitAsInt32(threeByteBuffer) {
+ const maskForNegativeNum = (255 << (3 * 8));
+ const maskForPositiveNum = 255 | (255 << 8) | (255 << 16);
+
+ var newInt = (threeByteBuffer.readUInt8(0) << 16) | (threeByteBuffer.readUInt8(1) << 8) | threeByteBuffer.readUInt8(2);
+ // 3byte int in 2s compliment
+ if (threeByteBuffer.readUInt8(0) > 127) {
+ //this is the two's compliment case
+ //i.e. number is negative so we need to simply
+ //add a byte of 1's on the front
+ netInt = newInt | maskForNegativeNum;
+ } else {
+ //I'm pretty sure we don't need this seeing as newInt is already 32-bits
+ // newInt = newInt & maskForPositiveNum;
+ }
+
+ // TODO: Apply the sclaing factor
+ return newInt;
+}
+
+var bufTemp = new Buffer('\x00\x00\x07');
+console.log(interpret24bitAsInt32(bufTemp));
+
+function interpret16bitAsInt32(twoByteBuffer) {
+ const maskForNegativeNum = (255 << (3 * 8)) | (255 << (2 * 8));
+ const maskForPositiveNum = 255 | (255 << 8);
+
+ var newInt = (twoByteBuffer.readUInt8(0) << 8) | twoByteBuffer.readUInt8(1);
+ // 3byte int in 2s compliment
+ if (twoByteBuffer.readUInt8(0) > 127) {
+ //this is the two's compliment case
+ //i.e. number is negative so we need to simply
+ //add a byte of 1's on the front
+ netInt = newInt | maskForNegativeNum;
+ } else {
+ //I'm pretty sure we don't need this seeing as newInt is already 32-bits
+ // newInt = newInt & maskForPositiveNum;
+ }
+
+ // TODO: Apply the sclaing factor
+ return newInt;
+}
+
View
@@ -0,0 +1,85 @@
+Start Byte
+0xA0
+10100000
+
+ID
+0-255
+00000001
+
+Channel 0
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 1
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 2
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 3
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 4
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 5
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 6
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Channel 7
+3 bytes
+Signed Int
+00000001
+00000001
+00000001
+
+Aux Data 0
+2 byte
+Signed Int
+00000001
+00000001
+
+Aux Data 1
+2 byte
+Signed Int
+00000001
+00000001
+
+Aux Data 2
+2 byte
+Signed Int
+00000001
+00000001
+
+Stop Byte
+0xC0
+10100000

0 comments on commit 210f4b3

Please sign in to comment.