Msg > Processing

msg2processing.zip

/**
Example of communication between Processing and Msg
*/

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress msgNetAddress;

PFont font;

int pin14;
int pin15;
int pin2;

int output;

void setup() {
        size(400, 204);

        /* start oscP5, listening for incoming messages at port 12000 */
        oscP5 = new OscP5(this, 8001);

        /* msgNetAddress is a NetAddress. a NetAddress takes 2 parameters,
        * an ip address and a port number. msgNetAddress is used as parameter in
        * oscP5.send() when sending osc packets to Msg.
        */
        msgNetAddress = new NetAddress("127.0.0.1", 8081);

        font = loadFont("font.vlw");
        textFont(font,48);
        textAlign(LEFT,TOP);
}

// Process received messages
void oscEvent(OscMessage theOscMessage) {
        /* check if theOscMessage has the address pattern we are looking for. */
        if ( theOscMessage.checkAddrPattern("/14") && theOscMessage.checkTypetag("i") ) {
                pin14 = theOscMessage.get(0).intValue();
        }
        else if ( theOscMessage.checkAddrPattern("/15") && theOscMessage.checkTypetag("i") ) {
                pin15 = theOscMessage.get(0).intValue();
        }
        else if ( theOscMessage.checkAddrPattern("/2") && theOscMessage.checkTypetag("i") ) {
                pin2 = theOscMessage.get(0).intValue();
        }
}

void draw() {


        if ( output == 1 ) {
                fill(0);
                background(255);
        } else {
                fill(255);
                background(0);
        }

        text("pin 14: "+pin14,30,30);
        text("pin 15: "+pin15,30,78);
        text("pin 2: "+pin2,30,126);


}


void mousePressed() {

        if ( output == 1 ) {
                output = 0;
        } else {
                output = 1;
        }

        /* create a new osc message object */
        OscMessage myMessage = new OscMessage("/13");

        myMessage.add(output); /* add an int to the osc message */

        /* send the message */
        oscP5.send(myMessage, msgNetAddress);
}