35C3_Teststrecke/lichtschranke/speedshow/speedshow.pde

192 lines
4.2 KiB
Plaintext
Raw Normal View History

2018-12-26 23:56:32 +00:00
import processing.serial.*;
import java.io.FileWriter;
import java.io.*;
import java.util.*;
2018-12-27 14:08:41 +00:00
import spout.*;
boolean spoutactive=true;
Spout spout;
2018-12-27 13:51:09 +00:00
PFont fontregular;
PImage image_line;
2018-12-26 23:56:32 +00:00
Serial myPort;
String val = "0.0";
2018-12-27 13:51:09 +00:00
int speeds_show=10;
2018-12-26 23:56:32 +00:00
float[] speeds=new float[speeds_show];
int speeds_index=0; //points to next array index
float speedfloat=0;
2018-12-27 13:51:09 +00:00
float triggerspeedmin=15; //current triggerspeed
2018-12-26 23:56:32 +00:00
Date d=new Date();
PrintWriter pw;
int unitdisplay=0;
2018-12-27 01:13:11 +00:00
int unitdisplay_count=9; //how much different units available
2018-12-26 23:56:32 +00:00
2018-12-27 13:51:09 +00:00
int xtextstart=10;
2018-12-26 23:56:32 +00:00
void setup() {
2018-12-27 14:18:00 +00:00
size(800, 600, P3D);
2018-12-26 23:56:32 +00:00
frameRate(30);
2018-12-27 13:51:09 +00:00
2018-12-27 14:18:00 +00:00
fontregular=createFont("Montserrat-Regular.ttf",64);
2018-12-27 13:51:09 +00:00
textFont(fontregular);
image_line= loadImage("bleeptrackline.png");
//String portName = "COM5";
2018-12-27 01:13:11 +00:00
//String portName = "/dev/ttyUSB0";
2018-12-27 13:51:09 +00:00
int useportID=-1;
println("Serialports:");
for(int i=0;i<Serial.list().length;i++){
if (Serial.list()[i].contains("ttyUSB") || Serial.list()[i].contains("COM")){
println(i+" "+Serial.list()[i]);
if (useportID<0){ //no port set yet
useportID=i;
}
}
}
String portName = Serial.list()[useportID];
println("Using Port:"+portName);
2018-12-26 23:56:32 +00:00
myPort = new Serial(this, portName, 115200);
2018-12-27 13:51:09 +00:00
sendSpeedTrapValue(int(triggerspeedmin));
2018-12-26 23:56:32 +00:00
pw=createWriter("speeds.txt");
background(0);
2018-12-27 14:08:41 +00:00
if (spoutactive){
spout=new Spout(this);
spout.createSender("Spout Processing");
}
2018-12-26 23:56:32 +00:00
}
// speed wird sein "20.08" oder
void draw() {
if ( myPort.available() > 0) {
2018-12-27 13:51:09 +00:00
String sread = myPort.readString();
println("serialRead: " + sread);
if (sread.length()>=7 && sread.substring(0,6).equals("speed=")){ //new speed measurement
val = sread.substring(6,sread.length()-1);
2018-12-26 23:56:32 +00:00
2018-12-27 13:51:09 +00:00
speedfloat=float(val);
speeds[speeds_index]=speedfloat;
speeds_index++;
speeds_index%=speeds_show;
println(d.getTime()+";"+speedfloat);
pw.println(d.getTime()+";"+speedfloat);
pw.flush();
unitdisplay=int(random(0,unitdisplay_count));
println("unitdisplay: "+unitdisplay);
triggerspeedmin=getTimesPercentile(0.8);
//triggerspeedmin=getTimesMax();
sendSpeedTrapValue(int(triggerspeedmin));
}
2018-12-26 23:56:32 +00:00
}
background(0);
2018-12-27 13:51:09 +00:00
fill(0,132,176);
textSize(100);
2018-12-26 23:56:32 +00:00
2018-12-27 13:51:09 +00:00
int xtext=xtextstart;
int ytext=100;
2018-12-26 23:56:32 +00:00
switch(unitdisplay){
case 0:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat,1,2) + " km/h" , xtext, ytext);
2018-12-26 23:56:32 +00:00
break;
case 1:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*0.621371,1,2) + " mph" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 2:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*0.000809848,1,6) + " Mach" , xtext, ytext);
2018-12-26 23:56:32 +00:00
break;
2018-12-27 01:13:11 +00:00
case 3:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*0.911344,1,2) + " ft/s" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 4:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*0.277778,1,2) + " m/s" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 5:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*0.539957,1,2) + " knots" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 6:
2018-12-27 14:08:41 +00:00
text(nf(speedfloat*0.000000000926567,1,9) + " c" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 7:
2018-12-27 13:51:09 +00:00
text(nf(speedfloat*54.67469,1,0) + " ft/min" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
case 8:
2018-12-27 13:51:09 +00:00
text(nf(pow((speedfloat*0.277778/0.836), 2.0/3),1,0) + " Bft" , xtext, ytext);
2018-12-27 01:13:11 +00:00
break;
2018-12-26 23:56:32 +00:00
}
2018-12-27 13:51:09 +00:00
//Line
image(image_line, 10, 100);
2018-12-26 23:56:32 +00:00
2018-12-27 13:51:09 +00:00
int reducesizefactor=40;
int lastytextpos=200;
2018-12-26 23:56:32 +00:00
for (int i=speeds_show;i>0;i--){
2018-12-27 13:51:09 +00:00
float colorpercentage=i*1.0/speeds_show;
fill(0,132*colorpercentage+163*(1-colorpercentage),176*colorpercentage+86*(1-colorpercentage));
textSize(50-(1-colorpercentage)*reducesizefactor);
text( nf(speeds[(speeds_show+speeds_index-1+i)%speeds_show], 1,2) + " km/h", xtextstart, lastytextpos);
lastytextpos+=50-(1-colorpercentage)*reducesizefactor;
2018-12-26 23:56:32 +00:00
}
2018-12-27 13:51:09 +00:00
textSize(40);
text( "speed trap: "+nf(triggerspeedmin,1,1) +" km/h", 10, height-50);
2018-12-26 23:56:32 +00:00
2018-12-27 14:08:41 +00:00
if (spoutactive){
spout.sendTexture();
}
2018-12-27 13:51:09 +00:00
}
float getTimesMax(){
float speedmax=0;
for (int i=0;i<speeds_show;i++){
if (speedmax<speeds[i]){
speedmax=speeds[i];
}
}
return speedmax;
}
float getTimesPercentile(float p){ //p==1 would be max value
float[] sortedspeeds=sort(speeds);
return sortedspeeds[int(p*sortedspeeds.length)];
}
void sendSpeedTrapValue(int v){
myPort.write(""+v+"\n");
println("Send trigger speed:"+v);
2018-12-26 23:56:32 +00:00
}