SHOHEI FUJIMOTO / Mbed 2 deprecated SimpleOscTester

Dependencies:   mbed

Fork of myOSC_test by Alvaro Cassinelli

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 // mbed IP address (server):
00005 #ifdef DHCP
00006 EthernetNetIf eth;
00007 #else
00008 EthernetNetIf eth(
00009     IpAddr(192,168,12,210),//your mbed IP Address
00010     IpAddr(255,255,255,0),//Network Mask
00011     IpAddr(),//Gateway
00012     IpAddr()//DNS
00013 );
00014 #endif
00015 
00016 //receive
00017 uint8_t receiveIp[] = { 192, 168, 12, 210 };//IP Address
00018 int receivePort     = 5678;//port
00019 
00020 //destination.
00021 uint8_t destIp[] = { 192, 168, 12, 51};//IP Address
00022 int destPort     = 10000;//port
00023 
00024 //send value propaty.
00025 char *topAddress = "/mbed";
00026 char *subAddress[3] = {"/test1","/test2","/test3"};
00027 
00028 //receive value propaty
00029 char *requestTopStr[3] = {"mbed1","mbed2","mbed3"};
00030 char *requestSubStr[3] = {"test1","test2","test3"};
00031 
00032 //instance
00033 OSCMessage recMes;
00034 OSCMessage sendMes;
00035 OSCClass osc;
00036 //OSCClass osc(&recMes);  // instantiate OSC communication object, and set the receiver container from the OSC packets
00037 Serial pc(USBTX, USBRX);
00038 
00039 //LED
00040 DigitalOut led(LED1);
00041 
00042 void processOSC(UDPSocketEvent e);
00043 
00044 int main()
00045 {
00046     // Set the Ethernet port:
00047     EthernetErr ethErr = eth.setup();
00048     if(ethErr) {
00049         //error
00050         return -1;
00051     }
00052 
00053 
00054     //(1) Sending message >>>
00055     // Set IP and Port:
00056     sendMes.setIp( destIp );
00057     sendMes.setPort( destPort );
00058 
00059     //(2) Receiving <<<
00060     recMes.setIp(receiveIp);
00061     osc.setReceiveMessage(&recMes); // this sets the receiver container for the OSC packets (we can avoid doing this if we use osc.getMessage() to get messages)
00062     osc.begin(receivePort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
00063 
00064     //loop
00065     while(true) {
00066         Net::poll();
00067 
00068         //Set data
00069         sendMes.setTopAddress(topAddress);//top address
00070         sendMes.setSubAddress(subAddress[0]);//sub address
00071         sendMes.setArgs("i", 1);//type , value
00072 
00073         osc.sendOsc(&sendMes);//send!
00074     }
00075 }
00076 
00077 
00078 //osc callback function
00079 void processOSC(UDPSocketEvent e)
00080 {
00081     osc.onUDPSocketEvent(e);
00082 
00083     if (osc.newMessage) {
00084     
00085         osc.newMessage = false; // note: if using: message = osc.getMessage(), then we don't need to do this explicitly.
00086 
00087         //(strcmp(str1,str2)==0)
00088         //if ... str1 = str2 >>> same
00089         if(strcmp(recMes.getAddress(0),requestTopStr[0])==0) {
00090             if(strcmp(recMes.getAddress(1),requestSubStr[0])==0) {
00091                 led = 1;//led HIGH
00092                 wait(0.2);
00093                 led = 0;//led LOW
00094                 wait(0.2);
00095                 led = 1;//led HIGH
00096                 wait(0.2);
00097                 led = 0;//led LOW
00098                 wait(0.2);
00099                 led = 1;//led HIGH
00100                 wait(0.2);
00101                 led = 0;//led LOW
00102                 wait(0.2);
00103             }
00104         } else {
00105             //action
00106             led = 0;//led LOW
00107         }
00108     }
00109 }