/** * (./) udp.pde - how to use UDP library as unicast connection * (cc) 2006, Cousot stephane for The Atelier Hypermedia * (->) http://hypermedia.loeil.org/processing/ * * Create a communication between Processing<->Pure Data @ http://puredata.info/ * This program also requires to run a small program on Pd to exchange data * (hum!!! for a complete experimentation), you can find the related Pd patch * at http://hypermedia.loeil.org/processing/udp.pd * * -- note that all Pd input/output messages are completed with the characters * ";\n". Don't refer to this notation for a normal use. -- */ // import UDP library import hypermedia.net.*; import java.util.*; Deque d = new ArrayDeque(); String myString; float x,y,angle; float num; float num2; UDP udp; // define the UDP object /** * init */ void setup() { size(820, 820); noSmooth(); background(0); translate(410, 410); stroke(255); strokeWeight(3); // create a new datagram connection on port 6000 // and wait for incomming message udp = new UDP( this, 6000 ); //udp.log( true ); // <-- printout the connection activity udp.listen( true ); } //process events void draw() { if (!d.isEmpty()) { String[] q = splitTokens(d.pop(), ","); num=float(q[0]); // Converts and prints float num2 = float(q[1]); // Converts and prints float //Pass from polars to cartesians adna dd 410 to be in the middle of the 820 by 820 screen. angle = num * 0.0174533; x = (sin(angle)*num2 + 410); y = (cos(angle)*num2 + 410); } if(num == 0 ) { background(0); translate(410, 410); } point(x, y); } /** * on key pressed event: * send the current key value over the network */ void keyPressed() { String message = str( key ); // the message to send String ip = "localhost"; // the remote IP address int port = 6100; // the destination port // formats the message for Pd message = message+";\n"; // send the message udp.send( message, ip, port ); } /** * To perform any action on datagram reception, you need to implement this * handler in your code. This method will be automatically called by the UDP * object each time he receive a nonnull message. * By default, this method have just one argument (the received message as * byte[] array), but in addition, two arguments (representing in order the * sender IP address and his port) can be set like below. */ // void receive( byte[] data ) { // <-- default handler void receive( byte[] data, String ip, int port ) { // <-- extended handler // get the "real" message = // forget the ";\n" at the end <-- !!! only for a communication with Pd !!! data = subset(data, 0, data.length); String message = new String( data ); d.add(message); // print the result }