I am trying to connect my Neurosky EEG sensor with processing code without using ThinkGear connector. ThinkGear connector is an application which read serial data and transmit over WebSocket so that other application can listen to that WebSocket and use the data. It transmit data like eSense, raw, eyeblink (single) as JSON.
Problem - Using websocket I can connect with the device and receive JSON data but this JSON doesn't contain double eye blink data which is really crucial for me hence I want to write my own ThinkGear like emulator in processing.
PROBLEM PART 1
I have been tying to read serial data using processing and I was successful but now I don't know how to interpret this data. I have been trying to read ThinkGear communication protocol but everything was going over my head. I hope someone will help me to fill this :) :)
PROBLEM PART 2
I don't understand the use of myPort.buffer(12);. Also what is the point of setting byte[] inBuffer = new byte[8] if it is going to take buffer size as set in the beginning with myPort. What is the point of using myPort.readBytes(inBuffer); when inBuffer give the same values as without using readBytes(). Why myPort.buffer(size) affect the byte[] size even though I hardcoded it to 8 and how come it is taking 12 values even after hardcoding byte[] size to 8?
CODE
import processing.serial.*;
Serial myPort; // Create object from Serial class
String myString="";
String arrayval="";
byte[] inBufferfinal = new byte[12];
void setup() {
size(600, 400);
String[] portName = Serial.list();
for (int i=0; i<portName.length; i++) {
if (portName[i]!=null) {
myPort = new Serial(this, portName[i], 9600); // baudrate: 57600, 9600, 115400
myPort.buffer(12);
println(portName[i] + " " + i);
}
}
//frameRate(4);
}
void draw() {
background(255);
visualizer(12, inBufferfinal );
fill(0);
//textAlign(CENTER);
//text(myString, width/2, height/2);
}
void serialEvent(Serial myPort) {
myString = "";
arrayval = "";
byte[] inBuffer = new byte[8];
while (myPort.available () > 0) {
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
inBufferfinal = inBuffer;
println(inBuffer, inBuffer.length);
if (inBuffer != null) {
for (int i=0; i<inBuffer.length; i++) {
myString = myString + hex(inBuffer[i])+" , " ;
arrayval = arrayval+ i + " , ";
}
}
}
}
void visualizer(int lengthofbyte, byte[] val) {
noStroke();
fill(0);
for (int i=0; i<lengthofbyte; i++) {
int h = val[i];
rect(230, i*20+20, h, 20);
text(h, 20, i*20+40);
}
}