osc test
Dependencies: EthernetNetIf mbed mbed
Fork of OSC by
mbedOSC.cpp@2:b5af09b0d540, 2013-10-04 (annotated)
- Committer:
- itotaka
- Date:
- Fri Oct 04 12:19:39 2013 +0000
- Revision:
- 2:b5af09b0d540
- Parent:
- 1:63b72e393989
to share
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tobyspark | 0:fdea65150534 | 1 | /* |
tobyspark | 0:fdea65150534 | 2 | mbedOSC.cpp |
tobyspark | 0:fdea65150534 | 3 | */ |
tobyspark | 0:fdea65150534 | 4 | |
tobyspark | 0:fdea65150534 | 5 | #include "mbed.h" |
tobyspark | 0:fdea65150534 | 6 | #include "mbedOSC.h" |
tobyspark | 0:fdea65150534 | 7 | #include "stdarg.h" |
tobyspark | 0:fdea65150534 | 8 | |
tobyspark | 0:fdea65150534 | 9 | OSCMessage::OSCMessage() { |
tobyspark | 0:fdea65150534 | 10 | // Initialize host address and port by default (as if this where the receiver message): |
tobyspark | 0:fdea65150534 | 11 | // host=new Host(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL); |
tobyspark | 0:fdea65150534 | 12 | } |
tobyspark | 0:fdea65150534 | 13 | |
tobyspark | 0:fdea65150534 | 14 | void OSCMessage::setPort(uint16_t _port){ |
tobyspark | 0:fdea65150534 | 15 | host.setPort(_port); |
tobyspark | 0:fdea65150534 | 16 | } |
tobyspark | 0:fdea65150534 | 17 | |
tobyspark | 0:fdea65150534 | 18 | |
tobyspark | 0:fdea65150534 | 19 | void OSCMessage::setIp(uint8_t *_ip){ |
tobyspark | 0:fdea65150534 | 20 | host.setIp(IpAddr(_ip[0], _ip[1], _ip[2], _ip[3])); |
tobyspark | 0:fdea65150534 | 21 | } |
tobyspark | 0:fdea65150534 | 22 | |
tobyspark | 0:fdea65150534 | 23 | |
tobyspark | 0:fdea65150534 | 24 | |
tobyspark | 0:fdea65150534 | 25 | void OSCMessage::setIp( uint8_t _ip1, |
tobyspark | 0:fdea65150534 | 26 | uint8_t _ip2, |
tobyspark | 0:fdea65150534 | 27 | uint8_t _ip3, |
tobyspark | 0:fdea65150534 | 28 | uint8_t _ip4 ){ |
tobyspark | 0:fdea65150534 | 29 | |
tobyspark | 0:fdea65150534 | 30 | host.setIp(IpAddr(_ip1, _ip2, _ip3, _ip4)); |
tobyspark | 0:fdea65150534 | 31 | } |
tobyspark | 0:fdea65150534 | 32 | |
tobyspark | 0:fdea65150534 | 33 | const IpAddr& OSCMessage::getIp(){ |
tobyspark | 0:fdea65150534 | 34 | return host.getIp(); |
tobyspark | 0:fdea65150534 | 35 | } |
tobyspark | 0:fdea65150534 | 36 | |
tobyspark | 0:fdea65150534 | 37 | |
tobyspark | 0:fdea65150534 | 38 | const int& OSCMessage::getPort(){ |
tobyspark | 0:fdea65150534 | 39 | return host.getPort(); |
tobyspark | 0:fdea65150534 | 40 | } |
tobyspark | 0:fdea65150534 | 41 | |
tobyspark | 0:fdea65150534 | 42 | |
tobyspark | 0:fdea65150534 | 43 | |
tobyspark | 0:fdea65150534 | 44 | uint8_t OSCMessage::getAddressNum(){ |
tobyspark | 0:fdea65150534 | 45 | |
tobyspark | 0:fdea65150534 | 46 | return addressNum; |
tobyspark | 0:fdea65150534 | 47 | } |
tobyspark | 0:fdea65150534 | 48 | |
tobyspark | 0:fdea65150534 | 49 | |
tobyspark | 0:fdea65150534 | 50 | uint8_t OSCMessage::getArgNum(){ |
tobyspark | 0:fdea65150534 | 51 | |
tobyspark | 0:fdea65150534 | 52 | return argNum; |
tobyspark | 0:fdea65150534 | 53 | } |
tobyspark | 0:fdea65150534 | 54 | |
tobyspark | 0:fdea65150534 | 55 | |
tobyspark | 0:fdea65150534 | 56 | |
tobyspark | 0:fdea65150534 | 57 | char * OSCMessage::getAddress(uint8_t _index){ |
tobyspark | 0:fdea65150534 | 58 | if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1; |
tobyspark | 0:fdea65150534 | 59 | return address[_index]; |
tobyspark | 0:fdea65150534 | 60 | |
tobyspark | 0:fdea65150534 | 61 | } |
tobyspark | 0:fdea65150534 | 62 | |
tobyspark | 0:fdea65150534 | 63 | |
tobyspark | 0:fdea65150534 | 64 | |
tobyspark | 0:fdea65150534 | 65 | char * OSCMessage::getTopAddress(){ |
tobyspark | 0:fdea65150534 | 66 | |
tobyspark | 0:fdea65150534 | 67 | return getAddress(0); |
tobyspark | 0:fdea65150534 | 68 | |
tobyspark | 0:fdea65150534 | 69 | } |
tobyspark | 0:fdea65150534 | 70 | |
tobyspark | 0:fdea65150534 | 71 | |
tobyspark | 0:fdea65150534 | 72 | char * OSCMessage::getSubAddress(){ |
tobyspark | 0:fdea65150534 | 73 | |
tobyspark | 0:fdea65150534 | 74 | return getAddress(1); |
tobyspark | 0:fdea65150534 | 75 | |
tobyspark | 0:fdea65150534 | 76 | } |
tobyspark | 0:fdea65150534 | 77 | |
tobyspark | 0:fdea65150534 | 78 | |
tobyspark | 0:fdea65150534 | 79 | char OSCMessage::getTypeTag(uint8_t _index){ |
tobyspark | 0:fdea65150534 | 80 | if(_index>MAX_ARG) _index=MAX_ARG-1; |
tobyspark | 0:fdea65150534 | 81 | return typeTag[_index]; |
tobyspark | 0:fdea65150534 | 82 | } |
tobyspark | 0:fdea65150534 | 83 | |
tobyspark | 0:fdea65150534 | 84 | |
tobyspark | 0:fdea65150534 | 85 | int32_t OSCMessage::getArgInt(uint8_t _index){ |
tobyspark | 0:fdea65150534 | 86 | int32_t *value; |
tobyspark | 0:fdea65150534 | 87 | if(_index > argNum) _index=argNum; |
tobyspark | 0:fdea65150534 | 88 | value = (int32_t *)arg[_index]; // cast to int32_t |
tobyspark | 0:fdea65150534 | 89 | return *value; |
tobyspark | 0:fdea65150534 | 90 | } |
tobyspark | 0:fdea65150534 | 91 | |
tobyspark | 0:fdea65150534 | 92 | |
tobyspark | 0:fdea65150534 | 93 | double OSCMessage::getArgFloat(uint8_t _index){ |
tobyspark | 1:63b72e393989 | 94 | float *value; |
tobyspark | 0:fdea65150534 | 95 | if(_index > argNum) _index=argNum; |
tobyspark | 1:63b72e393989 | 96 | value = (float *)arg[_index]; // cast to float not double for correct parsing on mbed! |
tobyspark | 0:fdea65150534 | 97 | return *value; |
tobyspark | 0:fdea65150534 | 98 | } |
tobyspark | 0:fdea65150534 | 99 | |
tobyspark | 0:fdea65150534 | 100 | |
tobyspark | 0:fdea65150534 | 101 | void OSCMessage::setTopAddress(char *_address){ |
tobyspark | 0:fdea65150534 | 102 | address[0]=_address; |
tobyspark | 0:fdea65150534 | 103 | address[1]=0; |
tobyspark | 0:fdea65150534 | 104 | addressNum=1; // Note: this "erases" the subaddress! (is this a good idea?) |
tobyspark | 0:fdea65150534 | 105 | } |
tobyspark | 0:fdea65150534 | 106 | |
tobyspark | 0:fdea65150534 | 107 | |
tobyspark | 0:fdea65150534 | 108 | void OSCMessage::setSubAddress(char *_address){ |
tobyspark | 0:fdea65150534 | 109 | address[1]=_address; |
tobyspark | 0:fdea65150534 | 110 | addressNum=2; // Note: this assumes the top address was already set! |
tobyspark | 0:fdea65150534 | 111 | } |
tobyspark | 0:fdea65150534 | 112 | |
tobyspark | 0:fdea65150534 | 113 | |
tobyspark | 0:fdea65150534 | 114 | |
tobyspark | 0:fdea65150534 | 115 | void OSCMessage::setAddress(char *_topAddress,char *_subAddress){ |
tobyspark | 0:fdea65150534 | 116 | setTopAddress(_topAddress); |
tobyspark | 0:fdea65150534 | 117 | setSubAddress(_subAddress); |
tobyspark | 0:fdea65150534 | 118 | addressNum=2; // (unnecessary...) |
tobyspark | 0:fdea65150534 | 119 | } |
tobyspark | 0:fdea65150534 | 120 | |
tobyspark | 0:fdea65150534 | 121 | |
tobyspark | 0:fdea65150534 | 122 | void OSCMessage::setAddress(uint8_t _index, char *_address){ |
tobyspark | 0:fdea65150534 | 123 | if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1; |
tobyspark | 0:fdea65150534 | 124 | address[_index]=_address; |
tobyspark | 0:fdea65150534 | 125 | addressNum=_index+1; |
tobyspark | 0:fdea65150534 | 126 | } |
tobyspark | 0:fdea65150534 | 127 | |
tobyspark | 0:fdea65150534 | 128 | |
tobyspark | 0:fdea65150534 | 129 | |
tobyspark | 0:fdea65150534 | 130 | void OSCMessage::setArgs(char *types,...){ |
tobyspark | 0:fdea65150534 | 131 | |
tobyspark | 0:fdea65150534 | 132 | va_list argList; |
tobyspark | 0:fdea65150534 | 133 | |
tobyspark | 0:fdea65150534 | 134 | argNum = strlen(types); |
tobyspark | 0:fdea65150534 | 135 | if(argNum>MAX_ARG) argNum=MAX_ARG-1; |
tobyspark | 0:fdea65150534 | 136 | |
tobyspark | 0:fdea65150534 | 137 | va_start( argList, types ); |
tobyspark | 0:fdea65150534 | 138 | for(uint8_t i=0 ; i < argNum ; i++){ |
tobyspark | 0:fdea65150534 | 139 | |
tobyspark | 0:fdea65150534 | 140 | typeTag[i]=types[i]; |
tobyspark | 0:fdea65150534 | 141 | |
tobyspark | 0:fdea65150534 | 142 | switch(types[i]) { |
tobyspark | 0:fdea65150534 | 143 | case 'i': |
tobyspark | 0:fdea65150534 | 144 | arg[i]=(uint32_t *)va_arg(argList, uint32_t *); |
tobyspark | 0:fdea65150534 | 145 | break; |
tobyspark | 0:fdea65150534 | 146 | case 'f': |
tobyspark | 0:fdea65150534 | 147 | arg[i]=va_arg(argList, double *); |
tobyspark | 0:fdea65150534 | 148 | break; |
tobyspark | 1:63b72e393989 | 149 | } |
tobyspark | 0:fdea65150534 | 150 | } |
tobyspark | 0:fdea65150534 | 151 | } |
tobyspark | 0:fdea65150534 | 152 | |
tobyspark | 0:fdea65150534 | 153 | // ================================================================================================================================================ |
tobyspark | 0:fdea65150534 | 154 | // ==================================== OSCClass for sending and receiving OSC messages using UDP protocol ======================================= |
tobyspark | 0:fdea65150534 | 155 | // ================================================================================================================================================ |
tobyspark | 0:fdea65150534 | 156 | //The class define an object wrapping the UDP functions to send and receive OSC messages |
tobyspark | 0:fdea65150534 | 157 | |
tobyspark | 0:fdea65150534 | 158 | OSCClass::OSCClass(){ |
tobyspark | 0:fdea65150534 | 159 | udpRec.setOnEvent(this, &OSCClass::onUDPSocketEvent); |
tobyspark | 0:fdea65150534 | 160 | newMessage=false; |
tobyspark | 0:fdea65150534 | 161 | } |
tobyspark | 0:fdea65150534 | 162 | |
tobyspark | 1:63b72e393989 | 163 | OSCClass::~OSCClass(){ |
tobyspark | 1:63b72e393989 | 164 | udpSend.resetOnEvent(); |
tobyspark | 1:63b72e393989 | 165 | udpRec.close(); |
tobyspark | 1:63b72e393989 | 166 | } |
tobyspark | 1:63b72e393989 | 167 | |
tobyspark | 0:fdea65150534 | 168 | OSCClass::OSCClass(OSCMessage *_mes){ |
tobyspark | 0:fdea65150534 | 169 | udpRec.setOnEvent(this, &OSCClass::onUDPSocketEvent); |
tobyspark | 0:fdea65150534 | 170 | receiverMessage = _mes; // note: receiverMessage MUST be a pointer to the message, because we will modify things in it |
tobyspark | 0:fdea65150534 | 171 | newMessage=false; |
tobyspark | 0:fdea65150534 | 172 | } |
tobyspark | 0:fdea65150534 | 173 | |
tobyspark | 0:fdea65150534 | 174 | void OSCClass::begin() |
tobyspark | 0:fdea65150534 | 175 | { |
tobyspark | 0:fdea65150534 | 176 | // setup receiver udp socket: |
tobyspark | 0:fdea65150534 | 177 | udpRec.bind(receiverMessage->host); |
tobyspark | 0:fdea65150534 | 178 | } |
tobyspark | 0:fdea65150534 | 179 | |
tobyspark | 0:fdea65150534 | 180 | |
tobyspark | 0:fdea65150534 | 181 | void OSCClass::begin(uint16_t _recievePort) |
tobyspark | 0:fdea65150534 | 182 | { |
tobyspark | 0:fdea65150534 | 183 | receiverMessage->host.setPort(_recievePort); |
tobyspark | 0:fdea65150534 | 184 | // setup receiver udp socket: |
tobyspark | 0:fdea65150534 | 185 | udpRec.bind(receiverMessage->host); |
tobyspark | 0:fdea65150534 | 186 | } |
tobyspark | 0:fdea65150534 | 187 | |
tobyspark | 0:fdea65150534 | 188 | |
tobyspark | 0:fdea65150534 | 189 | void OSCClass::setReceiveMessage(OSCMessage *_mes){ |
tobyspark | 0:fdea65150534 | 190 | receiverMessage = _mes; |
tobyspark | 0:fdea65150534 | 191 | } |
tobyspark | 0:fdea65150534 | 192 | |
tobyspark | 0:fdea65150534 | 193 | void OSCClass::onUDPSocketEvent(UDPSocketEvent e) |
tobyspark | 0:fdea65150534 | 194 | { |
tobyspark | 0:fdea65150534 | 195 | switch(e) |
tobyspark | 0:fdea65150534 | 196 | { |
tobyspark | 0:fdea65150534 | 197 | case UDPSOCKET_READABLE: //The only event for now |
tobyspark | 0:fdea65150534 | 198 | //char buf[256] = {0}; |
tobyspark | 0:fdea65150534 | 199 | Host auxhost; |
tobyspark | 0:fdea65150534 | 200 | buflength = udpRec.recvfrom( rcvBuff, 256, &auxhost ); // QUESTION: auxhost should be equal to the receiver host I guess... |
tobyspark | 0:fdea65150534 | 201 | if ( buflength > 0 ) { |
tobyspark | 0:fdea65150534 | 202 | //printf("\r\nFrom %d.%d.%d.%d:\r\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]); |
tobyspark | 0:fdea65150534 | 203 | decodePacket(receiverMessage); // convert to OSC message, and save it in receiverMessage |
tobyspark | 0:fdea65150534 | 204 | newMessage=true; |
tobyspark | 0:fdea65150534 | 205 | |
tobyspark | 0:fdea65150534 | 206 | messageReceivedCallback.call(); |
tobyspark | 0:fdea65150534 | 207 | } |
tobyspark | 0:fdea65150534 | 208 | break; |
tobyspark | 0:fdea65150534 | 209 | } |
tobyspark | 0:fdea65150534 | 210 | } |
tobyspark | 0:fdea65150534 | 211 | |
tobyspark | 0:fdea65150534 | 212 | /* |
tobyspark | 0:fdea65150534 | 213 | Decode UDP packet and save it in the OSCMessage structure |
tobyspark | 0:fdea65150534 | 214 | */ |
tobyspark | 0:fdea65150534 | 215 | void OSCClass::decodePacket( OSCMessage *_mes) { |
tobyspark | 0:fdea65150534 | 216 | |
tobyspark | 0:fdea65150534 | 217 | //uint16_t lenBuff; |
tobyspark | 0:fdea65150534 | 218 | uint8_t d; |
tobyspark | 0:fdea65150534 | 219 | uint8_t messagePos=0; |
tobyspark | 0:fdea65150534 | 220 | uint8_t adrCount=0; |
tobyspark | 0:fdea65150534 | 221 | uint8_t adrMesPos=0; |
tobyspark | 0:fdea65150534 | 222 | uint8_t packetCount=0; |
tobyspark | 0:fdea65150534 | 223 | uint8_t packetPos=4; |
tobyspark | 0:fdea65150534 | 224 | |
tobyspark | 0:fdea65150534 | 225 | |
tobyspark | 0:fdea65150534 | 226 | //W5100.writeSn(socketNo, SnIR, SnIR::RECV); |
tobyspark | 0:fdea65150534 | 227 | //lenBuff=recvfrom(socketNo, rcvBuff, 1, receiverMessage->ip, &receiverMessage->port); |
tobyspark | 0:fdea65150534 | 228 | |
tobyspark | 0:fdea65150534 | 229 | receiverMessage->address[0]=tempAddress[0]; |
tobyspark | 0:fdea65150534 | 230 | |
tobyspark | 0:fdea65150534 | 231 | //(1) address process start ========================================= |
tobyspark | 0:fdea65150534 | 232 | do{ |
tobyspark | 0:fdea65150534 | 233 | d=rcvBuff[messagePos]; |
tobyspark | 0:fdea65150534 | 234 | |
tobyspark | 0:fdea65150534 | 235 | |
tobyspark | 0:fdea65150534 | 236 | if( (d=='/') && (messagePos>0) ){ |
tobyspark | 0:fdea65150534 | 237 | |
tobyspark | 0:fdea65150534 | 238 | if(adrCount<MAX_ADDRESS){ |
tobyspark | 0:fdea65150534 | 239 | tempAddress[adrCount][adrMesPos]=0; |
tobyspark | 0:fdea65150534 | 240 | |
tobyspark | 0:fdea65150534 | 241 | adrCount++; |
tobyspark | 0:fdea65150534 | 242 | adrMesPos=0; |
tobyspark | 0:fdea65150534 | 243 | |
tobyspark | 0:fdea65150534 | 244 | receiverMessage->address[adrCount]=tempAddress[adrCount]; |
tobyspark | 0:fdea65150534 | 245 | } |
tobyspark | 0:fdea65150534 | 246 | |
tobyspark | 0:fdea65150534 | 247 | } |
tobyspark | 0:fdea65150534 | 248 | |
tobyspark | 0:fdea65150534 | 249 | if(adrCount<MAX_ADDRESS){ |
tobyspark | 0:fdea65150534 | 250 | //Added this in to remove the slashes out of final output |
tobyspark | 0:fdea65150534 | 251 | if(d!='/'){ |
tobyspark | 0:fdea65150534 | 252 | tempAddress[adrCount][adrMesPos]=d; |
tobyspark | 0:fdea65150534 | 253 | |
tobyspark | 0:fdea65150534 | 254 | if(packetCount>3) { |
tobyspark | 0:fdea65150534 | 255 | packetCount=0; |
tobyspark | 0:fdea65150534 | 256 | packetPos+=4; |
tobyspark | 0:fdea65150534 | 257 | } |
tobyspark | 0:fdea65150534 | 258 | |
tobyspark | 0:fdea65150534 | 259 | adrMesPos++; |
tobyspark | 0:fdea65150534 | 260 | } |
tobyspark | 0:fdea65150534 | 261 | } |
tobyspark | 0:fdea65150534 | 262 | messagePos++; |
tobyspark | 0:fdea65150534 | 263 | packetCount++; |
tobyspark | 0:fdea65150534 | 264 | |
tobyspark | 0:fdea65150534 | 265 | }while(d!=0); |
tobyspark | 0:fdea65150534 | 266 | |
tobyspark | 0:fdea65150534 | 267 | |
tobyspark | 0:fdea65150534 | 268 | if(adrCount<MAX_ADDRESS) adrCount++; |
tobyspark | 0:fdea65150534 | 269 | receiverMessage->addressNum=adrCount; |
tobyspark | 0:fdea65150534 | 270 | |
tobyspark | 0:fdea65150534 | 271 | messagePos=packetPos; |
tobyspark | 0:fdea65150534 | 272 | |
tobyspark | 0:fdea65150534 | 273 | //(2) type tag process starts ========================================= |
tobyspark | 0:fdea65150534 | 274 | packetCount=0; |
tobyspark | 0:fdea65150534 | 275 | packetPos+=4; |
tobyspark | 0:fdea65150534 | 276 | |
tobyspark | 0:fdea65150534 | 277 | uint8_t typeTagPos=0; |
tobyspark | 0:fdea65150534 | 278 | uint8_t tempArgNum=0; |
tobyspark | 0:fdea65150534 | 279 | |
tobyspark | 0:fdea65150534 | 280 | while(rcvBuff[messagePos]!=0 ){ |
tobyspark | 0:fdea65150534 | 281 | |
tobyspark | 0:fdea65150534 | 282 | if(rcvBuff[messagePos] != ',') { |
tobyspark | 0:fdea65150534 | 283 | |
tobyspark | 0:fdea65150534 | 284 | if(typeTagPos<MAX_ARG){ |
tobyspark | 0:fdea65150534 | 285 | receiverMessage->typeTag[tempArgNum]=rcvBuff[messagePos]; |
tobyspark | 0:fdea65150534 | 286 | tempArgNum++; |
tobyspark | 0:fdea65150534 | 287 | } |
tobyspark | 0:fdea65150534 | 288 | typeTagPos++; |
tobyspark | 0:fdea65150534 | 289 | |
tobyspark | 0:fdea65150534 | 290 | } |
tobyspark | 0:fdea65150534 | 291 | |
tobyspark | 0:fdea65150534 | 292 | packetCount++; |
tobyspark | 0:fdea65150534 | 293 | |
tobyspark | 0:fdea65150534 | 294 | if(packetCount>3) { |
tobyspark | 0:fdea65150534 | 295 | packetCount=0; |
tobyspark | 0:fdea65150534 | 296 | packetPos+=4; |
tobyspark | 0:fdea65150534 | 297 | } |
tobyspark | 0:fdea65150534 | 298 | |
tobyspark | 0:fdea65150534 | 299 | messagePos++; |
tobyspark | 0:fdea65150534 | 300 | } |
tobyspark | 0:fdea65150534 | 301 | |
tobyspark | 0:fdea65150534 | 302 | receiverMessage->argNum=tempArgNum; |
tobyspark | 0:fdea65150534 | 303 | |
tobyspark | 0:fdea65150534 | 304 | messagePos=packetPos; |
tobyspark | 0:fdea65150534 | 305 | |
tobyspark | 0:fdea65150534 | 306 | //(3) tempArg process starts ========================================= |
tobyspark | 0:fdea65150534 | 307 | for(int i=0;i<tempArgNum;i++){ |
tobyspark | 0:fdea65150534 | 308 | |
tobyspark | 0:fdea65150534 | 309 | adrMesPos=3; |
tobyspark | 0:fdea65150534 | 310 | |
tobyspark | 0:fdea65150534 | 311 | receiverMessage->arg[i]=tempArg[i]; |
tobyspark | 0:fdea65150534 | 312 | |
tobyspark | 0:fdea65150534 | 313 | for(int j=0;j<4;j++){ |
tobyspark | 0:fdea65150534 | 314 | |
tobyspark | 0:fdea65150534 | 315 | tempArg[i][adrMesPos]=rcvBuff[messagePos]; |
tobyspark | 0:fdea65150534 | 316 | |
tobyspark | 0:fdea65150534 | 317 | messagePos++; |
tobyspark | 0:fdea65150534 | 318 | adrMesPos--; |
tobyspark | 0:fdea65150534 | 319 | } |
tobyspark | 0:fdea65150534 | 320 | |
tobyspark | 0:fdea65150534 | 321 | } |
tobyspark | 0:fdea65150534 | 322 | |
tobyspark | 0:fdea65150534 | 323 | |
tobyspark | 0:fdea65150534 | 324 | } |
tobyspark | 0:fdea65150534 | 325 | |
tobyspark | 0:fdea65150534 | 326 | |
tobyspark | 0:fdea65150534 | 327 | |
tobyspark | 0:fdea65150534 | 328 | OSCMessage * OSCClass::getMessage(){ |
tobyspark | 0:fdea65150534 | 329 | newMessage=false; // this indicate the user READ the message |
tobyspark | 0:fdea65150534 | 330 | return receiverMessage; |
tobyspark | 0:fdea65150534 | 331 | } |
tobyspark | 0:fdea65150534 | 332 | |
tobyspark | 0:fdea65150534 | 333 | |
tobyspark | 0:fdea65150534 | 334 | void OSCClass::sendOsc( OSCMessage *_mes ) |
tobyspark | 0:fdea65150534 | 335 | { |
tobyspark | 0:fdea65150534 | 336 | uint8_t lengthEnd; |
tobyspark | 0:fdea65150534 | 337 | uint8_t lengthStart; |
tobyspark | 0:fdea65150534 | 338 | char buff[128]; |
tobyspark | 0:fdea65150534 | 339 | |
tobyspark | 0:fdea65150534 | 340 | sendContainer = _mes; |
tobyspark | 0:fdea65150534 | 341 | |
tobyspark | 0:fdea65150534 | 342 | //バッファ初期値 |
tobyspark | 0:fdea65150534 | 343 | buff[0]=0; |
tobyspark | 0:fdea65150534 | 344 | |
tobyspark | 0:fdea65150534 | 345 | //1) Add name spaces: |
tobyspark | 0:fdea65150534 | 346 | for(int i=0;i<sendContainer->addressNum;i++){ |
tobyspark | 0:fdea65150534 | 347 | |
tobyspark | 0:fdea65150534 | 348 | strcat(buff,sendContainer->address[i]); // note: an address is for instance: "/test" (including the "/") |
tobyspark | 0:fdea65150534 | 349 | |
tobyspark | 0:fdea65150534 | 350 | } |
tobyspark | 0:fdea65150534 | 351 | |
tobyspark | 0:fdea65150534 | 352 | // pad with 0s to align in multiples of 4: |
tobyspark | 0:fdea65150534 | 353 | lengthStart=strlen(buff); |
tobyspark | 0:fdea65150534 | 354 | lengthEnd=lengthStart+(4-(lengthStart%4)); |
tobyspark | 0:fdea65150534 | 355 | for(int i=lengthStart ; i<lengthEnd; i++){ |
tobyspark | 0:fdea65150534 | 356 | buff[i]=0; |
tobyspark | 0:fdea65150534 | 357 | } |
tobyspark | 0:fdea65150534 | 358 | |
tobyspark | 0:fdea65150534 | 359 | lengthStart=lengthEnd; |
tobyspark | 0:fdea65150534 | 360 | |
tobyspark | 0:fdea65150534 | 361 | //2) Add TypeTag: |
tobyspark | 0:fdea65150534 | 362 | buff[lengthEnd++]=','; // Note: type tag is for instance: ",if" |
tobyspark | 0:fdea65150534 | 363 | for(int i=0;i<sendContainer->argNum;i++){ |
tobyspark | 0:fdea65150534 | 364 | buff[lengthEnd++]=sendContainer->typeTag[i]; |
tobyspark | 0:fdea65150534 | 365 | } |
tobyspark | 0:fdea65150534 | 366 | |
tobyspark | 0:fdea65150534 | 367 | // pad with 0s to align in multiples of 4: |
tobyspark | 0:fdea65150534 | 368 | lengthStart=lengthEnd; |
tobyspark | 0:fdea65150534 | 369 | lengthEnd=lengthStart+(4-(lengthStart%4)); |
tobyspark | 0:fdea65150534 | 370 | for(int i=lengthStart ; i<lengthEnd; i++){ |
tobyspark | 0:fdea65150534 | 371 | buff[i]=0; |
tobyspark | 0:fdea65150534 | 372 | } |
tobyspark | 0:fdea65150534 | 373 | |
tobyspark | 0:fdea65150534 | 374 | //3) add argument values (Note: here only big endian): |
tobyspark | 0:fdea65150534 | 375 | uint8_t *v; |
tobyspark | 0:fdea65150534 | 376 | for(int i=0;i<sendContainer->argNum;i++){ |
tobyspark | 0:fdea65150534 | 377 | uint8_t valuePos=3; |
tobyspark | 0:fdea65150534 | 378 | v=(uint8_t *)sendContainer->arg[i]; |
tobyspark | 0:fdea65150534 | 379 | |
tobyspark | 0:fdea65150534 | 380 | buff[lengthEnd++]=v[valuePos--]; |
tobyspark | 0:fdea65150534 | 381 | buff[lengthEnd++]=v[valuePos--]; |
tobyspark | 0:fdea65150534 | 382 | buff[lengthEnd++]=v[valuePos--]; |
tobyspark | 0:fdea65150534 | 383 | buff[lengthEnd++]=v[valuePos]; |
tobyspark | 0:fdea65150534 | 384 | |
tobyspark | 0:fdea65150534 | 385 | } |
tobyspark | 0:fdea65150534 | 386 | |
tobyspark | 0:fdea65150534 | 387 | //4) Send udp packet: |
tobyspark | 0:fdea65150534 | 388 | //sendto( socketNo, (uint8_t *)buff, lengthEnd, sendContainer->ip, sendContainer->port ); |
tobyspark | 0:fdea65150534 | 389 | udpSend.sendto(buff , lengthEnd, &(sendContainer->host)); |
tobyspark | 0:fdea65150534 | 390 | } |
tobyspark | 0:fdea65150534 | 391 | |
tobyspark | 0:fdea65150534 | 392 | |
tobyspark | 0:fdea65150534 | 393 | /* |
tobyspark | 0:fdea65150534 | 394 | flush a receive buffer |
tobyspark | 0:fdea65150534 | 395 | void OSCClass::flush() { |
tobyspark | 0:fdea65150534 | 396 | while ( available() ){} |
tobyspark | 0:fdea65150534 | 397 | } |
tobyspark | 0:fdea65150534 | 398 | */ |
tobyspark | 0:fdea65150534 | 399 | |
tobyspark | 0:fdea65150534 | 400 | |
tobyspark | 0:fdea65150534 | 401 | void OSCClass::stop() { |
tobyspark | 0:fdea65150534 | 402 | //close( socketNo ); |
tobyspark | 0:fdea65150534 | 403 | udpSend.resetOnEvent(); // disables callback |
tobyspark | 0:fdea65150534 | 404 | } |
tobyspark | 0:fdea65150534 | 405 | |
tobyspark | 0:fdea65150534 | 406 |