Processing Code has 4 tabs
/////////////////////////////////////////GUI2_demo4_plus Tab
import processing.serial.*;
Serial myPort; // Create object from Serial class
float PX=0;
float PY=0;
float PZ=0;
float ROT=0;
float sx=0;
float sy=0;
float sz=0;
float sa=0;
char datatype;
byte [] inData = new byte[5];
int inptr=0;
int rstate=0;
float www=0;
void serialEvent(Serial myPort)
{
while (myPort.available ()>0)
{
switch(rstate)
{
case 0:
datatype = myPort.readChar();
if (datatype=='x'||datatype=='y'||datatype=='z'||datatype=='a')
{
rstate=1;
inptr=0;
}
break;
case 1:
inData[inptr++]=(byte)myPort.readChar();
if (inptr==4)
{
int intbit = 0;
intbit = (inData[3] << 24) | ((inData[2] & 0xff) << 16) | ((inData[1] & 0xff) << 8) | (inData[0] & 0xff);
float f = Float.intBitsToFloat(intbit);
switch(datatype)
{
case 'x':
sx=f;
break;
case 'y':
sy=f;
break;
case 'z':
PX=sx;
PY=sy;
PZ=f;
ROT=sa;
break;
case 'a':
sa=f;
break;
}
rstate=0;
}
break;
}
}
}
class pointer
{
PVector tip = new PVector(0, 0, 0);
float rotation=0;
boolean active=false;
boolean demoMode=false;
pointer(PApplet p,int index)
{
myPort =new Serial(p, Serial.list()[0], 19200);
active=true;
}
void update()
{
if (demoMode)
{
tip.x=(mouseX-width/2)/4;
tip.y=(mouseY-height/2)/4;
tip.z=40+20*sin(www+=0.01);//random(20,50);//40.0;
}
else
{
// tip.set(PX,PY,PZ);
tip.x=PX;
tip.y=PY;
tip.z=PZ;
rotation=ROT;
}
}
};
///////////////////////////////////////////////////////////////////////////OBJECTS TAB
//============================================================
// Measuring objects for 3D digitizer
// Objects implements function for 3D and 2D screen drawing and
// 2D export.
// (F)DZL 2015
//============================================================
//============================================================
// Base class for all objects
//============================================================
public abstract class object
{
abstract void draw(); //-Draw to screen (3D)
abstract void modify(PVector p); //-Modify object
abstract void project(); //-Draw to 2D
abstract void export(); //-Export (draw .PDF compatible)
};
//============================================================
// Single 3D point (1mmm box)
//============================================================
class gpoint extends object
{
PVector pos;
color lineColor=color(255);
color projectColor=color(100);
color exportColor=color(0);
gpoint(PVector p)
{
pos.x=p.x;
pos.y=p.y;
pos.z=p.z;
}
gpoint(float x, float y, float z)
{
pos=new PVector(x, y, z);
}
void draw()
{
stroke(lineColor);
pushMatrix();
translate(pos.x, pos.y, pos.z);
box(1);
popMatrix();
}
void project()
{
stroke(projectColor);
line(pos.x-5, pos.y, pos.x+5, pos.y);
line(pos.x, pos.y-5, pos.x, pos.y+5);
}
void export()
{
stroke(exportColor);
line(pos.x-5, pos.y, pos.x+5, pos.y);
line(pos.x, pos.y-5, pos.x, pos.y+5);
}
void modify(PVector p)
{
pos.x=p.x;
pos.y=p.y;
pos.z=p.z;
}
}
//============================================================
// Single 10mm circle
//============================================================
class gcircle extends object
{
PVector pos;
color projectColor=color(100);
color exportColor=color(0);
color lineColor=color(255);
boolean filled=true;
gcircle(PVector p)
{
pos.x=p.x;
pos.y=p.y;
pos.z=p.z;
}
gcircle(float x, float y, float z)
{
pos=new PVector(x, y, z);
}
void draw()
{
stroke(lineColor);
pushMatrix();
translate(pos.x, pos.y, pos.z);
ellipse(0, 0, 10, 10);
popMatrix();
}
void project()
{
stroke(projectColor);
ellipse(pos.x, pos.y, 10, 10);
}
void export()
{
if (filled)
{
noStroke();
fill(exportColor);
} else
{
noFill();
stroke(exportColor);
}
ellipse(pos.x, pos.y, 10, 10);
}
void modify(PVector p)
{
pos.x=p.x;
pos.y=p.y;
pos.z=p.z;
}
}
//============================================================
// Open loop feature
//============================================================
class gfeature extends object
{
boolean filled=true;
PVector pos=new PVector(0, 0, 0);
color projectColor=color(100);
color exportColor=color(100, 100, 0);
color lineColor=color(255, 255, 0);
color anchorColor=color(0, 255, 0);
ArrayList<PVector> figure = new ArrayList<PVector>();
gfeature(PVector p)
{
pos.x=p.x;
pos.y=p.y;
pos.z=p.z;
figure.add(new PVector(pos.x, pos.y, pos.z));
}
gfeature(float x, float y, float z)
{
pos=new PVector(x, y, z);
figure.add(new PVector(pos.x, pos.y, pos.z));
}
void draw()
{
pushMatrix();
translate(pos.x, pos.y, pos.z);
stroke(anchorColor);
ellipse(0, 0, 5, 5);
popMatrix();
float x0=pos.x;
float y0=pos.y;
float z0=pos.z;
stroke(lineColor);
for (PVector p : figure)
{
line(x0, y0, z0, p.x, p.y, p.z);
x0=p.x;
y0=p.y;
z0=p.z;
}
line(x0, y0, z0, pos.x, pos.y, pos.z);
}
void project()
{
stroke(projectColor);
float x0=pos.x;
float y0=pos.y;
float z0=pos.z;
for (PVector p : figure)
{
line(x0, y0, p.x, p.y);
x0=p.x;
y0=p.y;
z0=p.z;
}
line(x0, y0, pos.x, pos.y);
}
void export()
{
if (filled)
{
noStroke();
fill(exportColor);
} else
{
noFill();
stroke(exportColor);
}
PShape loop=createShape();
loop.beginShape();
for (PVector p : figure)
{
loop.vertex(p.x, p.y);
}
loop.vertex(pos.x, pos.y);
loop.endShape();
shape(loop);
}
void modify(PVector p)
{
figure.add(new PVector(p.x, p.y, p.z));
}
}
////////////////////////////////////////////////////////////////////////////POINTER TAB
import processing.serial.*;
Serial myPort; // Create object from Serial class
float PX=0;
float PY=0;
float PZ=0;
float ROT=0;
float sx=0;
float sy=0;
float sz=0;
float sa=0;
char datatype;
byte [] inData = new byte[5];
int inptr=0;
int rstate=0;
float www=0;
void serialEvent(Serial myPort)
{
while (myPort.available ()>0)
{
switch(rstate)
{
case 0:
datatype = myPort.readChar();
if (datatype=='x'||datatype=='y'||datatype=='z'||datatype=='a')
{
rstate=1;
inptr=0;
}
break;
case 1:
inData[inptr++]=(byte)myPort.readChar();
if (inptr==4)
{
int intbit = 0;
intbit = (inData[3] << 24) | ((inData[2] & 0xff) << 16) | ((inData[1] & 0xff) << 8) | (inData[0] & 0xff);
float f = Float.intBitsToFloat(intbit);
switch(datatype)
{
case 'x':
sx=f;
break;
case 'y':
sy=f;
break;
case 'z':
PX=sx;
PY=sy;
PZ=f;
ROT=sa;
break;
case 'a':
sa=f;
break;
}
rstate=0;
}
break;
}
}
}
class pointer
{
PVector tip = new PVector(0, 0, 0);
float rotation=0;
boolean active=false;
boolean demoMode=false;
pointer(PApplet p,int index)
{
myPort =new Serial(p, Serial.list()[0], 19200);
active=true;
}
void update()
{
if (demoMode)
{
tip.x=(mouseX-width/2)/4;
tip.y=(mouseY-height/2)/4;
tip.z=40+20*sin(www+=0.01);//random(20,50);//40.0;
}
else
{
// tip.set(PX,PY,PZ);
tip.x=PX;
tip.y=PY;
tip.z=PZ;
rotation=ROT;
}
}
};
////////////////////////////////////////////////////////////////////////////////GUI TAB
//============================================================
// Simple GUI controls
// Controls are activated by key and mouse click
// Contextual hels is displayed on mouse over
// (F)DZL 2015
//============================================================
class control
{
//Position and size
int x=0;
int y=0;
int w=60;
int h=35;
//-Colors
color faceColor=color(98, 206, 198);
color textColor=color(75, 42, 0);
//-Key to activate control
char hotkey;
//-Discriptive caption
String caption;
//-Mouse-over help
String help;
//-Mouse and key state
boolean clickState=false;
boolean keyState=false;
boolean mouseOver=false;
//-Updated for redraw
boolean change=false;
//-Typematic control
int timer=0;
boolean typematic=false;
//-Constructor
control(int px, int py, char k, String c,String h)
{
x=px;
y=py;
hotkey=k;
caption=c;
help=h;
}
//-Draw
void draw()
{
if (clickState||keyState)
stroke(255, 0, 0);
else
stroke(50);
fill(faceColor);
rect(x, y, w, h);
fill(textColor);
textSize(20);
text("-"+hotkey+"-", x+5, y+17);
textSize(14);
text(caption, x+8, y+33);
}
//-Draw help
void drawHelp()
{
textSize(20);
fill(200);
text(help,x+w+5,y+20);
}
//-Update. Handles keys and mouse
boolean update()
{
boolean result=false;
mouseOver=false;
if ((mouseX>x)&&(mouseX<(x+w)) && (mouseY>y)&&(mouseY<(y+h)))
{
mouseOver=true;
if (mousePressed==true)
{
if (!clickState)
{
clickState=true;
change=true;
}
} else
{
if (clickState==true)
{
result=true;
clickState=false;
change=true;
}
}
} else
if (mousePressed==false)
{
clickState=false;
change=true;
}
if (key==hotkey)
{
if (keyPressed==true)
{
mouseOver=true;
if (!keyState)
{
keyState=true;
change=true;
timer=millis()+500;
result=true;
} else
{
if (typematic)
{
if (millis()>timer)
{
timer+=10;
result=true;
}
}
}
} else
{
if (keyState==true)
{
keyState=false;
change=true;
}
}
} else
if (keyPressed==false)
{
keyState=false;
change=true;
}
return result;
}
};