import processing.serial.*; import controlP5.*; import processing.pdf.*; /* Analizza il campione raccolti con Arduino con il progr adc_read_5 */ /***********************************************************************/ int portNumber = 1; /* change the 0,1,2 etc. to match your port */ /***********************************************************************/ PrintWriter output; ControlP5 cp5; Serial myPort; PFont font1; PFont font2; int buttonColor = color(0,255,0); int captionColor = color(0,0,0); String portName; int portSpeed = 9600; int flag=0; int bufLen = 50; int bufNum = 32; /* Massimo 32 */ int nBuf = 0; int npoint = bufLen*bufNum/2; /* variabili per modifiche alla trasf di fourier */ int xmin = 0; int xmax = 0; float fatt = 1.; int hscale = 1; float pi = 3.14159; int[] x = new int[npoint]; byte[] buff = new byte[bufLen]; float[] xrec = new float[npoint]; float[][] xf = new float[4][npoint]; boolean recordPdf = false; void setup() { // List all the available serial ports printArray(Serial.list()); size(900,700); background(255,255,255); stroke(0, 0, 0); String portName = Serial.list()[portNumber]; myPort = new Serial(this, portName, portSpeed); myPort.buffer(bufLen); font1 = createFont("arial",15); font2 = createFont("arial",12); cp5 = new ControlP5(this); cp5.setColorBackground(color(255,255,255)); cp5.setFont(font2); init(); } void draw() { switch (flag) { case 0: /* Attesa comandi */ break; case 1: /* legge dati da Arduino */ clear(); break; case 2: background(255,255,255); /* esegue trasformazione */ drawGraph(x, 80, 60, npoint, 200, 10, 10, 0, npoint, hscale, " TENSIONE CAMPIONATA (canali ADC)"); xf = trasform(x); drawGraph(int(xf[2]), 80, 300, npoint,300, 10, 10, 1, npoint, 1, " MODULO DELLA TRASFORMATA"); flag = 0; break; case 3: flag = 0; break; case 4: flag = 0; break; } } void init() { /* Crea bottoni e textfields */ cp5.addButton("START") .setValue(0) .setPosition(50,10) .setSize(50,35) .setColorBackground(buttonColor) .setColorCaptionLabel(captionColor) .addCallback(new CallbackListener() { public void controlEvent(CallbackEvent event) { if (event.getAction() == ControlP5.ACTION_RELEASED) { flag = 1; // println("button start flag ", flag); myPort.write(byte(bufNum)); } } } ); cp5.addButton("SAVE") .setValue(0) .setPosition(150,10) .setSize(50,35) .setColorBackground(buttonColor) .setColorCaptionLabel(captionColor) .addCallback(new CallbackListener() { public void controlEvent(CallbackEvent event) { if (event.getAction() == ControlP5.ACTION_RELEASED) { flag = 4; // println("button save flag ", flag); String nomeFile = new String(cp5.get(Textfield.class,"nome_file").getText()); printFile(nomeFile); } } } ); cp5.addTextfield("nome_file") .setPosition(210,10) .setSize(100,25) .setFont(font1) .setFocus(true) .setText("nome_file") .setColor(color(0,0,0)) .setColorCursor(color(0,0,0)) ; textAlign(LEFT, TOP); textSize(14); fill(0,0,0); text("Premere START per avviare la presa dati" , 400, 10); text("Premere SAVE per salvare su file i risultati" , 400, 30); } void serialEvent(Serial myPort) { // println("Serial Event"); flag = 1; int numberOfData = myPort.readBytes(buff); int j =0; int k = 0; for (int i=0; i 7) { // println("input ", i," ", k, " ", x[i]," j ", j); } } println("DATI RICEVUTI ", numberOfData, " Buffer ",nBuf, " k ", k ); nBuf = nBuf+ 1; if(nBuf == bufNum) { myPort.clear(); println("ACQUISIZIONE COMPLETATA" ); flag = 2; nBuf = 0; } } float[][] trasform(int input[]) { /* Restituisce la trasformata di Fourier dell'array input */ int np = input.length; float[][] tf = new float[4][np]; for(int k=0; k < np; k++) { tf[0][k] = 0.; tf[1][k] = 0.; for(int n=0; n < np; n++) { float arg = k*2.*pi*n/npoint; tf[0][k] = tf[0][k] + x[n]*cos(arg); tf[1][k] = tf[1][k] - x[n]*sin(arg); } tf[2][k] = sqrt(tf[0][k]*tf[0][k] + tf[1][k]*tf[1][k]); tf[3][k] = log(tf[2][k])/log(10); } println("CALCOLO TRASFORMATA COMPLETATO"); return tf; } void drawGraph(int[] v, int startx, int starty, int w, int h, int divx, int divy, int indexmin, int indexmax, int hscale, String title) { /* Crea grafico dell'array v */ stroke(0,0,0); fill(255,255,255); rect(startx,starty, w, h); /* Titolo */ textAlign(CENTER, TOP); textSize(16); fill(0,0,0); text(title, startx + w/2, starty + 10); /* Crea scale */ if(divx > 0) { int curx = 0; int cury = starty + h; int passo = w/divx; for(int i=0; i < divx; i++) { curx = startx + passo*i; line(curx, cury, curx, cury+5); textAlign(CENTER, TOP); textSize(16); text(i*passo/hscale, curx, cury+15); } } if(divy > 0) { int cury = 0; int curx = startx; int passo = h/divy; for(int i=0; i < divy; i++) { cury = starty + h - passo*i; line(curx, cury, curx - 5, cury); } } /* Azzera elementi non voluti */ for(int i=0; i < indexmin; ++i) { v[i] = 0; } for(int i=indexmax; i < v.length; ++i) { v[i] = 0; } /* determina il massimo */ int ymax = max(v); // println("massimo ", ymax); textAlign(RIGHT, CENTER); textSize(16); text(ymax, startx-10, starty + (2*h)/10); text(ymax/2, startx-10, starty + (6*h)/10); text(ymax/4, startx-10, starty + (8*h)/10); text((ymax/4)*3, startx-10, starty + (4*h)/10); float fatty = 0.8*h/ymax; float xold = 0; float yold = 0; for(int k=0; k < v.length; k++) { float ygr = (starty + h) - v[k]*fatty; float xgr = startx + k*hscale; if(xgr > (startx + w)) break; if(k == 0) point(xgr, ygr); else { line(xold, yold, xgr, ygr); } xold = xgr; yold = ygr; } } void printFile(String nomeFile) { if(nomeFile.length() > 0) { output = createWriter(nomeFile); output.println("n v(n) Re(T) Im(T) Mod(T) "); output.println(" "); for(int i = 0; i < npoint; i++) { output.print(i); output.print(" "); output.print(x[i]); output.print(" "); output.print(xf[0][i]); output.print(" "); output.print(xf[1][i]); output.print(" "); output.print(xf[2][i]); output.print(" "); output.println(); } output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file println("Dati salvati sul file: ", nomeFile); } else { println("ERRORE: Manca il nome del file da salvare!"); } }