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