User Tools

Site Tools


cs276:sendmouse

SendMouse

This example features two sketches (oscP5sendMouse and oscP5rcvMouse) communicating through OSC. The first sketch sends message to port 9001 on the local host (127.0.0.1) and the second sketch listens for message on the same. Note that both sketches are written so they can both send/receive (in fact you can have a sketch send messages and receive them itself), but in practice the communication is only one way.

Click on the drawing window created by oscP5sendMouse and see rectangles pop up on the second sketch's drawing window.

oscP5sendMouse

/**
 * oscP5sendMouse by andreas schlegel / modified by spc
 * example shows how to create osc messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
 
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400,400);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,12000);
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",9001);
  background(color(0, 255, 0));
}


void draw() { 
}

void mousePressed() {
  /* in the following different ways of creating osc messages are shown by example */
  OscMessage myMessage = new OscMessage("/test");
  
  myMessage.add(mouseX); /* add an int to the osc message */
  myMessage.add(mouseY);
  myMessage.add(12.34); /* add a float to the osc message */
  myMessage.add("some text"); /* add a string to the osc message */
  myMessage.add(new byte[] {0x00, 0x01, 0x10, 0x20}); /* add a byte blob to the osc message */
  myMessage.add(new int[] {1,2,3,4}); /* add an int array to the osc message */

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  text("### received an osc message.", 0, 0);
  text(" addrpattern: "+theOscMessage.addrPattern(), 0, 40);
  text(" typetag: "+theOscMessage.typetag(), 0, 80);
}

oscP5rcvMouse

/**
 * oscP5sendreceive by andreas schlegel
 * example shows how to send and receive osc messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
 
import oscP5.*;
import netP5.*;
  
OscP5 oscP5;
NetAddress myRemoteLocation;
int xCoord, yCoord;
boolean newMsg = false;

void setup() {
  size(400,400);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,9001);
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
  background(122);  
}


void draw() {
  if (newMsg) {
    // try drawing a rectangle at the sent coordinates
    fill(random(256), random(256), random(256));
    rect((float) xCoord, (float) yCoord, 30, 50);
    newMsg = false;
    } 
}

void mousePressed() {
  /* in the following different ways of creating osc messages are shown by example */
  OscMessage myMessage = new OscMessage("/test");
  
  myMessage.add(123); /* add an int to the osc message */

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  println("### received an osc message.");
  println(" addr: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  xCoord = theOscMessage.get(0).intValue();
  yCoord = theOscMessage.get(1).intValue();
  println(" mouseX: "+xCoord);
  println(" mouseY: "+yCoord);
  newMsg = true;
}
cs276/sendmouse.txt · Last modified: 2019/12/06 14:57 by scarl