We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Updating text (Read 946 times)
Updating text
Apr 30th, 2010, 6:25am
 
The text draws over itself without clearing so it quickly just becomes a blob in the top left corner. I tried drawing a rectangle over the area but the flicker was awful. Why? How can I update text without flicker?

Code:
//PROCESSING


// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, works on tab deliminated values followed by a new line by buffering up to newline in setup
// then reading each element in to a variable by incrementing through the tab characters in the string variable

// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.
// Modified for 2D memsic accelerometer by C Rand April 2010.

import processing.serial.*;

Serial myPort;  // The serial port
int xPos = 1;   // horizontal position of the graph
PFont font; //variable to hold font

void setup () {
 // set the window size:
 size(640,480);  

 // List all the available serial ports
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my  Arduino, so I open Serial.list()[0].
 // Open whatever port is the one you're using.
 myPort = new Serial(this, Serial.list()[0], 57600);
 // don't generate a serialEvent() unless you get a newline character:
 myPort.bufferUntil('\n');
 // set inital background:
 background(0);
  //load my font
 font = loadFont("Consolas-24.vlw");

}
void draw () {
 // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
 // get the first ASCII string:
 String inStringX = myPort.readStringUntil('\t');
 
 if (inStringX != null) {
   // trim off any whitespace:
   inStringX = trim(inStringX);
   // convert to an int and map to upper half of the screen height:
float inByteX = float(inStringX);
    println(inStringX);
 inByteX = map(inByteX, 0, 1023, height/2 , height);
   // draw the X line by varying line height:
   
   stroke(50,55,200);
   line(xPos, height/2, xPos, height - inByteX);

 // get the second ASCII string:
String inStringY = myPort.readStringUntil('\n');



 // convert to an int and map to lower half the screen height:
float inByteY = float(inStringY);
 inByteY = map(inByteY, 0,1023, 0, height/2);


    // draw the Y line by varying line height:
   stroke(50,100,255);
   line(xPos, height, xPos, height - inByteY);
   
   //Draw readings as text
   //Trying to draw readings at text here
   //Makes program readings lag behind the accelerometer motion by 5 seconds
   //great response time if you comment out the following text chunk
   

   textFont(font);
fill(0, 102, 153);
 text(inStringX, 15, 30);
fill(0, 102, 153);
 text(inStringY, 15, 60);



   // at the edge of the screen, go back to the beginning:
   if (xPos >= width) {
xPos = 0;
background(0);
   }
   else {
// increment the horizontal position:
xPos++;
   }
 }
}
Re: Text slowing down my graph
Reply #1 - Apr 30th, 2010, 6:44am
 
> So, Question one. Why is drawing the text of the value making my input lag?

because you're loading the font every single frame.
move this to setup (and make font global):
 font = loadFont("Consolas-24.vlw");

Re: Text slowing down my graph
Reply #2 - Apr 30th, 2010, 7:35am
 
koogy wrote on Apr 30th, 2010, 6:44am:
> So, Question one. Why is drawing the text of the value making my input lag

because you're loading the font every single frame.
move this to setup (and make font global):
 font = loadFont("Consolas-24.vlw");


So I juggled PFont declaration to before setup()
Moved loadFont statement to setup and the speed is great now but I cant seem to find a good way to display the value as text. The text writes over itself every frame but never clears the block from the old reading.
Re: Updating text
Reply #3 - Apr 30th, 2010, 7:37am
 
use a background coloured rect to clear it before writing new value
Re: Updating text
Reply #4 - Apr 30th, 2010, 7:54am
 
Yeah after I moved the loadImage I put the rect with stroke and fill @ 0,0,0

Works great now, thanks...
Page Index Toggle Pages: 1