281 lines
6.6 KiB
Plaintext
281 lines
6.6 KiB
Plaintext
import processing.serial.*;
|
|
import processing.net.*;
|
|
import java.io.FileWriter;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import spout.*;
|
|
|
|
boolean useSerial=true;
|
|
boolean useCubes=true;
|
|
Serial myCubes;
|
|
boolean updateCubes=false;
|
|
int cubecounter=0;
|
|
|
|
boolean spoutactive=false;
|
|
Spout spout;
|
|
PFont fontregular;
|
|
PFont font2am;
|
|
PFont font2amBig;
|
|
PImage image_line;
|
|
Serial myPort;
|
|
Server myServer;
|
|
String val = "0.0";
|
|
int speeds_show=3;
|
|
float[] speeds=new float[speeds_show];
|
|
int speeds_index=0; //points to next array index
|
|
float speedfloat=0;
|
|
float triggerspeedmin=15; //current triggerspeed
|
|
int unitdisplay=0;
|
|
int unitdisplay_count=9; //how much different units available
|
|
int xtextstart=10;
|
|
|
|
int[] randPosX=new int[speeds_show];
|
|
int[] randPosY=new int[speeds_show];
|
|
float[] randRot=new float[speeds_show];
|
|
long[] showTime=new long[speeds_show];
|
|
|
|
void setup() {
|
|
//size(800, 600, P3D);
|
|
size(1280, 800, P3D);
|
|
frameRate(30);
|
|
//fullScreen();
|
|
|
|
randPosX[0]=int(width/2+width*-0.1);
|
|
randPosX[1]=int(width/2+width*0.05);
|
|
randPosX[2]=int(width/2+width*-0.05);
|
|
|
|
randPosY[0]=int(height/2+height*-0.25);
|
|
randPosY[1]=int(height/2+height*0);
|
|
randPosY[2]=int(height/2+height*0.27);
|
|
|
|
fontregular=createFont("Blackout Midnight Umlauts.ttf",64);
|
|
//textFont(fontregular);
|
|
|
|
|
|
font2am=createFont("Blackout Zwo Uhr Nachts.ttf",64);
|
|
textFont(font2am);
|
|
|
|
font2amBig=createFont("Blackout Zwo Uhr Nachts.ttf",150);
|
|
|
|
|
|
myServer = new Server(this, 2323);
|
|
image_line = loadImage("bleeptrackline.png");
|
|
|
|
//String portName = "COM5";
|
|
//String portName = "/dev/ttyUSB0";
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (useSerial) {
|
|
String portName = Serial.list()[useportID];
|
|
portName="/dev/ttyUSB0";
|
|
println("Using Port:"+portName);
|
|
myPort = new Serial(this, portName, 115200);
|
|
|
|
|
|
}
|
|
|
|
if (useCubes) {
|
|
String portNameCubes="/dev/ttyUSB1";
|
|
myCubes = new Serial(this, portNameCubes, 115200);
|
|
}
|
|
|
|
sendSpeedTrapValue(int(triggerspeedmin));
|
|
|
|
background(0);
|
|
|
|
|
|
if (spoutactive){
|
|
spout=new Spout(this);
|
|
spout.createSender("Spout Processing");
|
|
}
|
|
}
|
|
|
|
|
|
// speed wird sein "20.08" oder
|
|
|
|
void draw() {
|
|
if (useSerial && myPort.available() > 0) {
|
|
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);
|
|
|
|
speedfloat=float(val);
|
|
|
|
speeds_index++;
|
|
speeds_index%=speeds_show;
|
|
showTime[speeds_index] = millis();
|
|
|
|
speeds[speeds_index]=speedfloat;
|
|
randRot[speeds_index] = random(-PI/10,PI/10);
|
|
randPosX[speeds_index] = int(width/2+width*random(-0.18,0.18));
|
|
|
|
updateCubes=true;
|
|
|
|
Date d=new Date();
|
|
|
|
println(d.getTime()+";"+speedfloat);
|
|
//pw.println(d.getTime()+";"+speedfloat);
|
|
//pw.flush();
|
|
appendTextToFile("speeds.txt",d.getTime()+";"+speedfloat);
|
|
|
|
unitdisplay=int(random(0,unitdisplay_count));
|
|
println("unitdisplay: "+unitdisplay);
|
|
|
|
triggerspeedmin=getTimesPercentile(0.8);
|
|
//triggerspeedmin=getTimesMax();
|
|
sendSpeedTrapValue(int(triggerspeedmin));
|
|
|
|
myServer.write(str(speedfloat));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
background(0);
|
|
colorMode(RGB, 255,255,255);
|
|
//fill(0,132,176); //35c3
|
|
fill(254,80,0); //36c3
|
|
|
|
//textSize(100);
|
|
|
|
|
|
|
|
for (int i=speeds_show;i>0;i--){
|
|
//text( nf(speeds[(speeds_show+speeds_index-1+i)%speeds_show], 1,2) + " km/h", xtextstart, lastytextpos);
|
|
int _speedIndex=(speeds_show+speeds_index-1+i)%speeds_show;
|
|
|
|
long _cshowTime=millis()-showTime[_speedIndex];
|
|
float _dim=1-constrain(_cshowTime/3000.0,0.0,1.0); //start at 1, goes down to 0 and stays there
|
|
fill(254*_dim,80*_dim,0); //36c3
|
|
|
|
textFont(font2amBig);
|
|
textAlign(CENTER, CENTER);
|
|
//text(nf(speeds[_speedIndex], 1,2) + " km/h" ,randPosX[_speedIndex],randPosY[_speedIndex]);
|
|
translate(randPosX[_speedIndex],randPosY[_speedIndex]);
|
|
rotate(randRot[_speedIndex]);
|
|
text(nf(speeds[_speedIndex], 1,2) + " km/h" ,0,0);
|
|
rotate(-randRot[_speedIndex]); //rotate back
|
|
translate(-randPosX[_speedIndex],-randPosY[_speedIndex]); //translate back
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
textSize(40);
|
|
text( "speed trap: "+nf(triggerspeedmin,1,1) +" km/h", 10, height-50);
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (spoutactive){
|
|
spout.sendTexture();
|
|
}
|
|
|
|
if (updateCubes) {
|
|
if (useCubes) {
|
|
colorMode(HSB, 360, 100, 100);
|
|
color col = color(random(0,360), 50, 50);
|
|
/*int _r=int(random(0,255));
|
|
int _g=int(random(0,255));
|
|
int _b=int(random(0,255));*/
|
|
//colorMode(RGB, 255,255,255);
|
|
int _r=int(red(col));
|
|
int _g=int(green(col));
|
|
int _b=int(blue(col));
|
|
|
|
|
|
myCubes.write("B,"+cubecounter+",10,"+_r+","+_g+","+_b+ 'X');
|
|
|
|
|
|
|
|
println("B,"+cubecounter+",10,"+_r+","+_g+","+_b+ 'X');
|
|
cubecounter++;
|
|
if (cubecounter>50){
|
|
updateCubes=false;
|
|
cubecounter=30;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void keyPressed() { //for testing
|
|
if (key == 't') {
|
|
println("t");
|
|
|
|
speedfloat=random(50);
|
|
|
|
|
|
speeds_index++;
|
|
speeds_index%=speeds_show;
|
|
showTime[speeds_index] = millis();
|
|
|
|
speeds[speeds_index]=speedfloat;
|
|
randRot[speeds_index] = random(-PI/10,PI/10);
|
|
randPosX[speeds_index] = int(width/2+width*random(-0.18,0.18));
|
|
|
|
updateCubes=true;
|
|
}
|
|
}
|
|
|
|
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){
|
|
if (useSerial) {
|
|
myPort.write(""+v+"\n");
|
|
println("Send trigger speed:"+v);
|
|
}
|
|
}
|
|
|
|
|
|
void appendTextToFile(String filename, String text){
|
|
File f = new File(dataPath(filename));
|
|
if(!f.exists()){
|
|
createFile(f);
|
|
}
|
|
try {
|
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
|
|
out.println(text);
|
|
out.close();
|
|
}catch (IOException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
void createFile(File f){
|
|
File parentDir = f.getParentFile();
|
|
try{
|
|
parentDir.mkdirs();
|
|
f.createNewFile();
|
|
}catch(Exception e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|