We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › OscP5 Multiple Listeners Added to Own Class
Page Index Toggle Pages: 1
OscP5 Multiple Listeners Added to Own Class (Read 672 times)
OscP5 Multiple Listeners Added to Own Class
Mar 7th, 2009, 2:59am
 
Hello,

I am attempting to add an OscP5 object to each instance of my own created class.  Basically, I want to send messages to each instance of the class created telling it what to do, but not listen to directions intended for other instances of the class.

It seems the addListener() function might work, but it is unclear to me on how to use this.  I also, tried extending the OscP5 class with my own class, but it was beyond my programming capabilities.

Here's the code that should better elucidate the problem (and yes, I am 100% sure the OSC messages can be sent and received the setup protocols);

Quote:
import oscP5.*;
import netP5.*;

OscP5 oscP5; 
NetAddress myRemoteLocation;

int theWidth = 100;
int theHeight = 100;
 
void setup() {
  background(0);
  size(theWidth, theHeight);
  oscP5 = new OscP5(this, 12000);
  myRemoteLocation = new NetAddress("127.0.0.1", 12000);
  
  E e = new E("one");
  E e2 = new E("two");

 
void draw() {
  


class E{
 
  String id;
 
   E (String name) {
     id = name;
   }

   void oscEvent(OscMessage theOscMessage) {
    if(theOscMessage.checkAddrPattern("/" + "one") == true) {
      println(id + " = " + "one");
    }
    if(theOscMessage.checkAddrPattern("/" + "two") == true) {
      println(id + " = " + "two");
    }  
  }
}







Re: OscP5 Multiple Listeners Added to Own Class
Reply #1 - Mar 7th, 2009, 7:09am
 
this modified example should get help you getting started with listeners using oscP5. your class E needs to implement an OscEventListener (this requires to include methods oscStatus and oscEvent see below.). after having created object e and e2, you need to add them to oscP5 so they are recognized as listeners when osc messages arrive in your sketch. that should be it.
best,
andreas


Code:

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

int theWidth = 100;
int theHeight = 100;

void setup() {
background(0);
size(theWidth, theHeight);
oscP5 = new OscP5(this, 12000);

E e = new E("one");
E e2 = new E("two");

oscP5.addListener(e);
oscP5.addListener(e2);
}

void draw() {
}

void mousePressed() {
println("\nmousePressed : sending an osc message to myself.");
NetAddress myRemoteLocation = new NetAddress("127.0.0.1",12000);
OscMessage myMessage = new OscMessage("/something");
oscP5.send(myMessage, myRemoteLocation);
}


public class E implements OscEventListener {

String id;

E (String name) {
id = name;
}

void oscStatus(OscStatus theStatus) {
}

void oscEvent(OscMessage theOscMessage) {
println("got an osc message @ listener "+id);
if(theOscMessage.checkAddrPattern("/" + "one") == true) {
println(id + " = " + "one");
}
if(theOscMessage.checkAddrPattern("/" + "two") == true) {
println(id + " = " + "two");
}
}
}


Re: OscP5 Multiple Listeners Added to Own Class
Reply #2 - Mar 20th, 2009, 7:20pm
 
thanks for the mod, yet it appears to not work properly or i am misusing the osc messages...

here's your code that has been modified, so that when an instance of an object receives an osc message (from a different program), it will print a line verifying the id associated with the message;

Quote:
import oscP5.*;  
import netP5.*;  
 
OscP5 oscP5;  
NetAddress myRemoteLocation; 
 
int theWidth = 100;
int theHeight = 100;
 
void setup() {
  background(0);
  size(theWidth, theHeight);
  oscP5 = new OscP5(this, 12000);  
 
  E e = new E("one");
  E e2 = new E("two");
  E e3 = new E("three");
 
  oscP5.addListener(e); 
  oscP5.addListener(e2); 
  oscP5.addListener(e3);
}  
 
void draw() {
}   
 
public class E implements OscEventListener {
 
  String id;
 
  E (String name) {
    id = name; 
  } 
 
  void oscStatus(OscStatus theStatus) {
  } 
 
  void oscEvent(OscMessage theOscMessage) {
    if(theOscMessage.checkAddrPattern("/" + "id") == true) {
       println("success @ ;" + id);
    }   
  } 



Re: OscP5 Multiple Listeners Added to Own Class
Reply #3 - Mar 21st, 2009, 7:20am
 
hi there,
the problem most probably lies in line
Code:

if(theOscMessage.checkAddrPattern("/" + "id") == true) {

instead of "/" + "id" (which creates a string "/id") use "/"+id (which creates, assuming String id = "yourID", a string "/yourID")

hope this solves the problem.
best,
andreas

Re: OscP5 Multiple Listeners Added to Own Class
Reply #4 - Mar 21st, 2009, 5:19pm
 
ah, silly me.

thanks a bunch!
Page Index Toggle Pages: 1