Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Thu Apr 17 08:04:14 2014 +0000
Revision:
47:199042980678
Parent:
30:d8af03f01cd4
publishing for sharing with Ken Iwasaki

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 30:d8af03f01cd4 1 /*
mbedalvaro 30:d8af03f01cd4 2 mbedOSC.cpp
mbedalvaro 30:d8af03f01cd4 3 */
mbedalvaro 30:d8af03f01cd4 4
mbedalvaro 30:d8af03f01cd4 5 #include "mbed.h"
mbedalvaro 30:d8af03f01cd4 6 #include "mbedOSC.h"
mbedalvaro 30:d8af03f01cd4 7 #include "stdarg.h"
mbedalvaro 30:d8af03f01cd4 8
mbedalvaro 30:d8af03f01cd4 9 OSCMessage::OSCMessage() {
mbedalvaro 30:d8af03f01cd4 10 // Initialize host address and port by default (as if this where the receiver message):
mbedalvaro 30:d8af03f01cd4 11 // host=new Host(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL);
mbedalvaro 30:d8af03f01cd4 12 }
mbedalvaro 30:d8af03f01cd4 13
mbedalvaro 30:d8af03f01cd4 14 /*
mbedalvaro 30:d8af03f01cd4 15 Set PortNo for the OSC Message
mbedalvaro 30:d8af03f01cd4 16 @param[in] _port PortNo (unsigned int)
mbedalvaro 30:d8af03f01cd4 17 @return None
mbedalvaro 30:d8af03f01cd4 18 */
mbedalvaro 30:d8af03f01cd4 19 void OSCMessage::setPort(uint16_t _port){
mbedalvaro 30:d8af03f01cd4 20 host.setPort(_port);
mbedalvaro 30:d8af03f01cd4 21 }
mbedalvaro 30:d8af03f01cd4 22
mbedalvaro 30:d8af03f01cd4 23 /*
mbedalvaro 30:d8af03f01cd4 24 Set IP Address of the OSC Message (for SENDING messages - for receiving this will be done when receiving something )
mbedalvaro 30:d8af03f01cd4 25 param[in] <-- _ip pointer of IP Address array (byte *)
mbedalvaro 30:d8af03f01cd4 26 Example: IP=192.168.0.99, then we have to do: ip[]={192,168,0,1}, then setIp(ip)
mbedalvaro 30:d8af03f01cd4 27 */
mbedalvaro 30:d8af03f01cd4 28 void OSCMessage::setIp(uint8_t *_ip){
mbedalvaro 30:d8af03f01cd4 29 host.setIp(IpAddr(_ip[0], _ip[1], _ip[2], _ip[3]));
mbedalvaro 30:d8af03f01cd4 30 }
mbedalvaro 30:d8af03f01cd4 31
mbedalvaro 30:d8af03f01cd4 32
mbedalvaro 30:d8af03f01cd4 33 /*!
mbedalvaro 30:d8af03f01cd4 34 Set IP Address to the OSC Message container (not through pointer)
mbedalvaro 30:d8af03f01cd4 35 Example: IP=192.168.0.99 => setIp(192,168,0,99)
mbedalvaro 30:d8af03f01cd4 36 */
mbedalvaro 30:d8af03f01cd4 37 void OSCMessage::setIp( uint8_t _ip1,
mbedalvaro 30:d8af03f01cd4 38 uint8_t _ip2,
mbedalvaro 30:d8af03f01cd4 39 uint8_t _ip3,
mbedalvaro 30:d8af03f01cd4 40 uint8_t _ip4 ){
mbedalvaro 30:d8af03f01cd4 41
mbedalvaro 30:d8af03f01cd4 42 host.setIp(IpAddr(_ip1, _ip2, _ip3, _ip4));
mbedalvaro 30:d8af03f01cd4 43 }
mbedalvaro 30:d8af03f01cd4 44
mbedalvaro 30:d8af03f01cd4 45 const IpAddr& OSCMessage::getIp(){
mbedalvaro 30:d8af03f01cd4 46 return host.getIp();
mbedalvaro 30:d8af03f01cd4 47 }
mbedalvaro 30:d8af03f01cd4 48
mbedalvaro 30:d8af03f01cd4 49
mbedalvaro 30:d8af03f01cd4 50 const int& OSCMessage::getPort(){
mbedalvaro 30:d8af03f01cd4 51 return host.getPort();
mbedalvaro 30:d8af03f01cd4 52 }
mbedalvaro 30:d8af03f01cd4 53
mbedalvaro 30:d8af03f01cd4 54
mbedalvaro 30:d8af03f01cd4 55 /*
mbedalvaro 30:d8af03f01cd4 56 Gets the number of the OSC message address
mbedalvaro 30:d8af03f01cd4 57 param[in] None
mbedalvaro 30:d8af03f01cd4 58 return number of the OSC message address (byte)
mbedalvaro 30:d8af03f01cd4 59 Examples: "/ard" --> the number of the addresses is 1
mbedalvaro 30:d8af03f01cd4 60 "/ard/test" --> the number of the addresses is 2
mbedalvaro 30:d8af03f01cd4 61 Attention: the maximum number of addresses is 2 (MAX_ADDRESS)
mbedalvaro 30:d8af03f01cd4 62 */
mbedalvaro 30:d8af03f01cd4 63 uint8_t OSCMessage::getAddressNum(){
mbedalvaro 30:d8af03f01cd4 64
mbedalvaro 30:d8af03f01cd4 65 return addressNum;
mbedalvaro 30:d8af03f01cd4 66 }
mbedalvaro 30:d8af03f01cd4 67
mbedalvaro 30:d8af03f01cd4 68
mbedalvaro 30:d8af03f01cd4 69 /*
mbedalvaro 30:d8af03f01cd4 70 Gets the number of the OSC message args
mbedalvaro 30:d8af03f01cd4 71 param[in] None
mbedalvaro 30:d8af03f01cd4 72 return number of the args (byte)
mbedalvaro 30:d8af03f01cd4 73 Example: "i" 123 --> number of the OSC message args is 1
mbedalvaro 30:d8af03f01cd4 74 "if" 123 54.24 --> number of the OSC message args is 2
mbedalvaro 30:d8af03f01cd4 75 Attention: the maximum number of args is 2 (MAX_ARG)
mbedalvaro 30:d8af03f01cd4 76 */
mbedalvaro 30:d8af03f01cd4 77 uint8_t OSCMessage::getArgNum(){
mbedalvaro 30:d8af03f01cd4 78
mbedalvaro 30:d8af03f01cd4 79 return argNum;
mbedalvaro 30:d8af03f01cd4 80 }
mbedalvaro 30:d8af03f01cd4 81
mbedalvaro 30:d8af03f01cd4 82
mbedalvaro 30:d8af03f01cd4 83 /*
mbedalvaro 30:d8af03f01cd4 84 Gets the address string of the OSC message
mbedalvaro 30:d8af03f01cd4 85 param [in] <-- _index is the index of the address string (byte)
mbedalvaro 30:d8af03f01cd4 86 return pointer of the address string (char *)
mbedalvaro 30:d8af03f01cd4 87 @note ex. "/ard/test"<br>
mbedalvaro 30:d8af03f01cd4 88 getAddress(0) = "/ard"<br>
mbedalvaro 30:d8af03f01cd4 89 getAddress(1) = "/test"
mbedalvaro 30:d8af03f01cd4 90 @attention It is maximum number of the addresses is 2<br>
mbedalvaro 30:d8af03f01cd4 91 In this case "/ard/test1/test2"<br>
mbedalvaro 30:d8af03f01cd4 92 ignore it after "/test2"
mbedalvaro 30:d8af03f01cd4 93 */
mbedalvaro 30:d8af03f01cd4 94 char * OSCMessage::getAddress(uint8_t _index){
mbedalvaro 30:d8af03f01cd4 95 if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1;
mbedalvaro 30:d8af03f01cd4 96 return address[_index];
mbedalvaro 30:d8af03f01cd4 97
mbedalvaro 30:d8af03f01cd4 98 }
mbedalvaro 30:d8af03f01cd4 99
mbedalvaro 30:d8af03f01cd4 100
mbedalvaro 30:d8af03f01cd4 101 /*
mbedalvaro 30:d8af03f01cd4 102 Gets the TopAddress string of the OSC message (this is just the address with index 0)
mbedalvaro 30:d8af03f01cd4 103 param[in] None
mbedalvaro 30:d8af03f01cd4 104 return pointer of the TopAddress string (char *), i.e. address[0]
mbedalvaro 30:d8af03f01cd4 105 Example: In the case "/ard/test", getTopAddress() = "/ard" (WITH the slash "/")
mbedalvaro 30:d8af03f01cd4 106 */
mbedalvaro 30:d8af03f01cd4 107 char * OSCMessage::getTopAddress(){
mbedalvaro 30:d8af03f01cd4 108
mbedalvaro 30:d8af03f01cd4 109 return getAddress(0);
mbedalvaro 30:d8af03f01cd4 110
mbedalvaro 30:d8af03f01cd4 111 }
mbedalvaro 30:d8af03f01cd4 112
mbedalvaro 30:d8af03f01cd4 113 /*
mbedalvaro 30:d8af03f01cd4 114 Gets the "SubAddress" string of the OSC message (this is just the address with index 1)
mbedalvaro 30:d8af03f01cd4 115 param[in] None
mbedalvaro 30:d8af03f01cd4 116 return pointer of the SubAddress string (char *), i.e. address[1]
mbedalvaro 30:d8af03f01cd4 117 Example: in the case "/ard/test", getSubAddress() = "/test" (WITH the slash "/")
mbedalvaro 30:d8af03f01cd4 118 */
mbedalvaro 30:d8af03f01cd4 119 char * OSCMessage::getSubAddress(){
mbedalvaro 30:d8af03f01cd4 120
mbedalvaro 30:d8af03f01cd4 121 return getAddress(1);
mbedalvaro 30:d8af03f01cd4 122
mbedalvaro 30:d8af03f01cd4 123 }
mbedalvaro 30:d8af03f01cd4 124
mbedalvaro 30:d8af03f01cd4 125 /*
mbedalvaro 30:d8af03f01cd4 126 Gets the TypeTag string (with index) of the OSC message
mbedalvaro 30:d8af03f01cd4 127 param[in] <--_index is the index of the TypeTag string (byte)
mbedalvaro 30:d8af03f01cd4 128 return: TypeTag char (char)
mbedalvaro 30:d8af03f01cd4 129 Example: in the case of a total typetag string equal to "if", getTypeTag(0) = 'i' and getTypeTag(1) = 'f'
mbedalvaro 30:d8af03f01cd4 130 Attention: MAX_ARG is maximum number of the args, if the index argument is larger, it will be constrained to this max.
mbedalvaro 30:d8af03f01cd4 131 */
mbedalvaro 30:d8af03f01cd4 132 char OSCMessage::getTypeTag(uint8_t _index){
mbedalvaro 30:d8af03f01cd4 133 if(_index>MAX_ARG) _index=MAX_ARG-1;
mbedalvaro 30:d8af03f01cd4 134 return typeTag[_index];
mbedalvaro 30:d8af03f01cd4 135 }
mbedalvaro 30:d8af03f01cd4 136
mbedalvaro 30:d8af03f01cd4 137 /*
mbedalvaro 30:d8af03f01cd4 138 Get the args of the OSC message with an integer value
mbedalvaro 30:d8af03f01cd4 139 param[in] <--_index is (an int, or uint8_t), corresponding to the index of the args (byte)
mbedalvaro 30:d8af03f01cd4 140 return: integer value (long, or int32_t)
mbedalvaro 30:d8af03f01cd4 141 Example: in the case "if" 123 54.24, getArgInt(0) = 123
mbedalvaro 30:d8af03f01cd4 142 Noe: "i" is integer, but the return type is "long"
mbedalvaro 30:d8af03f01cd4 143 Note: When a index is bigger than the number of the args, it is set to the number of the args
mbedalvaro 30:d8af03f01cd4 144 */
mbedalvaro 30:d8af03f01cd4 145 int32_t OSCMessage::getArgInt(uint8_t _index){
mbedalvaro 30:d8af03f01cd4 146 int32_t *value;
mbedalvaro 30:d8af03f01cd4 147 if(_index > argNum) _index=argNum;
mbedalvaro 30:d8af03f01cd4 148 value = (int32_t *)arg[_index]; // cast to int32_t
mbedalvaro 30:d8af03f01cd4 149 return *value;
mbedalvaro 30:d8af03f01cd4 150 }
mbedalvaro 30:d8af03f01cd4 151
mbedalvaro 30:d8af03f01cd4 152 /*
mbedalvaro 30:d8af03f01cd4 153 Get the args of the OSC message with a float value
mbedalvaro 30:d8af03f01cd4 154 param[in] <--_index is the index of the args
mbedalvaro 30:d8af03f01cd4 155 return: float value (double)
mbedalvaro 30:d8af03f01cd4 156 note: In this case "if" 123 54.24, getArgFloat(1) = 54.24
mbedalvaro 30:d8af03f01cd4 157 attention: arg declared as float, but return value cast as "double"
mbedalvaro 30:d8af03f01cd4 158 attention: When index is bigger than the number of the args, it is set to the number of the args
mbedalvaro 30:d8af03f01cd4 159 */
mbedalvaro 30:d8af03f01cd4 160 double OSCMessage::getArgFloat(uint8_t _index){
mbedalvaro 30:d8af03f01cd4 161 double *value;
mbedalvaro 30:d8af03f01cd4 162 if(_index > argNum) _index=argNum;
mbedalvaro 30:d8af03f01cd4 163 value = (double *)arg[_index];
mbedalvaro 30:d8af03f01cd4 164 return *value;
mbedalvaro 30:d8af03f01cd4 165 }
mbedalvaro 30:d8af03f01cd4 166
mbedalvaro 30:d8af03f01cd4 167 /*
mbedalvaro 30:d8af03f01cd4 168 Set TopAddress string of OSC Message
mbedalvaro 30:d8af03f01cd4 169 param[in] <-- _address is a string pointer for the TopAddress String (char *). NOTE: is this a good idea? why not pass as const, and do allocation here?
mbedalvaro 30:d8af03f01cd4 170 return: None
mbedalvaro 30:d8af03f01cd4 171 Example: if the complete address string is "/ard/test", we set the topaddress as follows: char top[]="/ard" (allocation done here!), then setTopAddress(top)
mbedalvaro 30:d8af03f01cd4 172 */
mbedalvaro 30:d8af03f01cd4 173 void OSCMessage::setTopAddress(char *_address){
mbedalvaro 30:d8af03f01cd4 174 address[0]=_address;
mbedalvaro 30:d8af03f01cd4 175 address[1]=0;
mbedalvaro 30:d8af03f01cd4 176 addressNum=1; // Note: this "erases" the subaddress! (is this a good idea?)
mbedalvaro 30:d8af03f01cd4 177 }
mbedalvaro 30:d8af03f01cd4 178
mbedalvaro 30:d8af03f01cd4 179 /*
mbedalvaro 30:d8af03f01cd4 180 Set SubAddress string of the OSC Message
mbedalvaro 30:d8af03f01cd4 181 param[in] <-- _address is a string pointer for the SubAddress String (char *)
mbedalvaro 30:d8af03f01cd4 182 return: None
mbedalvaro 30:d8af03f01cd4 183 Example: if the complete address string is "/ard/test", we set the subaddress as follows: char sub[]="/test" (allocation done here!), then setSubAddress(sub)
mbedalvaro 30:d8af03f01cd4 184 Attention: we should call first setTopAddress, and then setSubAddress. The order is important. This does not seems like a good idea...
mbedalvaro 30:d8af03f01cd4 185 */
mbedalvaro 30:d8af03f01cd4 186 void OSCMessage::setSubAddress(char *_address){
mbedalvaro 30:d8af03f01cd4 187 address[1]=_address;
mbedalvaro 30:d8af03f01cd4 188 addressNum=2; // Note: this assumes the top address was already set!
mbedalvaro 30:d8af03f01cd4 189 }
mbedalvaro 30:d8af03f01cd4 190
mbedalvaro 30:d8af03f01cd4 191
mbedalvaro 30:d8af03f01cd4 192 /*
mbedalvaro 30:d8af03f01cd4 193 Set the complete Address string of the OSC Message (top and sub addresses)
mbedalvaro 30:d8af03f01cd4 194 param[in] <-- _topAddress and _subAddress are the string pointers to top and sub addresses (char *)
mbedalvaro 30:d8af03f01cd4 195 return: None
mbedalvaro 30:d8af03f01cd4 196 Example: in the case "/ard/test", we need to do: char top[]="/ard", char sub[]="/test", and then setAddress(top,sub)
mbedalvaro 30:d8af03f01cd4 197 Reminder: in this implementation, the maximum number of addresses is MAX_ADDRESS=2
mbedalvaro 30:d8af03f01cd4 198 */
mbedalvaro 30:d8af03f01cd4 199 void OSCMessage::setAddress(char *_topAddress,char *_subAddress){
mbedalvaro 30:d8af03f01cd4 200 setTopAddress(_topAddress);
mbedalvaro 30:d8af03f01cd4 201 setSubAddress(_subAddress);
mbedalvaro 30:d8af03f01cd4 202 addressNum=2; // (unnecessary...)
mbedalvaro 30:d8af03f01cd4 203 }
mbedalvaro 30:d8af03f01cd4 204
mbedalvaro 30:d8af03f01cd4 205 /*
mbedalvaro 30:d8af03f01cd4 206 Set address string using index (here 0 or 1)
mbedalvaro 30:d8af03f01cd4 207 Example: "/ard/test", char adr[]="/ard", setAddress(0,adr), char adr2[]="/test", setAddress(1,adr)
mbedalvaro 30:d8af03f01cd4 208 */
mbedalvaro 30:d8af03f01cd4 209 void OSCMessage::setAddress(uint8_t _index, char *_address){
mbedalvaro 30:d8af03f01cd4 210 if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1;
mbedalvaro 30:d8af03f01cd4 211 address[_index]=_address;
mbedalvaro 30:d8af03f01cd4 212 addressNum=_index+1;
mbedalvaro 30:d8af03f01cd4 213 }
mbedalvaro 30:d8af03f01cd4 214
mbedalvaro 30:d8af03f01cd4 215
mbedalvaro 30:d8af03f01cd4 216 /*
mbedalvaro 30:d8af03f01cd4 217 Set TypeTag and args to the OSC Message container
mbedalvaro 30:d8af03f01cd4 218 @param[in] types TypeTag string "i"(integer) or"f"(float) (char *)
mbedalvaro 30:d8af03f01cd4 219 @param[in] ... Pointer of the Args(variable argument) ..
mbedalvaro 30:d8af03f01cd4 220 @return None
mbedalvaro 30:d8af03f01cd4 221 @Example:
mbedalvaro 30:d8af03f01cd4 222 (1) integer 123: (NOTE: integers are LONG)
mbedalvaro 30:d8af03f01cd4 223 long v1=123; sendMes.setArgs("i",&v1)
mbedalvaro 30:d8af03f01cd4 224 (2)integer:123 and float:52.14
mbedalvaro 30:d8af03f01cd4 225 long v1=123; double v2=52.14; sendMes.setArgs("if",&v1,&v2)
mbedalvaro 30:d8af03f01cd4 226 Attention: in this implementation, the maximum number of the args is 2
mbedalvaro 30:d8af03f01cd4 227 (if setArgs("iff",&v1,&v2,&v3), data is ignored after &v3)
mbedalvaro 30:d8af03f01cd4 228 */
mbedalvaro 30:d8af03f01cd4 229 void OSCMessage::setArgs(char *types,...){
mbedalvaro 30:d8af03f01cd4 230
mbedalvaro 30:d8af03f01cd4 231 va_list argList;
mbedalvaro 30:d8af03f01cd4 232
mbedalvaro 30:d8af03f01cd4 233 argNum = strlen(types);
mbedalvaro 30:d8af03f01cd4 234 if(argNum>MAX_ARG) argNum=MAX_ARG-1;
mbedalvaro 30:d8af03f01cd4 235
mbedalvaro 30:d8af03f01cd4 236 va_start( argList, types );
mbedalvaro 30:d8af03f01cd4 237 for(uint8_t i=0 ; i < argNum ; i++){
mbedalvaro 30:d8af03f01cd4 238
mbedalvaro 30:d8af03f01cd4 239 typeTag[i]=types[i];
mbedalvaro 30:d8af03f01cd4 240
mbedalvaro 30:d8af03f01cd4 241 switch(types[i]) {
mbedalvaro 30:d8af03f01cd4 242 case 'i':
mbedalvaro 30:d8af03f01cd4 243 arg[i]=(uint32_t *)va_arg(argList, uint32_t *);
mbedalvaro 30:d8af03f01cd4 244 break;
mbedalvaro 30:d8af03f01cd4 245 case 'f':
mbedalvaro 30:d8af03f01cd4 246 arg[i]=va_arg(argList, double *);
mbedalvaro 30:d8af03f01cd4 247 break;
mbedalvaro 30:d8af03f01cd4 248 // case 'b': // blob
mbedalvaro 30:d8af03f01cd4 249 // arg[i]=va_arg(argList, double *);
mbedalvaro 30:d8af03f01cd4 250 // break;
mbedalvaro 30:d8af03f01cd4 251 }
mbedalvaro 30:d8af03f01cd4 252
mbedalvaro 30:d8af03f01cd4 253 }
mbedalvaro 30:d8af03f01cd4 254
mbedalvaro 30:d8af03f01cd4 255 }
mbedalvaro 30:d8af03f01cd4 256
mbedalvaro 30:d8af03f01cd4 257 void OSCMessage::clearArgs() {
mbedalvaro 30:d8af03f01cd4 258 argNum=0;
mbedalvaro 30:d8af03f01cd4 259 }
mbedalvaro 30:d8af03f01cd4 260
mbedalvaro 30:d8af03f01cd4 261 // ================================================================================================================================================
mbedalvaro 30:d8af03f01cd4 262 // ==================================== OSCClass for sending and receiving OSC messages using UDP protocol =======================================
mbedalvaro 30:d8af03f01cd4 263 // ================================================================================================================================================
mbedalvaro 30:d8af03f01cd4 264 //The class define an object wrapping the UDP functions to send and receive OSC messages
mbedalvaro 30:d8af03f01cd4 265
mbedalvaro 30:d8af03f01cd4 266 OSCClass::OSCClass(){
mbedalvaro 30:d8af03f01cd4 267 newMessage=false;
mbedalvaro 30:d8af03f01cd4 268 }
mbedalvaro 30:d8af03f01cd4 269
mbedalvaro 30:d8af03f01cd4 270 /*
mbedalvaro 30:d8af03f01cd4 271 This sets "binds" the received message to the receiver container of the communication object
mbedalvaro 30:d8af03f01cd4 272 param[in]<--_mes is a pointer to the "receiveing" OSC message (OSCMessage *)
mbedalvaro 30:d8af03f01cd4 273 */
mbedalvaro 30:d8af03f01cd4 274 OSCClass::OSCClass(OSCMessage *_mes){
mbedalvaro 30:d8af03f01cd4 275 receiverMessage = _mes; // note: receiverMessage MUST be a pointer to the message, because we will modify things in it
mbedalvaro 30:d8af03f01cd4 276 newMessage=false;
mbedalvaro 30:d8af03f01cd4 277 }
mbedalvaro 30:d8af03f01cd4 278
mbedalvaro 30:d8af03f01cd4 279 /*
mbedalvaro 30:d8af03f01cd4 280 This initializes the OSC communication object with default receiving port (DEFAULT_REC_PORT)
mbedalvaro 30:d8af03f01cd4 281 param[in]: None
mbedalvaro 30:d8af03f01cd4 282 return: None
mbedalvaro 30:d8af03f01cd4 283 */
mbedalvaro 30:d8af03f01cd4 284 void OSCClass::begin(void (*pcallbackOnPacketArrival)(UDPSocketEvent))
mbedalvaro 30:d8af03f01cd4 285 {
mbedalvaro 30:d8af03f01cd4 286 // setup receiver udp socket, as well as callback function:
mbedalvaro 30:d8af03f01cd4 287 udpRec.bind(receiverMessage->host);
mbedalvaro 30:d8af03f01cd4 288 udpRec.setOnEvent(pcallbackOnPacketArrival); // note: onUDPSocketEvent is a member of OSCClass
mbedalvaro 30:d8af03f01cd4 289 }
mbedalvaro 30:d8af03f01cd4 290
mbedalvaro 30:d8af03f01cd4 291 /*
mbedalvaro 30:d8af03f01cd4 292 Initialize an OSC object with arbitrary listening port
mbedalvaro 30:d8af03f01cd4 293 param[in] <-- _recievePort, is the listening ("receiving") Port No (unsigned int)
mbedalvaro 30:d8af03f01cd4 294 return: None
mbedalvaro 30:d8af03f01cd4 295 */
mbedalvaro 30:d8af03f01cd4 296 void OSCClass::begin(uint16_t _recievePort, void (*pcallbackOnPacketArrival)(UDPSocketEvent))
mbedalvaro 30:d8af03f01cd4 297 {
mbedalvaro 30:d8af03f01cd4 298 receiverMessage->host.setPort(_recievePort);
mbedalvaro 30:d8af03f01cd4 299 // setup receiver udp socket, as well as callback function:
mbedalvaro 30:d8af03f01cd4 300 udpRec.bind(receiverMessage->host);
mbedalvaro 30:d8af03f01cd4 301 udpRec.setOnEvent(pcallbackOnPacketArrival);
mbedalvaro 30:d8af03f01cd4 302 }
mbedalvaro 30:d8af03f01cd4 303
mbedalvaro 30:d8af03f01cd4 304 /*
mbedalvaro 30:d8af03f01cd4 305 Set a OSC receive message container
mbedalvaro 30:d8af03f01cd4 306 param[in] _mes Pointer to the OSC receive message container (OSCMessage *)
mbedalvaro 30:d8af03f01cd4 307 return None
mbedalvaro 30:d8af03f01cd4 308 */
mbedalvaro 30:d8af03f01cd4 309 void OSCClass::setReceiveMessage(OSCMessage *_mes){
mbedalvaro 30:d8af03f01cd4 310 receiverMessage = _mes;
mbedalvaro 30:d8af03f01cd4 311 }
mbedalvaro 30:d8af03f01cd4 312
mbedalvaro 30:d8af03f01cd4 313 /*
mbedalvaro 30:d8af03f01cd4 314 callback function when an upd message arrives (it will be transformed as OSC message)
mbedalvaro 30:d8af03f01cd4 315 */
mbedalvaro 30:d8af03f01cd4 316 void OSCClass::onUDPSocketEvent(UDPSocketEvent e)
mbedalvaro 30:d8af03f01cd4 317 {
mbedalvaro 30:d8af03f01cd4 318 switch(e)
mbedalvaro 30:d8af03f01cd4 319 {
mbedalvaro 30:d8af03f01cd4 320 case UDPSOCKET_READABLE: //The only event for now
mbedalvaro 30:d8af03f01cd4 321 //char buf[256] = {0};
mbedalvaro 30:d8af03f01cd4 322 Host auxhost;
mbedalvaro 30:d8af03f01cd4 323 buflength = udpRec.recvfrom( rcvBuff, 256, &auxhost ); // QUESTION: auxhost should be equal to the receiver host I guess...
mbedalvaro 30:d8af03f01cd4 324 if ( buflength > 0 ) {
mbedalvaro 30:d8af03f01cd4 325 //printf("\r\nFrom %d.%d.%d.%d:\r\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]);
mbedalvaro 30:d8af03f01cd4 326 decodePacket(receiverMessage); // convert to OSC message, and save it in receiverMessage
mbedalvaro 30:d8af03f01cd4 327 newMessage=true;
mbedalvaro 30:d8af03f01cd4 328 }
mbedalvaro 30:d8af03f01cd4 329 break;
mbedalvaro 30:d8af03f01cd4 330 }
mbedalvaro 30:d8af03f01cd4 331 }
mbedalvaro 30:d8af03f01cd4 332
mbedalvaro 30:d8af03f01cd4 333 /*
mbedalvaro 30:d8af03f01cd4 334 Decode UDP packet and save it in the OSCMessage structure
mbedalvaro 30:d8af03f01cd4 335 */
mbedalvaro 30:d8af03f01cd4 336 void OSCClass::decodePacket( OSCMessage *_mes) {
mbedalvaro 30:d8af03f01cd4 337
mbedalvaro 30:d8af03f01cd4 338 //uint16_t lenBuff;
mbedalvaro 30:d8af03f01cd4 339 uint8_t d;
mbedalvaro 30:d8af03f01cd4 340 uint8_t messagePos=0;
mbedalvaro 30:d8af03f01cd4 341 uint8_t adrCount=0;
mbedalvaro 30:d8af03f01cd4 342 uint8_t adrMesPos=0;
mbedalvaro 30:d8af03f01cd4 343 uint8_t packetCount=0;
mbedalvaro 30:d8af03f01cd4 344 uint8_t packetPos=4;
mbedalvaro 30:d8af03f01cd4 345
mbedalvaro 30:d8af03f01cd4 346
mbedalvaro 30:d8af03f01cd4 347 //W5100.writeSn(socketNo, SnIR, SnIR::RECV);
mbedalvaro 30:d8af03f01cd4 348 //lenBuff=recvfrom(socketNo, rcvBuff, 1, receiverMessage->ip, &receiverMessage->port);
mbedalvaro 30:d8af03f01cd4 349
mbedalvaro 30:d8af03f01cd4 350 receiverMessage->address[0]=tempAddress[0];
mbedalvaro 30:d8af03f01cd4 351
mbedalvaro 30:d8af03f01cd4 352 //(1) address process start =========================================
mbedalvaro 30:d8af03f01cd4 353 do{
mbedalvaro 30:d8af03f01cd4 354 d=rcvBuff[messagePos];
mbedalvaro 30:d8af03f01cd4 355
mbedalvaro 30:d8af03f01cd4 356
mbedalvaro 30:d8af03f01cd4 357 if( (d=='/') && (messagePos>0) ){
mbedalvaro 30:d8af03f01cd4 358
mbedalvaro 30:d8af03f01cd4 359 if(adrCount<MAX_ADDRESS){
mbedalvaro 30:d8af03f01cd4 360 tempAddress[adrCount][adrMesPos]=0;
mbedalvaro 30:d8af03f01cd4 361
mbedalvaro 30:d8af03f01cd4 362 adrCount++;
mbedalvaro 30:d8af03f01cd4 363 adrMesPos=0;
mbedalvaro 30:d8af03f01cd4 364
mbedalvaro 30:d8af03f01cd4 365 receiverMessage->address[adrCount]=tempAddress[adrCount];
mbedalvaro 30:d8af03f01cd4 366 }
mbedalvaro 30:d8af03f01cd4 367
mbedalvaro 30:d8af03f01cd4 368 }
mbedalvaro 30:d8af03f01cd4 369
mbedalvaro 30:d8af03f01cd4 370 if(adrCount<MAX_ADDRESS){
mbedalvaro 30:d8af03f01cd4 371 //Added this in to remove the slashes out of final output
mbedalvaro 30:d8af03f01cd4 372 if(d!='/'){
mbedalvaro 30:d8af03f01cd4 373 tempAddress[adrCount][adrMesPos]=d;
mbedalvaro 30:d8af03f01cd4 374
mbedalvaro 30:d8af03f01cd4 375 if(packetCount>3) {
mbedalvaro 30:d8af03f01cd4 376 packetCount=0;
mbedalvaro 30:d8af03f01cd4 377 packetPos+=4;
mbedalvaro 30:d8af03f01cd4 378 }
mbedalvaro 30:d8af03f01cd4 379
mbedalvaro 30:d8af03f01cd4 380 adrMesPos++;
mbedalvaro 30:d8af03f01cd4 381 }
mbedalvaro 30:d8af03f01cd4 382 }
mbedalvaro 30:d8af03f01cd4 383 messagePos++;
mbedalvaro 30:d8af03f01cd4 384 packetCount++;
mbedalvaro 30:d8af03f01cd4 385
mbedalvaro 30:d8af03f01cd4 386 }while(d!=0);
mbedalvaro 30:d8af03f01cd4 387
mbedalvaro 30:d8af03f01cd4 388
mbedalvaro 30:d8af03f01cd4 389 if(adrCount<MAX_ADDRESS) adrCount++;
mbedalvaro 30:d8af03f01cd4 390 receiverMessage->addressNum=adrCount;
mbedalvaro 30:d8af03f01cd4 391
mbedalvaro 30:d8af03f01cd4 392 messagePos=packetPos;
mbedalvaro 30:d8af03f01cd4 393
mbedalvaro 30:d8af03f01cd4 394 //(2) type tag process starts =========================================
mbedalvaro 30:d8af03f01cd4 395 packetCount=0;
mbedalvaro 30:d8af03f01cd4 396 packetPos+=4;
mbedalvaro 30:d8af03f01cd4 397
mbedalvaro 30:d8af03f01cd4 398 uint8_t typeTagPos=0;
mbedalvaro 30:d8af03f01cd4 399 uint8_t tempArgNum=0;
mbedalvaro 30:d8af03f01cd4 400
mbedalvaro 30:d8af03f01cd4 401 while(rcvBuff[messagePos]!=0 ){
mbedalvaro 30:d8af03f01cd4 402
mbedalvaro 30:d8af03f01cd4 403 if(rcvBuff[messagePos] != ',') {
mbedalvaro 30:d8af03f01cd4 404
mbedalvaro 30:d8af03f01cd4 405 if(typeTagPos<MAX_ARG){
mbedalvaro 30:d8af03f01cd4 406 receiverMessage->typeTag[tempArgNum]=rcvBuff[messagePos];
mbedalvaro 30:d8af03f01cd4 407 tempArgNum++;
mbedalvaro 30:d8af03f01cd4 408 }
mbedalvaro 30:d8af03f01cd4 409 typeTagPos++;
mbedalvaro 30:d8af03f01cd4 410
mbedalvaro 30:d8af03f01cd4 411 }
mbedalvaro 30:d8af03f01cd4 412
mbedalvaro 30:d8af03f01cd4 413 packetCount++;
mbedalvaro 30:d8af03f01cd4 414
mbedalvaro 30:d8af03f01cd4 415 if(packetCount>3) {
mbedalvaro 30:d8af03f01cd4 416 packetCount=0;
mbedalvaro 30:d8af03f01cd4 417 packetPos+=4;
mbedalvaro 30:d8af03f01cd4 418 }
mbedalvaro 30:d8af03f01cd4 419
mbedalvaro 30:d8af03f01cd4 420 messagePos++;
mbedalvaro 30:d8af03f01cd4 421 }
mbedalvaro 30:d8af03f01cd4 422
mbedalvaro 30:d8af03f01cd4 423 receiverMessage->argNum=tempArgNum;
mbedalvaro 30:d8af03f01cd4 424
mbedalvaro 30:d8af03f01cd4 425 messagePos=packetPos;
mbedalvaro 30:d8af03f01cd4 426
mbedalvaro 30:d8af03f01cd4 427 //(3) tempArg process starts =========================================
mbedalvaro 30:d8af03f01cd4 428 for(int i=0;i<tempArgNum;i++){
mbedalvaro 30:d8af03f01cd4 429
mbedalvaro 30:d8af03f01cd4 430 adrMesPos=3;
mbedalvaro 30:d8af03f01cd4 431
mbedalvaro 30:d8af03f01cd4 432 receiverMessage->arg[i]=tempArg[i];
mbedalvaro 30:d8af03f01cd4 433
mbedalvaro 30:d8af03f01cd4 434 for(int j=0;j<4;j++){
mbedalvaro 30:d8af03f01cd4 435
mbedalvaro 30:d8af03f01cd4 436 tempArg[i][adrMesPos]=rcvBuff[messagePos];
mbedalvaro 30:d8af03f01cd4 437
mbedalvaro 30:d8af03f01cd4 438 messagePos++;
mbedalvaro 30:d8af03f01cd4 439 adrMesPos--;
mbedalvaro 30:d8af03f01cd4 440 }
mbedalvaro 30:d8af03f01cd4 441
mbedalvaro 30:d8af03f01cd4 442 }
mbedalvaro 30:d8af03f01cd4 443
mbedalvaro 30:d8af03f01cd4 444
mbedalvaro 30:d8af03f01cd4 445 }
mbedalvaro 30:d8af03f01cd4 446
mbedalvaro 30:d8af03f01cd4 447
mbedalvaro 30:d8af03f01cd4 448 /*
mbedalvaro 30:d8af03f01cd4 449 Get the received OSC message (note: this is another way to access the message directly from the OSCClass object).
mbedalvaro 30:d8af03f01cd4 450 The advantage is that we will signal that we read the message, and will be able to query if a NEW message arrived
mbedalvaro 30:d8af03f01cd4 451 (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)
mbedalvaro 30:d8af03f01cd4 452 */
mbedalvaro 30:d8af03f01cd4 453 OSCMessage * OSCClass::getMessage(){
mbedalvaro 30:d8af03f01cd4 454 newMessage=false; // this indicate the user READ the message
mbedalvaro 30:d8af03f01cd4 455 return receiverMessage;
mbedalvaro 30:d8af03f01cd4 456 }
mbedalvaro 30:d8af03f01cd4 457
mbedalvaro 30:d8af03f01cd4 458 /*
mbedalvaro 30:d8af03f01cd4 459 Send an OSC Message (message contain the host ip and port where the message data has to be sent)
mbedalvaro 30:d8af03f01cd4 460 param[in] _mes Pointer to the OSC message container (OSCMessage *)
mbedalvaro 30:d8af03f01cd4 461 return None
mbedalvaro 30:d8af03f01cd4 462 */
mbedalvaro 30:d8af03f01cd4 463 void OSCClass::sendOsc( OSCMessage *_mes )
mbedalvaro 30:d8af03f01cd4 464 {
mbedalvaro 30:d8af03f01cd4 465 uint8_t lengthEnd;
mbedalvaro 30:d8af03f01cd4 466 uint8_t lengthStart;
mbedalvaro 30:d8af03f01cd4 467 char buff[128];
mbedalvaro 30:d8af03f01cd4 468
mbedalvaro 30:d8af03f01cd4 469 sendContainer = _mes;
mbedalvaro 30:d8af03f01cd4 470
mbedalvaro 30:d8af03f01cd4 471 //&#12496;&#12483;&#12501;&#12449;&#21021;&#26399;&#20516;
mbedalvaro 30:d8af03f01cd4 472 buff[0]=0;
mbedalvaro 30:d8af03f01cd4 473
mbedalvaro 30:d8af03f01cd4 474 //1) Add name spaces:
mbedalvaro 30:d8af03f01cd4 475 for(int i=0;i<sendContainer->addressNum;i++){
mbedalvaro 30:d8af03f01cd4 476
mbedalvaro 30:d8af03f01cd4 477 strcat(buff,sendContainer->address[i]); // note: an address is for instance: "/test" (including the "/")
mbedalvaro 30:d8af03f01cd4 478
mbedalvaro 30:d8af03f01cd4 479 }
mbedalvaro 30:d8af03f01cd4 480
mbedalvaro 30:d8af03f01cd4 481 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 482 lengthStart=strlen(buff);
mbedalvaro 30:d8af03f01cd4 483 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 484 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 485 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 486 }
mbedalvaro 30:d8af03f01cd4 487
mbedalvaro 30:d8af03f01cd4 488 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 489
mbedalvaro 30:d8af03f01cd4 490 //2) Add TypeTag:
mbedalvaro 30:d8af03f01cd4 491 buff[lengthEnd++]=','; // Note: type tag is for instance: ",if"
mbedalvaro 30:d8af03f01cd4 492 for(int i=0;i<sendContainer->argNum;i++){
mbedalvaro 30:d8af03f01cd4 493 buff[lengthEnd++]=sendContainer->typeTag[i];
mbedalvaro 30:d8af03f01cd4 494 }
mbedalvaro 30:d8af03f01cd4 495
mbedalvaro 30:d8af03f01cd4 496 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 497 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 498 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 499 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 500 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 501 }
mbedalvaro 30:d8af03f01cd4 502
mbedalvaro 30:d8af03f01cd4 503 //3) add argument values (Note: here only big endian, this is, FIRST the MOST SIGNIFICANT BYTE):
mbedalvaro 30:d8af03f01cd4 504 uint8_t *v;
mbedalvaro 30:d8af03f01cd4 505 for(int i=0;i<sendContainer->argNum;i++){
mbedalvaro 30:d8af03f01cd4 506 uint8_t valuePos=3;
mbedalvaro 30:d8af03f01cd4 507 v=(uint8_t *)sendContainer->arg[i];
mbedalvaro 30:d8af03f01cd4 508
mbedalvaro 30:d8af03f01cd4 509 buff[lengthEnd++]=v[valuePos--];
mbedalvaro 30:d8af03f01cd4 510 buff[lengthEnd++]=v[valuePos--];
mbedalvaro 30:d8af03f01cd4 511 buff[lengthEnd++]=v[valuePos--];
mbedalvaro 30:d8af03f01cd4 512 buff[lengthEnd++]=v[valuePos];
mbedalvaro 30:d8af03f01cd4 513
mbedalvaro 30:d8af03f01cd4 514 }
mbedalvaro 30:d8af03f01cd4 515
mbedalvaro 30:d8af03f01cd4 516 //4) Send udp packet:
mbedalvaro 30:d8af03f01cd4 517 //sendto( socketNo, (uint8_t *)buff, lengthEnd, sendContainer->ip, sendContainer->port );
mbedalvaro 30:d8af03f01cd4 518 udpSend.sendto(buff , lengthEnd, &(sendContainer->host));
mbedalvaro 30:d8af03f01cd4 519 }
mbedalvaro 30:d8af03f01cd4 520
mbedalvaro 30:d8af03f01cd4 521 // this is bad, should be integrated on the above, but no time:
mbedalvaro 30:d8af03f01cd4 522 void OSCClass::sendOscBlob( uint8_t * myblob, int sizeblob, OSCMessage *_mes )
mbedalvaro 30:d8af03f01cd4 523 {
mbedalvaro 30:d8af03f01cd4 524
mbedalvaro 30:d8af03f01cd4 525 // esto es mamarrachezco:
mbedalvaro 30:d8af03f01cd4 526 sendContainer = _mes;
mbedalvaro 30:d8af03f01cd4 527
mbedalvaro 30:d8af03f01cd4 528 uint8_t lengthEnd;
mbedalvaro 30:d8af03f01cd4 529 uint8_t lengthStart;
mbedalvaro 30:d8af03f01cd4 530
mbedalvaro 30:d8af03f01cd4 531 char buff[50+sizeblob];
mbedalvaro 30:d8af03f01cd4 532 buff[0]=0;
mbedalvaro 30:d8af03f01cd4 533
mbedalvaro 30:d8af03f01cd4 534 //1) Add name spaces (will be just '/posblob' for the blob)
mbedalvaro 30:d8af03f01cd4 535 strcat(buff,"/posblob");
mbedalvaro 30:d8af03f01cd4 536
mbedalvaro 30:d8af03f01cd4 537 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 538 lengthStart=strlen(buff);
mbedalvaro 30:d8af03f01cd4 539 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 540 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 541 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 542 }
mbedalvaro 30:d8af03f01cd4 543
mbedalvaro 30:d8af03f01cd4 544 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 545
mbedalvaro 30:d8af03f01cd4 546 //2) Add TypeTag:
mbedalvaro 30:d8af03f01cd4 547 buff[lengthEnd++]=','; //here type tag is just ",b" (one blob)
mbedalvaro 30:d8af03f01cd4 548 buff[lengthEnd++]='b';
mbedalvaro 30:d8af03f01cd4 549
mbedalvaro 30:d8af03f01cd4 550 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 551 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 552 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 553 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 554 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 555 }
mbedalvaro 30:d8af03f01cd4 556
mbedalvaro 30:d8af03f01cd4 557 //3) add blob (Note: here it is up to the user to get the values right - big endian?):
mbedalvaro 30:d8af03f01cd4 558 // First the size (as an int32, where the type int32 is a 32-bit big-endian two's complement integer)
mbedalvaro 30:d8af03f01cd4 559 buff[lengthEnd++]=0;
mbedalvaro 30:d8af03f01cd4 560 buff[lengthEnd++]=0;
mbedalvaro 30:d8af03f01cd4 561 buff[lengthEnd++]=0;
mbedalvaro 30:d8af03f01cd4 562 buff[lengthEnd++]=(char)sizeblob; // note: the loops always have less than 255 points...
mbedalvaro 30:d8af03f01cd4 563 // Add blob data:
mbedalvaro 30:d8af03f01cd4 564 memcpy(&(buff[lengthEnd]), myblob, sizeblob);
mbedalvaro 30:d8af03f01cd4 565 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 566 lengthStart=lengthEnd+sizeblob;
mbedalvaro 30:d8af03f01cd4 567 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 568 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 569 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 570 }
mbedalvaro 30:d8af03f01cd4 571
mbedalvaro 30:d8af03f01cd4 572 //4) Send udp packet:
mbedalvaro 30:d8af03f01cd4 573 //sendto( socketNo, (uint8_t *)buff, lengthEnd, sendContainer->ip, sendContainer->port );
mbedalvaro 30:d8af03f01cd4 574 udpSend.sendto(buff , lengthEnd, &(sendContainer->host));
mbedalvaro 30:d8af03f01cd4 575 }
mbedalvaro 30:d8af03f01cd4 576
mbedalvaro 30:d8af03f01cd4 577 // NOT FINISHED!!!!
mbedalvaro 30:d8af03f01cd4 578 void OSCClass::sendOscString( uint8_t * myblob, int sizeblob, OSCMessage *_mes )
mbedalvaro 30:d8af03f01cd4 579 {
mbedalvaro 30:d8af03f01cd4 580 uint8_t lengthEnd;
mbedalvaro 30:d8af03f01cd4 581 uint8_t lengthStart;
mbedalvaro 30:d8af03f01cd4 582
mbedalvaro 30:d8af03f01cd4 583 char buff[50+sizeblob];
mbedalvaro 30:d8af03f01cd4 584 buff[0]=0;
mbedalvaro 30:d8af03f01cd4 585
mbedalvaro 30:d8af03f01cd4 586 //1) Add name spaces (will be just '/posblob' for the blob)
mbedalvaro 30:d8af03f01cd4 587 strcat(buff,"/posString");
mbedalvaro 30:d8af03f01cd4 588
mbedalvaro 30:d8af03f01cd4 589 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 590 lengthStart=strlen(buff);
mbedalvaro 30:d8af03f01cd4 591 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 592 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 593 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 594 }
mbedalvaro 30:d8af03f01cd4 595
mbedalvaro 30:d8af03f01cd4 596 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 597
mbedalvaro 30:d8af03f01cd4 598 //2) Add TypeTag:
mbedalvaro 30:d8af03f01cd4 599 buff[lengthEnd++]=','; //here type tag is just ",b" (one blob)
mbedalvaro 30:d8af03f01cd4 600 buff[lengthEnd++]='s';
mbedalvaro 30:d8af03f01cd4 601
mbedalvaro 30:d8af03f01cd4 602 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 603 lengthStart=lengthEnd;
mbedalvaro 30:d8af03f01cd4 604 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 605 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 606 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 607 }
mbedalvaro 30:d8af03f01cd4 608
mbedalvaro 30:d8af03f01cd4 609 // Add blob data:
mbedalvaro 30:d8af03f01cd4 610 memcpy(&(buff[lengthEnd]), myblob, sizeblob);
mbedalvaro 30:d8af03f01cd4 611 // pad with 0s to align in multiples of 4:
mbedalvaro 30:d8af03f01cd4 612 lengthStart=lengthEnd+sizeblob;
mbedalvaro 30:d8af03f01cd4 613 lengthEnd=lengthStart+(4-(lengthStart%4));
mbedalvaro 30:d8af03f01cd4 614 for(int i=lengthStart ; i<lengthEnd; i++){
mbedalvaro 30:d8af03f01cd4 615 buff[i]=0;
mbedalvaro 30:d8af03f01cd4 616 }
mbedalvaro 30:d8af03f01cd4 617
mbedalvaro 30:d8af03f01cd4 618 //4) Send udp packet:
mbedalvaro 30:d8af03f01cd4 619 //sendto( socketNo, (uint8_t *)buff, lengthEnd, sendContainer->ip, sendContainer->port );
mbedalvaro 30:d8af03f01cd4 620 udpSend.sendto(buff , lengthEnd, &(sendContainer->host));
mbedalvaro 30:d8af03f01cd4 621 }
mbedalvaro 30:d8af03f01cd4 622
mbedalvaro 30:d8af03f01cd4 623 /*
mbedalvaro 30:d8af03f01cd4 624 flush a receive buffer
mbedalvaro 30:d8af03f01cd4 625 void OSCClass::flush() {
mbedalvaro 30:d8af03f01cd4 626 while ( available() ){}
mbedalvaro 30:d8af03f01cd4 627 }
mbedalvaro 30:d8af03f01cd4 628 */
mbedalvaro 30:d8af03f01cd4 629
mbedalvaro 30:d8af03f01cd4 630 /*
mbedalvaro 30:d8af03f01cd4 631 Stop OSC communication (in fact, only the receiver - the server side)
mbedalvaro 30:d8af03f01cd4 632 */
mbedalvaro 30:d8af03f01cd4 633 void OSCClass::stop() {
mbedalvaro 30:d8af03f01cd4 634 //close( socketNo );
mbedalvaro 30:d8af03f01cd4 635 udpSend.resetOnEvent(); // disables callback
mbedalvaro 30:d8af03f01cd4 636 }
mbedalvaro 30:d8af03f01cd4 637
mbedalvaro 30:d8af03f01cd4 638