/*
* graphical representations
* client
*/
import processing.net.*;
import processing.serial.*;
// serial variables
String portname = "/dev/tty.usbserial-A4001o19";
Serial port;
String buf = "";
int cr = 13; // ASCII return
int lf = 10; // ASCII linefeed
// network variables
Client c;
int input, oldInput;
// other variables
int count, oldCount;
PFont font;
void setup() {
frameRate(20);
// set background
size(300, 385);
background(#DCDCDC);
smooth();
// text
font = loadFont("Verdana-20.vlw");
textFont(font);
fill(0, 0, 0);
textAlign(CENTER, TOP);
text("Remote User", 150, 146);
text("You", 150, 326);
// divider
stroke(0,0,0);
strokeWeight(3);
line(0, 194, 400, 194);
/* // crosshair
strokeWeight(1);
line(150, 0, 150, 510);
line(0, 255, 400, 255);*/
// set variables (to middle of no signal range)
count = 128;
input = 128;
// connect with serial (Arduino)
port = new Serial(this, portname, 9600);
/*if (port != null)
println("connected to port over serial");
else
println("serial failed");*/
// connect to server
c = new Client(this, "136.152.144.124", 5204);
/*if (c != null)
println("connected to server");
else
println("failed to connect");*/
}
void draw(){
float a, x1, y1, x2, y2;
float r = 60.0;
// receive data from other person
if (c.available() > 0) {
oldInput = input;
input = c.read();
//println("received from client: " + input);
}
// else
//println("no data from server");
smooth();
// Remote User
//if ((input > (oldInput+1)) || (input < (oldInput-1))) {
// erase current arc (re-draw in background color)
fill(#DCDCDC);
strokeWeight(2);
stroke(#DCDCDC);
arc(150, 140, 200, 200, PI, TWO_PI); // re-draw arc
// negative
if (input <= 127) {
// re-draw arc - red
fill(255, (0+(input*2)), (0+(input*2))); // fill red
strokeWeight(1);
stroke(255, (0+(input*2)), (0+(input*2)));
arc(150, 140, 200, 200, PI, TWO_PI);
// draw spindle
if (input <= 1)
input = 0;
a = (input/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 125;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
// positive
else if (input >= 128) {
int input2 = input-155; // (0 to 100)
fill((255-(input2*2)), 255, (255-(input2*2))); //fill green
strokeWeight(1);
stroke((255-(input2*2)), 255, (255-(input2*2)));
arc(150, 140, 200, 200, PI, TWO_PI);
// draw spindle
if (input >= 251)
input = 255;
a = (input/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 125;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
//}
// you
//if ((count > (oldCount+1)) || (count < (oldCount-1))) {
// erase current arc (re-draw in background color)
fill(#DCDCDC);
strokeWeight(2);
stroke(#DCDCDC);
arc(150, 320, 200, 200, PI, TWO_PI); // re-draw arc
// negative
if (count <= 127) {
// re-draw arc - red
fill(255, (0+(count*2)), (0+(count*2))); // fill red
strokeWeight(1);
stroke(255, (0+(count*2)), (0+(count*2)));
arc(150, 320, 200, 200, PI, TWO_PI);
// draw spindle
if (count <= 1)
count = 0;
a = (count/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 300;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
// positive
else if (count >= 128) {
int count2 = count-155; // (0 to 100)
fill((255-(count2*2)), 255, (255-(count2*2))); //fill green
strokeWeight(1);
stroke((255-(count2*2)), 255, (255-(count2*2)));
arc(150, 320, 200, 200, PI, TWO_PI);
// draw spindle
if (count >= 251)
count = 255;
a = (count/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 300;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
//}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int d = port.read();
if (d != lf && d != cr) {
buf += char(d);
}
if (d == lf) {
oldCount = count;
count = int(buf);
//println("from arduino="+count);
buf = "";
// only send data if there is a change
if ((count > (oldCount+2)) || (count < (oldCount-2)) || frameCount%10 == 0)
c.write(count); // send your value to other person
//println("sent: " + count);
}
}
server
/*
* graphical representations
* client
*/
//import processing.net.*;
import processing.serial.*;
// serial variables
String portname = "/dev/tty.usbserial-A4001o19";
Serial port;
String buf = "";
int cr = 13; // ASCII return
int lf = 10; // ASCII linefeed
// network variables
/*Server s;
Client c;*/
int input, oldInput;
// other variables
int count, oldCount;
PFont font;
void setup() {
frameRate(2);
// set background
size(300, 385);
background(#DCDCDC);
smooth();
// text
font = loadFont("Verdana-20.vlw");
textFont(font);
fill(0, 0, 0);
textAlign(CENTER, TOP);
text("Remote User", 150, 146);
text("You", 150, 326);
// divider
stroke(0,0,0);
strokeWeight(3);
line(0, 194, 400, 194);
/* // crosshair
strokeWeight(1);
line(150, 0, 150, 510);
line(0, 255, 400, 255);*/
// set variables (to middle of no signal range)
count = 128;
input = 128;
// connect with serial (Arduino)
port = new Serial(this, portname, 9600);
if (port != null)
println("connected to port over serial");
else
println("serial failed");
/* s = new Server(this, 5204); // start
if (s != null)
println("connected to server");
else
println("failed to connect");*/
}
void draw(){
float a, x1, y1, x2, y2;
float r = 60.0;
// receive data from other person
/* c = s.available();
if (c!= null) {
input = c.read();
println("received from client: " + input);
}
else
println("no data from client");*/
smooth();
// Remote User
if ((input > (oldInput+1)) || (input < (oldInput-1))) {
// erase current arc (re-draw in background color)
fill(#DCDCDC);
strokeWeight(2);
stroke(#DCDCDC);
arc(150, 140, 200, 200, PI, TWO_PI); // re-draw arc
// negative
if (input <= 127) {
// re-draw arc - red
fill(255, (0+(input*2)), (0+(input*2))); // fill red
strokeWeight(1);
stroke(255, (0+(input*2)), (0+(input*2)));
arc(150, 140, 200, 200, PI, TWO_PI);
// draw spindle
if (input <= 1)
input = 0;
a = (input/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 125;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
// positive
else if (input >= 128) {
int input2 = input-155; // (0 to 100)
fill((255-(input2*2)), 255, (255-(input2*2))); //fill green
strokeWeight(1);
stroke((255-(input2*2)), 255, (255-(input2*2)));
arc(150, 140, 200, 200, PI, TWO_PI);
// draw spindle
if (input >= 251)
input = 255;
a = (input/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 125;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
}
// you
if ((count > (oldCount+1)) || (count < (oldCount-1))) {
// erase current arc (re-draw in background color)
fill(#DCDCDC);
strokeWeight(2);
stroke(#DCDCDC);
arc(150, 320, 200, 200, PI, TWO_PI); // re-draw arc
// negative
if (count <= 127) {
// re-draw arc - red
fill(255, (0+(count*2)), (0+(count*2))); // fill red
strokeWeight(1);
stroke(255, (0+(count*2)), (0+(count*2)));
arc(150, 320, 200, 200, PI, TWO_PI);
// draw spindle
if (count <= 1)
count = 0;
a = (count/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 300;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
// positive
else if (count >= 128) {
int count2 = count-155; // (0 to 100)
fill((255-(count2*2)), 255, (255-(count2*2))); //fill green
strokeWeight(1);
stroke((255-(count2*2)), 255, (255-(count2*2)));
arc(150, 320, 200, 200, PI, TWO_PI);
// draw spindle
if (count >= 251)
count = 255;
a = (count/1.42 + 180) * (float)PI/180;
x1 = 150;
y1 = 300;
x2 = (int) (cos(a)*r + x1);
y2 = (int) (sin(a)*r + y1);
stroke(0, 0, 0);
strokeWeight(3);
line(x1, y1, x2, y2);
}
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int d = port.read();
if (d != lf && d != cr) {
buf += char(d);
}
if (d == lf) {
oldCount = count;
count = int(buf);
println("count="+count);
buf = "";
/* s.write(count); // send your value to other person
println("sent to client: " + count);*/
}
}
Images