simple tester example >>> osc destination & receive

Dependencies:   mbed

Fork of myOSC_test by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Sat Oct 15 14:15:55 2011 +0000
Revision:
0:f76708a93fb3
Child:
1:818cf7a97804
First working test of this simple OSC library.

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 DigitalOut myled(LED1);
mbedalvaro 0:f76708a93fb3 5
mbedalvaro 0:f76708a93fb3 6 // mbed IP address (server):
mbedalvaro 0:f76708a93fb3 7 #ifdef DHCP
mbedalvaro 0:f76708a93fb3 8 EthernetNetIf eth;
mbedalvaro 0:f76708a93fb3 9 #else
mbedalvaro 0:f76708a93fb3 10 EthernetNetIf eth(
mbedalvaro 0:f76708a93fb3 11 IpAddr(10,0,0,2), //IP Address
mbedalvaro 0:f76708a93fb3 12 IpAddr(255,255,255,0), //Network Mask
mbedalvaro 0:f76708a93fb3 13 IpAddr(10,0,0,1), //Gateway
mbedalvaro 0:f76708a93fb3 14 IpAddr(10,0,0,1) //DNS
mbedalvaro 0:f76708a93fb3 15 );
mbedalvaro 0:f76708a93fb3 16 #endif
mbedalvaro 0:f76708a93fb3 17
mbedalvaro 0:f76708a93fb3 18 //uint8_t serverMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
mbedalvaro 0:f76708a93fb3 19 uint8_t serverIp[] = { 10, 0, 0, 2 }; // not needed perhaps!
mbedalvaro 0:f76708a93fb3 20 int serverPort = 10000;
mbedalvaro 0:f76708a93fb3 21
mbedalvaro 0:f76708a93fb3 22 uint8_t destIp[] = { 10, 0, 0, 1};
mbedalvaro 0:f76708a93fb3 23 int destPort = 12000;
mbedalvaro 0:f76708a93fb3 24
mbedalvaro 0:f76708a93fb3 25 char *topAddress="/mbed";
mbedalvaro 0:f76708a93fb3 26 char *subAddress[3]={ "/test1" , "/test2" , "/test3" };
mbedalvaro 0:f76708a93fb3 27
mbedalvaro 0:f76708a93fb3 28 OSCMessage recMes;
mbedalvaro 0:f76708a93fb3 29 OSCMessage sendMes;
mbedalvaro 0:f76708a93fb3 30
mbedalvaro 0:f76708a93fb3 31 OSCClass osc;
mbedalvaro 0:f76708a93fb3 32 //OSCClass osc(&recMes); // instantiate OSC communication object, and set the receiver container from the OSC packets
mbedalvaro 0:f76708a93fb3 33
mbedalvaro 0:f76708a93fb3 34 Serial pc(USBTX, USBRX);
mbedalvaro 0:f76708a93fb3 35
mbedalvaro 0:f76708a93fb3 36 void processOSC(UDPSocketEvent e);
mbedalvaro 0:f76708a93fb3 37
mbedalvaro 0:f76708a93fb3 38 int main() {
mbedalvaro 0:f76708a93fb3 39
mbedalvaro 0:f76708a93fb3 40 // make debug port:
mbedalvaro 0:f76708a93fb3 41 pc.baud(115200);
mbedalvaro 0:f76708a93fb3 42
mbedalvaro 0:f76708a93fb3 43 // Set the Ethernet port:
mbedalvaro 0:f76708a93fb3 44 printf("Setting up...\r\n");
mbedalvaro 0:f76708a93fb3 45 EthernetErr ethErr = eth.setup();
mbedalvaro 0:f76708a93fb3 46 if(ethErr)
mbedalvaro 0:f76708a93fb3 47 {
mbedalvaro 0:f76708a93fb3 48 printf("Error %d in setup.\r\n", ethErr);
mbedalvaro 0:f76708a93fb3 49 return -1;
mbedalvaro 0:f76708a93fb3 50 }
mbedalvaro 0:f76708a93fb3 51 printf("Setup OK\r\n");
mbedalvaro 0:f76708a93fb3 52
mbedalvaro 0:f76708a93fb3 53
mbedalvaro 0:f76708a93fb3 54 //(1) Sending message:
mbedalvaro 0:f76708a93fb3 55 // Set IP and Port:
mbedalvaro 0:f76708a93fb3 56 sendMes.setIp( destIp );
mbedalvaro 0:f76708a93fb3 57 sendMes.setPort( destPort );
mbedalvaro 0:f76708a93fb3 58 // Set data:
mbedalvaro 0:f76708a93fb3 59 // sendMes.setTopAddress(topAddress);
mbedalvaro 0:f76708a93fb3 60
mbedalvaro 0:f76708a93fb3 61 //setting osc functionnality:
mbedalvaro 0:f76708a93fb3 62 //(2) Receiving:
mbedalvaro 0:f76708a93fb3 63 // recMes.setIp( serverIp ); // not needed?
mbedalvaro 0:f76708a93fb3 64 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)
mbedalvaro 0:f76708a93fb3 65 osc.begin(serverPort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
mbedalvaro 0:f76708a93fb3 66
mbedalvaro 0:f76708a93fb3 67 //========================================== INFINITE LOOP (in USER PROGRAM CONTEXT) ===================================================================
mbedalvaro 0:f76708a93fb3 68 while(true) {
mbedalvaro 0:f76708a93fb3 69 Net::poll();
mbedalvaro 0:f76708a93fb3 70
mbedalvaro 0:f76708a93fb3 71 // ... Do whatever needs to be done (note: the osc processing is done in the processOSC function.
mbedalvaro 0:f76708a93fb3 72 // BUT DO NOT FORGET to call osc.onUDPSocketEvent(e). This is messy... should find a better way to do it.
mbedalvaro 0:f76708a93fb3 73
mbedalvaro 0:f76708a93fb3 74 }
mbedalvaro 0:f76708a93fb3 75 }
mbedalvaro 0:f76708a93fb3 76
mbedalvaro 0:f76708a93fb3 77
mbedalvaro 0:f76708a93fb3 78 // This is the callback function called when there are packets on the listening socket. It is not nice to have it
mbedalvaro 0:f76708a93fb3 79 // here, but for the time being having a "wrapping global" is the simplest solution (we cannot pass a member-function pointer
mbedalvaro 0:f76708a93fb3 80 // as handler to the upd object).
mbedalvaro 0:f76708a93fb3 81 void processOSC(UDPSocketEvent e) {
mbedalvaro 0:f76708a93fb3 82 osc.onUDPSocketEvent(e);
mbedalvaro 0:f76708a93fb3 83
mbedalvaro 0:f76708a93fb3 84 if (osc.newMessage) { // in fact, there is no need to check this if using the method of a global callback function - it is clear this is a new packet... however, it may be
mbedalvaro 0:f76708a93fb3 85 // interesting to use a timer, and process data (answers, etc) only after a certain amount of time, so as to avoid blocking the program in IRQ context...
mbedalvaro 0:f76708a93fb3 86
mbedalvaro 0:f76708a93fb3 87 //pc.printf("we received top address %s\n", recMes.getAddress(0) );
mbedalvaro 0:f76708a93fb3 88
mbedalvaro 0:f76708a93fb3 89 osc.newMessage=false; // note: if using: message=osc.getMessage(), then we don't need to do this explicitly.
mbedalvaro 0:f76708a93fb3 90
mbedalvaro 0:f76708a93fb3 91 // Test:
mbedalvaro 0:f76708a93fb3 92 if( !strcmp( recMes.getAddress(0) , "mbed" ) ) {
mbedalvaro 0:f76708a93fb3 93 pc.printf("we received topAddress= mbed \n");
mbedalvaro 0:f76708a93fb3 94 if( !strcmp( recMes.getAddress(1) , "test1" ) )
mbedalvaro 0:f76708a93fb3 95 pc.printf("we received subAddress= test1 \n");
mbedalvaro 0:f76708a93fb3 96
mbedalvaro 0:f76708a93fb3 97 // Send some osc message:
mbedalvaro 0:f76708a93fb3 98 sendMes.setTopAddress("/working...");
mbedalvaro 0:f76708a93fb3 99 osc.sendOsc(&sendMes);
mbedalvaro 0:f76708a93fb3 100 }
mbedalvaro 0:f76708a93fb3 101 }
mbedalvaro 0:f76708a93fb3 102
mbedalvaro 0:f76708a93fb3 103 }
mbedalvaro 0:f76708a93fb3 104
mbedalvaro 0:f76708a93fb3 105
mbedalvaro 0:f76708a93fb3 106 /* EXAMPLE SEND/RECEIVE on PROCESSING:
mbedalvaro 0:f76708a93fb3 107
mbedalvaro 0:f76708a93fb3 108 // oscP5sendreceive by andreas schlegel
mbedalvaro 0:f76708a93fb3 109 // example shows how to send and receive osc messages.
mbedalvaro 0:f76708a93fb3 110 // oscP5 website at http://www.sojamo.de/oscP5
mbedalvaro 0:f76708a93fb3 111
mbedalvaro 0:f76708a93fb3 112
mbedalvaro 0:f76708a93fb3 113 import oscP5.*;
mbedalvaro 0:f76708a93fb3 114 import netP5.*;
mbedalvaro 0:f76708a93fb3 115
mbedalvaro 0:f76708a93fb3 116 OscP5 oscP5;
mbedalvaro 0:f76708a93fb3 117 NetAddress myRemoteLocation;
mbedalvaro 0:f76708a93fb3 118
mbedalvaro 0:f76708a93fb3 119 void setup() {
mbedalvaro 0:f76708a93fb3 120 size(400,400);
mbedalvaro 0:f76708a93fb3 121 frameRate(25);
mbedalvaro 0:f76708a93fb3 122 // start oscP5, listening for incoming messages at port 12000
mbedalvaro 0:f76708a93fb3 123 oscP5 = new OscP5(this,12000);
mbedalvaro 0:f76708a93fb3 124
mbedalvaro 0:f76708a93fb3 125 // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
mbedalvaro 0:f76708a93fb3 126 // an ip address and a port number. myRemoteLocation is used as parameter in
mbedalvaro 0:f76708a93fb3 127 // oscP5.send() when sending osc packets to another computer, device,
mbedalvaro 0:f76708a93fb3 128 // application. usage see below. for testing purposes the listening port
mbedalvaro 0:f76708a93fb3 129 // and the port of the remote location address are the same, hence you will
mbedalvaro 0:f76708a93fb3 130 // send messages back to this sketch.
mbedalvaro 0:f76708a93fb3 131 myRemoteLocation = new NetAddress("10.0.0.2",10000);
mbedalvaro 0:f76708a93fb3 132 }
mbedalvaro 0:f76708a93fb3 133
mbedalvaro 0:f76708a93fb3 134
mbedalvaro 0:f76708a93fb3 135 void draw() {
mbedalvaro 0:f76708a93fb3 136 background(0);
mbedalvaro 0:f76708a93fb3 137 }
mbedalvaro 0:f76708a93fb3 138
mbedalvaro 0:f76708a93fb3 139 void mousePressed() {
mbedalvaro 0:f76708a93fb3 140 // in the following different ways of creating osc messages are shown by example
mbedalvaro 0:f76708a93fb3 141 OscMessage myMessage = new OscMessage("/mbed/test1");
mbedalvaro 0:f76708a93fb3 142
mbedalvaro 0:f76708a93fb3 143 myMessage.add(123); // add an int to the osc message
mbedalvaro 0:f76708a93fb3 144
mbedalvaro 0:f76708a93fb3 145 // send the message
mbedalvaro 0:f76708a93fb3 146 oscP5.send(myMessage, myRemoteLocation);
mbedalvaro 0:f76708a93fb3 147 }
mbedalvaro 0:f76708a93fb3 148
mbedalvaro 0:f76708a93fb3 149
mbedalvaro 0:f76708a93fb3 150 // incoming osc message are forwarded to the oscEvent method.
mbedalvaro 0:f76708a93fb3 151 void oscEvent(OscMessage theOscMessage) {
mbedalvaro 0:f76708a93fb3 152 // print the address pattern and the typetag of the received OscMessage
mbedalvaro 0:f76708a93fb3 153 print("### received an osc message.");
mbedalvaro 0:f76708a93fb3 154 print(" addrpattern: "+theOscMessage.addrPattern());
mbedalvaro 0:f76708a93fb3 155 println(" typetag: "+theOscMessage.typetag());
mbedalvaro 0:f76708a93fb3 156 }
mbedalvaro 0:f76708a93fb3 157
mbedalvaro 0:f76708a93fb3 158 */