从串口绘制数据时,如何在处理中写入text()?
How to write text() in processing when plotting data from serial port?
最近我正在学习使用我的 arduino UNO 进行处理,我检查了 arduino graph tutorial 并且它很有魅力,但是当你绘制数据时我无法刷新简单的文本,例如,如果我有一个通过串口传输的实时数据源。
我想写一个带有值的文本,但它似乎以某种方式变得 overshadowed/overwritten,并且无法与数字一起显示。
检查下图:
绘制该数据的源代码如下:
/*
GreenLine Temperature Graph,
Modified from https://www.arduino.cc/en/Tutorial/Graph
c1b3r
*/
import processing.serial.*;
Serial myPort;
int xPos = 1;
color eggshell = color(255, 253, 248);
int temperaturaActual;
float temperaturaPreviaAltura = 0 ;
String inDataArduino;
PFont font;
void setup () {
size(600,600);
//fullScreen();
frameRate(30);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0);
font = createFont("Arial",32,true);
}
void draw () {
int Xmaxgraph = int(width-(width/4));
println(Xmaxgraph,width, height);
temperaturaActual = int(inDataArduino);
float alturaTemperatura = map(temperaturaActual, 0, 1023, 0, height);
stroke(255,255,255);
line(Xmaxgraph, 0, Xmaxgraph, height);
textSize(40);
fill(255, 0, 0);
textFont(font,16);
text("Temp°", width - 150, 40);
text(temperaturaActual, width - 150, 90);
fill(255,255,0);
text("Estado", width - 150, height-100);
stroke(0,255,0);
line(xPos-1, height - temperaturaPreviaAltura, xPos, height- alturaTemperatura);
temperaturaPreviaAltura = alturaTemperatura;
if (xPos >= Xmaxgraph) {
xPos = 0;
background(0);
} else {
xPos++;
}
}
void serialEvent (Serial myPort) {
inDataArduino = myPort.readStringUntil('\n');
if (inDataArduino != null) {
inDataArduino = trim(inDataArduino);
}
}
语法如下highlighted code。
为什么会发生这种行为?我该怎么做才能解决这种情况并在绘制数据时清楚地看到文本?
感谢您对此的帮助,
谢谢,
H.
大多数 Processing sketches 都会在每次调用 draw()
的顶部调用 background()
函数来清除旧帧。您只是在图形超出屏幕右边缘时才调用 background()
函数。因此对于大多数帧,您直接在之前的帧之上绘制。这就是为什么您会看到文本堆叠在自身之上。
要解决此问题,您需要每帧调用 background()
函数(这需要重构您的代码以每次都重新绘制整个图形),或者您可能只绘制一个矩形即可在前面的文字上。
最近我正在学习使用我的 arduino UNO 进行处理,我检查了 arduino graph tutorial 并且它很有魅力,但是当你绘制数据时我无法刷新简单的文本,例如,如果我有一个通过串口传输的实时数据源。
我想写一个带有值的文本,但它似乎以某种方式变得 overshadowed/overwritten,并且无法与数字一起显示。
检查下图:
绘制该数据的源代码如下:
/*
GreenLine Temperature Graph,
Modified from https://www.arduino.cc/en/Tutorial/Graph
c1b3r
*/
import processing.serial.*;
Serial myPort;
int xPos = 1;
color eggshell = color(255, 253, 248);
int temperaturaActual;
float temperaturaPreviaAltura = 0 ;
String inDataArduino;
PFont font;
void setup () {
size(600,600);
//fullScreen();
frameRate(30);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0);
font = createFont("Arial",32,true);
}
void draw () {
int Xmaxgraph = int(width-(width/4));
println(Xmaxgraph,width, height);
temperaturaActual = int(inDataArduino);
float alturaTemperatura = map(temperaturaActual, 0, 1023, 0, height);
stroke(255,255,255);
line(Xmaxgraph, 0, Xmaxgraph, height);
textSize(40);
fill(255, 0, 0);
textFont(font,16);
text("Temp°", width - 150, 40);
text(temperaturaActual, width - 150, 90);
fill(255,255,0);
text("Estado", width - 150, height-100);
stroke(0,255,0);
line(xPos-1, height - temperaturaPreviaAltura, xPos, height- alturaTemperatura);
temperaturaPreviaAltura = alturaTemperatura;
if (xPos >= Xmaxgraph) {
xPos = 0;
background(0);
} else {
xPos++;
}
}
void serialEvent (Serial myPort) {
inDataArduino = myPort.readStringUntil('\n');
if (inDataArduino != null) {
inDataArduino = trim(inDataArduino);
}
}
语法如下highlighted code。
为什么会发生这种行为?我该怎么做才能解决这种情况并在绘制数据时清楚地看到文本?
感谢您对此的帮助, 谢谢, H.
大多数 Processing sketches 都会在每次调用 draw()
的顶部调用 background()
函数来清除旧帧。您只是在图形超出屏幕右边缘时才调用 background()
函数。因此对于大多数帧,您直接在之前的帧之上绘制。这就是为什么您会看到文本堆叠在自身之上。
要解决此问题,您需要每帧调用 background()
函数(这需要重构您的代码以每次都重新绘制整个图形),或者您可能只绘制一个矩形即可在前面的文字上。