osc test

Dependencies:   EthernetNetIf mbed mbed

Fork of OSC by Toby Harris

Committer:
itotaka
Date:
Fri Oct 04 12:19:39 2013 +0000
Revision:
2:b5af09b0d540
to share

Who changed what in which revision?

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