Using oscP5 with Buttons

Hi, I’d like to change the background color of the second sketch using buttons from the first one. I get them to talk to each other using mouse click back and forth, but I’m struggling to switch it to a button. I first started it out with just a mousePressed, but want to move it to buttons.

Here’s the code for the first Sketch

import oscP5.*;
import netP5.*;
import interfascia.*;
 
OscP5 oscP5Location2;
NetAddress location1;

GUIController c1;
IFButton b1, b2;
IFLabel l;

int c =0;
 
void setup() {
  size(400, 400);
  
   c1 = new GUIController (this);
  
  b1 = new IFButton ("Green", 30, 35, 60, 30);
  b2 = new IFButton ("Blue", 110, 35, 60, 30);

  b1.addActionListener(this);
  b2.addActionListener(this);

  c1.add (b1);
  c1.add (b2);
 
  oscP5Location2 = new OscP5(this, 6001);
  location1 = new NetAddress("127.0.0.1", 5001);
}
 
void draw() {
   background(255,c, 0);

}
 
void mousePressed() {  
  OscMessage myMessage = new OscMessage("/test");
 
  myMessage.add("Location 2: Transmit");
  myMessage.add(mouseX);
 
  oscP5Location2.send(myMessage, location1); 
  println("Sending message.");
  
}
 
void oscEvent(OscMessage theOscMessage, GUIEvent e) {  
  String incomingHeader = theOscMessage.get(0).stringValue();
  //int incomingMouseX = theOscMessage.get(1).intValue();
  int incomingButton1 = theOscMessage.get(1).intValue();
  
   if (e.getSource() == b1) {
    background(random(255),random(255),random(255));
  } else if (e.getSource() == b2) {
   background(100, 100, 130);
 }
 
  // print out the message
  print("OSC Message Received: ");
  println(incomingHeader + " " + incomingButton1);
}

This is the second sketch

import oscP5.*;
import netP5.*;
 
OscP5 oscP5Location1;
NetAddress location2;
 int c =0;
 
void setup() {
  size(400, 400);
 
  oscP5Location1 = new OscP5(this, 5001);
  location2 = new NetAddress("127.0.0.1", 6001);
}
 
void draw() {
   background(255,c, 0);

}
 
void mousePressed() {  
  OscMessage myMessage = new OscMessage("/test");
 
  myMessage.add("Location 1: Transmit");
  myMessage.add(mouseX);
  myMessage.add(mouseY);
 
  oscP5Location1.send(myMessage, location2); 
  println("Sending message.");
  
}
 
void oscEvent(OscMessage theOscMessage) {  
  String incomingHeader = theOscMessage.get(0).stringValue();
  int incomingMouseX = theOscMessage.get(1).intValue();

  if (mousePressed) {
    background(random(255),random(255),random(255));
  } else {
    background(255);
  }
  
  
  // print out the message
  print("OSC Message Received: ");
  println(incomingHeader + " " + incomingMouseX);
  c = incomingMouseX;

}
1 Like

hi,

-a- i not see what example of INTERFASCIA button you follow?

-b- i would start with something symmetric
( between 2 computers ( using VNC i see both screens ) )

// make a symmetric sketch send recieve via network OSC by Button press

/* oscP5sendreceive by andreas schlegel */
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup_osc() {
  oscP5 = new OscP5(this,12345);
  myRemoteLocation = new NetAddress("192.168.1.204",12345);  // RPI4
  //myRemoteLocation = new NetAddress("192.168.1.4",12345);    // PC
}

//___________________________________
import interfascia.*;
GUIController c;
IFButton b1,b2;

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}

color bg;

void setup() {
  size(100,100);
  bg = color(200);
  setup_osc();
  setup_button();
}

void draw() {
  background(bg);  
}

void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5.send(myMessage, myRemoteLocation); 
}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if      ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else                   bg = color(200,0,0);
  }
}


hope that example helps, sorry just my first try of oscP5 and interfascia lib…

1 Like

Thanks for trying. I’ll work on seeing if this helps, but it’s not on two different computers. It’s on the same computer, but two sketches are open.

I oscP5 file I’m working with is fromhere.

The button I’m using is from the examples in the interfascia lib.

I got it to work!!

Here’s the code I ended up using

Sketch 1

import oscP5.*;
import netP5.*;

import interfascia.*;
GUIController c;
IFButton b1,b2;
 
OscP5 oscP5Location1;
NetAddress location2;

 
 void setup_osc() {
  oscP5Location1 = new OscP5(this, 5001);
  location2 = new NetAddress("127.0.0.1", 6001);
}

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}
 color bg;
void setup() {
  
  size(400, 400);
 bg = color(200);

  
  setup_osc();
  setup_button();
}
 
void draw() {
   background(bg);
  
}
void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
 
  
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5Location1.send(myMessage, location2); 
  println("Sending message.");
  

}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if      ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else                   bg = color(200,0,0);
  }
}

Sketch 2

import oscP5.*;
import netP5.*;
 
OscP5 oscP5Location2;
NetAddress location1;

import interfascia.*;
GUIController c;
IFButton b1,b2;

color bg;


void setup_osc() {
  
  oscP5Location2 = new OscP5(this, 6001);
  location1 = new NetAddress("127.0.0.1", 5001);
}

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}
 
void setup() {
  size(400, 400);
  bg = color(200);
  setup_osc();
  setup_button();
}
 
void draw() {
   background(bg);
 
}

void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5Location2.send(myMessage, location1); 
  println("Sending message.");

}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if      ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else                   bg = color(200,0,0);
  }
}

2 Likes