Skip to content

Instantly share code, notes, and snippets.

@prasertsakd
Last active March 16, 2016 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasertsakd/025767297c3e449be465 to your computer and use it in GitHub Desktop.
Save prasertsakd/025767297c3e449be465 to your computer and use it in GitHub Desktop.
/**
* ControlP5 Button
* this example shows how to create buttons with controlP5.
*
* find a list of public methods available for the Button Controller
* at the bottom of this sketch's source code
*
* by Andreas Schlegel, 2012
* www.sojamo.de/libraries/controlp5
*
*/
import controlP5.*;
import hypermedia.net.*;
UDP udp; // define the UDP object
String ip = "192.168.43.xxxx"; // the remote IP address
int port = 9998; // the destination port
ControlP5 cp5;
int myColor = color(255);
int c1,c2;
float n,n1;
void setup() {
size(400,400);
udp = new UDP( this, 11111 );
//udp.log( true ); // <-- printout the connection activity
udp.listen( true );
noStroke();
cp5 = new ControlP5(this);
// create a new button with name 'buttonA'
cp5.addButton("Button_ON")
.setValue(0)
.setPosition(100,130)
.setSize(200,49)
;
// and add another 2 buttons
cp5.addButton("Button_OFF")
.setValue(100)
.setPosition(100,210)
.setSize(200,49)
;
//cp5.addButton("colorC")
// .setPosition(100,210)
// .setSize(200,19)
// .setValue(0)
// ;
}
void draw() {
background(myColor);
myColor = lerpColor(c1,c2,n);
n += (1-n)* 0.1;
}
public void controlEvent(ControlEvent theEvent) {
println(theEvent.getController().getName());
n = 0;
}
// function colorA will receive changes from
// controller with name colorA
public void Button_ON(int theValue) {
//byte[] data = new byte[3]; // the data to be send
//data[0]=byte(0);
//data[1]=byte(160);
//data[2]=byte(100);
udp.send( "H", ip, port );
println("a button event from colorA: "+theValue);
c1 = c2;
c2 = color(0,160,100);
}
// function colorB will receive changes from
// controller with name colorB
public void Button_OFF(int theValue) {
println("a button event from colorB: "+theValue);
c1 = c2;
c2 = color(150,0,0);
//byte[] data = new byte[3]; // the data to be send
//data[0]=byte(150);
//data[1]=byte(0);
//data[2]=byte(0);
//udp.send( data, ip, port );
udp.send( "L", ip, port );
}
// function colorC will receive changes from
// controller with name colorC
public void colorC(int theValue) {
println("a button event from colorC: "+theValue);
c1 = c2;
c2 = color(255,255,0);
//byte[] data = new byte[3]; // the data to be send
//data[0]=byte(255);
//data[1]=byte(255);
//data[2]=byte(0);
// udp.send( data, ip, port );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment