I'm new to programming and most of what I do is copied and not from any thorough knowledge of programming. I know a little when I need to know a lot more to do what I want.
I'm trying to use a serial connection to move a map. To do this I found the modestmaps library and the example translates the mouse position to move a map. That's neat, but I want to be able to move the map with GPS over serial. I have an arduino program that can read a GPS rx and output the lat and lon in a buffer. I broke the connector though and don't have GPS at the moment so I just tried to test the use of serial with the modestmaps.
Here's what happened, I decided to send attitude X,Y,Z and get it to act like a mouse by translating X and Y by converting using the map(); That seems to work to get a mouselike coordinate, but I can't do much with it. I can get modestmaps to display the arduino XYZ and I can get it to keep the map grabbed.
What I couldn't figure out at first is how to get the modestmaps to see that a change in the arduino XYZ is the same as a mouse movement. I got over that hurdle and can now read GPS. The parsing is done on the Arduino and lat and lon is sent as an array.
Here's all the borrowed code that's currently working to read serial:
import processing.opengl.*;
import com.modestmaps.*;
import com.modestmaps.core.*;
import com.modestmaps.geo.*;
import com.modestmaps.providers.*;
import processing.serial.*;
Serial myPort; // Create object from Serial class
final String serialPort = "COM4"; // replace this with your serial port. On windows you will need something like "COM1".
//final String serialPort = "COM4"; // replace this with your serial port. On windows you will need something like "COM1".
float [] q = new float [4];
float [] hq = null;
float [] Euler = new float [3]; // psi, theta, phi
float newX = q[0] / 10000000;
float newY = q[1] / 10000000;
int lf = 10; // 10 is '\n' in ASCII
byte[] inBuffer = new byte[22]; // this is the number of chars on each line from the Arduino (including /r/n)
InteractiveMap map;
ZoomButton out = new ZoomButton(5,5,14,14,false);
ZoomButton in = new ZoomButton(22,5,14,14,true);
PanButton up = new PanButton(14,25,14,14,UP);
PanButton down = new PanButton(14,57,14,14,DOWN);
PanButton left = new PanButton(5,41,14,14,LEFT);
PanButton right = new PanButton(22,41,14,14,RIGHT);
//PFont letters;
Button[] buttons = {
in, out, up, down, left, right };
PFont font;
boolean gui = true;
final int burst = 32;
int count = 0;
void myDelay(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) { }
}
void setup() {
// size(300,600);
// size(VIEW_SIZE_X, VIEW_SIZE_Y);
size(640, 480, OPENGL);
smooth();
// myPort = new Serial(this, serialPort, 115200);
myPort = new Serial(this, serialPort, 38400);
String template = "http://{S}.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png";
String[] subdomains = new String[] { "otile1", "otile2", "otile3", "otile4" }; // optional
map = new InteractiveMap(this, new TemplatedMapProvider(template, subdomains));
map.setCenterZoom(new Location(39.425, -77.461), 17);
// map.setCenterZoom(new Location(newX, -newY), 17);
font = createFont("Helvetica", 12);
myPort.clear();
}
float decodeFloat(String inString) {
byte [] inData = new byte[4];
if(inString.length() == 8) {
inData[0] = (byte) unhex(inString.substring(0, 2));
inData[1] = (byte) unhex(inString.substring(2, 4));
inData[2] = (byte) unhex(inString.substring(4, 6));
inData[3] = (byte) unhex(inString.substring(6, 8));
}
int intbits = (inData[3] << 24) | ((inData[2] & 0xff) << 16) | ((inData[1] & 0xff) << 8) | (inData[0] & 0xff);
return Float.intBitsToFloat(intbits);
}
void serialEvent(Serial pp) {
if(pp.available() >= 18) {
String inputString = pp.readStringUntil('\n');
//print(inputString);
if (inputString != null && inputString.length() > 0) {
String [] inputStringArr = split(inputString, ",");
if(inputStringArr.length >= 5) { // q1,q2,q3,q4,\r\n so we have 5 elements
q[0] = decodeFloat(inputStringArr[0]);
q[1] = decodeFloat(inputStringArr[1]);
q[2] = decodeFloat(inputStringArr[2]);
q[3] = decodeFloat(inputStringArr[3]);
}
}
count = count + 1;
if(burst == count) { // ask more data when burst completed
pp.write("q" + char(burst));
count = 0;
}
} }
void draw() {
// background(0);
float newX = q[0] / 10000000;
float newY = q[1] / 10000000;
println((float)newX + " " + (float)newY);
println();
map.draw();
map.mouseDragged();
if (gui) {
textFont(font, 12);
map.setCenterZoom(new Location(newX, newY), 17);
Location location = map.pointLocation(newX, -newY);
// draw the mouse location, bottom left:
fill(0);
noStroke();
rect(5, height-5-g.textSize, textWidth("mouse: " + location), g.textSize+textDescent());
fill(255,255,0);
textAlign(LEFT, BOTTOM);
text("mouse: " + location, 5, height-5);
// grab the center
// location = map.pointLocation(newX, newY);
location = map.pointLocation(width/2, height/2);
// draw the center location, bottom right:
fill(0);
noStroke();
float rw = textWidth("map: " + location);
rect(width-5-rw, height-5-g.textSize, rw, g.textSize+textDescent());
fill(255,255,0);
textAlign(RIGHT, BOTTOM);
text("map: " + location, width-5, height-5);
// location = new Location(39.425, -77.461); //(51.500, -0.126);
location = new Location(newX, -newY); //(51.500, -0.126);
Point2f p = map.locationPoint(location);
fill(0,255,128);
stroke(255,255,0);
ellipse(p.x, p.y, 10, 10);
}
}
void mouseDragged() {
boolean hand = true;
if (gui) {
for (int i = 0; i < buttons.length; i++) {
hand = hand || buttons[i].mouseOver();
// if (!hand) break;
}
}
if (hand) {
map.mouseDragged();
}
}