simple tester example >>> osc destination & receive

Dependencies:   mbed

Fork of myOSC_test by Alvaro Cassinelli

Committer:
sfjmt
Date:
Tue Aug 06 10:12:56 2013 +0000
Revision:
2:44f1e5803762
Parent:
0:f76708a93fb3
revision_01

Who changed what in which revision?

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