Skip to content

Commit c4a6714

Browse files
committed
save data and Mac serial port long names
1 parent bda7451 commit c4a6714

File tree

3 files changed

+211
-21
lines changed

3 files changed

+211
-21
lines changed

processing/oscilloscope_4ch/Com.pde

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Com{
22
Serial port;
3-
String ports[]=append(Serial.list(),"Serial");
3+
String ports[]=append(Serial.list(),"select serial");
44
String portName;
55
int indPort=ports.length-1;
66
//String speed="115200";
77
//String speed="250000";
8-
String speeds[]={"9600","115200","250000","speed"};
8+
String speeds[]={"9600","115200","250000","select speed"};
99
//int portSpeed;
1010
int indSpeed=speeds.length-1;
1111
int p=-1;
12-
int x,y,w,h,u; // u=w/7 com1(4u), speed(4u), ok/x(2u)
12+
int x,y,w,h,dh;
13+
TextBox title, onOff, selectSerial, selectSpeed, refresh;
1314
boolean conectado=false;
1415
boolean erro=false;
1516
color cor=color(0);
@@ -19,26 +20,43 @@ class Com{
1920
//constructor
2021
Com(Serial portt,int xt, int yt, int wt, int ht){
2122
x=xt; y=yt; w=wt; h=ht;
22-
u=w/11;
23-
}
23+
dh=h/3;
24+
title= new TextBox("Configurar Serial",CENTER,x,y,int(0.7*w),dh);
25+
refresh=new TextBox("refresh",CENTER,int(x+0.7*w),y,int(0.3*w),dh);
26+
//refresh=new TextBox("refresh",CENTER,int(x+0.6*w),y+2*h/3,int(0.4*w),dh);
27+
selectSerial=new TextBox("select serial",CENTER,x,y+h/3,w,dh);
28+
selectSpeed=new TextBox("select speed",CENTER,x,y+2*h/3,int(0.7*w),dh);
29+
onOff=new TextBox("off",CENTER,int(x+0.7*w),y+2*h/3,int(0.3*w),dh);
30+
//onOff=new TextBox("off",CENTER,int(x+0.7*w),y,int(0.3*w),dh);
31+
}
2432
void display(){
2533
strokeWeight(1); stroke(0);fill(200);
34+
onOff.tex="off";
2635
if (conectado) {
2736
cor=color(0,255,0);
37+
onOff.tex="on";
2838
// fill(cor);
2939
} else if (erro) {
3040
cor=color(255,0,255);
3141
} else {
3242
cor=color(200);
3343
// fill(200);
3444
}
35-
fill(cor); rect(x,y-20,11*u,20);
45+
//fill(cor);
46+
title.display(cor);
47+
onOff.display(cor);
48+
selectSerial.display(cor);
49+
selectSpeed.display(cor);
50+
refresh.display(cor);
51+
/*
52+
rect(x,y-20,11*u,20);
3653
rect(x,y,u,h); rect(x+u,y,4*u,h); rect(x+5*u,y,4*u,h); rect(x+9*u,y,2*u,h);
3754
fill(0);textAlign(CENTER,CENTER); text("Configurar a Serial",x+w/2,y-12);
3855
//text("*",x+u/2,y+h/2); text(ports[indPort],x+3*u,y+h/2); text(speeds[indSpeed],x+7*u,y+h/2);
3956
text("*",x+u/2,y+h/2); text(ports[indPort],x+3*u,y+h/2); text(speeds[indSpeed],x+7*u,y+h/2);
4057
if (conectado) tex="on"; else tex="off";
4158
text(tex,x+10*u,y+h/2);//9*u,y+h/2);
59+
*/
4260
}
4361
//int mouseLeftClick(){
4462

@@ -58,37 +76,50 @@ class Com{
5876

5977
int mouseClicado(){
6078
int r=0;
61-
if (mouseY>y && mouseY<y+h){
62-
if (mouseX>x && mouseX<x+u) { // recarregar a lista das COMs
63-
if (!conectado) {
64-
ports=append(Serial.list(),"Serial");
65-
indPort=ports.length-1;
79+
80+
//if (mouseY>y && mouseY<y+h){
81+
if (refresh.mouseClicado()) { // recarregar a lista das COMs
82+
if (!conectado) { // not connected
83+
ports=append(Serial.list(),"select serial");
84+
if (ports.length>1){
85+
indPort=ports.length-2;
86+
}else{
87+
indPort=ports.length-1;
88+
}
89+
selectSerial.tex=ports[indPort];
90+
indSpeed=1;
91+
selectSpeed.tex=speeds[indSpeed];
6692
}
67-
} else if (mouseX>x+u && mouseX<x+5*u) { // mudar porta serial
93+
} else if (selectSerial.mouseClicado()) { // mudar porta serial
94+
//println("Com=mouseClicado");
6895
if (!conectado){
6996
indPort++;
7097
if (indPort>=ports.length) indPort=0;
7198
}
72-
} else if (mouseX>x+5*u && mouseX<x+9*u) { // mudar speed (baudrate)
99+
selectSerial.tex=ports[indPort];
100+
} else if (selectSpeed.mouseClicado()) { // mudar speed (baudrate)
73101
if (!conectado){
74102
indSpeed++;
75103
if (indSpeed>=speeds.length) indSpeed=0;
76104
}
77-
} else if (mouseX>x+9*u && mouseX<x+w){ // mudar X (desconectado) para ok (conectado)
105+
selectSpeed.tex=speeds[indSpeed];
106+
} else if (onOff.mouseClicado()){ // mudar X (desconectado) para ok (conectado)
78107
if (conectado){ // desconectar
79108
r=-1; // -1 => desconectar
80109
//port.stop();
81110
//conectado=false;
82111
} else { // conectar
83112
//if (indPort<ports.length-1 && indSpeed<speeds.length-1){
84-
if (indPort<ports.length-1){
113+
if (indPort<ports.length-1 && indSpeed<3){
114+
//println("speeds[",indSpeed,"]=",speeds[indSpeed]);
85115
//port=new Serial(this,"COM3",9600);
86116
//port=new Serial(this,ports[indPort],int(speeds[indSpeed]));
87117
r=1; // retorna 1 para conectar
88118
}
89119
}
90120
}
91-
}
121+
//}
92122
return r;
93123
}
124+
94125
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class TextBox{
2+
int x,y,w,h;
3+
int hAlign;
4+
String tex;
5+
//constructor
6+
TextBox(String texi,int hAligni, int xi, int yi, int wi, int hi){
7+
tex=texi; hAlign=hAligni;
8+
x=xi; y=yi; w=wi; h=hi;
9+
}
10+
void display(color bgColor){
11+
fill(bgColor);
12+
rect(x,y,w,h);
13+
fill(0);
14+
if (hAlign==LEFT){
15+
textAlign(LEFT,CENTER);
16+
text(tex,x+5,y+h/2);
17+
} else{
18+
textAlign(CENTER,CENTER);
19+
text(tex,x+w/2,y+h/2);
20+
}
21+
}
22+
23+
boolean mouseClicado(){
24+
boolean r=false;
25+
if (mouseX>x && mouseX<x+w && mouseY>y && mouseY<y+h){
26+
r=true;
27+
// println(tex," mouseClicado=",r);
28+
}
29+
return r;
30+
}
31+
}

processing/oscilloscope_4ch/oscilloscope_4ch.pde

Lines changed: 133 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ color cor[]={color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), color(255,255
2525

2626
import processing.serial.*;
2727

28+
/*
29+
08-Jun-2017 - output file "dados.txt" when click button
30+
t (ms)<tab>ch0 (mV)<tab>ch1 (mV)
31+
0<tab>1215<tab>123
32+
1<tab>2123<tab>2
33+
2<tab>2350<tab>350
34+
*/
35+
PrintWriter output;
36+
boolean outputOpen=false;
37+
Botao save;
38+
int qSave=0;
39+
40+
2841
// configuração dos objetos
2942
Serial port;
3043
Com com;
@@ -97,8 +110,10 @@ void setup() {
97110
marg1=tela.x+tela.w+10;
98111
marg2=marg1+200;
99112

100-
com=new Com(port, tela.x+tela.w-175, tela.y-30, 175, 20);
101-
113+
//16-Jun-2017 serial port names are too long in Mac - Roman Random
114+
com=new Com(port, tela.x+tela.w-250, 5, 250, 55);
115+
//com=new Com(port, tela.x+tela.w-175, tela.y-30, 175, 20);
116+
102117
//XYZ=new Botao("XYZ", marg2, tela.y, 45, 20);
103118
//XYZy=tela.y+5*Q;
104119
//for (int k=0; k<3;k++){
@@ -137,6 +152,10 @@ void setup() {
137152
grafDif=new CheckBox("ver",calcFreq.x+calcFreq.w,calcFreq.y,15);
138153
//ruido=new Dial(escLinear, altMove, !nInt, fmt, "ruido", "V", 0, 0, 2, calcFreq.x+20, calcFreq.y+17, 100, 20);
139154

155+
// 08-jun-2017 - button to save data in data.txt
156+
save=new Botao("salvar datax.txt",calcFreq.x,calcFreq.y+calcFreq.h+5,150,20);
157+
158+
140159
//medidor de resistor/capacitor
141160
pnlRC=new Painel("", tela.x, tela.y+tela.h+10, 125, 40);
142161
RC=new CheckBox("medir res./cap.", pnlRC.x, pnlRC.y, 15);
@@ -168,7 +187,7 @@ void draw() {
168187
background(100);
169188
fill(0, 255, 255);
170189
textAlign(LEFT, TOP);
171-
textSize(24);
190+
textSize(18);
172191
text("BegOscopio "+versao, tela.x, 12);
173192
fill(0);
174193
textSize(12);
@@ -199,6 +218,7 @@ void draw() {
199218
calcFreq.display();
200219
grafDif.display();
201220
//ruido.display();
221+
save.display();
202222
com.display();
203223
resetEixos.display();
204224
resetMedir.display();
@@ -387,7 +407,8 @@ void mouseClicked() {
387407
com.conectado=false;
388408
com.erro=true;
389409
}
390-
if (com.conectado){
410+
411+
if (com.conectado){ // if connected start default values
391412
//initProgram();
392413
for (int k=0;k<4;k++){
393414
canal[k].chN.clicado=true;
@@ -403,6 +424,7 @@ void mouseClicked() {
403424
println("variasAmostra.clicado=",variasAmostras.clicado);
404425

405426
}
427+
406428
} else if (r==-1) { //retornou -1 então fechar serial
407429
port.stop();
408430
com.conectado=false;
@@ -549,6 +571,70 @@ void mouseClicked() {
549571
verPontos.mouseClicado();
550572
calcFreq.mouseClicado();
551573
grafDif.mouseClicado();
574+
575+
//08-Jun-2017 write data to file
576+
if (save.mouseClicado()){
577+
// 14-Jun-2017 save fluxo or save memory
578+
//println("fluxoContinuo.clicado=",fluxoContinuo.clicado);
579+
if (fluxoContinuo.clicado){
580+
if (outputOpen==false){ // não está gravando, então iniciar a gravação
581+
//println("outputOpen==false => ",outputOpen);
582+
String fileName ="dataf"+nf(year(),4)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+".txt";
583+
output=createWriter(fileName);
584+
outputOpen=true;
585+
save.tex="salvando";
586+
// cabeçalho
587+
//output.println("BegOscopio v"+versao+" "+nf(year())+"-"+nf(month())+"-"+nf(day())+" "+nf(hour())+":"+nf(minute())+":"+nf(second()));
588+
output.print("dt(");output.print(dt.v.printV());output.print(dt.unidade);output.print(")");
589+
for (int k=0; k<4; k++){
590+
if (canal[k].chN.clicado){
591+
output.print('\t');output.print("ch");output.print(k);output.print("(mV)");
592+
}
593+
}
594+
output.println();
595+
qSave=0;
596+
// ao entrar cada dado no fluxo gravar em output.print()
597+
// gravar na rotina de entrada
598+
} else { // save já está gravando, então parar a gravação
599+
//println("outputOpen==true => ",outputOpen);
600+
output.close();
601+
outputOpen=false;
602+
qSave=1;
603+
if (qSave>10) {qSave=1;}
604+
save.tex="salvar datax.txt" + "-"+qSave;
605+
save.clicado=false;
606+
}
607+
} else {
608+
String fileName ="data"+nf(year(),4)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+".txt";
609+
output=createWriter(fileName);
610+
// cabeçalho
611+
//output.println("BegOscopio v"+versao+" "+nf(year())+"-"+nf(month())+"-"+nf(day())+" "+nf(hour())+":"+nf(minute())+":"+nf(second()));
612+
output.print("dt(");output.print(dt.v.printV());output.print(dt.unidade);output.print(")");
613+
for (int k=0; k<4; k++){
614+
if (canal[k].chN.clicado){
615+
output.print('\t');output.print("ch");output.print(k);output.print("(mV)");
616+
}
617+
}
618+
output.println();
619+
// dados
620+
float f=5000.0/1023.0;
621+
for (int k2=0; k2<q.v.v;k2++){
622+
output.print(k2);
623+
for (int k=0; k<4; k++) {
624+
if (canal[k].chN.clicado){
625+
output.print('\t');output.print(int(canal[k].v[k2]*f));
626+
}
627+
}
628+
output.println();
629+
}
630+
631+
output.close();
632+
qSave+=1;
633+
if (qSave>10) {qSave=1;}
634+
save.tex="salvar datax.txt" + "-"+qSave;
635+
save.clicado=false;
636+
}
637+
}
552638
//ruido.mouseClicado();
553639

554640
//se clicou em dt ou q então enviar comando para Arduino e ajustar tela
@@ -577,6 +663,9 @@ void mouseClicked() {
577663
if (umaAmostra.mouseClicado()) { // receber apenas Uma Amostra
578664
variasAmostras.clicado=false;
579665
fluxoContinuo.clicado=false;
666+
if (outputOpen) {
667+
fecharDados();
668+
}
580669
if (com.conectado) {
581670
port.write("1");
582671
}
@@ -590,7 +679,7 @@ void mouseClicked() {
590679
break;
591680
}
592681
}
593-
println("k2=",k2);
682+
//println("k2=",k2);
594683

595684
if (k2>=0 && k2<=3){
596685
pnlAmostra.piscar=true;
@@ -604,6 +693,9 @@ void mouseClicked() {
604693
if (variasAmostras.mouseClicado()) {
605694
umaAmostra.clicado=false;
606695
fluxoContinuo.clicado=false;
696+
if (outputOpen) {
697+
fecharDados();
698+
}
607699
if (com.conectado) {
608700
if (variasAmostras.clicado) {
609701
port.write("vo");
@@ -623,6 +715,9 @@ void mouseClicked() {
623715

624716
} else {
625717
port.write("fx");
718+
if (outputOpen){
719+
fecharDados();
720+
}
626721
}
627722
} else {
628723
fluxoContinuo.clicado=false;
@@ -823,6 +918,16 @@ void keyReleased(){
823918
keyPressed=false;
824919
}
825920

921+
922+
void fecharDados(){
923+
output.close();
924+
outputOpen=false;
925+
if (qSave>10) {qSave=1;}
926+
save.tex="salvar datax.txt" + "-"+qSave;
927+
save.clicado=false;
928+
}
929+
930+
826931
/* ==========================================
827932
Comando enviados para o Arduino
828933
========================================== */
@@ -895,6 +1000,29 @@ void serialEvent(Serial p) {
8951000
dtReal.setV(float(tex2[1]));
8961001
if (dtReal.v-dt.v.v>1.1*dt.v.v){ dtErro=true;} else {dtErro=false;}
8971002

1003+
// salvar em arquivo
1004+
if (outputOpen) {
1005+
float f=5000.0/1023.0;
1006+
//for (int k2=0; k2<q.v.v;k2++){
1007+
int k2=int(q.v.v-1);
1008+
output.print(qSave);
1009+
qSave+=1;
1010+
for (int k=0; k<4; k++) {
1011+
if (canal[k].chN.clicado){
1012+
output.print('\t');output.print(int(canal[k].v[k2]*f));
1013+
}
1014+
}
1015+
output.println();
1016+
if (qSave % 100==0) { // de 100 em 100
1017+
save.tex="salvando "+nf(qSave);
1018+
output.flush();
1019+
}
1020+
//}
1021+
}
1022+
1023+
//println("cmd=",cmd," val=",val," dtReal=",dtReal.printV());
1024+
1025+
8981026
} else if (cmd.equals("chq")) { // entra qtd e quais canais serão recebidos
8991027
int v[]=int(splitTokens(val));
9001028
//voltar // println("========================");

0 commit comments

Comments
 (0)