SHARE
TWEET

readAnalog.java (Processing)

Darker666 Mar 16th, 2016 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2. import java.awt.event.*;
  3. // The serial port:
  4. Serial myPort;
  5.  
  6. static int MAX_GRAPH_SIZE = 65536;
  7. static byte LINE_WIDTH = 1;
  8.  
  9. long start_time = 0;
  10.  
  11.  
  12. long last_time_displayed = 0;
  13. long last_time_loaded_new = 0;
  14. long last_time_received = 0;
  15. short display_speed_ratio = 1;
  16. int elements_received = 0;
  17. int elements_received_last = 0;
  18. boolean receive = true;
  19.  
  20. long animation_step = 0;
  21. long animation_timeout = 0;
  22.  
  23. int traffic_speed_counter = 0;
  24. int traffic_speed_counter_old = 0;
  25. long temporary_timer = 0;
  26. float last_speed = 0;
  27. //boolean connected = false;
  28. //Array of bytes received
  29. ArrayList<Short> graph;
  30. ArrayList<PImage> images;
  31.  
  32. void setup() {
  33.    size(800, 600);
  34.    // List serial ports and chose the first one
  35.    println(Serial.list());
  36.    while(myPort==null) {
  37.        try {
  38.          myPort = new Serial(this, Serial.list()[6], 9600);
  39.        }
  40.        catch(Throwable e) {
  41.            println("Failed to connect, waiting and retrying.");
  42.            try {Thread.sleep(1000);} catch(Throwable ee) {break;}
  43.        }
  44.    }
  45.    //Initialise the byte array
  46.    graph = new ArrayList<Short>();
  47.    
  48.    //Window configuration
  49.    
  50.    if (frame != null) {
  51.      frame.setResizable(true);
  52.    }
  53.    //Aditional events
  54.    //addMouseWheelListener(new MouseWheelListener() {
  55.    // public void mouseWheelMoved(MouseWheelEvent mwe) {
  56.    //   mouseWheel(mwe.getWheelRotation());
  57.    //}});
  58.    //Loading images and fonts
  59.    images = new ArrayList<PImage>();
  60.    images.add(loadImage("hi-tech-clock_transparent.png"));
  61.    resizePreserve(images.get(0), (short)32, (short)32);
  62.    
  63.    //Time
  64.    start_time = millis();
  65.    
  66.    try {Thread.sleep(3000);} catch(Throwable ee) {return;}
  67.    sendMessageCode((short)2, (short)50);
  68.    //Reset
  69.    sendMessageCode((short)1, (short)0);
  70.    //Initialise arduino
  71.    sendMessageCode((byte)0, (byte)1);
  72.  
  73. /*
  74.    new Thread() {
  75.         @Override
  76.         public void run() {
  77.            short num = 0;
  78.            while(true) {
  79.                num++;
  80.                if(num>255)
  81.                  num = 0;
  82.                sendMessageCode((short)3, (short)num);
  83.                try {Thread.sleep(500);} catch(Throwable ee) {break;}
  84.            }
  85.         }
  86.    }.start();*/
  87. }
  88.  
  89. void serialEvent(Serial robotPort) {
  90.  
  91.   while (robotPort.available() >= 2) {
  92.     short number = 0;
  93.     for(byte i=0; i<2; i++) {
  94.       short received = (short)robotPort.read();
  95.       if(received<0) {
  96.         println("No data in serial though available() returned true!");
  97.         i=10;
  98.         break;
  99.       }
  100.       if(received>255) {
  101.         println("Number greater than 255 received!");
  102.         i=10;
  103.         break;
  104.       }
  105.      
  106.       //println("Byte received: "+Integer.toString((int)received));
  107.       if(i==0) {
  108.         print(received + " + ");
  109.         number += received;
  110.       }
  111.       else {
  112.         number += received*256;
  113.         println(received + "*256 (that's " + received*256 + ") = " + number);
  114.       }
  115.        
  116.       traffic_speed_counter++;
  117.     }
  118.     if(display_speed_ratio>1) {
  119.       elements_received++;
  120.       elements_received_last++;
  121.     }
  122.     last_time_received = millis();
  123.     if(number>1024||number<0) {
  124.       //if(number-1024<32)  //Is data
  125.       //  processMessageCode(
  126.       println("Strange number received: "+number);
  127.       // Remove first ten bits from number
  128.       println("Code: "+(number-1024));
  129.       //sendMessageCode((byte)1, (byte)0);
  130.       break;
  131.     }
  132.     else if(receive)
  133.       graph.add(number);
  134.   }
  135.   //if(graph.size()>MAX_GRAPH_SIZE)
  136.   //  graph.removeRange(0, graph.size()-MAX_GRAPH_SIZE);
  137.   while(graph.size()>MAX_GRAPH_SIZE)
  138.     graph.remove(0);
  139.   //println("Die!");
  140. }
  141.  
  142.  
  143. void draw() {
  144.   background(0);
  145.  
  146.   int graph_offset = 0;
  147.   if(display_speed_ratio>1) {
  148.     if(millis()-last_time_loaded_new>display_speed_ratio*(millis()-last_time_received)) {
  149.       println("Adding " + Integer.toString(elements_received_last/display_speed_ratio) +" elements out of " + elements_received + " new ones.");
  150.       elements_received -= elements_received_last/display_speed_ratio;
  151.       elements_received_last = 0;
  152.       last_time_loaded_new = millis();
  153.     }
  154.     graph_offset = width>((graph.size()-elements_received)*LINE_WIDTH)?0:(graph.size()-elements_received)-floor((width/LINE_WIDTH));
  155.    
  156.   }
  157.   else {
  158.     graph_offset = width>(graph.size()*LINE_WIDTH)?0:graph.size()-floor((width/LINE_WIDTH));
  159.   }
  160.  
  161.   int lastY = -1;
  162.   int max = (graph.size()-1);
  163.   stroke(0, 140, 0);
  164.   for(int i=0; i+graph_offset<max; i++) {
  165.      /*if((i*40)%256>=128)
  166.        stroke(0, 255-(i*40)%128, 0);
  167.      else
  168.        stroke(0, 128+(i*40)%128, 0);
  169.      line(i*LINE_WIDTH,height-(int)(((float)height)*(((float)graph.get(i+graph_offset))/1024.0)), i*LINE_WIDTH, height);*/
  170.      int Y = height-(int)(((float)height)*(((float)graph.get(i+graph_offset))/1024.0));
  171.      if(lastY!=-1) {
  172.        line(i*LINE_WIDTH-1,lastY, i*LINE_WIDTH, Y);
  173.      }
  174.      lastY = Y;
  175.      //println(((float)height)*(((float)graph.get(i+graph_offset))/1024.0));
  176.   }
  177.  
  178.   //Various graph marks
  179.   stroke(128,0,0);
  180.   for(short i=0; i<width; i+=20) {
  181.     line(i,height/2, i+10, height/2);
  182.   }
  183.   byte no_marks = (byte)(height/50);
  184.  
  185.   if(no_marks<3)
  186.     no_marks = 3;
  187.   float mark_distance = (float)((float)height/((float)no_marks-1));
  188.   float mark_step = (float)(1024.0/((float)no_marks-1.0));
  189.  
  190.   textSize(32);
  191.   stroke(128);
  192.   for(byte i=0; (i+0)<no_marks; i+=1) {
  193.      
  194.      float pos = height-mark_distance*i;
  195.      line(0, pos, 5, pos);
  196.      line(textWidth(Integer.toString(round(i*mark_step)))+5, pos, width, pos);
  197.      if(i+1==no_marks)
  198.        text(1024, 5,pos+16);
  199.      else
  200.        text((int)round(i*mark_step), 5,pos+16);
  201.      fill(0, 102, 153);
  202.   }
  203.  
  204.   //Clock
  205.   short clock_size = (short)(images.get(0).width+5+textWidth(display_speed_ratio+"x"));
  206.   short clock_start = (short)(width-(clock_size+20));
  207.   fill(0,0,0,128);  //color red semi-transparent
  208.   strokeWeight(0);
  209.   rect(clock_start-2,2,clock_start+clock_size+2,images.get(0).height);  //solid black square
  210.  
  211.   image(images.get(0),clock_start, 2);
  212.   fill(0, 102, 0);
  213.   text(display_speed_ratio+"x", clock_start+images.get(0).width+5,32);
  214.  
  215.   //Communication speed
  216.   if(millis()-temporary_timer>=1000) {
  217.     // println("Bytes downloaded since last check: "+ (traffic_speed_counter-traffic_speed_counter_old)+ " (total: "+traffic_speed_counter+")");
  218.     last_speed = ((float)(traffic_speed_counter-traffic_speed_counter_old));//((float)traffic_speed_counter*1000)/((float)millis()-(float)last_time_displayed);
  219.     traffic_speed_counter_old =traffic_speed_counter;
  220.     temporary_timer = millis();
  221.   }
  222.   String speed_text = numberFormat((float)last_speed, 1024.0, (byte)1);
  223.   short speed_size = (short)(32+textWidth(new String("1000.1MB")));
  224.   short speed_start = (short)(clock_start-speed_size);
  225.  
  226.   fill(128,128,128);
  227.   rect(speed_start,15,30,2);  //wire
  228.   if(last_speed>0) {
  229.     fill(0,255,0);
  230.     for(byte i=0; i<28; i+=4) {
  231.       rect(speed_start+i+(last_speed>500?animation_step%4:(animation_step%16)/4),15,1,2);  //signal
  232.     }
  233.   }
  234.   strokeWeight(1);
  235.   fill(0, 102, 0);
  236.   text(speed_text, speed_start+32, 32);
  237.  
  238.  
  239.  
  240.  
  241.   //Reset display clocks
  242.   if(millis()-animation_timeout>=50) {
  243.     animation_step++;
  244.     animation_timeout = millis();
  245.   }
  246.   last_time_displayed = millis();
  247. }
  248. String numberFormat(float number, float base) {
  249.   return numberFormat(number, base, (byte)2);
  250. }
  251. String numberFormat(float number, byte float_spaces) {
  252.   return numberFormat(number, 1000.0, float_spaces);
  253. }
  254.  
  255. String numberFormat(float number, float base, byte float_spaces) {
  256.   String[] add = {"T","G","M","K", "", "m","u","n","p"};
  257.   byte pos = 4;
  258.   if(number!=0) {
  259.     while(number<1.0&&(pos+1)<add.length) {
  260.       number*=base;
  261.       pos++;
  262.     }
  263.     while(number>base&&(pos-1)>=0) {
  264.       number/=base;
  265.       pos--;
  266.     }
  267.   }
  268.   return Float.toString(((float)round(number*pow(10, float_spaces)))/pow(10, float_spaces))+add[pos];
  269. }
  270.  
  271. void sendMessageCode(int code, int data) {
  272.    myPort.write(code);
  273.    myPort.write(data);
  274.    println("Send: "+code+", "+data);
  275. }
  276. void resizePreserve(PImage image, short max_w, short max_h) {
  277.  
  278.   float scale = 1;
  279.   float scale2 = 1;
  280.   if(image.height>max_h) {
  281.     scale = ((float)max_h)/((float)image.height);
  282.   }
  283.   if(image.width>max_w) {
  284.     scale2 = ((float)max_w)/((float)image.width);
  285.   }
  286.   if(scale!=1||scale2!=1) {
  287.     scale = scale>scale2?scale2:scale;
  288.     image.resize((int)(((float)image.width)*scale), (int)(((float)image.height)*scale));
  289.   }
  290. }
  291.  
  292.  
  293. void mouseWheel(int delta) {
  294.   display_speed_ratio += delta;
  295.   if(display_speed_ratio<1)
  296.     display_speed_ratio = 1;
  297.    
  298.   if(display_speed_ratio == 1) {
  299.     elements_received_last = 0;
  300.     elements_received = 0;
  301.   }
  302.   //println("Displaying at speed 1/"+display_speed_ratio+" real speed.");
  303. }
  304.  
  305.  
  306. void keyPressed() {
  307.   switch(keyCode) {
  308.     case 82 : {
  309.       receive = !receive;
  310.       sendMessageCode((short)0, (short)(receive?1:0));
  311.       break;
  312.     }
  313.     case 39 : {
  314.       elements_received_last = 0;
  315.       elements_received = 0;
  316.       break;
  317.     }
  318.     default : {
  319.        println("Key '"+keyCode+"' has no effect.");
  320.     }
  321.   }
  322. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top