ProcessingDataDDisplay

From SGMK-SSAM-WIKI
Revision as of 23:16, 7 May 2013 by Kiilo (talk | contribs) (Created page with "processing code /* SerialGraph.pde Language: Processing (C)2012 noro24.de, http://www.noro24.de This program is free software: you can redistribute it and/or modify it ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

processing code

/*

SerialGraph.pde
Language: Processing

(C)2012 noro24.de, http://www.noro24.de

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version. http://www.gnu.org/licenses/

Based on a sketch of Tom Igoe

http://wiki.processing.org/w/Tom_Igoe_Interview

  • /

import processing.serial.*;

// how many graph to display int MAX_INPUTS = 6; boolean fontInitialized = false; Serial serial;

// array of value ranges for each graph float[] MAX_VAL = new float[MAX_INPUTS]; float[] MIN_VAL = new float[MAX_INPUTS]; // Names of each graph String[] NAMES = new String[MAX_INPUTS]; // colors of each graph color[] COLS = new color[MAX_INPUTS];

float[] previousValue = new float[MAX_INPUTS]; // array of previous values int xpos = 0; // x position of the graph PFont font; // font for writing text to the window

void setup () {

 // size of window
 size(800, 800);        
 // Serial portname
 // under Linux we must a make a symbolic link
 // from the usb device to /dev/ttyS80 or whatever you want
 // e.g. sudo ln -s /dev/ttyACM0 /dev/ttyS80
 String portName = "COM3";
 serial = new Serial(this, portName, 38400);
 serial.clear();
 // don't generate a serialEvent() until you get a newline (\n) byte:
 serial.bufferUntil('\n');
 // create a font with the fourth font available to the system:
 font = createFont(PFont.list()[3], 14);
 textFont(font);
 fontInitialized = true;
 // set inital background:
 background(0);
 // turn on antialiasing:
 smooth();
 
 MIN_VAL[0] = -20000;
 MIN_VAL[1] = -40000;
 MIN_VAL[2] = -40000;  
 MIN_VAL[3] = -40000;
 MIN_VAL[4] = -40000;  
 MIN_VAL[5] = -40000;
 
 MAX_VAL[0] = 20000;
 MAX_VAL[1] = 40000;
 MAX_VAL[2] = 40000;  
 MAX_VAL[3] = 40000;  
 MAX_VAL[4] = 40000;  
 MAX_VAL[5] = 40000;  
 
 NAMES[0] = "Araw";
 NAMES[1] = "Vraw";
 NAMES[2] = "PitchRaw";
 NAMES[3] = "PitchKalman";
 NAMES[4] = "five";
 NAMES[5] = "sixn";
 
 COLS[0] = color(255, 0, 0); // red
 COLS[1] = color(0, 255, 0); // green
 COLS[2] = color(0, 0, 255); // blue
 COLS[3] = color(255, 255, 0); // violett
 COLS[4] = color(0, 0, 255); // blue
 COLS[5] = color(255, 255, 0); // violett

}

void draw () {

 // nothing happens in the draw loop, 
 // but it's needed to keep the program running

}

void serialEvent (Serial serial) {

 // get the ASCII string:
 String inString = serial.readStringUntil('\n');

 // if it's not empty:
 if (inString != null) {
   // trim off any whitespace:
   inString = trim(inString);

   // convert to an array of ints:
   int incomingValues[] = int(split(inString, ","));

   if (incomingValues.length <= MAX_INPUTS && incomingValues.length > 0) {
     for (int i = 0; i < incomingValues.length; i++) {
       // graphing range (0 to window height/number of values):
       float ypos = map(incomingValues[i], MIN_VAL[i], MAX_VAL[i], 0, height/incomingValues.length);

       // figure out the y position for this particular graph:
       float graphBottom = i * height/incomingValues.length;
       ypos = ypos + graphBottom;

       // make a black block to erase the previous text:
       noStroke();
       fill(0);
       rect(10, graphBottom+1, 110, 20);

       fill(255);
       int textPos = int(graphBottom) + 14;
       if (fontInitialized) {
         text(NAMES[i] + ":" + incomingValues[i], 10, textPos);
       }
       
       
       
       // draw a line at the bottom of each graph:
       stroke(127);
       line(0, graphBottom, width, graphBottom);
       // x -axis
       //line(0, graphBottom/2, width, graphBottom/2);
       
       // change colors to draw the graph line:
       stroke(COLS[i]);
       line(xpos, previousValue[i], xpos+1, ypos);
       
     
       
       // save the current value to be the next time's previous value:
       previousValue[i] = ypos;
     }
   }
   // if you've drawn to the edge of the window, start at the beginning again:
   if (xpos >= width) {
     xpos = 0;
     background(0);
   } 
   else {
     xpos++;
   }
 }

}