Advertisement
KRITSADA

Processing Serial

Sep 18th, 2019
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2.  
  3.       String val;     // Data received from the serial port
  4.       Serial serialConnectionObject;
  5.       int serialChannelIndexInt;
  6.       float lat;
  7.       float lng;
  8.       int rectSizeX = 40 ;
  9.       int rectSizeY = 40;
  10.       float latcoord = 0;
  11.       float longcoord = 0;
  12.       boolean newData = false;
  13.       float xCoord;
  14.       float yCoord;
  15.  
  16.       void setup()
  17.       {
  18.         background(0);
  19.         size(400,400, P2D);
  20.         InitSerialConnectionVoid(0);
  21.         String portName = Serial.list()[serialChannelIndexInt]; //change the 0 to a 1 or 2 etc. to match your port
  22.         println(portName);
  23.         serialConnectionObject.bufferUntil('n');
  24.       }
  25.  
  26.       void draw()
  27.       {
  28.         beginCamera();
  29.         camera();
  30.         translate(lat,lng);
  31.         endCamera();
  32.  
  33.         if(newData) {
  34.           fill(#FFFFFF, 10);
  35.           noStroke();
  36.           rectMode(CENTER);
  37.           scale(2);
  38.           rect(xCoord, yCoord, rectSizeX, rectSizeY);
  39.         }
  40.  
  41.       }
  42.  
  43.       void serialEvent(Serial p) {
  44.         try {
  45.           println("test");
  46.             val = p.readString();
  47.               println(val);
  48.             JSONObject json = parseJSONObject(val);
  49.             if (json == null) {
  50.               newData = false;
  51.             } else {
  52.               println(json.toString());
  53.               lat = json.getFloat("lat");
  54.               lng = json.getFloat("lng");
  55.               println(lat);
  56.               LatLngtoXY(lat,lng);
  57.               println(xCoord, yCoord);
  58.               newData=true;
  59.             }
  60.             //latcoord = map(lat, lat-0.00045000045, lat+0.00045000045, 0, width);
  61.             //longcoord = map(lng, lng-0.00045000063, lng+0.00045000063, 0, height);
  62.  
  63.  
  64.           }
  65.         catch(RuntimeException e) {
  66.            e.printStackTrace();
  67.         }
  68.  
  69.       }
  70.  
  71.       void LatLngtoXY(float lat, float lon) {
  72.           float mapWidth    = width;
  73.           float mapHeight   = height;
  74.  
  75.           // get x value
  76.           xCoord = (lon+180)*(mapWidth/360);
  77.  
  78.           // convert from degrees to radians
  79.           float latRad = lat*PI/180;
  80.           // get y value
  81.           float mercN = log(tan((PI/4)+(latRad/2)));
  82.           yCoord     = (mapHeight/2)-(mapWidth*mercN/(2*PI));
  83.       }
  84.  
  85.       void InitSerialConnectionVoid(int _serialChannelIndexInt){
  86.  
  87.           serialChannelIndexInt = _serialChannelIndexInt;
  88.  
  89.           if(serialChannelIndexInt == Serial.list().length){ return; }
  90.           try{
  91.  
  92.               serialConnectionObject = new Serial(this, Serial.list()[serialChannelIndexInt], 115200);
  93.               serialConnectionObject.bufferUntil('n');
  94.  
  95.           }
  96.           catch (RuntimeException e){
  97.  
  98.               serialConnectionObject = null;
  99.               println(e);
  100.               println("SERIAL CONNECTION FAILED");
  101.               InitSerialConnectionVoid(serialChannelIndexInt + 1);
  102.  
  103.           }
  104.  
  105.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement