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:
Tue Mar 13 22:42:25 2012 +0000
Revision:
0:49cdaebd52d5
Child:
1:ab7dc9550de6
Changes:
- udpRec.setOnEvent is now properly encapsulated within the class
- the message received callback is now a public property using mbed\s FunctionPointer class
- swapped out the EthernetNetIf source for the NetService library, which despite the name is an updated version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tobyspark 0:49cdaebd52d5 1 /* mbedOSC.h
tobyspark 0:49cdaebd52d5 2 This is an OSC library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the
tobyspark 0:49cdaebd52d5 3 Arduino with Ethernet shield. I have also used parts of the OSC Transceiver(Sender/Receiver) code by xshige
tobyspark 0:49cdaebd52d5 4 written by: Alvaro Cassinelli, 7.10.2011
tobyspark 0:49cdaebd52d5 5 tweaked by: Toby Harris / *spark audio-visual, 13.4.2012
tobyspark 0:49cdaebd52d5 6
tobyspark 0:49cdaebd52d5 7 This library is free software; you can redistribute it and/or
tobyspark 0:49cdaebd52d5 8 modify it under the terms of the GNU Lesser General Public
tobyspark 0:49cdaebd52d5 9 License version 2.1 as published by the Free Software Foundation.
tobyspark 0:49cdaebd52d5 10 Open Sound Control http://opensoundcontrol.org/
tobyspark 0:49cdaebd52d5 11
tobyspark 0:49cdaebd52d5 12 mbedOSC version 0.1 Specification (similar to Recotana's OSCClass library)
tobyspark 0:49cdaebd52d5 13
tobyspark 0:49cdaebd52d5 14 ********
tobyspark 0:49cdaebd52d5 15 Address : max 2
tobyspark 0:49cdaebd52d5 16 "/ard"
tobyspark 0:49cdaebd52d5 17 "/ard/output" --address[0]="/ard" :max 15character
tobyspark 0:49cdaebd52d5 18 --address[1]="/output" :max 15character
tobyspark 0:49cdaebd52d5 19
tobyspark 0:49cdaebd52d5 20 *******
tobyspark 0:49cdaebd52d5 21 TypeTag : max 2
tobyspark 0:49cdaebd52d5 22
tobyspark 0:49cdaebd52d5 23 "i" - long or unsigned long
tobyspark 0:49cdaebd52d5 24 "f" - double
tobyspark 0:49cdaebd52d5 25
tobyspark 0:49cdaebd52d5 26 *******
tobyspark 0:49cdaebd52d5 27 arg : max 2
tobyspark 0:49cdaebd52d5 28
tobyspark 0:49cdaebd52d5 29 *******
tobyspark 0:49cdaebd52d5 30 Example of an OSC message: "/mbed/test1, if 50 32.4" (Note: this is not the byte string
tobyspark 0:49cdaebd52d5 31 sent as UDP packet - there are no spaces, and arguments are in binary, BIG ENDIAN)
tobyspark 0:49cdaebd52d5 32 */
tobyspark 0:49cdaebd52d5 33
tobyspark 0:49cdaebd52d5 34 #ifndef mbedOSC_h
tobyspark 0:49cdaebd52d5 35 #define mbedOSC_h
tobyspark 0:49cdaebd52d5 36
tobyspark 0:49cdaebd52d5 37 #include "mbed.h"
tobyspark 0:49cdaebd52d5 38 #include "EthernetNetIf.h"
tobyspark 0:49cdaebd52d5 39 #include "UDPSocket.h"
tobyspark 0:49cdaebd52d5 40
tobyspark 0:49cdaebd52d5 41 // setup IP of destination (computer):
tobyspark 0:49cdaebd52d5 42 #define DEFAULT_SEND_PORT 12000
tobyspark 0:49cdaebd52d5 43 //Host sendHost(IpAddr(10, 0, 0, 1), DEFAULT_SEND_PORT, NULL); // Send Port
tobyspark 0:49cdaebd52d5 44 // set IP of origin of UDP packets - the mbed acts as a SERVER here, and needs to bind the socket to the "client" (the computer)
tobyspark 0:49cdaebd52d5 45 #define DEFAULT_RECEIVE_PORT 57130
tobyspark 0:49cdaebd52d5 46 //Host recHost(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL); // Receive Port
tobyspark 0:49cdaebd52d5 47 //UDPSocket udpRec,udpSend;
tobyspark 0:49cdaebd52d5 48
tobyspark 0:49cdaebd52d5 49
tobyspark 0:49cdaebd52d5 50 #define MAX_ADDRESS 2
tobyspark 0:49cdaebd52d5 51 #define MAX_ARG 2
tobyspark 0:49cdaebd52d5 52
tobyspark 0:49cdaebd52d5 53 #define TYPE_INT 1
tobyspark 0:49cdaebd52d5 54 #define TYPE_FLOAT 2
tobyspark 0:49cdaebd52d5 55
tobyspark 0:49cdaebd52d5 56
tobyspark 0:49cdaebd52d5 57 /*
tobyspark 0:49cdaebd52d5 58 Container class for OSC messages (receiving or sending)
tobyspark 0:49cdaebd52d5 59 */
tobyspark 0:49cdaebd52d5 60 class OSCMessage{
tobyspark 0:49cdaebd52d5 61
tobyspark 0:49cdaebd52d5 62 private:
tobyspark 0:49cdaebd52d5 63
tobyspark 0:49cdaebd52d5 64 char *address[MAX_ADDRESS]; // these are strings (as char*)
tobyspark 0:49cdaebd52d5 65 uint8_t addressNum; // current number of addresses in the message (ex: "/ard/test" --> the number of the addresses is 2)
tobyspark 0:49cdaebd52d5 66
tobyspark 0:49cdaebd52d5 67 char typeTag[MAX_ARG];
tobyspark 0:49cdaebd52d5 68
tobyspark 0:49cdaebd52d5 69 void *arg[MAX_ARG];
tobyspark 0:49cdaebd52d5 70 uint8_t argNum;
tobyspark 0:49cdaebd52d5 71
tobyspark 0:49cdaebd52d5 72 // Information about the connection:
tobyspark 0:49cdaebd52d5 73 //uint8_t ip[4];
tobyspark 0:49cdaebd52d5 74 //uint16_t port;
tobyspark 0:49cdaebd52d5 75 Host host;
tobyspark 0:49cdaebd52d5 76
tobyspark 0:49cdaebd52d5 77 public:
tobyspark 0:49cdaebd52d5 78
tobyspark 0:49cdaebd52d5 79 OSCMessage();
tobyspark 0:49cdaebd52d5 80
tobyspark 0:49cdaebd52d5 81 const IpAddr& getIp(); // return IpAddr object
tobyspark 0:49cdaebd52d5 82 const int& getPort(); // return port
tobyspark 0:49cdaebd52d5 83
tobyspark 0:49cdaebd52d5 84 //ex. address patern "/adr/test"
tobyspark 0:49cdaebd52d5 85 // address[2]={"/ard" , "/test"}
tobyspark 0:49cdaebd52d5 86 char *getAddress(uint8_t _index); //retturn address
tobyspark 0:49cdaebd52d5 87 char *getTopAddress(); //return address[0] :"/ard"
tobyspark 0:49cdaebd52d5 88 char *getSubAddress(); //return address[1] :"/test"
tobyspark 0:49cdaebd52d5 89 uint8_t getAddressNum(); //return 2
tobyspark 0:49cdaebd52d5 90
tobyspark 0:49cdaebd52d5 91 // 'i': long(int32_t)
tobyspark 0:49cdaebd52d5 92 // 'f': double
tobyspark 0:49cdaebd52d5 93 //ex 'if' 123 54.21
tobyspark 0:49cdaebd52d5 94 char getTypeTag(uint8_t _index); //_index=0 ->'i'
tobyspark 0:49cdaebd52d5 95 //_index=1 ->'f'
tobyspark 0:49cdaebd52d5 96
tobyspark 0:49cdaebd52d5 97 uint8_t getArgNum(); //return 2
tobyspark 0:49cdaebd52d5 98
tobyspark 0:49cdaebd52d5 99 int32_t getArgInt(uint8_t _index); //_index=0 -> 123
tobyspark 0:49cdaebd52d5 100 double getArgFloat(uint8_t _index); //_index=1 -> 54.21
tobyspark 0:49cdaebd52d5 101
tobyspark 0:49cdaebd52d5 102
tobyspark 0:49cdaebd52d5 103 void setTopAddress(char *_address); //set address[0]
tobyspark 0:49cdaebd52d5 104 void setSubAddress(char *_address); //set address[1]
tobyspark 0:49cdaebd52d5 105 void setAddress(char *_topAddress,
tobyspark 0:49cdaebd52d5 106 char *_subAddress);
tobyspark 0:49cdaebd52d5 107 void setAddress(uint8_t _index, //set 0,address[0]
tobyspark 0:49cdaebd52d5 108 char *_address);
tobyspark 0:49cdaebd52d5 109 //set 1,address[1]
tobyspark 0:49cdaebd52d5 110
tobyspark 0:49cdaebd52d5 111 void setIp( uint8_t *_ip ); //set ip
tobyspark 0:49cdaebd52d5 112
tobyspark 0:49cdaebd52d5 113 void setIp(uint8_t _ip1, //set(192,
tobyspark 0:49cdaebd52d5 114 uint8_t _ip2, // 168,
tobyspark 0:49cdaebd52d5 115 uint8_t _ip3, // 0,
tobyspark 0:49cdaebd52d5 116 uint8_t _ip4); // 100)
tobyspark 0:49cdaebd52d5 117
tobyspark 0:49cdaebd52d5 118 void setPort( uint16_t _port );
tobyspark 0:49cdaebd52d5 119
tobyspark 0:49cdaebd52d5 120 //ex. long v1=100
tobyspark 0:49cdaebd52d5 121 // double v2=123.21
tobyspark 0:49cdaebd52d5 122 void setArgs( char *types , ... ); //set ("if",&v1,&v2)
tobyspark 0:49cdaebd52d5 123
tobyspark 0:49cdaebd52d5 124 friend class OSCClass;
tobyspark 0:49cdaebd52d5 125
tobyspark 0:49cdaebd52d5 126 };
tobyspark 0:49cdaebd52d5 127
tobyspark 0:49cdaebd52d5 128
tobyspark 0:49cdaebd52d5 129
tobyspark 0:49cdaebd52d5 130 /* ==================================== OSCClass for sending and receiving OSC messages using UDP protocol ===================================== */
tobyspark 0:49cdaebd52d5 131
tobyspark 0:49cdaebd52d5 132 #include "UDPSocket.h"
tobyspark 0:49cdaebd52d5 133
tobyspark 0:49cdaebd52d5 134 class OSCClass {
tobyspark 0:49cdaebd52d5 135
tobyspark 0:49cdaebd52d5 136 private:
tobyspark 0:49cdaebd52d5 137
tobyspark 0:49cdaebd52d5 138 UDPSocket udpRec,udpSend;
tobyspark 0:49cdaebd52d5 139 char rcvBuff[256]; // raw buffer for UDP packets (udpRec.recvfrom( buf, 256, &host ) ))
tobyspark 0:49cdaebd52d5 140 int buflength;
tobyspark 0:49cdaebd52d5 141
tobyspark 0:49cdaebd52d5 142 OSCMessage *receiverMessage;
tobyspark 0:49cdaebd52d5 143 OSCMessage *sendContainer;
tobyspark 0:49cdaebd52d5 144
tobyspark 0:49cdaebd52d5 145 char tempAddress[MAX_ADDRESS][16];
tobyspark 0:49cdaebd52d5 146 uint8_t tempArg[MAX_ARG][4];
tobyspark 0:49cdaebd52d5 147
tobyspark 0:49cdaebd52d5 148 void decodePacket( OSCMessage *_mes); // makes OSC message from packet
tobyspark 0:49cdaebd52d5 149
tobyspark 0:49cdaebd52d5 150 public:
tobyspark 0:49cdaebd52d5 151
tobyspark 0:49cdaebd52d5 152 OSCClass();
tobyspark 0:49cdaebd52d5 153 OSCClass(OSCMessage *_mes); // set the receiver message container
tobyspark 0:49cdaebd52d5 154 void onUDPSocketEvent(UDPSocketEvent e);
tobyspark 0:49cdaebd52d5 155
tobyspark 0:49cdaebd52d5 156 //init osc
tobyspark 0:49cdaebd52d5 157 void begin();
tobyspark 0:49cdaebd52d5 158 void begin(uint16_t _recievePort);
tobyspark 0:49cdaebd52d5 159 void stop();
tobyspark 0:49cdaebd52d5 160
tobyspark 0:49cdaebd52d5 161 //new OSC data in the receiver message container:
tobyspark 0:49cdaebd52d5 162 bool newMessage;
tobyspark 0:49cdaebd52d5 163
tobyspark 0:49cdaebd52d5 164 void setReceiveMessage( OSCMessage *_mes ); //set receive OSCmessage container (note: the message has a "host" object from which we get the upd packets)
tobyspark 0:49cdaebd52d5 165 OSCMessage *getMessage(); //return received OSCmessage
tobyspark 0:49cdaebd52d5 166
tobyspark 0:49cdaebd52d5 167 //buffer clear
tobyspark 0:49cdaebd52d5 168 //void flush();
tobyspark 0:49cdaebd52d5 169
tobyspark 0:49cdaebd52d5 170 //OSC send
tobyspark 0:49cdaebd52d5 171 void sendOsc( OSCMessage *_mes ); //set&send OSCmessage (note: it will be sent to the host defined in the message container)
tobyspark 0:49cdaebd52d5 172
tobyspark 0:49cdaebd52d5 173 //to be set by host program, will be called on receipt of an OSC message
tobyspark 0:49cdaebd52d5 174 FunctionPointer messageReceivedCallback;
tobyspark 0:49cdaebd52d5 175 };
tobyspark 0:49cdaebd52d5 176
tobyspark 0:49cdaebd52d5 177 #endif