使用Processing编写的加速度采集数据绘制软件(二)
processing吧
全部回复
仅看楼主
level 2
中z_1xx 楼主
//绘制表格
void drawForm(float x, float y)
{
int i = 0;
textSize(12);
strokeWeight(2);
noFill();
stroke(0,255,0);
rect(x + 50,y, 1243, 400);
strokeWeight(1);
for(i = 1; i < 9; i++){
line(x + 50, y + i * 50, x + 1290, y + i * 50);
}
for(i = 1; i < 22; i++){
line(x + 50 + i* 59, y , x + 50 + i * 59, y + 400);
}
fill(0,255,255);
text(" 40000", x + 12,y + 6);
text(" 30000", x + 12,y + 56);
text(" 20000", x + 12,y + 106);
text(" 10000", x + 12,y + 156);
text(" 0", x + 12,y + 206);
text("-10000", x + 12,y + 256);
text("-20000", x + 12,y + 306);
text("-30000", x + 12,y + 356);
text("-40000", x + 12,y + 406);
textSize(16);
fill(255,0,255);
text("X: ", x + 1300, y + 300);
strokeWeight(1);
stroke(255,0,255);
line(x + 1320, y + 300, x + 1360, y + 300);
fill(255,128,0);
text("Y: ", x + 1300, y + 350);
stroke(255,128,0);
line(x + 1320, y + 350, x + 1360, y + 350);
fill(255,255,0);
text("Z: ", x + 1300, y + 400);
stroke(255,255,0);
line(x + 1320, y + 400, x + 1360, y + 400);
}
byte button(float x , float y , String str)
{
byte ret = 0;
fill(0,255,0);
stroke(255,255,0);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 100) ) &&(mouseY < (y + 30))){
fill(128,255,255);
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
fill(0,128,128);
ret = 1;
}
}
rect(x,y, 100, 30);
fill(105);
textSize(24);
text(str, x + 24, y + 24);
return ret;
}
boolean X_checkBox = false;
boolean Y_checkBox = false;
boolean Z_checkBox = false;
boolean S_checkBox = false;
void CheckBoxHandle()
{
if(CheckBox(400,500,X_checkBox, "X")== 1){
X_checkBox = (X_checkBox == false) ? true: false;
}
if(CheckBox(460,500,Y_checkBox, "Y") == 1){
Y_checkBox = (Y_checkBox == false) ? true: false;
}
if(CheckBox(520,500, Z_checkBox, "Z") == 1){
Z_checkBox = (Z_checkBox == false) ? true: false;
}
if(CheckBox(400,540, S_checkBox, "自动保存")== 1){
S_checkBox = (S_checkBox == false) ? true: false;
}
}
boolean checkBox_GetXFlag()
{
return X_checkBox;
}
boolean checkBox_GetYFlag()
{
return Y_checkBox;
}
boolean checkBox_GetZFlag()
{
return Z_checkBox;
}
boolean checkBox_GetAutoSaveFlag()
{
return S_checkBox;
}
byte CheckBox(float x, float y, boolean sL, String str)
{
noFill();
stroke(255,255,0);
if(sL){
fill(0,255,0);
}
rect(x, y, 20, 20);
textSize(24);
fill(200);
text(str, x + 30, y + 18);
if((mouseX > x) && (mouseY > y) && (mouseX < (x + 20) ) &&(mouseY < (y + 20))){
if (mousePressedFlag && (mouseButton == LEFT)) {
mousePressedFlag = false;
return 1;
}
}
return 0;
}
int name = 0;
String strName = String.valueOf(name);
void Save_Pic()
{
if(SaveFlag){
SaveFlag = false;
saveFrame(strName + ".png");
name++;
strName = String.valueOf(name);
}
}
void Save_En()
{
SaveFlag = true;
}
//void Save_Dis()
//{
// SaveFlag = false;
//}
float accX = 0.0;
float accY = 0.0;
float accZ = 0.0;
float[] accXbuf = new float[45];
float[] accYbuf = new float[45];
float[] accZbuf = new float[45];
float[] accTmpbuf = new float[45];
byte i = 0;
byte buffSize = 43;
boolean bufFullFlag = false;
String strPdfName = "test";
byte coll = 0;
void Jiasudu_handle(){
if(Serial_GetComOpenFlag()){
Jiasudu_DrawLineX(20,50);
coll++;
if(coll >= 61){
coll = 0;
if(checkBox_GetAutoSaveFlag()){
Save_En();
}
}
}
if(Serial_GetdataFlag() == false){
return;
}
accX = Serial_ReadAcc('X');
accXbuf[i] = accX;
accY = Serial_ReadAcc('Y');
accYbuf[i] = accY;
accZ = Serial_ReadAcc('Z');
accZbuf[i] = accZ;
println(accX,accY,accZ);
i++;
if(i >= buffSize){
for(byte n = 0; n < buffSize; n++){
accTmpbuf[n] = accXbuf[n];
}
for(byte n = 0; n < buffSize; n++){
accXbuf[n] = accTmpbuf[n + 1];
}
for(byte n = 0; n < buffSize; n++){
accTmpbuf[n] = accYbuf[n];
}
for(byte n = 0; n < buffSize; n++){
accYbuf[n] = accTmpbuf[n + 1];
}
for(byte n = 0; n < buffSize; n++){
accTmpbuf[n] = accZbuf[n];
}
for(byte n = 0; n < buffSize; n++){
accZbuf[n] = accTmpbuf[n + 1];
}
i--;
bufFullFlag = true;
}
printArray(accZbuf);
}
void Jiasudu_DrawLineX(float x, float y)
{
float t = x + 50;
strokeWeight(2);
for(int n = 0; n < 41; n++){
if(checkBox_GetXFlag()){
stroke(255,0,255);
line(t + n * 30, y + 206 - accXbuf[n] / 200, t + (n + 1) * 30, y + 206 - accXbuf[n + 1] / 200);
}
if(checkBox_GetYFlag()){
stroke(255,128,0);
line(t + n * 30, y + 206 - accYbuf[n] / 200, t + (n + 1) * 30, y + 206 - accYbuf[n + 1] / 200);
}
if(checkBox_GetZFlag()){
stroke(255,255,0);
line(t + n * 30, y + 206 - accZbuf[n] / 200, t + (n + 1) * 30, y + 206 - accZbuf[n + 1] / 200);
}
}
}
byte click = 0;
byte comNum = 0;
byte Serial_StopFlag = 0;
boolean Serial_OpenFlag = false;
boolean Serial_DatareadFlag = false;
byte[] Serial_buffer = new byte[6];
Serial myPort;
String strSerial = "com";
boolean Serial_GetdataFlag = false;
String[] strComBuff = new String[4];
void Serial_Handle(float x, float y){
if(click != 0){
comNum = 0;
Serial_DrawComX(20, 50);
}
else{
textSize(16);
fill(255);
text(SerialName + " 9600
bp
s", x + 600, y + 500);
}
Serial_GetdataFromCom();
fill(255,0,255);
text( "X: "+ strComBuff[0], x + 50, y + 470);
fill(255,128,0);
text("Y: "+strComBuff[1], x + 50, y + 500);
fill(255,255,0);
text("Z: " + strComBuff[2], x + 50, y + 530);
}
void Serial_DrawComX(float x, float y){
int i = 0;
int len = ComX.length;
textSize(24);
while(i < len){
if(comNum != 0){//选择了端口
break;
}
Serial_DrawCom(x + 600, y + 485 + i * 30, ComX[i]);
i++;
}
}
void Serial_DrawCom(float x, float y, String str){
fill(255);
noStroke();
rect(x, y, 100, 30);
if(mouseX > x && mouseY > y && mouseX < x + 100 && mouseY < y + 30){
fill(175,183,203);
rect(x,y, 100, 30);
if (mousePressed && (mouseButton == LEFT)) { //按键按下
if(str.equals("COM1") == true){
comNum = 1;
}
else if(str.equals("COM2") == true){
comNum = 1;
}
else if(str.equals("COM3") == true){
comNum = 1;
}
else if(str.equals("COM4") == true){
comNum = 1;
}
else if(str.equals("COM5") == true){
comNum = 1;
}
else if(str.equals("COM6") == true){
comNum = 1;
}
else if(str.equals("COM7") == true){
comNum = 1;
}
else if(str.equals("COM8") == true){
comNum = 1;
}
else if(str.equals("COM9") == true){
comNum = 1;
}
if(str.equals(SerialName) != true){
SerialName = str;
if(comNum != 0){
click = 0;
if(Serial_StopFlag != 0){
myPort.stop();//停止上一个串口
}
myPort =new Serial(this,SerialName,9600 );
myPort.bufferUntil(10);
Serial_StopFlag = 1;
Serial_OpenFlag = true;
}
}
click = 0;
}
}
fill(0,0,255);
text(str,x + 10, y + 25);
}
void Serial_SetClick()
{
click = 1;
}
void Serial_GetdataFromCom()
{
if(Serial_OpenFlag != true){
strComBuff[0] = " ";
strComBuff[1] = " ";
strComBuff[2] = " ";
return;
}
//if(myPort.available() > 0){
// Serial_GetdataFlag = true;
// strSerial = myPort.readString();
// strComBuff = strSerial.split(",");
//}
}
float Serial_String2float(String str)
{
float a = 0;
a = float(str);
return a;
}
float Serial_ReadAcc(char accX)
{
float acc = 0;
Serial_GetdataFlag = false;
if(accX == 'X'){
acc = Serial_String2float(strComBuff[0]);
}
if(accX == 'Y'){
acc = Serial_String2float(strComBuff[1]);
}
if(accX == 'Z'){
acc = Serial_String2float(strComBuff[2]);
}
return acc;
}
boolean Serial_GetdataFlag()
{
return Serial_GetdataFlag;
}
boolean Serial_GetComOpenFlag()
{
return Serial_OpenFlag;
}
void serialEvent(Serial p) {
Serial_GetdataFlag = true;
strSerial = p.readString();
strComBuff = strSerial.split(",");
}
2016年07月27日 15点07分 1
1