simpler version with cube control for 36c3

This commit is contained in:
interfisch 2019-12-27 13:05:52 +01:00
parent e625736197
commit d0e77d7cac
6 changed files with 280 additions and 203 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,203 +0,0 @@
import processing.serial.*;
import processing.net.*;
import java.io.FileWriter;
import java.io.*;
import java.util.*;
import spout.*;
boolean spoutactive=true;
Spout spout;
PFont fontregular;
PImage image_line;
Serial myPort;
Server myServer;
String val = "0.0";
int speeds_show=10;
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;
void setup() {
size(800, 600, P3D);
frameRate(30);
fontregular=createFont("Montserrat-Regular.ttf",64);
textFont(fontregular);
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;
}
}
}
String portName = Serial.list()[useportID];
println("Using Port:"+portName);
myPort = new Serial(this, portName, 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 ( 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[speeds_index]=speedfloat;
speeds_index++;
speeds_index%=speeds_show;
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);
fill(0,132,176);
textSize(100);
int xtext=xtextstart;
int ytext=100;
switch(unitdisplay){
case 0:
text(nf(speedfloat,1,2) + " km/h" , xtext, ytext);
break;
case 1:
text(nf(speedfloat*0.621371,1,2) + " mph" , xtext, ytext);
break;
case 2:
text(nf(speedfloat*0.000809848,1,6) + " Mach" , xtext, ytext);
break;
case 3:
text(nf(speedfloat*0.911344,1,2) + " ft/s" , xtext, ytext);
break;
case 4:
text(nf(speedfloat*0.277778,1,2) + " m/s" , xtext, ytext);
break;
case 5:
text(nf(speedfloat*0.539957,1,2) + " knots" , xtext, ytext);
break;
case 6:
text(nf(speedfloat*0.000000000926567,1,9) + " c" , xtext, ytext);
break;
case 7:
text(nf(speedfloat*54.67469,1,0) + " ft/min" , xtext, ytext);
break;
case 8:
text(nf(pow((speedfloat*0.277778/0.836), 2.0/3),1,0) + " Bft" , xtext, ytext);
break;
}
//Line
image(image_line, 10, 100);
int reducesizefactor=40;
int lastytextpos=200;
for (int i=speeds_show;i>0;i--){
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;
}
textSize(40);
text( "speed trap: "+nf(triggerspeedmin,1,1) +" km/h", 10, height-50);
if (spoutactive){
spout.sendTexture();
}
}
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);
}
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();
}
}

View File

@ -0,0 +1,280 @@
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();
}
}