I'm a new user and a new processing developer. I have your same problem because I would update my RollingGraph to visualize an ECG trace.
The firmware on my P.C.B. boards send a ECG sample every 3 ms. Thus the sample frequency is 333 sample/sec on 10 resolution bits. The problem is that the serialEvent receive data correctly but the Rolling graph refresh rate is wrong because I lose some sample every time.
import processing.serial.*;
// Need G4P library
import g4p_controls.*;
import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;
static int ECG_GRAPH_HEIGTH=300; //set altezza grafico ECG
float inByte;
float[] inByteArray=new float[10]; //buffer circolare dati seriali
Serial myPort; // The serial port
String ecgXLabel="Time[s]";
String ecgYLabel="Amplitude[mV]";
float scalaAmpiezzeEcgmV= ((3.3/1024)/1.159);
int counterSerialEvent=0; //contatore eventi seriale per buffer circolare
boolean serialFullBuffer=false; //flag per buffer seriale pieno
int counterComputePoint=(inByteArray.length)/2;
RollingLine2DTrace r;
Graph2D g;
class eq implements ILine2DEquation
{
public double computePoint(double x,int pos)
{
/*counterComputePoint++;
if(counterComputePoint==(inByteArray.length-1))
counterComputePoint=0;
return inByteArray[counterComputePoint]; */
return inByte;
}
}
public void setup(){
//size(displayWidth, displayHeight, JAVA2D); //to automatic resize object for different screen size
size(800, 600, JAVA2D);
// List all the available serial ports
println(Serial.list());
String portName = "/dev/ttyUSB0";
myPort = new Serial(this,portName, 38400); //la scheda Heart_core comunica @ 38400 bps
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
r = new RollingLine2DTrace(new eq(),10,0.003f); //istanza retta disegno ECG
r.setTraceColour(0, 0, 0); //colore della traccia ECG = nero
r.setLineWidth(1); //larghezza della traccia ECG
g = new Graph2D(this, 400, 300, false); //istanza grafico traccia ECG
g.addTrace(r);
g.setYAxisMax(ECG_GRAPH_HEIGTH*scalaAmpiezzeEcgmV);
g.position.y = 25;
g.position.x = 100; //posizione iniziale del grafico
g.setYAxisTickSpacing(0.05);
g.setYAxisLabelAccuracy(2); //Sets the decimal places to show on the Y-Axis tick labels
g.setXAxisTickSpacing(1); //imposta il valore di 1 sec come visualizzazione sull'asseX
g.setXAxisLabelAccuracy(0);
g.setXAxisMax(5f); //imposta la finestra a 5 sec
g.setXAxisLabel(ecgXLabel);
g.setYAxisLabel(ecgYLabel);
createGUI();
customGUI();
// Place your setup code here
}
public void draw()
{
background(230);
//if(serialFullBuffer)
//g.draw();
g.draw();
}
void serialEvent (Serial myPort)
{
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, (ECG_GRAPH_HEIGTH*scalaAmpiezzeEcgmV)); //converte il valore in mV sul segnale d'ingresso
inByteArr
ay[counterSerialEvent] = inByte;
counterSerialEvent++;
if (counterSerialEvent==(inByteArray.length-1))
{
serialFullBuffer=true; //buffer seriale pieno
counterSerialEvent=0;
}
}
}
// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){
}
also there is a GUI interface but this is another story....