Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- String val; // Data received from the serial port
- Serial serialConnectionObject;
- int serialChannelIndexInt;
- float lat;
- float lng;
- int rectSizeX = 40 ;
- int rectSizeY = 40;
- float latcoord = 0;
- float longcoord = 0;
- boolean newData = false;
- float xCoord;
- float yCoord;
- void setup()
- {
- background(0);
- size(400,400, P2D);
- InitSerialConnectionVoid(0);
- String portName = Serial.list()[serialChannelIndexInt]; //change the 0 to a 1 or 2 etc. to match your port
- println(portName);
- serialConnectionObject.bufferUntil('n');
- }
- void draw()
- {
- beginCamera();
- camera();
- translate(lat,lng);
- endCamera();
- if(newData) {
- fill(#FFFFFF, 10);
- noStroke();
- rectMode(CENTER);
- scale(2);
- rect(xCoord, yCoord, rectSizeX, rectSizeY);
- }
- }
- void serialEvent(Serial p) {
- try {
- println("test");
- val = p.readString();
- println(val);
- JSONObject json = parseJSONObject(val);
- if (json == null) {
- newData = false;
- } else {
- println(json.toString());
- lat = json.getFloat("lat");
- lng = json.getFloat("lng");
- println(lat);
- LatLngtoXY(lat,lng);
- println(xCoord, yCoord);
- newData=true;
- }
- //latcoord = map(lat, lat-0.00045000045, lat+0.00045000045, 0, width);
- //longcoord = map(lng, lng-0.00045000063, lng+0.00045000063, 0, height);
- }
- catch(RuntimeException e) {
- e.printStackTrace();
- }
- }
- void LatLngtoXY(float lat, float lon) {
- float mapWidth = width;
- float mapHeight = height;
- // get x value
- xCoord = (lon+180)*(mapWidth/360);
- // convert from degrees to radians
- float latRad = lat*PI/180;
- // get y value
- float mercN = log(tan((PI/4)+(latRad/2)));
- yCoord = (mapHeight/2)-(mapWidth*mercN/(2*PI));
- }
- void InitSerialConnectionVoid(int _serialChannelIndexInt){
- serialChannelIndexInt = _serialChannelIndexInt;
- if(serialChannelIndexInt == Serial.list().length){ return; }
- try{
- serialConnectionObject = new Serial(this, Serial.list()[serialChannelIndexInt], 115200);
- serialConnectionObject.bufferUntil('n');
- }
- catch (RuntimeException e){
- serialConnectionObject = null;
- println(e);
- println("SERIAL CONNECTION FAILED");
- InitSerialConnectionVoid(serialChannelIndexInt + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement