An Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige written by: Alvaro Cassinelli, October 2011 tweaked by: Toby Harris / *spark audio-visual, March 2012

Dependents:   SPK-DVIMXR

Committer:
tobyspark
Date:
Sun Apr 15 15:50:42 2012 +0000
Revision:
0:fdea65150534
mbedOSC__spk as program to OSC as library

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(
tobyspark 0:fdea65150534 12 IpAddr(10,0,0,2), //IP Address
tobyspark 0:fdea65150534 13 IpAddr(255,255,255,0), //Network Mask
tobyspark 0:fdea65150534 14 IpAddr(10,0,0,1), //Gateway
tobyspark 0:fdea65150534 15 IpAddr(10,0,0,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
tobyspark 0:fdea65150534 22 OSCClass osc;
tobyspark 0:fdea65150534 23
tobyspark 0:fdea65150534 24 // The message objects to send and receive with
tobyspark 0:fdea65150534 25 OSCMessage recMes;
tobyspark 0:fdea65150534 26 OSCMessage sendMes;
tobyspark 0:fdea65150534 27
tobyspark 0:fdea65150534 28 // Setting - The port we're listening to on the mbed for OSC messages
tobyspark 0:fdea65150534 29 int mbedListenPort = 10000;
tobyspark 0:fdea65150534 30
tobyspark 0:fdea65150534 31 // Setting - The address and port we're going to send to, from the mbed
tobyspark 0:fdea65150534 32 uint8_t destIp[] = { 10, 0, 0, 1};
tobyspark 0:fdea65150534 33 int destPort = 12000;
tobyspark 0:fdea65150534 34
tobyspark 0:fdea65150534 35 //// mbed input
tobyspark 0:fdea65150534 36
tobyspark 0:fdea65150534 37 DigitalIn button(p21);
tobyspark 0:fdea65150534 38 bool buttonLastState;
tobyspark 0:fdea65150534 39
tobyspark 0:fdea65150534 40 //// Our messageReceivedCallback function
tobyspark 0:fdea65150534 41 void processOSC() {
tobyspark 0:fdea65150534 42
tobyspark 0:fdea65150534 43 // If this function has been called, the OSC message just received will have been parsed into our recMes OSCMessage object
tobyspark 0:fdea65150534 44 // Note we can access recMes here, outside of the main loop, as we created it as a global variable.
tobyspark 0:fdea65150534 45
tobyspark 0:fdea65150534 46 // TASK: If this message one we want, do something about it.
tobyspark 0:fdea65150534 47 // In this example we're listening for messages with a top address of "mbed".
tobyspark 0:fdea65150534 48 // Note the strcmp function returns 0 if identical, so !strcmp is true if the two strings are the same
tobyspark 0:fdea65150534 49 if ( !strcmp( recMes.getAddress(0) , "mbed" ) ) {
tobyspark 0:fdea65150534 50 printf("OSC Message received addressed to mbed \r\n");
tobyspark 0:fdea65150534 51 if ( !strcmp( recMes.getAddress(1) , "test1" ) )
tobyspark 0:fdea65150534 52 printf("Received subAddress= test1 \r\n");
tobyspark 0:fdea65150534 53
tobyspark 0:fdea65150534 54 // Send some osc message:
tobyspark 0:fdea65150534 55 sendMes.setTopAddress("/working...");
tobyspark 0:fdea65150534 56 osc.sendOsc(&sendMes);
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
tobyspark 0:fdea65150534 63 //// TASK: Set up the Ethernet port
tobyspark 0:fdea65150534 64 printf("Setting up ethernet...\r\n");
tobyspark 0:fdea65150534 65 EthernetErr ethErr = eth.setup();
tobyspark 0:fdea65150534 66 if (ethErr) {
tobyspark 0:fdea65150534 67 printf("Ethernet Failed to setup. Error: %d\r\n", ethErr);
tobyspark 0:fdea65150534 68 return -1;
tobyspark 0:fdea65150534 69 }
tobyspark 0:fdea65150534 70 printf("Ethernet OK\r\n");
tobyspark 0:fdea65150534 71
tobyspark 0:fdea65150534 72 //// TASK: Set up OSC message sending
tobyspark 0:fdea65150534 73
tobyspark 0:fdea65150534 74 // In the OSC message container we've made for send messages, set where we want it to go:
tobyspark 0:fdea65150534 75 sendMes.setIp( destIp );
tobyspark 0:fdea65150534 76 sendMes.setPort( destPort );
tobyspark 0:fdea65150534 77
tobyspark 0:fdea65150534 78 //// TASK: Set up OSC message receiving
tobyspark 0:fdea65150534 79
tobyspark 0:fdea65150534 80 // In the OSC send/receive object...
tobyspark 0:fdea65150534 81 // Set the OSC message container for it to parse received messages into
tobyspark 0:fdea65150534 82 osc.setReceiveMessage(&recMes);
tobyspark 0:fdea65150534 83
tobyspark 0:fdea65150534 84 // Tell it to begin listening for OSC messages at the port specified (the IP address we know already, it's the mbed's!).
tobyspark 0:fdea65150534 85 osc.begin(mbedListenPort);
tobyspark 0:fdea65150534 86
tobyspark 0:fdea65150534 87 // 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 88 // 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 89 // For more info how this works, see http://mbed.org/cookbook/FunctionPointer
tobyspark 0:fdea65150534 90 osc.messageReceivedCallback.attach(&processOSC);
tobyspark 0:fdea65150534 91
tobyspark 0:fdea65150534 92 //// TASK: Prime button change detection
tobyspark 0:fdea65150534 93 buttonLastState = button;
tobyspark 0:fdea65150534 94
tobyspark 0:fdea65150534 95 //// TASK: GO!
tobyspark 0:fdea65150534 96
tobyspark 0:fdea65150534 97 // We've finished setting up, now loop this forever...
tobyspark 0:fdea65150534 98 while (true) {
tobyspark 0:fdea65150534 99 // This polls the network connection for new activity, without keeping on calling this you won't receive any OSC!
tobyspark 0:fdea65150534 100 Net::poll();
tobyspark 0:fdea65150534 101
tobyspark 0:fdea65150534 102 // Has the button changed?
tobyspark 0:fdea65150534 103 if (button != buttonLastState) {
tobyspark 0:fdea65150534 104 // If so, lets update the lastState variable and then send an OSC message
tobyspark 0:fdea65150534 105 buttonLastState = button;
tobyspark 0:fdea65150534 106
tobyspark 0:fdea65150534 107 sendMes.setTopAddress("/mbed");
tobyspark 0:fdea65150534 108 sendMes.setSubAddress("/button");
tobyspark 0:fdea65150534 109 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).
tobyspark 0:fdea65150534 110 osc.sendOsc(&sendMes);
tobyspark 0:fdea65150534 111
tobyspark 0:fdea65150534 112 printf("Sent OSC message /mbed/button \r\n");
tobyspark 0:fdea65150534 113 }
tobyspark 0:fdea65150534 114
tobyspark 0:fdea65150534 115 // ... Do whatever needs to be done by your mbed otherwise. If an OSC message is received, your messageReceivedCallback will run (in this case, processOSC()).
tobyspark 0:fdea65150534 116 }
tobyspark 0:fdea65150534 117 }