Takayuki Ito / Mbed 2 deprecated OSCTest_2

Dependencies:   EthernetNetIf mbed mbed

Fork of OSC by Toby Harris

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, 5};
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             int na = recMes.getArgNum();
00055             printf("numArgs: %d \r\n", na);
00056             
00057             for (int i = 0; i < na; i++) {
00058                 int v = recMes.getArgInt(i);
00059                 printf("%d, \r\n", v);
00060             }
00061                                
00062         // Send some osc message:
00063         sendMes.setTopAddress("/working...");
00064 //        sendMes.setArgs("i", &v1);
00065         osc.sendOsc(&sendMes);
00066     }
00067 }
00068 
00069 ////  M A I N
00070 int main() {
00071 
00072     //// TASK: Set up the Ethernet port
00073     printf("Setting up ethernet...\r\n");
00074     EthernetErr ethErr = eth.setup();
00075     if (ethErr) {
00076         printf("Ethernet Failed to setup. Error: %d\r\n", ethErr);
00077         return -1;
00078     }
00079     printf("Ethernet OK\r\n");
00080 
00081     //// TASK: Set up OSC message sending
00082 
00083     // In the OSC message container we've made for send messages, set where we want it to go:
00084     sendMes.setIp( destIp );
00085     sendMes.setPort( destPort );
00086 
00087     //// TASK: Set up OSC message receiving
00088 
00089     // In the OSC send/receive object...
00090     // Set the OSC message container for it to parse received messages into
00091     osc.setReceiveMessage(&recMes);
00092 
00093     // Tell it to begin listening for OSC messages at the port specified (the IP address we know already, it's the mbed's!).
00094     osc.begin(mbedListenPort);
00095 
00096     // 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.
00097     // This line does that, attaching a callback function we've written before getting to this point, in this case it's called processOSC
00098     // For more info how this works, see http://mbed.org/cookbook/FunctionPointer
00099     osc.messageReceivedCallback.attach(&processOSC);
00100 
00101     //// TASK: Prime button change detection
00102     buttonLastState = button;
00103 
00104     //// TASK: GO!
00105 
00106     // We've finished setting up, now loop this forever...
00107     while (true) {
00108         // This polls the network connection for new activity, without keeping on calling this you won't receive any OSC!
00109         Net::poll();
00110 
00111         // Has the button changed?
00112         if (button != buttonLastState) {
00113             // If so, lets update the lastState variable and then send an OSC message
00114             buttonLastState = button;
00115             
00116             sendMes.setTopAddress("/mbed");
00117             sendMes.setSubAddress("/button");
00118             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).
00119             osc.sendOsc(&sendMes);
00120             
00121             printf("Sent OSC message /mbed/button \r\n");
00122         }
00123 
00124         // ... Do whatever needs to be done by your mbed otherwise. If an OSC message is received, your messageReceivedCallback will run (in this case, processOSC()).
00125     }
00126 }