very basic controlp5 problem
in
Contributed Library Questions
•
1 year ago
hi all
after along struggle ive managed to cobble this code together merged from various sketches
it runs but I would like to improve it
im sure its very messy so some advice cleaning it up would be a help.
I ve had to comment out the all the code relating to the COG ARROW as the time slider is some how locked
to the rotation of the arrow
the slider allows me to pause the play of track and see current speed and lat,long time etc
but I have not worked out if or how to reverse the play any pointers with this would also be a great help
heres the code and sample gps text file
import controlP5.*;
ControlP5 controlP5;
int time;
boolean playPause = true; // true = play, false = pause
boolean revPause = true; // true = rev, false = pause
void play() { playPause = !playPause; } // toggle
void rev() { revPause = !revPause; } // toggle
//-----------------
String[] lines;
int index = 0;
//---------
float latitude =0.0;
String northSouth;
float longitude = 0.0;
String eastWest;
float heading = 0.0;
float COG =0.0;
float speed =0.0;
int hrs,mins,secs;
int thisDay, thisMonth,thisYear;
//-----------------------------------
float x;
float y;
PImage raster;
//-------
void setup(){
size(1000,600);
// background(0);
smooth();
controlP5 = new ControlP5(this);
controlP5.addSlider("back",0,width,10,height-55,width-75,20);
controlP5.addButton("rev",0,width-40,height-55,30,20);
controlP5.addSlider("time",0,width,10,height-30,width-75,20);
controlP5.addButton("play",0,width-40,height-30,30,20);
//}
PFont myFont =createFont(PFont.list()[2],24);
textFont(myFont);
//-------------
ellipseMode(CENTER);
smooth();
frameRate(10);
lines = loadStrings("gprmc.txt");
println("there are " + lines.length + " lines");
for (int index=0; index < lines.length; index++) {
println(lines[index]);
}
}
void draw (){ if (playPause) { time++; controlP5.controller("play").setLabel("STOP"); }
else { controlP5.controller("play").setLabel("Play"); }
if (revPause) { time++; controlP5.controller("rev").setLabel("back"); }
else { controlP5.controller("play").setLabel("Play"); }
if (time >= width) { time = 0; } // reset when right side is reached (only useful for this particular sketch)
controlP5.controller("time").setValue(time); // set the slider to time value
if (revPause) { time--; controlP5.controller("rev").setLabel("back"); }
else { controlP5.controller("back").setLabel("rev"); }
//randomSeed(1); // controls the randomness, to get the same output every time
for (int i=index; i<time; i++) { // the time variable controls the output
float c = map(i,0,time,0,255);
fill(255,255,255);
// }
background(0);
fill(255);
text(thisMonth+"/"+thisDay+"/"+thisYear,50,30);
text(hrs+":"+mins+":"+secs +" ",50,50);
text("Lng" +" " +eastWest, 50,70);
text("Lat " +" "+northSouth , 50,90);
text("heading "+ heading + "degrees",50,110);
// text(hrs+":"+mins+":"+secs +"GMT ",50,130);
//text("Lat " +" "+northSouth , 50,130);
// text("heading "+ heading + "degrees",50,150);
text("Speed M/Sec "+ speed ,50,170);
if (index < lines.length) {
String[] pieces = split(lines[index], ',');
int time = int(pieces[1]);
if (pieces.length == 13) {
float latitude = float(pieces[3]) / 100;
float longitude = float(pieces[5]) /100;
heading = float (pieces[7])*100.0;
northSouth = (pieces[5]);
eastWest = (pieces [3]);
speed = float(pieces [7]);
COG = float(pieces [8]);
int date = int (pieces[9]);
thisYear =date%100+2000;
//second two digits are month
thisMonth =(date%10000)/100;
//first two digits of date are day
thisDay = date/10000 ;
if (pieces.length == 13) {float mapX = float(pieces[3]);
float mapY = float(pieces[5]);
// println(mapX + " " + mapY);
println(pieces);
float w_start = 00048.0990; // Longtitude start (largest longitude value)
float w_end = 00048.06929; // Longtitude end (smallest longitude value)
float h_start = 5120.1023; // Latitude start (largest latitude value)
float h_end = 5120.060012; // Latitude end (smallest latitude value)
int mapX1 = 0;
int mapX2 = width;
int mapY1 = 0;
int mapY2 = height;
x = int(((180+mapY)/360)*width);
y = int(height - ((90+mapX)/180)*height);
x = map(mapY, w_start, w_end, mapX1, mapX2);
y = map(mapX, h_end, h_start, mapY1, mapY2);
ellipse(x, y, 10, 10);
hrs= time/1000;
//second two digits=minutes
mins = (time%10000)/100;
//last two digits =seconds
secs = (time%100);}
}
//{ //drawArrow(COG);
}
// }
// }
//--------------
// void drawArrow (float angle){
// translate(width/2,height/2);
//fill(80,200,230);
//ellipse(0,0,50,50);
// fill (0);
// rotate(radians(angle));
//draw the arrow .centre of arrow is at 0.0
// triangle (-10,0,0,-20,10,0);
// rect(-2,0,4,20);{
//}
// }
// point(x, y);
// }
// Go to the next line for the next run through draw()
index = index +1;
}
}
1