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

Dependencies:   NetServices mbed

Committer:
tobyspark
Date:
Sun Apr 15 15:05:19 2012 +0000
Revision:
11:853a9e887023
Parent:
1:ab7dc9550de6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tobyspark 0:49cdaebd52d5 1 #include "mbed.h"
tobyspark 0:49cdaebd52d5 2 #include "mbedOSC.h"
tobyspark 0:49cdaebd52d5 3
tobyspark 11:853a9e887023 4 //// ETHERNET
tobyspark 0:49cdaebd52d5 5
tobyspark 11:853a9e887023 6 // Ethernet can be created with *either* an address assigned by DHCP or a static IP address. Uncomment the define line for DHCP
tobyspark 11:853a9e887023 7 //#define DHCP
tobyspark 0:49cdaebd52d5 8 #ifdef DHCP
tobyspark 0:49cdaebd52d5 9 EthernetNetIf eth;
tobyspark 0:49cdaebd52d5 10 #else
tobyspark 0:49cdaebd52d5 11 EthernetNetIf eth(
tobyspark 11:853a9e887023 12 IpAddr(10,0,0,2), //IP Address
tobyspark 11:853a9e887023 13 IpAddr(255,255,255,0), //Network Mask
tobyspark 11:853a9e887023 14 IpAddr(10,0,0,1), //Gateway
tobyspark 11:853a9e887023 15 IpAddr(10,0,0,1) //DNS
tobyspark 0:49cdaebd52d5 16 );
tobyspark 0:49cdaebd52d5 17 #endif
tobyspark 0:49cdaebd52d5 18
tobyspark 11:853a9e887023 19 //// OSC
tobyspark 11:853a9e887023 20
tobyspark 11:853a9e887023 21 // The object to do the work of sending and receiving
tobyspark 11:853a9e887023 22 OSCClass osc;
tobyspark 11:853a9e887023 23
tobyspark 11:853a9e887023 24 // The message objects to send and receive with
tobyspark 0:49cdaebd52d5 25 OSCMessage recMes;
tobyspark 0:49cdaebd52d5 26 OSCMessage sendMes;
tobyspark 11:853a9e887023 27
tobyspark 11:853a9e887023 28 // Setting - The port we're listening to on the mbed for OSC messages
tobyspark 11:853a9e887023 29 int mbedListenPort = 10000;
tobyspark 11:853a9e887023 30
tobyspark 11:853a9e887023 31 // Setting - The address and port we're going to send to, from the mbed
tobyspark 11:853a9e887023 32 uint8_t destIp[] = { 10, 0, 0, 1};
tobyspark 11:853a9e887023 33 int destPort = 12000;
tobyspark 11:853a9e887023 34
tobyspark 11:853a9e887023 35 //// mbed input
tobyspark 11:853a9e887023 36
tobyspark 11:853a9e887023 37 DigitalIn button(p21);
tobyspark 11:853a9e887023 38 bool buttonLastState;
tobyspark 0:49cdaebd52d5 39
tobyspark 11:853a9e887023 40 //// Our messageReceivedCallback function
tobyspark 11:853a9e887023 41 void processOSC() {
tobyspark 11:853a9e887023 42
tobyspark 11:853a9e887023 43 // If this function has been called, the OSC message just received will have been parsed into our recMes OSCMessage object
tobyspark 11:853a9e887023 44 // Note we can access recMes here, outside of the main loop, as we created it as a global variable.
tobyspark 11:853a9e887023 45
tobyspark 11:853a9e887023 46 // TASK: If this message one we want, do something about it.
tobyspark 11:853a9e887023 47 // In this example we're listening for messages with a top address of "mbed".
tobyspark 11:853a9e887023 48 // Note the strcmp function returns 0 if identical, so !strcmp is true if the two strings are the same
tobyspark 11:853a9e887023 49 if ( !strcmp( recMes.getAddress(0) , "mbed" ) ) {
tobyspark 11:853a9e887023 50 printf("OSC Message received addressed to mbed \r\n");
tobyspark 11:853a9e887023 51 if ( !strcmp( recMes.getAddress(1) , "test1" ) )
tobyspark 11:853a9e887023 52 printf("Received subAddress= test1 \r\n");
tobyspark 11:853a9e887023 53
tobyspark 11:853a9e887023 54 // Send some osc message:
tobyspark 11:853a9e887023 55 sendMes.setTopAddress("/working...");
tobyspark 11:853a9e887023 56 osc.sendOsc(&sendMes);
tobyspark 0:49cdaebd52d5 57 }
tobyspark 0:49cdaebd52d5 58 }
tobyspark 0:49cdaebd52d5 59
tobyspark 11:853a9e887023 60 //// M A I N
tobyspark 11:853a9e887023 61 int main() {
tobyspark 11:853a9e887023 62
tobyspark 11:853a9e887023 63 //// TASK: Set up the Ethernet port
tobyspark 11:853a9e887023 64 printf("Setting up ethernet...\r\n");
tobyspark 11:853a9e887023 65 EthernetErr ethErr = eth.setup();
tobyspark 11:853a9e887023 66 if (ethErr) {
tobyspark 11:853a9e887023 67 printf("Ethernet Failed to setup. Error: %d\r\n", ethErr);
tobyspark 11:853a9e887023 68 return -1;
tobyspark 11:853a9e887023 69 }
tobyspark 11:853a9e887023 70 printf("Ethernet OK\r\n");
tobyspark 11:853a9e887023 71
tobyspark 11:853a9e887023 72 //// TASK: Set up OSC message sending
tobyspark 11:853a9e887023 73
tobyspark 11:853a9e887023 74 // In the OSC message container we've made for send messages, set where we want it to go:
tobyspark 11:853a9e887023 75 sendMes.setIp( destIp );
tobyspark 11:853a9e887023 76 sendMes.setPort( destPort );
tobyspark 11:853a9e887023 77
tobyspark 11:853a9e887023 78 //// TASK: Set up OSC message receiving
tobyspark 11:853a9e887023 79
tobyspark 11:853a9e887023 80 // In the OSC send/receive object...
tobyspark 11:853a9e887023 81 // Set the OSC message container for it to parse received messages into
tobyspark 11:853a9e887023 82 osc.setReceiveMessage(&recMes);
tobyspark 11:853a9e887023 83
tobyspark 11:853a9e887023 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 11:853a9e887023 85 osc.begin(mbedListenPort);
tobyspark 0:49cdaebd52d5 86
tobyspark 11:853a9e887023 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 11:853a9e887023 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 11:853a9e887023 89 // For more info how this works, see http://mbed.org/cookbook/FunctionPointer
tobyspark 11:853a9e887023 90 osc.messageReceivedCallback.attach(&processOSC);
tobyspark 11:853a9e887023 91
tobyspark 11:853a9e887023 92 //// TASK: Prime button change detection
tobyspark 11:853a9e887023 93 buttonLastState = button;
tobyspark 11:853a9e887023 94
tobyspark 11:853a9e887023 95 //// TASK: GO!
tobyspark 11:853a9e887023 96
tobyspark 11:853a9e887023 97 // We've finished setting up, now loop this forever...
tobyspark 11:853a9e887023 98 while (true) {
tobyspark 11:853a9e887023 99 // This polls the network connection for new activity, without keeping on calling this you won't receive any OSC!
tobyspark 11:853a9e887023 100 Net::poll();
tobyspark 11:853a9e887023 101
tobyspark 11:853a9e887023 102 // Has the button changed?
tobyspark 11:853a9e887023 103 if (button != buttonLastState) {
tobyspark 11:853a9e887023 104 // If so, lets update the lastState variable and then send an OSC message
tobyspark 11:853a9e887023 105 buttonLastState = button;
tobyspark 11:853a9e887023 106
tobyspark 11:853a9e887023 107 sendMes.setTopAddress("/mbed");
tobyspark 11:853a9e887023 108 sendMes.setSubAddress("/button");
tobyspark 11:853a9e887023 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 11:853a9e887023 110 osc.sendOsc(&sendMes);
tobyspark 11:853a9e887023 111
tobyspark 11:853a9e887023 112 printf("Sent OSC message /mbed/button \r\n");
tobyspark 0:49cdaebd52d5 113 }
tobyspark 11:853a9e887023 114
tobyspark 11:853a9e887023 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 11:853a9e887023 116 }
tobyspark 0:49cdaebd52d5 117 }
tobyspark 0:49cdaebd52d5 118
tobyspark 0:49cdaebd52d5 119 /* EXAMPLE SEND/RECEIVE on PROCESSING:
tobyspark 0:49cdaebd52d5 120
tobyspark 0:49cdaebd52d5 121 // oscP5sendreceive by andreas schlegel
tobyspark 0:49cdaebd52d5 122 // example shows how to send and receive osc messages.
tobyspark 0:49cdaebd52d5 123 // oscP5 website at http://www.sojamo.de/oscP5
tobyspark 0:49cdaebd52d5 124
tobyspark 11:853a9e887023 125
tobyspark 0:49cdaebd52d5 126 import oscP5.*;
tobyspark 0:49cdaebd52d5 127 import netP5.*;
tobyspark 11:853a9e887023 128
tobyspark 0:49cdaebd52d5 129 OscP5 oscP5;
tobyspark 0:49cdaebd52d5 130 NetAddress myRemoteLocation;
tobyspark 0:49cdaebd52d5 131
tobyspark 0:49cdaebd52d5 132 void setup() {
tobyspark 0:49cdaebd52d5 133 size(400,400);
tobyspark 0:49cdaebd52d5 134 frameRate(25);
tobyspark 11:853a9e887023 135 // start oscP5, listening for incoming messages at port 12000
tobyspark 0:49cdaebd52d5 136 oscP5 = new OscP5(this,12000);
tobyspark 11:853a9e887023 137
tobyspark 0:49cdaebd52d5 138 // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
tobyspark 0:49cdaebd52d5 139 // an ip address and a port number. myRemoteLocation is used as parameter in
tobyspark 11:853a9e887023 140 // oscP5.send() when sending osc packets to another computer, device,
tobyspark 0:49cdaebd52d5 141 // application. usage see below. for testing purposes the listening port
tobyspark 0:49cdaebd52d5 142 // and the port of the remote location address are the same, hence you will
tobyspark 0:49cdaebd52d5 143 // send messages back to this sketch.
tobyspark 0:49cdaebd52d5 144 myRemoteLocation = new NetAddress("10.0.0.2",10000);
tobyspark 0:49cdaebd52d5 145 }
tobyspark 0:49cdaebd52d5 146
tobyspark 0:49cdaebd52d5 147
tobyspark 0:49cdaebd52d5 148 void draw() {
tobyspark 11:853a9e887023 149 background(0);
tobyspark 0:49cdaebd52d5 150 }
tobyspark 0:49cdaebd52d5 151
tobyspark 0:49cdaebd52d5 152 void mousePressed() {
tobyspark 11:853a9e887023 153 // in the following different ways of creating osc messages are shown by example
tobyspark 0:49cdaebd52d5 154 OscMessage myMessage = new OscMessage("/mbed/test1");
tobyspark 11:853a9e887023 155
tobyspark 11:853a9e887023 156 myMessage.add(123); // add an int to the osc message
tobyspark 0:49cdaebd52d5 157
tobyspark 11:853a9e887023 158 // send the message
tobyspark 11:853a9e887023 159 oscP5.send(myMessage, myRemoteLocation);
tobyspark 0:49cdaebd52d5 160 }
tobyspark 0:49cdaebd52d5 161
tobyspark 0:49cdaebd52d5 162
tobyspark 11:853a9e887023 163 // incoming osc message are forwarded to the oscEvent method.
tobyspark 0:49cdaebd52d5 164 void oscEvent(OscMessage theOscMessage) {
tobyspark 11:853a9e887023 165 // print the address pattern and the typetag of the received OscMessage
tobyspark 0:49cdaebd52d5 166 print("### received an osc message.");
tobyspark 0:49cdaebd52d5 167 print(" addrpattern: "+theOscMessage.addrPattern());
tobyspark 0:49cdaebd52d5 168 println(" typetag: "+theOscMessage.typetag());
tobyspark 0:49cdaebd52d5 169 }
tobyspark 0:49cdaebd52d5 170
tobyspark 0:49cdaebd52d5 171 */