Processing
Note: Return to tutorial view.
Overview
The Make Controller Kit can connect to Processing via OSC, a popular protocol used to communicate between otherwise disparate environments and devices.
Processing is an open source programming language and environment for people who want to program images, animation, and sound. It is used by students, artists, designers, architects, researchers, and hobbyists for learning, prototyping, and production. It is created to teach fundamentals of computer programming within a visual context and to serve as a software sketchbook and professional production tool.
Processing does not normally provide a way to communicate via OSC, but there is a freely available OSC library for Processing called oscP5. Installing the library is as easy as downloading it and putting it in the library directory within your Processing installation.
The Processing environment.
Setup
On the board, confirm that- The input voltage jumpers are set to 3.3V.
- The output voltage jumpers are set to 5V.
- The DIP switches are all set to off.
- The trimpot jumper is connected.
- You're running Heavy.
See the Application Board Overview for info about the jumpers and DIP switch. Then, plug the board into your computer via USB - the USB connection is used only to power the board in this case, not to communicate with it. You could alternatively connect a 9V power supply to the Main Power Connector on the Application Board.
The Make Controller communicates with your PC via your network connection. There are two ways to connect your Controller to your PC via Ethernet:
- Connect an Ethernet cable between the Make Controller and a router or switch on your local network.
- Connect an Ethernet cable directly between your board and the Make Controller - click here for a guide on how to set that up.
Download
The oscP5 library can be downloaded here. Once you've unzipped it:- Create a new folder called oscP5 in the Processing's libraries folder.
- Drag the library folder from the oscP5 download into your newly created folder.
- Confirm oscP5.jar is in the library folder, and you're all set.
LED Control
Controlling an LED on the Application Board is one of the simplest things to do from Processing, making it an excellent first thing to try. The name of the LED subsystem on the board is appled - Application LED. There are 4 LEDs, numbered 0 - 3, and they have a state - whether they're on or off. The argument that comes after the address sets the value of state. So we create an OSC message in the form /subsystem/device/property argument
The code below creates a 400 x 400 pixel window. If you click it, it will toggle LED 0 on the Application Board on and off. Copy the code, paste it into Processing, and hit Run.
// Make Controller Kit - LEDControl
// Toggle the Application Board LEDs on a mouse press
// based on oscP5message by andreas schlegel
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress makeControllerAddress;
int toggleState;
void setup()
{
size( 400, 400 );
frameRate( 25 );
// start oscP5, listening for incoming messages at port 10000
oscP5 = new OscP5( this, 10000 );
// set the address of the board we're sending to...
makeControllerAddress = new NetAddress("192.168.0.200", 10000);
toggleState = 1;
}
void draw()
{
background(0);
}
void mousePressed()
{
// create a new OSC message
OscMessage myMessage = new OscMessage("/appled/0/state");
myMessage.add( toggleState ); // add an argument to the message
oscP5.send( myMessage, makeControllerAddress ); // send the message
// toggle the state of the LED and save it for next time
if( toggleState == 1 )
toggleState = 0;
else
toggleState = 1;
}
All the real action happens in mousePressed( ) - we're sending a message to App LED 0 each time the mouse is pressed. We create a new OscMessage, with an address of /appled/0/state, and then add our argument to it using add( ). The value that we add to the message, toggleState, alternates each time between 0 and 1, so we can toggle the LED on and off by pressing the mouse. As always, make sure the address used for makeControllerAddress matches the network settings you've given your board.
Read the Analog Inputs
To read an analog input, send an OSC message to ask for the current value, and the board will send an OSC message back with the value. Unlike the OSC messages sent to the LEDs, an OSC message asking to read a value will not have an argument on the end of it - this is what distinguishes a read message from a write message, the lack of an argument.
The name of the analog input subsystem is analogin, there are 8 devices numbered 0 - 7, and they have a value. 'Read' messages have the form /subsystem/device/property, with no argument. The code below creates two circles and modifies their size based on the value coming from the trimpot. Copy and paste the code below into Processing - you're ready to run your program.
The size of the circles in the sketch is controlled by the trimpot on the Application Board.
// Make Controller Kit - Analog In
// Send out 'read' messages every frame, and then grab the message coming back from the board.
// based on oscP5message by andreas schlegel
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress makeControllerAddress;
OscMessage myMessage;
int trimpot;
void setup()
{
size(400,400);
frameRate(25);
//start oscP5, listening for incoming messages at port 10000
oscP5 = new OscP5(this, 10000);
makeControllerAddress = new NetAddress( "192.168.0.200", 10000 );
myMessage = new OscMessage("/analogin/7/value");
trimpot = 0;
}
void draw()
{
background(25);
oscP5.send(myMessage, makeControllerAddress); // send the message
// use the trimpot value to size the circles
fill( 240 );
ellipse( 100, 100, trimpot/2, trimpot/2 );
fill( 150 );
ellipse( 250, 250, trimpot/4, trimpot/4 );
}
// this gets called back whenever there's a new OSC message coming back from the board.
void oscEvent(OscMessage theOscMessage)
{ // compare the incoming address to the address we asked for.
if( theOscMessage.addrpattern().equals( "/analogin/7/value" ) )
trimpot = theOscMessage.get(0).intValue();
}
By placing the call to oscP5.send( ) in draw( ), we're asking for an update from the board at the framerate of the sketch - 24 times a second in this case, which we specified in setup( ).
oscEvent( ) gets called when there's an incoming OSC packet - there are a number of methods provided to read the different parameters of the incoming OSC message, but we only care about whether the address matches the address we sent out. So we check the addrpattern( ) of the incoming OscMessage and test to see if it matches myMessage, which is reading analog in 7, the trimpot. Confirm the trimpot jumper is on, and you should be able to adjust the size of the circles onscreen by turning the trimpot.