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

Dependents:   SPK-DVIMXR

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.h Source File

example.h

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 }