support OSC-string
Fork of OSC by
mbedOSC.h@5:5d585d5107da, 2016-02-16 (annotated)
- Committer:
- casiotone401
- Date:
- Tue Feb 16 11:30:55 2016 +0000
- Revision:
- 5:5d585d5107da
- Parent:
- 4:601f6a1141fb
- Child:
- 6:a47004fb44f5
bug fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tobyspark | 0:fdea65150534 | 1 | /* mbed OSC Library |
tobyspark | 0:fdea65150534 | 2 | This is an Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the |
tobyspark | 0:fdea65150534 | 3 | Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige |
tobyspark | 0:fdea65150534 | 4 | written by: Alvaro Cassinelli, October 2011 |
tobyspark | 0:fdea65150534 | 5 | tweaked by: Toby Harris / *spark audio-visual, March 2012 |
casiotone401 | 4:601f6a1141fb | 6 | |
tobyspark | 0:fdea65150534 | 7 | This library is free software; you can redistribute it and/or |
tobyspark | 0:fdea65150534 | 8 | modify it under the terms of the GNU Lesser General Public |
tobyspark | 0:fdea65150534 | 9 | License version 2.1 as published by the Free Software Foundation. |
tobyspark | 0:fdea65150534 | 10 | Open Sound Control http://opensoundcontrol.org/ |
tobyspark | 0:fdea65150534 | 11 | */ |
casiotone401 | 4:601f6a1141fb | 12 | |
casiotone401 | 2:507dea4cc97a | 13 | #pragma O3 |
casiotone401 | 2:507dea4cc97a | 14 | #pragma Otime |
casiotone401 | 4:601f6a1141fb | 15 | |
tobyspark | 0:fdea65150534 | 16 | #ifndef mbedOSC_h |
tobyspark | 0:fdea65150534 | 17 | #define mbedOSC_h |
casiotone401 | 4:601f6a1141fb | 18 | |
tobyspark | 0:fdea65150534 | 19 | #include "mbed.h" |
tobyspark | 0:fdea65150534 | 20 | #include "EthernetNetIf.h" |
tobyspark | 0:fdea65150534 | 21 | #include "UDPSocket.h" |
casiotone401 | 4:601f6a1141fb | 22 | |
tobyspark | 0:fdea65150534 | 23 | // setup IP of destination (computer): |
tobyspark | 0:fdea65150534 | 24 | #define DEFAULT_SEND_PORT 12000 |
tobyspark | 0:fdea65150534 | 25 | //Host sendHost(IpAddr(10, 0, 0, 1), DEFAULT_SEND_PORT, NULL); // Send Port |
tobyspark | 0:fdea65150534 | 26 | // 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:fdea65150534 | 27 | #define DEFAULT_RECEIVE_PORT 57130 |
tobyspark | 0:fdea65150534 | 28 | //Host recHost(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL); // Receive Port |
tobyspark | 0:fdea65150534 | 29 | //UDPSocket udpRec,udpSend; |
casiotone401 | 4:601f6a1141fb | 30 | |
casiotone401 | 4:601f6a1141fb | 31 | |
tobyspark | 0:fdea65150534 | 32 | #define MAX_ADDRESS 2 |
casiotone401 | 2:507dea4cc97a | 33 | #define MAX_ARG 4 |
casiotone401 | 4:601f6a1141fb | 34 | |
tobyspark | 0:fdea65150534 | 35 | #define TYPE_INT 1 |
casiotone401 | 2:507dea4cc97a | 36 | #define TYPE_FLOAT 2 |
casiotone401 | 4:601f6a1141fb | 37 | |
casiotone401 | 4:601f6a1141fb | 38 | |
tobyspark | 0:fdea65150534 | 39 | /** Container class for OSC messages (receiving or sending) |
tobyspark | 0:fdea65150534 | 40 | @note mbedOSC version 0.1 Specification (similar to Recotana's OSCClass library) |
tobyspark | 0:fdea65150534 | 41 | Example of an OSC message: "/mbed/test1, if 50 32.4" |
tobyspark | 0:fdea65150534 | 42 | ie. "Address TypeTag Args" |
tobyspark | 0:fdea65150534 | 43 | Address : max 2 |
tobyspark | 0:fdea65150534 | 44 | "/ard" |
tobyspark | 0:fdea65150534 | 45 | "/ard/output" |
tobyspark | 0:fdea65150534 | 46 | --address[0]="/ard" :max 15character |
tobyspark | 0:fdea65150534 | 47 | --address[1]="/output" :max 15character |
tobyspark | 0:fdea65150534 | 48 | TypeTag : max 2 |
tobyspark | 0:fdea65150534 | 49 | "i" - long or unsigned long |
tobyspark | 0:fdea65150534 | 50 | "f" - double |
tobyspark | 0:fdea65150534 | 51 | arg : max 2 |
tobyspark | 0:fdea65150534 | 52 | (Note: The byte string as seen here is not sent as UDP packet directly - there are no spaces, and arguments are in binary, BIG ENDIAN) |
tobyspark | 0:fdea65150534 | 53 | */ |
tobyspark | 0:fdea65150534 | 54 | class OSCMessage{ |
tobyspark | 0:fdea65150534 | 55 | |
tobyspark | 0:fdea65150534 | 56 | private: |
tobyspark | 0:fdea65150534 | 57 | |
tobyspark | 0:fdea65150534 | 58 | char *address[MAX_ADDRESS]; // these are strings (as char*) |
casiotone401 | 2:507dea4cc97a | 59 | uint8_t addressNum; // current number of addresses in the message (ex: "/ard/test" --> the number of the addresses is 2) |
tobyspark | 0:fdea65150534 | 60 | |
casiotone401 | 2:507dea4cc97a | 61 | char typeTag[MAX_ARG]; |
tobyspark | 0:fdea65150534 | 62 | void *arg[MAX_ARG]; |
casiotone401 | 5:5d585d5107da | 63 | |
casiotone401 | 5:5d585d5107da | 64 | typedef union |
casiotone401 | 5:5d585d5107da | 65 | { |
casiotone401 | 5:5d585d5107da | 66 | char * s; |
casiotone401 | 5:5d585d5107da | 67 | int i; |
casiotone401 | 5:5d585d5107da | 68 | float f; |
casiotone401 | 5:5d585d5107da | 69 | double d; |
casiotone401 | 5:5d585d5107da | 70 | |
casiotone401 | 5:5d585d5107da | 71 | } OSCArg; |
casiotone401 | 5:5d585d5107da | 72 | |
casiotone401 | 5:5d585d5107da | 73 | OSCArg args[MAX_ARG]; |
casiotone401 | 5:5d585d5107da | 74 | |
casiotone401 | 2:507dea4cc97a | 75 | uint8_t argNum; |
tobyspark | 0:fdea65150534 | 76 | |
tobyspark | 0:fdea65150534 | 77 | // Information about the connection: |
casiotone401 | 5:5d585d5107da | 78 | //uint8_t ip[4]; |
tobyspark | 0:fdea65150534 | 79 | //uint16_t port; |
tobyspark | 0:fdea65150534 | 80 | Host host; |
tobyspark | 0:fdea65150534 | 81 | |
tobyspark | 0:fdea65150534 | 82 | public: |
tobyspark | 0:fdea65150534 | 83 | /** Create a container for an OSC message to be received or sent */ |
tobyspark | 0:fdea65150534 | 84 | OSCMessage(); |
casiotone401 | 4:601f6a1141fb | 85 | |
tobyspark | 0:fdea65150534 | 86 | /** Return the IpAddr object */ |
casiotone401 | 2:507dea4cc97a | 87 | const IpAddr& getIp(); |
tobyspark | 0:fdea65150534 | 88 | /** Return the port */ |
tobyspark | 0:fdea65150534 | 89 | const int& getPort(); |
tobyspark | 0:fdea65150534 | 90 | |
tobyspark | 0:fdea65150534 | 91 | /** Gets the address string of the OSC message |
tobyspark | 0:fdea65150534 | 92 | * |
tobyspark | 0:fdea65150534 | 93 | * @param[in] _index The index of the address string (byte) |
tobyspark | 0:fdea65150534 | 94 | * @return pointer of the address string (char *) |
tobyspark | 0:fdea65150534 | 95 | * @note ex. "/ard/test"<br> |
tobyspark | 0:fdea65150534 | 96 | * getAddress(0) = "/ard"<br> |
tobyspark | 0:fdea65150534 | 97 | * getAddress(1) = "/test" |
tobyspark | 0:fdea65150534 | 98 | * @attention It is maximum number of the addresses is 2<br> |
tobyspark | 0:fdea65150534 | 99 | * In this case "/ard/test1/test2"<br> |
tobyspark | 0:fdea65150534 | 100 | * ignore it after "/test2" |
tobyspark | 0:fdea65150534 | 101 | */ |
tobyspark | 0:fdea65150534 | 102 | char *getAddress(uint8_t _index); //retturn address |
tobyspark | 0:fdea65150534 | 103 | |
tobyspark | 0:fdea65150534 | 104 | /** Gets the TopAddress string of the OSC message (this is just the address with index 0) |
tobyspark | 0:fdea65150534 | 105 | @return pointer of the TopAddress string (char *), i.e. address[0] |
tobyspark | 0:fdea65150534 | 106 | Example: In the case "/ard/test", getTopAddress() = "/ard" (WITH the slash "/") |
tobyspark | 0:fdea65150534 | 107 | */ |
tobyspark | 0:fdea65150534 | 108 | char *getTopAddress(); //return address[0] :"/ard" |
casiotone401 | 4:601f6a1141fb | 109 | |
tobyspark | 0:fdea65150534 | 110 | /** |
tobyspark | 0:fdea65150534 | 111 | Gets the "SubAddress" string of the OSC message (this is just the address with index 1) |
tobyspark | 0:fdea65150534 | 112 | @return pointer of the SubAddress string (char *), i.e. address[1] |
tobyspark | 0:fdea65150534 | 113 | Example: in the case "/ard/test", getSubAddress() = "/test" (WITH the slash "/") |
tobyspark | 0:fdea65150534 | 114 | */ |
tobyspark | 0:fdea65150534 | 115 | char *getSubAddress(); //return address[1] :"/test" |
casiotone401 | 4:601f6a1141fb | 116 | |
tobyspark | 0:fdea65150534 | 117 | /** |
tobyspark | 0:fdea65150534 | 118 | Gets the number of the OSC message address |
tobyspark | 0:fdea65150534 | 119 | @return number of the OSC message address (byte) |
tobyspark | 0:fdea65150534 | 120 | Examples: "/ard" --> the number of the addresses is 1 |
tobyspark | 0:fdea65150534 | 121 | "/ard/test" --> the number of the addresses is 2 |
tobyspark | 0:fdea65150534 | 122 | Attention: the maximum number of addresses is 2 (MAX_ADDRESS) |
tobyspark | 0:fdea65150534 | 123 | */ |
casiotone401 | 2:507dea4cc97a | 124 | uint8_t getAddressNum(); //return 2 |
tobyspark | 0:fdea65150534 | 125 | |
tobyspark | 0:fdea65150534 | 126 | /** |
tobyspark | 0:fdea65150534 | 127 | Gets the TypeTag string (with index) of the OSC message |
tobyspark | 0:fdea65150534 | 128 | @param[in] _index The index of the TypeTag string (byte) |
tobyspark | 0:fdea65150534 | 129 | @return: TypeTag char (char) |
tobyspark | 0:fdea65150534 | 130 | Example: in the case of a total typetag string equal to "if", getTypeTag(0) = 'i' and getTypeTag(1) = 'f' |
tobyspark | 0:fdea65150534 | 131 | Attention: MAX_ARG is maximum number of the args, if the index argument is larger, it will be constrained to this max. |
tobyspark | 0:fdea65150534 | 132 | */ |
tobyspark | 0:fdea65150534 | 133 | char getTypeTag(uint8_t _index); //_index=0 ->'i' |
tobyspark | 0:fdea65150534 | 134 | //_index=1 ->'f' |
casiotone401 | 4:601f6a1141fb | 135 | |
tobyspark | 0:fdea65150534 | 136 | /** |
tobyspark | 0:fdea65150534 | 137 | Gets the number of the OSC message args |
tobyspark | 0:fdea65150534 | 138 | @return number of the args (byte) |
tobyspark | 0:fdea65150534 | 139 | Example: "i" 123 --> number of the OSC message args is 1 |
tobyspark | 0:fdea65150534 | 140 | "if" 123 54.24 --> number of the OSC message args is 2 |
tobyspark | 0:fdea65150534 | 141 | Attention: the maximum number of args is 2 (MAX_ARG) |
tobyspark | 0:fdea65150534 | 142 | */ |
casiotone401 | 3:f4118f0bc1ab | 143 | uint8_t getArgNum(); //return 2 |
tobyspark | 0:fdea65150534 | 144 | |
tobyspark | 0:fdea65150534 | 145 | /** |
tobyspark | 0:fdea65150534 | 146 | Get the args of the OSC message with an integer value |
tobyspark | 0:fdea65150534 | 147 | @param[in] _index An int or uint8_t corresponding to the index of the args (byte) |
casiotone401 | 2:507dea4cc97a | 148 | @return: integer value (long, or int16_t) |
tobyspark | 0:fdea65150534 | 149 | Example: in the case "if" 123 54.24, getArgInt(0) = 123 |
tobyspark | 0:fdea65150534 | 150 | Noe: "i" is integer, but the return type is "long" |
tobyspark | 0:fdea65150534 | 151 | Note: When a index is bigger than the number of the args, it is set to the number of the args |
tobyspark | 0:fdea65150534 | 152 | */ |
casiotone401 | 3:f4118f0bc1ab | 153 | int getArgInt(uint8_t _index); //_index=0 -> 123 |
casiotone401 | 4:601f6a1141fb | 154 | |
tobyspark | 0:fdea65150534 | 155 | /** |
tobyspark | 0:fdea65150534 | 156 | Get the args of the OSC message with a float value |
tobyspark | 0:fdea65150534 | 157 | @param[in] _index The index of the args |
tobyspark | 0:fdea65150534 | 158 | @return: float value (double) |
tobyspark | 0:fdea65150534 | 159 | note: In this case "if" 123 54.24, getArgFloat(1) = 54.24 |
tobyspark | 0:fdea65150534 | 160 | attention: arg declared as float, but return value cast as "double" |
tobyspark | 0:fdea65150534 | 161 | attention: When index is bigger than the number of the args, it is set to the number of the args |
tobyspark | 0:fdea65150534 | 162 | */ |
casiotone401 | 4:601f6a1141fb | 163 | float getArgFloat(uint8_t _index); //_index=1 -> 54.21 |
tobyspark | 0:fdea65150534 | 164 | |
tobyspark | 0:fdea65150534 | 165 | |
tobyspark | 0:fdea65150534 | 166 | /** |
tobyspark | 0:fdea65150534 | 167 | Set TopAddress string of OSC Message |
tobyspark | 0:fdea65150534 | 168 | @param[in] _address A string pointer for the TopAddress String (char *). NOTE: is this a good idea? why not pass as const, and do allocation here? |
tobyspark | 0:fdea65150534 | 169 | Example: if the complete address string is "/ard/test", we set the topaddress as follows: char top[]="/ard" (allocation done here!), then setTopAddress(top) |
tobyspark | 0:fdea65150534 | 170 | */ |
tobyspark | 0:fdea65150534 | 171 | void setTopAddress(char *_address); //set address[0] |
casiotone401 | 4:601f6a1141fb | 172 | |
tobyspark | 0:fdea65150534 | 173 | /** |
tobyspark | 0:fdea65150534 | 174 | Set SubAddress string of the OSC Message |
tobyspark | 0:fdea65150534 | 175 | @param[in] _address A string pointer for the SubAddress String (char *) |
tobyspark | 0:fdea65150534 | 176 | Example: if the complete address string is "/ard/test", we set the subaddress as follows: char sub[]="/test" (allocation done here!), then setSubAddress(sub) |
tobyspark | 0:fdea65150534 | 177 | Attention: we should call first setTopAddress, and then setSubAddress. The order is important. This does not seems like a good idea... |
tobyspark | 0:fdea65150534 | 178 | */ |
tobyspark | 0:fdea65150534 | 179 | void setSubAddress(char *_address); //set address[1] |
casiotone401 | 4:601f6a1141fb | 180 | |
tobyspark | 0:fdea65150534 | 181 | /** |
tobyspark | 0:fdea65150534 | 182 | Set the complete Address string of the OSC Message (top and sub addresses) |
tobyspark | 0:fdea65150534 | 183 | @param[in] _topAddress, _subAddress The string pointers to top and sub addresses (char *) |
tobyspark | 0:fdea65150534 | 184 | Example: in the case "/ard/test", we need to do: char top[]="/ard", char sub[]="/test", and then setAddress(top,sub) |
tobyspark | 0:fdea65150534 | 185 | Reminder: in this implementation, the maximum number of addresses is MAX_ADDRESS=2 |
tobyspark | 0:fdea65150534 | 186 | */ |
tobyspark | 0:fdea65150534 | 187 | void setAddress(char *_topAddress, |
tobyspark | 0:fdea65150534 | 188 | char *_subAddress); |
casiotone401 | 4:601f6a1141fb | 189 | |
tobyspark | 0:fdea65150534 | 190 | /** |
tobyspark | 0:fdea65150534 | 191 | Set address string using index (here 0 or 1) |
tobyspark | 0:fdea65150534 | 192 | Example: "/ard/test", char adr[]="/ard", setAddress(0,adr), char adr2[]="/test", setAddress(1,adr) |
tobyspark | 0:fdea65150534 | 193 | */ |
tobyspark | 0:fdea65150534 | 194 | void setAddress(uint8_t _index, //set 0,address[0] |
tobyspark | 0:fdea65150534 | 195 | char *_address); |
tobyspark | 0:fdea65150534 | 196 | //set 1,address[1] |
casiotone401 | 4:601f6a1141fb | 197 | |
tobyspark | 0:fdea65150534 | 198 | /** |
tobyspark | 0:fdea65150534 | 199 | Set IP Address of the OSC Message (for SENDING messages - for receiving this will be done when receiving something ) |
tobyspark | 0:fdea65150534 | 200 | @param[in] _ip Pointer of IP Address array (byte *) |
tobyspark | 0:fdea65150534 | 201 | Example: IP=192.168.0.99, then we have to do: ip[]={192,168,0,1}, then setIp(ip) |
tobyspark | 0:fdea65150534 | 202 | */ |
tobyspark | 0:fdea65150534 | 203 | void setIp( uint8_t *_ip ); //set ip |
casiotone401 | 4:601f6a1141fb | 204 | |
tobyspark | 0:fdea65150534 | 205 | /** |
tobyspark | 0:fdea65150534 | 206 | Set IP Address to the OSC Message container (not through pointer) |
tobyspark | 0:fdea65150534 | 207 | Example: IP=192.168.0.99 => setIp(192,168,0,99) |
tobyspark | 0:fdea65150534 | 208 | */ |
tobyspark | 0:fdea65150534 | 209 | void setIp(uint8_t _ip1, //set(192, |
tobyspark | 0:fdea65150534 | 210 | uint8_t _ip2, // 168, |
tobyspark | 0:fdea65150534 | 211 | uint8_t _ip3, // 0, |
tobyspark | 0:fdea65150534 | 212 | uint8_t _ip4); // 100) |
casiotone401 | 4:601f6a1141fb | 213 | |
tobyspark | 0:fdea65150534 | 214 | /* |
tobyspark | 0:fdea65150534 | 215 | Set PortNo for the OSC Message |
tobyspark | 0:fdea65150534 | 216 | @param[in] _port PortNo (unsigned int) |
tobyspark | 0:fdea65150534 | 217 | @return None |
tobyspark | 0:fdea65150534 | 218 | */ |
tobyspark | 0:fdea65150534 | 219 | void setPort( uint16_t _port ); |
tobyspark | 0:fdea65150534 | 220 | |
tobyspark | 0:fdea65150534 | 221 | /** |
tobyspark | 0:fdea65150534 | 222 | Set TypeTag and args to the OSC Message container |
tobyspark | 0:fdea65150534 | 223 | @param[in] types TypeTag string "i"(integer) or"f"(float) (char *) |
tobyspark | 0:fdea65150534 | 224 | @param[in] ... Pointer of the Args(variable argument) .. |
tobyspark | 0:fdea65150534 | 225 | @Example: |
tobyspark | 0:fdea65150534 | 226 | (1) integer 123: (NOTE: integers are LONG) |
tobyspark | 0:fdea65150534 | 227 | long v1=123; sendMes.setArgs("i",&v1) |
tobyspark | 0:fdea65150534 | 228 | (2)integer:123 and float:52.14 |
tobyspark | 0:fdea65150534 | 229 | long v1=123; double v2=52.14; sendMes.setArgs("if",&v1,&v2) |
tobyspark | 0:fdea65150534 | 230 | Attention: in this implementation, the maximum number of the args is 2 |
tobyspark | 0:fdea65150534 | 231 | (if setArgs("iff",&v1,&v2,&v3), data is ignored after &v3) |
tobyspark | 0:fdea65150534 | 232 | */ |
tobyspark | 0:fdea65150534 | 233 | void setArgs( char *types , ... ); //set ("if",&v1,&v2) |
casiotone401 | 2:507dea4cc97a | 234 | |
casiotone401 | 2:507dea4cc97a | 235 | //## Cortex-M is little Endian |
casiotone401 | 2:507dea4cc97a | 236 | /* |
casiotone401 | 2:507dea4cc97a | 237 | based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c |
casiotone401 | 2:507dea4cc97a | 238 | |
casiotone401 | 2:507dea4cc97a | 239 | if the system is little endian, it will flip the bits |
casiotone401 | 2:507dea4cc97a | 240 | if the system is big endian, it'll do nothing |
casiotone401 | 2:507dea4cc97a | 241 | */ |
casiotone401 | 2:507dea4cc97a | 242 | template<typename T> |
casiotone401 | 2:507dea4cc97a | 243 | static inline T BigEndian(const T& x) |
casiotone401 | 2:507dea4cc97a | 244 | { |
casiotone401 | 2:507dea4cc97a | 245 | const int one = 1; |
casiotone401 | 2:507dea4cc97a | 246 | const char sig = *(char*)&one; |
casiotone401 | 2:507dea4cc97a | 247 | if (sig == 0) return x; // for big endian machine just return the input |
casiotone401 | 2:507dea4cc97a | 248 | T ret; |
casiotone401 | 2:507dea4cc97a | 249 | int size = sizeof(T); |
casiotone401 | 2:507dea4cc97a | 250 | char* src = (char*)&x + sizeof(T) - 1; |
casiotone401 | 2:507dea4cc97a | 251 | char* dst = (char*)&ret; |
casiotone401 | 2:507dea4cc97a | 252 | while (size-- > 0){ |
casiotone401 | 2:507dea4cc97a | 253 | *dst++ = *src--; |
casiotone401 | 2:507dea4cc97a | 254 | } |
casiotone401 | 2:507dea4cc97a | 255 | return ret; |
casiotone401 | 2:507dea4cc97a | 256 | } |
tobyspark | 0:fdea65150534 | 257 | |
tobyspark | 0:fdea65150534 | 258 | friend class OSCClass; |
tobyspark | 0:fdea65150534 | 259 | |
tobyspark | 0:fdea65150534 | 260 | }; |
casiotone401 | 4:601f6a1141fb | 261 | |
casiotone401 | 4:601f6a1141fb | 262 | |
casiotone401 | 4:601f6a1141fb | 263 | |
tobyspark | 0:fdea65150534 | 264 | /* ==================================== OSCClass for sending and receiving OSC messages using UDP protocol ===================================== */ |
casiotone401 | 4:601f6a1141fb | 265 | |
tobyspark | 0:fdea65150534 | 266 | #include "UDPSocket.h" |
casiotone401 | 4:601f6a1141fb | 267 | |
tobyspark | 0:fdea65150534 | 268 | /** Wraps the UDP functions to send and receive OSC messages */ |
tobyspark | 0:fdea65150534 | 269 | class OSCClass { |
tobyspark | 0:fdea65150534 | 270 | |
tobyspark | 0:fdea65150534 | 271 | private: |
tobyspark | 0:fdea65150534 | 272 | |
tobyspark | 0:fdea65150534 | 273 | UDPSocket udpRec,udpSend; |
casiotone401 | 2:507dea4cc97a | 274 | char rcvBuff[128]; // raw buffer for UDP packets (udpRec.recvfrom( buf, 256, &host ) )) |
tobyspark | 0:fdea65150534 | 275 | int buflength; |
tobyspark | 0:fdea65150534 | 276 | |
tobyspark | 0:fdea65150534 | 277 | OSCMessage *receiverMessage; |
tobyspark | 0:fdea65150534 | 278 | OSCMessage *sendContainer; |
tobyspark | 0:fdea65150534 | 279 | |
tobyspark | 0:fdea65150534 | 280 | char tempAddress[MAX_ADDRESS][16]; |
tobyspark | 0:fdea65150534 | 281 | uint8_t tempArg[MAX_ARG][4]; |
tobyspark | 0:fdea65150534 | 282 | |
tobyspark | 0:fdea65150534 | 283 | void onUDPSocketEvent(UDPSocketEvent e); |
tobyspark | 0:fdea65150534 | 284 | |
tobyspark | 0:fdea65150534 | 285 | void decodePacket( OSCMessage *_mes); // makes OSC message from packet |
casiotone401 | 4:601f6a1141fb | 286 | |
tobyspark | 0:fdea65150534 | 287 | public: |
tobyspark | 0:fdea65150534 | 288 | |
tobyspark | 0:fdea65150534 | 289 | friend class UDPSocket; |
tobyspark | 0:fdea65150534 | 290 | |
tobyspark | 0:fdea65150534 | 291 | /** Create an object to send and receive OSC messages */ |
tobyspark | 0:fdea65150534 | 292 | OSCClass(); |
tobyspark | 1:63b72e393989 | 293 | ~OSCClass(); |
tobyspark | 0:fdea65150534 | 294 | /** |
tobyspark | 0:fdea65150534 | 295 | This sets "binds" the received message to the receiver container of the communication object |
tobyspark | 0:fdea65150534 | 296 | @param[in] _mes A pointer to the "receiveing" OSC message (OSCMessage *) |
tobyspark | 0:fdea65150534 | 297 | */ |
tobyspark | 0:fdea65150534 | 298 | OSCClass(OSCMessage *_mes); // set the receiver message container |
tobyspark | 0:fdea65150534 | 299 | |
tobyspark | 0:fdea65150534 | 300 | /** |
tobyspark | 0:fdea65150534 | 301 | This initializes the OSC communication object with default receiving port (DEFAULT_REC_PORT) |
tobyspark | 0:fdea65150534 | 302 | */ |
tobyspark | 0:fdea65150534 | 303 | void begin(); |
casiotone401 | 4:601f6a1141fb | 304 | |
tobyspark | 0:fdea65150534 | 305 | /** |
tobyspark | 0:fdea65150534 | 306 | Initialize an OSC object with arbitrary listening port |
tobyspark | 0:fdea65150534 | 307 | @param[in] _recievePort The listening ("receiving") Port No (unsigned int) |
tobyspark | 0:fdea65150534 | 308 | */ |
tobyspark | 0:fdea65150534 | 309 | void begin(uint16_t _recievePort); |
casiotone401 | 4:601f6a1141fb | 310 | |
tobyspark | 0:fdea65150534 | 311 | /** |
tobyspark | 0:fdea65150534 | 312 | Stop OSC communication (in fact, only the receiver - the server side) |
tobyspark | 0:fdea65150534 | 313 | */ |
tobyspark | 0:fdea65150534 | 314 | void stop(); |
tobyspark | 0:fdea65150534 | 315 | |
tobyspark | 0:fdea65150534 | 316 | /** |
tobyspark | 0:fdea65150534 | 317 | Returns whether there is new OSC data in the receiver message container. |
tobyspark | 0:fdea65150534 | 318 | */ |
tobyspark | 0:fdea65150534 | 319 | bool newMessage; |
casiotone401 | 4:601f6a1141fb | 320 | |
tobyspark | 0:fdea65150534 | 321 | /** |
tobyspark | 0:fdea65150534 | 322 | Set a OSC receive message container |
tobyspark | 0:fdea65150534 | 323 | @param[in] _mes Pointer to the OSC receive message container (OSCMessage *) |
tobyspark | 0:fdea65150534 | 324 | */ |
tobyspark | 0:fdea65150534 | 325 | void setReceiveMessage( OSCMessage *_mes ); //set receive OSCmessage container (note: the message has a "host" object from which we get the upd packets) |
casiotone401 | 4:601f6a1141fb | 326 | |
tobyspark | 0:fdea65150534 | 327 | /** |
tobyspark | 0:fdea65150534 | 328 | Get the received OSC message (note: this is another way to access the message directly from the OSCClass object). |
tobyspark | 0:fdea65150534 | 329 | The advantage is that we will signal that we read the message, and will be able to query if a NEW message arrived |
tobyspark | 0:fdea65150534 | 330 | (Alternatively, one could have a function pointer to pass to the OSC object, that will be called each time a new packet is received: TO DO) |
tobyspark | 0:fdea65150534 | 331 | */ |
tobyspark | 0:fdea65150534 | 332 | OSCMessage *getMessage(); //return received OSCmessage |
tobyspark | 0:fdea65150534 | 333 | |
tobyspark | 0:fdea65150534 | 334 | /** |
tobyspark | 0:fdea65150534 | 335 | Send an OSC Message (message contain the host ip and port where the message data has to be sent) |
tobyspark | 0:fdea65150534 | 336 | @param[in] _mes Pointer to the OSC message container (OSCMessage *) |
tobyspark | 0:fdea65150534 | 337 | */ |
tobyspark | 0:fdea65150534 | 338 | void sendOsc( OSCMessage *_mes ); //set&send OSCmessage (note: it will be sent to the host defined in the message container) |
casiotone401 | 4:601f6a1141fb | 339 | |
tobyspark | 0:fdea65150534 | 340 | /** |
tobyspark | 0:fdea65150534 | 341 | A function pointer to be set by host program that will be called on receipt of an OSC message |
tobyspark | 0:fdea65150534 | 342 | @code |
tobyspark | 0:fdea65150534 | 343 | osc.messageReceivedCallback.attach(&processOSC); |
tobyspark | 0:fdea65150534 | 344 | @endcode |
tobyspark | 0:fdea65150534 | 345 | */ |
tobyspark | 0:fdea65150534 | 346 | FunctionPointer messageReceivedCallback; |
casiotone401 | 2:507dea4cc97a | 347 | |
casiotone401 | 4:601f6a1141fb | 348 | |
casiotone401 | 2:507dea4cc97a | 349 | |
tobyspark | 0:fdea65150534 | 350 | }; |
casiotone401 | 4:601f6a1141fb | 351 | |
casiotone401 | 4:601f6a1141fb | 352 | #endif |