An Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige written by: Alvaro Cassinelli, October 2011 tweaked by: Toby Harris / *spark audio-visual, March 2012

Dependencies:   NetServices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbedOSC.h"
00003 
00004 //// ETHERNET
00005 
00006 // Ethernet can be created with *either* an address assigned by DHCP or a static IP address. Uncomment the define line for DHCP
00007 //#define DHCP
00008 #ifdef DHCP
00009 EthernetNetIf eth;
00010 #else
00011 EthernetNetIf eth(
00012     IpAddr(10,0,0,2), //IP Address
00013     IpAddr(255,255,255,0), //Network Mask
00014     IpAddr(10,0,0,1), //Gateway
00015     IpAddr(10,0,0,1)  //DNS
00016 );
00017 #endif
00018 
00019 //// OSC
00020 
00021 // The object to do the work of sending and receiving
00022 OSCClass osc;
00023 
00024 // The message objects to send and receive with
00025 OSCMessage recMes;
00026 OSCMessage sendMes;
00027 
00028 // Setting - The port we're listening to on the mbed for OSC messages
00029 int  mbedListenPort  = 10000;
00030 
00031 // Setting - The address and port we're going to send to, from the mbed
00032 uint8_t destIp[]  = { 10, 0, 0, 1};
00033 int  destPort = 12000;
00034 
00035 //// mbed input
00036 
00037 DigitalIn button(p21);
00038 bool buttonLastState;
00039 
00040 //// Our messageReceivedCallback function
00041 void processOSC() {
00042 
00043     // If this function has been called, the OSC message just received will have been parsed into our recMes OSCMessage object
00044     // Note we can access recMes here, outside of the main loop, as we created it as a global variable.
00045 
00046     // TASK: If this message one we want, do something about it.
00047     // In this example we're listening for messages with a top address of "mbed".
00048     // Note the strcmp function returns 0 if identical, so !strcmp is true if the two strings are the same
00049     if ( !strcmp( recMes.getAddress(0) , "mbed" ) ) {
00050         printf("OSC Message received addressed to mbed \r\n");
00051         if ( !strcmp( recMes.getAddress(1) , "test1" ) )
00052             printf("Received subAddress= test1 \r\n");
00053 
00054         // Send some osc message:
00055         sendMes.setTopAddress("/working...");
00056         osc.sendOsc(&sendMes);
00057     }
00058 }
00059 
00060 ////  M A I N
00061 int main() {
00062 
00063     //// TASK: Set up the Ethernet port
00064     printf("Setting up ethernet...\r\n");
00065     EthernetErr ethErr = eth.setup();
00066     if (ethErr) {
00067         printf("Ethernet Failed to setup. Error: %d\r\n", ethErr);
00068         return -1;
00069     }
00070     printf("Ethernet OK\r\n");
00071 
00072     //// TASK: Set up OSC message sending
00073 
00074     // In the OSC message container we've made for send messages, set where we want it to go:
00075     sendMes.setIp( destIp );
00076     sendMes.setPort( destPort );
00077 
00078     //// TASK: Set up OSC message receiving
00079 
00080     // In the OSC send/receive object...
00081     // Set the OSC message container for it to parse received messages into
00082     osc.setReceiveMessage(&recMes);
00083 
00084     // Tell it to begin listening for OSC messages at the port specified (the IP address we know already, it's the mbed's!).
00085     osc.begin(mbedListenPort);
00086 
00087     // Rather than constantly checking to see whether there are new messages waiting, the object can call some code of ours to run when a message is received.
00088     // This line does that, attaching a callback function we've written before getting to this point, in this case it's called processOSC
00089     // For more info how this works, see http://mbed.org/cookbook/FunctionPointer
00090     osc.messageReceivedCallback.attach(&processOSC);
00091 
00092     //// TASK: Prime button change detection
00093     buttonLastState = button;
00094 
00095     //// TASK: GO!
00096 
00097     // We've finished setting up, now loop this forever...
00098     while (true) {
00099         // This polls the network connection for new activity, without keeping on calling this you won't receive any OSC!
00100         Net::poll();
00101 
00102         // Has the button changed?
00103         if (button != buttonLastState) {
00104             // If so, lets update the lastState variable and then send an OSC message
00105             buttonLastState = button;
00106             
00107             sendMes.setTopAddress("/mbed");
00108             sendMes.setSubAddress("/button");
00109             sendMes.setArgs("i", (long)button); // The payload will be the button state as an integer, ie. 0 or 1. We need to cast to 'long' for ints (and 'double' for floats).// The payload will be the button state as an integer, ie. 0 or 1. We need to cast to 'long' for ints (and 'double' for floats).
00110             osc.sendOsc(&sendMes);
00111             
00112             printf("Sent OSC message /mbed/button \r\n");
00113         }
00114 
00115         // ... Do whatever needs to be done by your mbed otherwise. If an OSC message is received, your messageReceivedCallback will run (in this case, processOSC()).
00116     }
00117 }
00118 
00119 /* EXAMPLE SEND/RECEIVE on PROCESSING:
00120 
00121 // oscP5sendreceive by andreas schlegel
00122 // example shows how to send and receive osc messages.
00123 // oscP5 website at http://www.sojamo.de/oscP5
00124 
00125 
00126 import oscP5.*;
00127 import netP5.*;
00128 
00129 OscP5 oscP5;
00130 NetAddress myRemoteLocation;
00131 
00132 void setup() {
00133   size(400,400);
00134   frameRate(25);
00135   // start oscP5, listening for incoming messages at port 12000
00136   oscP5 = new OscP5(this,12000);
00137 
00138   // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
00139   // an ip address and a port number. myRemoteLocation is used as parameter in
00140   // oscP5.send() when sending osc packets to another computer, device,
00141   // application. usage see below. for testing purposes the listening port
00142   // and the port of the remote location address are the same, hence you will
00143   // send messages back to this sketch.
00144   myRemoteLocation = new NetAddress("10.0.0.2",10000);
00145 }
00146 
00147 
00148 void draw() {
00149   background(0);
00150 }
00151 
00152 void mousePressed() {
00153   // in the following different ways of creating osc messages are shown by example
00154   OscMessage myMessage = new OscMessage("/mbed/test1");
00155 
00156   myMessage.add(123); // add an int to the osc message
00157 
00158   // send the message
00159   oscP5.send(myMessage, myRemoteLocation);
00160 }
00161 
00162 
00163 // incoming osc message are forwarded to the oscEvent method.
00164 void oscEvent(OscMessage theOscMessage) {
00165   // print the address pattern and the typetag of the received OscMessage
00166   print("### received an osc message.");
00167   print(" addrpattern: "+theOscMessage.addrPattern());
00168   println(" typetag: "+theOscMessage.typetag());
00169 }
00170 
00171 */