support OSC-string

Dependents:   OSCtoCVConverter

Fork of OSC by Toby Harris

Committer:
casiotone401
Date:
Tue Mar 08 11:50:30 2016 +0000
Revision:
8:73bce95a6853
Parent:
6:a47004fb44f5
minor change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tobyspark 0:fdea65150534 1 #include "mbed.h"
tobyspark 0:fdea65150534 2 #include "mbedOSC.h"
tobyspark 0:fdea65150534 3
tobyspark 0:fdea65150534 4 //// ETHERNET
tobyspark 0:fdea65150534 5
tobyspark 0:fdea65150534 6 // Ethernet can be created with *either* an address assigned by DHCP or a static IP address. Uncomment the define line for DHCP
tobyspark 0:fdea65150534 7 //#define DHCP
tobyspark 0:fdea65150534 8 #ifdef DHCP
tobyspark 0:fdea65150534 9 EthernetNetIf eth;
tobyspark 0:fdea65150534 10 #else
tobyspark 0:fdea65150534 11 EthernetNetIf eth(
casiotone401 6:a47004fb44f5 12 IpAddr(192,168,1,6), //IP Address
tobyspark 0:fdea65150534 13 IpAddr(255,255,255,0), //Network Mask
casiotone401 6:a47004fb44f5 14 IpAddr(192,168,1,1), //Gateway
casiotone401 6:a47004fb44f5 15 IpAddr(192,168,1,1) //DNS
tobyspark 0:fdea65150534 16 );
tobyspark 0:fdea65150534 17 #endif
tobyspark 0:fdea65150534 18
tobyspark 0:fdea65150534 19 //// OSC
tobyspark 0:fdea65150534 20
tobyspark 0:fdea65150534 21 // The object to do the work of sending and receiving
casiotone401 6:a47004fb44f5 22 OSCClass oscs;
tobyspark 0:fdea65150534 23
tobyspark 0:fdea65150534 24 // The message objects to send and receive with
tobyspark 0:fdea65150534 25 OSCMessage recMes;
casiotone401 6:a47004fb44f5 26 OSCMessage sendsMes;
tobyspark 0:fdea65150534 27 // Setting - The port we're listening to on the mbed for OSC messages
casiotone401 6:a47004fb44f5 28 int mbedListenPort = 12345;
tobyspark 0:fdea65150534 29
tobyspark 0:fdea65150534 30 // Setting - The address and port we're going to send to, from the mbed
casiotone401 6:a47004fb44f5 31 uint8_t destIp[] = { 192, 168, 1, 2};
casiotone401 6:a47004fb44f5 32 int destPort = 8000;
tobyspark 0:fdea65150534 33
tobyspark 0:fdea65150534 34 //// mbed input
tobyspark 0:fdea65150534 35
tobyspark 0:fdea65150534 36 //// Our messageReceivedCallback function
tobyspark 0:fdea65150534 37 void processOSC() {
tobyspark 0:fdea65150534 38
tobyspark 0:fdea65150534 39 // If this function has been called, the OSC message just received will have been parsed into our recMes OSCMessage object
tobyspark 0:fdea65150534 40 // Note we can access recMes here, outside of the main loop, as we created it as a global variable.
tobyspark 0:fdea65150534 41
tobyspark 0:fdea65150534 42 // TASK: If this message one we want, do something about it.
tobyspark 0:fdea65150534 43 // In this example we're listening for messages with a top address of "mbed".
tobyspark 0:fdea65150534 44 // Note the strcmp function returns 0 if identical, so !strcmp is true if the two strings are the same
casiotone401 6:a47004fb44f5 45 if ( !strcmp( recMes.getAddress(0) , "mode" ) ) {
casiotone401 6:a47004fb44f5 46
casiotone401 6:a47004fb44f5 47 sendsMes.setTopAddress("/reset");
casiotone401 6:a47004fb44f5 48 sendsMes.setArgs("s", recMes.getArgString(0));
casiotone401 6:a47004fb44f5 49 oscs.sendOsc(&sendsMes);
casiotone401 6:a47004fb44f5 50
casiotone401 6:a47004fb44f5 51 } else if (!strcmp( recMes.getAddress(0) , "ctrl7" ) ) {
casiotone401 6:a47004fb44f5 52
casiotone401 6:a47004fb44f5 53 sendsMes.setTopAddress("/scale");
casiotone401 6:a47004fb44f5 54 sendsMes.setArgs("f", recMes.getArgFloat(0));
casiotone401 6:a47004fb44f5 55 oscs.sendOsc(&sendsMes);
casiotone401 6:a47004fb44f5 56 }
tobyspark 0:fdea65150534 57
tobyspark 0:fdea65150534 58 }
tobyspark 0:fdea65150534 59
tobyspark 0:fdea65150534 60 //// M A I N
tobyspark 0:fdea65150534 61 int main() {
tobyspark 0:fdea65150534 62
casiotone401 6:a47004fb44f5 63
tobyspark 0:fdea65150534 64 EthernetErr ethErr = eth.setup();
tobyspark 0:fdea65150534 65 if (ethErr) {
tobyspark 0:fdea65150534 66 return -1;
tobyspark 0:fdea65150534 67 }
tobyspark 0:fdea65150534 68
tobyspark 0:fdea65150534 69 //// TASK: Set up OSC message sending
tobyspark 0:fdea65150534 70
tobyspark 0:fdea65150534 71 // In the OSC message container we've made for send messages, set where we want it to go:
casiotone401 6:a47004fb44f5 72 sendsMes.setIp( destIp );
casiotone401 6:a47004fb44f5 73 sendsMes.setPort( destPort );
tobyspark 0:fdea65150534 74
tobyspark 0:fdea65150534 75 //// TASK: Set up OSC message receiving
tobyspark 0:fdea65150534 76
tobyspark 0:fdea65150534 77 // In the OSC send/receive object...
tobyspark 0:fdea65150534 78 // Set the OSC message container for it to parse received messages into
casiotone401 6:a47004fb44f5 79 oscs.setReceiveMessage(&recMes);
tobyspark 0:fdea65150534 80
tobyspark 0:fdea65150534 81 // Tell it to begin listening for OSC messages at the port specified (the IP address we know already, it's the mbed's!).
casiotone401 6:a47004fb44f5 82 oscs.begin(mbedListenPort);
tobyspark 0:fdea65150534 83
tobyspark 0:fdea65150534 84 // 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.
tobyspark 0:fdea65150534 85 // This line does that, attaching a callback function we've written before getting to this point, in this case it's called processOSC
tobyspark 0:fdea65150534 86 // For more info how this works, see http://mbed.org/cookbook/FunctionPointer
casiotone401 6:a47004fb44f5 87 oscs.messageReceivedCallback.attach(&processOSC);
tobyspark 0:fdea65150534 88
tobyspark 0:fdea65150534 89
tobyspark 0:fdea65150534 90
tobyspark 0:fdea65150534 91 // We've finished setting up, now loop this forever...
tobyspark 0:fdea65150534 92 while (true) {
tobyspark 0:fdea65150534 93 // This polls the network connection for new activity, without keeping on calling this you won't receive any OSC!
tobyspark 0:fdea65150534 94 Net::poll();
tobyspark 0:fdea65150534 95 }
casiotone401 6:a47004fb44f5 96 }