Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- import java.awt.event.*;
- // The serial port:
- Serial myPort;
- static int MAX_GRAPH_SIZE = 65536;
- static byte LINE_WIDTH = 1;
- long start_time = 0;
- long last_time_displayed = 0;
- long last_time_loaded_new = 0;
- long last_time_received = 0;
- short display_speed_ratio = 1;
- int elements_received = 0;
- int elements_received_last = 0;
- boolean receive = true;
- long animation_step = 0;
- long animation_timeout = 0;
- int traffic_speed_counter = 0;
- int traffic_speed_counter_old = 0;
- long temporary_timer = 0;
- float last_speed = 0;
- //boolean connected = false;
- //Array of bytes received
- ArrayList<Short> graph;
- ArrayList<PImage> images;
- void setup() {
- size(800, 600);
- // List serial ports and chose the first one
- println(Serial.list());
- while(myPort==null) {
- try {
- myPort = new Serial(this, Serial.list()[6], 9600);
- }
- catch(Throwable e) {
- println("Failed to connect, waiting and retrying.");
- try {Thread.sleep(1000);} catch(Throwable ee) {break;}
- }
- }
- //Initialise the byte array
- graph = new ArrayList<Short>();
- //Window configuration
- if (frame != null) {
- frame.setResizable(true);
- }
- //Aditional events
- //addMouseWheelListener(new MouseWheelListener() {
- // public void mouseWheelMoved(MouseWheelEvent mwe) {
- // mouseWheel(mwe.getWheelRotation());
- //}});
- //Loading images and fonts
- images = new ArrayList<PImage>();
- images.add(loadImage("hi-tech-clock_transparent.png"));
- resizePreserve(images.get(0), (short)32, (short)32);
- //Time
- start_time = millis();
- try {Thread.sleep(3000);} catch(Throwable ee) {return;}
- sendMessageCode((short)2, (short)50);
- //Reset
- sendMessageCode((short)1, (short)0);
- //Initialise arduino
- sendMessageCode((byte)0, (byte)1);
- /*
- new Thread() {
- @Override
- public void run() {
- short num = 0;
- while(true) {
- num++;
- if(num>255)
- num = 0;
- sendMessageCode((short)3, (short)num);
- try {Thread.sleep(500);} catch(Throwable ee) {break;}
- }
- }
- }.start();*/
- }
- void serialEvent(Serial robotPort) {
- while (robotPort.available() >= 2) {
- short number = 0;
- for(byte i=0; i<2; i++) {
- short received = (short)robotPort.read();
- if(received<0) {
- println("No data in serial though available() returned true!");
- i=10;
- break;
- }
- if(received>255) {
- println("Number greater than 255 received!");
- i=10;
- break;
- }
- //println("Byte received: "+Integer.toString((int)received));
- if(i==0) {
- print(received + " + ");
- number += received;
- }
- else {
- number += received*256;
- println(received + "*256 (that's " + received*256 + ") = " + number);
- }
- traffic_speed_counter++;
- }
- if(display_speed_ratio>1) {
- elements_received++;
- elements_received_last++;
- }
- last_time_received = millis();
- if(number>1024||number<0) {
- //if(number-1024<32) //Is data
- // processMessageCode(
- println("Strange number received: "+number);
- // Remove first ten bits from number
- println("Code: "+(number-1024));
- //sendMessageCode((byte)1, (byte)0);
- break;
- }
- else if(receive)
- graph.add(number);
- }
- //if(graph.size()>MAX_GRAPH_SIZE)
- // graph.removeRange(0, graph.size()-MAX_GRAPH_SIZE);
- while(graph.size()>MAX_GRAPH_SIZE)
- graph.remove(0);
- //println("Die!");
- }
- void draw() {
- background(0);
- int graph_offset = 0;
- if(display_speed_ratio>1) {
- if(millis()-last_time_loaded_new>display_speed_ratio*(millis()-last_time_received)) {
- println("Adding " + Integer.toString(elements_received_last/display_speed_ratio) +" elements out of " + elements_received + " new ones.");
- elements_received -= elements_received_last/display_speed_ratio;
- elements_received_last = 0;
- last_time_loaded_new = millis();
- }
- graph_offset = width>((graph.size()-elements_received)*LINE_WIDTH)?0:(graph.size()-elements_received)-floor((width/LINE_WIDTH));
- }
- else {
- graph_offset = width>(graph.size()*LINE_WIDTH)?0:graph.size()-floor((width/LINE_WIDTH));
- }
- int lastY = -1;
- int max = (graph.size()-1);
- stroke(0, 140, 0);
- for(int i=0; i+graph_offset<max; i++) {
- /*if((i*40)%256>=128)
- stroke(0, 255-(i*40)%128, 0);
- else
- stroke(0, 128+(i*40)%128, 0);
- line(i*LINE_WIDTH,height-(int)(((float)height)*(((float)graph.get(i+graph_offset))/1024.0)), i*LINE_WIDTH, height);*/
- int Y = height-(int)(((float)height)*(((float)graph.get(i+graph_offset))/1024.0));
- if(lastY!=-1) {
- line(i*LINE_WIDTH-1,lastY, i*LINE_WIDTH, Y);
- }
- lastY = Y;
- //println(((float)height)*(((float)graph.get(i+graph_offset))/1024.0));
- }
- //Various graph marks
- stroke(128,0,0);
- for(short i=0; i<width; i+=20) {
- line(i,height/2, i+10, height/2);
- }
- byte no_marks = (byte)(height/50);
- if(no_marks<3)
- no_marks = 3;
- float mark_distance = (float)((float)height/((float)no_marks-1));
- float mark_step = (float)(1024.0/((float)no_marks-1.0));
- textSize(32);
- stroke(128);
- for(byte i=0; (i+0)<no_marks; i+=1) {
- float pos = height-mark_distance*i;
- line(0, pos, 5, pos);
- line(textWidth(Integer.toString(round(i*mark_step)))+5, pos, width, pos);
- if(i+1==no_marks)
- text(1024, 5,pos+16);
- else
- text((int)round(i*mark_step), 5,pos+16);
- fill(0, 102, 153);
- }
- //Clock
- short clock_size = (short)(images.get(0).width+5+textWidth(display_speed_ratio+"x"));
- short clock_start = (short)(width-(clock_size+20));
- fill(0,0,0,128); //color red semi-transparent
- strokeWeight(0);
- rect(clock_start-2,2,clock_start+clock_size+2,images.get(0).height); //solid black square
- image(images.get(0),clock_start, 2);
- fill(0, 102, 0);
- text(display_speed_ratio+"x", clock_start+images.get(0).width+5,32);
- //Communication speed
- if(millis()-temporary_timer>=1000) {
- // println("Bytes downloaded since last check: "+ (traffic_speed_counter-traffic_speed_counter_old)+ " (total: "+traffic_speed_counter+")");
- last_speed = ((float)(traffic_speed_counter-traffic_speed_counter_old));//((float)traffic_speed_counter*1000)/((float)millis()-(float)last_time_displayed);
- traffic_speed_counter_old =traffic_speed_counter;
- temporary_timer = millis();
- }
- String speed_text = numberFormat((float)last_speed, 1024.0, (byte)1);
- short speed_size = (short)(32+textWidth(new String("1000.1MB")));
- short speed_start = (short)(clock_start-speed_size);
- fill(128,128,128);
- rect(speed_start,15,30,2); //wire
- if(last_speed>0) {
- fill(0,255,0);
- for(byte i=0; i<28; i+=4) {
- rect(speed_start+i+(last_speed>500?animation_step%4:(animation_step%16)/4),15,1,2); //signal
- }
- }
- strokeWeight(1);
- fill(0, 102, 0);
- text(speed_text, speed_start+32, 32);
- //Reset display clocks
- if(millis()-animation_timeout>=50) {
- animation_step++;
- animation_timeout = millis();
- }
- last_time_displayed = millis();
- }
- String numberFormat(float number, float base) {
- return numberFormat(number, base, (byte)2);
- }
- String numberFormat(float number, byte float_spaces) {
- return numberFormat(number, 1000.0, float_spaces);
- }
- String numberFormat(float number, float base, byte float_spaces) {
- String[] add = {"T","G","M","K", "", "m","u","n","p"};
- byte pos = 4;
- if(number!=0) {
- while(number<1.0&&(pos+1)<add.length) {
- number*=base;
- pos++;
- }
- while(number>base&&(pos-1)>=0) {
- number/=base;
- pos--;
- }
- }
- return Float.toString(((float)round(number*pow(10, float_spaces)))/pow(10, float_spaces))+add[pos];
- }
- void sendMessageCode(int code, int data) {
- myPort.write(code);
- myPort.write(data);
- println("Send: "+code+", "+data);
- }
- void resizePreserve(PImage image, short max_w, short max_h) {
- float scale = 1;
- float scale2 = 1;
- if(image.height>max_h) {
- scale = ((float)max_h)/((float)image.height);
- }
- if(image.width>max_w) {
- scale2 = ((float)max_w)/((float)image.width);
- }
- if(scale!=1||scale2!=1) {
- scale = scale>scale2?scale2:scale;
- image.resize((int)(((float)image.width)*scale), (int)(((float)image.height)*scale));
- }
- }
- void mouseWheel(int delta) {
- display_speed_ratio += delta;
- if(display_speed_ratio<1)
- display_speed_ratio = 1;
- if(display_speed_ratio == 1) {
- elements_received_last = 0;
- elements_received = 0;
- }
- //println("Displaying at speed 1/"+display_speed_ratio+" real speed.");
- }
- void keyPressed() {
- switch(keyCode) {
- case 82 : {
- receive = !receive;
- sendMessageCode((short)0, (short)(receive?1:0));
- break;
- }
- case 39 : {
- elements_received_last = 0;
- elements_received = 0;
- break;
- }
- default : {
- println("Key '"+keyCode+"' has no effect.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement