XY Pad (OSC ) to procesing

Hi!
Im a CG animator and Im learning a little of scripting to improve my skills. I was wondering if someone know how to get the x and y values of a XY Pad, using oscP5. (https://vimeo.com/14734980)

I don’t have idea of which methods should I need to use to get this result, I started with these lines, but I think that something is missing, in order to separate the x and y and send

import oscP5.*;
import netP5.*;

OscP5 oscP5;

float posx = 0;
float posy= 0;

void setup() {
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 8000 */
  oscP5 = new OscP5(this, 8000);

  oscP5.plug(this, "positionx", "/1/xy2");
  oscP5.plug(this, "positiony", "/1/xy2");

Thanks in advance

1 Like

Hi! Have you looked at

examples/Contributed Libraries/oscP5/oscP5SendReceive

? Also, you could first run a test program to see in which format you are actually receiving the data:

import oscP5.*;
import netP5.*;
  
OscP5 oscP5;
NetAddress self;

void setup() {
  oscP5 = new OscP5(this, 12000); 
  self = new NetAddress("127.0.0.1", 12000);
}

void draw() {
}

void mousePressed() {
  OscMessage msg = new OscMessage("/mouse");  
  msg.add(mouseX);
  msg.add(mouseY);
  oscP5.send(msg, self); 
}

void oscEvent(OscMessage msg) {
  println(msg);
  println(msg.arguments());
}

This program prints the message info and arguments to the console. You can use that same program (changing the ports maybe) to see what you receive from your XY pad. Maybe you are receiving two integers, or two floats, or a string…

1 Like

Hey Thanks man! I will try with this code. I look up for this root (examples/Contributed Libraries/oscP5/oscP5SendReceive) in this website?

The Processing IDE has an Examples menu, inside File. All the libraries you install normally come with examples, which appear under Contributed Libraries inside the Examples menu. Processing comes with hundreds of examples for you to learn from.

1 Like

Thanks man! I rebuild my code using your example as a guide :slight_smile:

import processing.net.*;
import oscP5.*;
import netP5.*;

float x=0;
float y=0;
float xval=0;

OscP5 oscP5;
NetAddress self;

void setup() {
  oscP5 = new OscP5(this, 8000); 
  self = new NetAddress("127.0.0.1", 8000);
}

void draw() {
}

void Pad() {
  OscMessage msg = new OscMessage("/1/xy2");  
  
  oscP5.send(msg, self); 
  float xval= msg.get(0).floatValue(); 
}

void oscEvent(OscMessage msg) {
  println(msg);
  println(msg.arguments());
  println(xval);
}

I don’t know why, the script is not saving the float properly, I think that is taking other values instead of the message argument.

Results
/192.168.1.67:9000 | /1/xy2 ff
1.0 1.0
0.0

I think I see a bit of confusion here…

void Pad() {
  OscMessage msg = new OscMessage("/1/xy2");
  oscP5.send(msg, self);

  // even if there's a global xval variable
  // the following line creates a local temporary variable
  // that gets deleted at the end of this function
  float xval= msg.get(0).floatValue();
  // Also, it's reading an osc argument out of a message
  // which was just SENT (instead of reading such an argument
  // from a received message below)
}

void oscEvent(OscMessage msg) {
  println(msg);
  println(msg.arguments());
  // the following line is printing a global variable called xval
  // which probably contains nothing
  println(xval);

  // what you probably want is to place the data received by this
  // function into the global variable xval, like this:
  xval= msg.get(0).floatValue();
  // if you added float at the beginning of the previous line,
  // the variable would be a local variable, and the content lost
  // at the end of this function
}

If the concept of local and global variables is not clear, you could check out this tutorial I made:
https://funprogramming.org/50-What-are-global-and-local-variables.html

2 Likes