simple tester example >>> osc destination & receive

Dependencies:   mbed

Fork of myOSC_test by Alvaro Cassinelli

Committer:
sfjmt
Date:
Tue Aug 06 10:12:56 2013 +0000
Revision:
2:44f1e5803762
Parent:
1:818cf7a97804
revision_01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:f76708a93fb3 1 #include "mbed.h"
mbedalvaro 0:f76708a93fb3 2 #include "mbedOSC.h"
mbedalvaro 0:f76708a93fb3 3
mbedalvaro 0:f76708a93fb3 4 // mbed IP address (server):
mbedalvaro 0:f76708a93fb3 5 #ifdef DHCP
mbedalvaro 0:f76708a93fb3 6 EthernetNetIf eth;
mbedalvaro 0:f76708a93fb3 7 #else
mbedalvaro 0:f76708a93fb3 8 EthernetNetIf eth(
sfjmt 2:44f1e5803762 9 IpAddr(192,168,12,210),//your mbed IP Address
sfjmt 2:44f1e5803762 10 IpAddr(255,255,255,0),//Network Mask
sfjmt 1:818cf7a97804 11 IpAddr(),//Gateway
sfjmt 1:818cf7a97804 12 IpAddr()//DNS
mbedalvaro 0:f76708a93fb3 13 );
mbedalvaro 0:f76708a93fb3 14 #endif
mbedalvaro 0:f76708a93fb3 15
sfjmt 1:818cf7a97804 16 //receive
sfjmt 2:44f1e5803762 17 uint8_t receiveIp[] = { 192, 168, 12, 210 };//IP Address
sfjmt 2:44f1e5803762 18 int receivePort = 5678;//port
sfjmt 1:818cf7a97804 19
sfjmt 1:818cf7a97804 20 //destination.
sfjmt 2:44f1e5803762 21 uint8_t destIp[] = { 192, 168, 12, 51};//IP Address
sfjmt 2:44f1e5803762 22 int destPort = 10000;//port
sfjmt 1:818cf7a97804 23
sfjmt 1:818cf7a97804 24 //send value propaty.
sfjmt 1:818cf7a97804 25 char *topAddress = "/mbed";
sfjmt 1:818cf7a97804 26 char *subAddress[3] = {"/test1","/test2","/test3"};
sfjmt 2:44f1e5803762 27
sfjmt 2:44f1e5803762 28 //receive value propaty
sfjmt 1:818cf7a97804 29 char *requestTopStr[3] = {"mbed1","mbed2","mbed3"};
sfjmt 1:818cf7a97804 30 char *requestSubStr[3] = {"test1","test2","test3"};
sfjmt 1:818cf7a97804 31
sfjmt 1:818cf7a97804 32 //instance
mbedalvaro 0:f76708a93fb3 33 OSCMessage recMes;
mbedalvaro 0:f76708a93fb3 34 OSCMessage sendMes;
sfjmt 1:818cf7a97804 35 OSCClass osc;
sfjmt 1:818cf7a97804 36 //OSCClass osc(&recMes); // instantiate OSC communication object, and set the receiver container from the OSC packets
sfjmt 1:818cf7a97804 37 Serial pc(USBTX, USBRX);
sfjmt 1:818cf7a97804 38
sfjmt 1:818cf7a97804 39 //LED
sfjmt 1:818cf7a97804 40 DigitalOut led(LED1);
sfjmt 1:818cf7a97804 41
sfjmt 1:818cf7a97804 42 void processOSC(UDPSocketEvent e);
sfjmt 1:818cf7a97804 43
sfjmt 1:818cf7a97804 44 int main()
sfjmt 1:818cf7a97804 45 {
sfjmt 1:818cf7a97804 46 // Set the Ethernet port:
sfjmt 1:818cf7a97804 47 EthernetErr ethErr = eth.setup();
sfjmt 1:818cf7a97804 48 if(ethErr) {
sfjmt 2:44f1e5803762 49 //error
sfjmt 1:818cf7a97804 50 return -1;
sfjmt 1:818cf7a97804 51 }
sfjmt 1:818cf7a97804 52
sfjmt 1:818cf7a97804 53
sfjmt 2:44f1e5803762 54 //(1) Sending message >>>
sfjmt 1:818cf7a97804 55 // Set IP and Port:
sfjmt 1:818cf7a97804 56 sendMes.setIp( destIp );
sfjmt 1:818cf7a97804 57 sendMes.setPort( destPort );
sfjmt 1:818cf7a97804 58
sfjmt 2:44f1e5803762 59 //(2) Receiving <<<
sfjmt 1:818cf7a97804 60 recMes.setIp(receiveIp);
sfjmt 1:818cf7a97804 61 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)
sfjmt 1:818cf7a97804 62 osc.begin(receivePort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
sfjmt 1:818cf7a97804 63
sfjmt 1:818cf7a97804 64 //loop
mbedalvaro 0:f76708a93fb3 65 while(true) {
mbedalvaro 0:f76708a93fb3 66 Net::poll();
sfjmt 1:818cf7a97804 67
sfjmt 1:818cf7a97804 68 //Set data
sfjmt 1:818cf7a97804 69 sendMes.setTopAddress(topAddress);//top address
sfjmt 1:818cf7a97804 70 sendMes.setSubAddress(subAddress[0]);//sub address
sfjmt 1:818cf7a97804 71 sendMes.setArgs("i", 1);//type , value
sfjmt 1:818cf7a97804 72
sfjmt 1:818cf7a97804 73 osc.sendOsc(&sendMes);//send!
mbedalvaro 0:f76708a93fb3 74 }
mbedalvaro 0:f76708a93fb3 75 }
mbedalvaro 0:f76708a93fb3 76
mbedalvaro 0:f76708a93fb3 77
sfjmt 1:818cf7a97804 78 //osc callback function
sfjmt 1:818cf7a97804 79 void processOSC(UDPSocketEvent e)
sfjmt 1:818cf7a97804 80 {
mbedalvaro 0:f76708a93fb3 81 osc.onUDPSocketEvent(e);
mbedalvaro 0:f76708a93fb3 82
sfjmt 1:818cf7a97804 83 if (osc.newMessage) {
sfjmt 2:44f1e5803762 84
sfjmt 1:818cf7a97804 85 osc.newMessage = false; // note: if using: message = osc.getMessage(), then we don't need to do this explicitly.
mbedalvaro 0:f76708a93fb3 86
sfjmt 1:818cf7a97804 87 //(strcmp(str1,str2)==0)
sfjmt 1:818cf7a97804 88 //if ... str1 = str2 >>> same
sfjmt 1:818cf7a97804 89 if(strcmp(recMes.getAddress(0),requestTopStr[0])==0) {
sfjmt 1:818cf7a97804 90 if(strcmp(recMes.getAddress(1),requestSubStr[0])==0) {
sfjmt 1:818cf7a97804 91 led = 1;//led HIGH
sfjmt 1:818cf7a97804 92 wait(0.2);
sfjmt 1:818cf7a97804 93 led = 0;//led LOW
sfjmt 1:818cf7a97804 94 wait(0.2);
sfjmt 1:818cf7a97804 95 led = 1;//led HIGH
sfjmt 1:818cf7a97804 96 wait(0.2);
sfjmt 1:818cf7a97804 97 led = 0;//led LOW
sfjmt 1:818cf7a97804 98 wait(0.2);
sfjmt 1:818cf7a97804 99 led = 1;//led HIGH
sfjmt 1:818cf7a97804 100 wait(0.2);
sfjmt 1:818cf7a97804 101 led = 0;//led LOW
sfjmt 1:818cf7a97804 102 wait(0.2);
sfjmt 1:818cf7a97804 103 }
sfjmt 1:818cf7a97804 104 } else {
sfjmt 1:818cf7a97804 105 //action
sfjmt 1:818cf7a97804 106 led = 0;//led LOW
sfjmt 1:818cf7a97804 107 }
sfjmt 1:818cf7a97804 108 }
sfjmt 1:818cf7a97804 109 }