OSC meeseage receiver for Sparkfun CC3000 WiFi Shield. Supports the following boards: FRDM-KL25Z,ST Nucleo F401RE,ST Nucleo F030R8,LPCXpresso1549,Seeduino-Arch-Pro.

Dependencies:   cc3000_hostdriver_mbedsocket mbed

Committer:
xshige
Date:
Sun Aug 31 04:28:50 2014 +0000
Revision:
0:1c9ac526d377
OSC receiver(Sparkfun CC3000 WiFi Sheild meets mbed!) Supports the following boards with CC3000 Shield.; FRDM-KL25Z,ST Nucleo F401RE,ST Nucleo F030R8,LPCXpresso1549,Seeduino-Arch-Pro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xshige 0:1c9ac526d377 1 /*
xshige 0:1c9ac526d377 2 <----------------------------------------------------------------------------------
xshige 0:1c9ac526d377 3
xshige 0:1c9ac526d377 4 OSC message Codec(encoder/decoder)
xshige 0:1c9ac526d377 5
xshige 0:1c9ac526d377 6 version: 1.2 (2014/ 8/30) decoder bufix
xshige 0:1c9ac526d377 7 version: 1.1 (2013/11/20) support BIG_ENDIAN by #define
xshige 0:1c9ac526d377 8 version: 1.0 (2013/11/10)
xshige 0:1c9ac526d377 9
xshige 0:1c9ac526d377 10 Copyright (C) 2011,2013,2014 S.Komatsu
xshige 0:1c9ac526d377 11 released under the MIT License: http://mbed.org/license/mit
xshige 0:1c9ac526d377 12
xshige 0:1c9ac526d377 13 please refer to: http://opensoundcontrol.org/introduction-osc for OSC(Open Sound Control)
xshige 0:1c9ac526d377 14
xshige 0:1c9ac526d377 15 The followings are supported:
xshige 0:1c9ac526d377 16
xshige 0:1c9ac526d377 17 Features:
xshige 0:1c9ac526d377 18 Packet Parsing (Client)
xshige 0:1c9ac526d377 19 Packet Construction (Server)
xshige 0:1c9ac526d377 20 Bundle NOT Support
xshige 0:1c9ac526d377 21 Timetag NOT Support
xshige 0:1c9ac526d377 22
xshige 0:1c9ac526d377 23 Type Support:
xshige 0:1c9ac526d377 24 i: int32
xshige 0:1c9ac526d377 25 b: blob
xshige 0:1c9ac526d377 26 s: string
xshige 0:1c9ac526d377 27 f: float32
xshige 0:1c9ac526d377 28 m: MIDI message(port id, status byte, data1, data2) // I don't know the detail
xshige 0:1c9ac526d377 29
xshige 0:1c9ac526d377 30 Change Log:
xshige 0:1c9ac526d377 31 Bug(string length is not correct in encoding) Fix on 2013/11/10 (v0.9 -> v1.0)
xshige 0:1c9ac526d377 32
xshige 0:1c9ac526d377 33 >----------------------------------------------------------------------------------
xshige 0:1c9ac526d377 34 */
xshige 0:1c9ac526d377 35
xshige 0:1c9ac526d377 36 #include "OSCmsgCodec.h"
xshige 0:1c9ac526d377 37 //#define BIG_ENDIAN
xshige 0:1c9ac526d377 38
xshige 0:1c9ac526d377 39 int lenAlign4B(int len) {
xshige 0:1c9ac526d377 40 if ((len % 4) == 0) {return len; }
xshige 0:1c9ac526d377 41 else {return len+4-(len % 4);}
xshige 0:1c9ac526d377 42 }
xshige 0:1c9ac526d377 43
xshige 0:1c9ac526d377 44
xshige 0:1c9ac526d377 45 int encOSCmsg(char *packet , union OSCarg *msg){
xshige 0:1c9ac526d377 46 // output buffer must be cleared before call this
xshige 0:1c9ac526d377 47 char *p, *s, *d, *typeTag;
xshige 0:1c9ac526d377 48 char c;
xshige 0:1c9ac526d377 49
xshige 0:1c9ac526d377 50 p=packet;
xshige 0:1c9ac526d377 51 d=p;
xshige 0:1c9ac526d377 52 s=msg[0].address; // address
xshige 0:1c9ac526d377 53 for(int i=0; i<strlen(msg[0].address); i++) *d++ = *s++;
xshige 0:1c9ac526d377 54 *d=0; // terminator
xshige 0:1c9ac526d377 55 // p += 4*((strlen(msg[0].address)+1)/4+1);
xshige 0:1c9ac526d377 56 p += lenAlign4B(strlen(msg[0].address)+1);
xshige 0:1c9ac526d377 57 //
xshige 0:1c9ac526d377 58 s=msg[1].typeTag;
xshige 0:1c9ac526d377 59 d=p;
xshige 0:1c9ac526d377 60 for(int i=0; i<strlen(msg[1].typeTag); i++) *d++ = *s++;
xshige 0:1c9ac526d377 61 *d=0; // terminator
xshige 0:1c9ac526d377 62 // p += 4*((strlen(msg[1].s)+1)/4+1);
xshige 0:1c9ac526d377 63 p += lenAlign4B(strlen(msg[1].typeTag)+1);
xshige 0:1c9ac526d377 64 //
xshige 0:1c9ac526d377 65 typeTag=msg[1].s+1; // skip ','
xshige 0:1c9ac526d377 66 for(int n=0; n<strlen(typeTag); n++){
xshige 0:1c9ac526d377 67 c = typeTag[n];
xshige 0:1c9ac526d377 68 if (('s'==c)) {
xshige 0:1c9ac526d377 69 s=msg[n+2].s;
xshige 0:1c9ac526d377 70 d=p;
xshige 0:1c9ac526d377 71 for(int i=0; i<strlen(msg[n+2].s); i++) *d++ = *s++;
xshige 0:1c9ac526d377 72 *d=0; // terminater
xshige 0:1c9ac526d377 73 // p += 4*((strlen(msg[n+2].s)+1)/4+1);
xshige 0:1c9ac526d377 74 p += lenAlign4B(strlen(msg[n+2].s)+1);
xshige 0:1c9ac526d377 75 }
xshige 0:1c9ac526d377 76 else if (('i'==c)||('f'==c)) {
xshige 0:1c9ac526d377 77 #ifdef BIG_ENDIAN
xshige 0:1c9ac526d377 78 // no change endian (big to big)
xshige 0:1c9ac526d377 79 p[0]=msg[n+2]._b[0];
xshige 0:1c9ac526d377 80 p[1]=msg[n+2]._b[1];
xshige 0:1c9ac526d377 81 p[2]=msg[n+2]._b[2];
xshige 0:1c9ac526d377 82 p[3]=msg[n+2]._b[3];
xshige 0:1c9ac526d377 83 #else
xshige 0:1c9ac526d377 84 // change endian (little to big)
xshige 0:1c9ac526d377 85 p[0]=msg[n+2]._b[3];
xshige 0:1c9ac526d377 86 p[1]=msg[n+2]._b[2];
xshige 0:1c9ac526d377 87 p[2]=msg[n+2]._b[1];
xshige 0:1c9ac526d377 88 p[3]=msg[n+2]._b[0];
xshige 0:1c9ac526d377 89 #endif
xshige 0:1c9ac526d377 90 p +=4;
xshige 0:1c9ac526d377 91 }
xshige 0:1c9ac526d377 92 else if ('b'==c) {
xshige 0:1c9ac526d377 93 // put length of blog
xshige 0:1c9ac526d377 94 #ifdef BIG_ENDIAN
xshige 0:1c9ac526d377 95 // no change endian (big to big)
xshige 0:1c9ac526d377 96 p[0]=msg[n+2]._b[0];
xshige 0:1c9ac526d377 97 p[1]=msg[n+2]._b[1];
xshige 0:1c9ac526d377 98 p[2]=msg[n+2]._b[2];
xshige 0:1c9ac526d377 99 p[3]=msg[n+2]._b[3];
xshige 0:1c9ac526d377 100 #else
xshige 0:1c9ac526d377 101 // change endian (little to big)
xshige 0:1c9ac526d377 102 p[0]=msg[n+2]._b[3];
xshige 0:1c9ac526d377 103 p[1]=msg[n+2]._b[2];
xshige 0:1c9ac526d377 104 p[2]=msg[n+2]._b[1];
xshige 0:1c9ac526d377 105 p[3]=msg[n+2]._b[0];
xshige 0:1c9ac526d377 106 #endif
xshige 0:1c9ac526d377 107 p +=4;
xshige 0:1c9ac526d377 108 // get ponter of blog (copy to msg[n].blog.p)
xshige 0:1c9ac526d377 109 s=msg[n+2].blob.p;
xshige 0:1c9ac526d377 110 d=p;
xshige 0:1c9ac526d377 111 for(int i=0; i<msg[n+2].blob.len; i++) *d++ = *s++;
xshige 0:1c9ac526d377 112 p += 4*(msg[n+2].blob.len/4+1);
xshige 0:1c9ac526d377 113 }
xshige 0:1c9ac526d377 114 else if ('m'==c) {
xshige 0:1c9ac526d377 115 // get midi data (copy to msg[n].m[])
xshige 0:1c9ac526d377 116 p[0]=msg[n+2].m[0];
xshige 0:1c9ac526d377 117 p[1]=msg[n+2].m[1];
xshige 0:1c9ac526d377 118 p[2]=msg[n+2].m[2];
xshige 0:1c9ac526d377 119 p[3]=msg[n+2].m[3];
xshige 0:1c9ac526d377 120 p +=4;
xshige 0:1c9ac526d377 121 }
xshige 0:1c9ac526d377 122 else {
xshige 0:1c9ac526d377 123 //printf("*** Not Supported TypeTag:%s ****\n",typeTag);
xshige 0:1c9ac526d377 124 }
xshige 0:1c9ac526d377 125 };
xshige 0:1c9ac526d377 126 return (p-packet); // return packet size
xshige 0:1c9ac526d377 127 };
xshige 0:1c9ac526d377 128
xshige 0:1c9ac526d377 129 void decOSCmsg(char *packet , union OSCarg *msg){
xshige 0:1c9ac526d377 130 // Caution: the returned result points to packet as blobs or strings (not newly allocatd)
xshige 0:1c9ac526d377 131 char *p, *typeTag;
xshige 0:1c9ac526d377 132 char c; int n;
xshige 0:1c9ac526d377 133
xshige 0:1c9ac526d377 134 msg[0].address = packet; // address
xshige 0:1c9ac526d377 135 msg[1].typeTag = packet+4*((strlen(msg[0].s)+1)/4+1);//typeTag
xshige 0:1c9ac526d377 136 typeTag=msg[1].s+1; // skip ','
xshige 0:1c9ac526d377 137
xshige 0:1c9ac526d377 138 // bugfix 2014/8/30
xshige 0:1c9ac526d377 139 if (strlen(typeTag)%2 == 0) p= msg[1].s+4*((strlen(msg[1].s)+1)/4);
xshige 0:1c9ac526d377 140 else p= msg[1].s+4*((strlen(msg[1].s)+1)/4+1);
xshige 0:1c9ac526d377 141
xshige 0:1c9ac526d377 142 for(n=0; n<strlen(typeTag); n++){
xshige 0:1c9ac526d377 143 c = typeTag[n];
xshige 0:1c9ac526d377 144 if (('s'==c)) {
xshige 0:1c9ac526d377 145 msg[n+2].s=p;
xshige 0:1c9ac526d377 146 //p += 4*((strlen(msg[n+2].s)+1)/4+1);
xshige 0:1c9ac526d377 147 p += lenAlign4B(strlen(msg[n+2].s)+1);
xshige 0:1c9ac526d377 148 }
xshige 0:1c9ac526d377 149 else if (('i'==c)||('f'==c)) {
xshige 0:1c9ac526d377 150 #ifdef BIG_ENDIAN
xshige 0:1c9ac526d377 151 // no change endian (big to big)
xshige 0:1c9ac526d377 152 msg[n+2]._b[0]=p[0];
xshige 0:1c9ac526d377 153 msg[n+2]._b[1]=p[1];
xshige 0:1c9ac526d377 154 msg[n+2]._b[2]=p[2];
xshige 0:1c9ac526d377 155 msg[n+2]._b[3]=p[3];
xshige 0:1c9ac526d377 156 #else
xshige 0:1c9ac526d377 157 // change endian (big to little)
xshige 0:1c9ac526d377 158 msg[n+2]._b[3]=p[0];
xshige 0:1c9ac526d377 159 msg[n+2]._b[2]=p[1];
xshige 0:1c9ac526d377 160 msg[n+2]._b[1]=p[2];
xshige 0:1c9ac526d377 161 msg[n+2]._b[0]=p[3];
xshige 0:1c9ac526d377 162 #endif
xshige 0:1c9ac526d377 163 p +=4;
xshige 0:1c9ac526d377 164 }
xshige 0:1c9ac526d377 165 else if ('b'==c) {
xshige 0:1c9ac526d377 166 // get lenth of blog (copy to msg[n].blog.len)
xshige 0:1c9ac526d377 167 #ifdef BIG_ENDIAN
xshige 0:1c9ac526d377 168 // no change endian (big to big)
xshige 0:1c9ac526d377 169 msg[n+2]._b[0]=p[0];
xshige 0:1c9ac526d377 170 msg[n+2]._b[1]=p[1];
xshige 0:1c9ac526d377 171 msg[n+2]._b[2]=p[2];
xshige 0:1c9ac526d377 172 msg[n+2]._b[3]=p[3];
xshige 0:1c9ac526d377 173 #else
xshige 0:1c9ac526d377 174 // change endian (big to little)
xshige 0:1c9ac526d377 175 msg[n+2]._b[3]=p[0];
xshige 0:1c9ac526d377 176 msg[n+2]._b[2]=p[1];
xshige 0:1c9ac526d377 177 msg[n+2]._b[1]=p[2];
xshige 0:1c9ac526d377 178 msg[n+2]._b[0]=p[3];
xshige 0:1c9ac526d377 179 #endif
xshige 0:1c9ac526d377 180 p +=4;
xshige 0:1c9ac526d377 181 // get ponter of blog (copy to msg[n].blog.p)
xshige 0:1c9ac526d377 182 msg[n+2].blob.p=p;
xshige 0:1c9ac526d377 183 //p += 4*(msg[n+2].blob.len/4+1);
xshige 0:1c9ac526d377 184 p += lenAlign4B(msg[n+2].blob.len+1);
xshige 0:1c9ac526d377 185 }
xshige 0:1c9ac526d377 186 else if ('m'==c) {
xshige 0:1c9ac526d377 187 // get midi data (copy to msg[n].m[])
xshige 0:1c9ac526d377 188 msg[n+2].m[0]=p[0];
xshige 0:1c9ac526d377 189 msg[n+2].m[1]=p[1];
xshige 0:1c9ac526d377 190 msg[n+2].m[2]=p[2];
xshige 0:1c9ac526d377 191 msg[n+2].m[3]=p[3];
xshige 0:1c9ac526d377 192 p +=4;
xshige 0:1c9ac526d377 193 }
xshige 0:1c9ac526d377 194 else {
xshige 0:1c9ac526d377 195 //printf("*** Not Supported TypeTag:%s ****\n",typeTag);
xshige 0:1c9ac526d377 196 }
xshige 0:1c9ac526d377 197 };
xshige 0:1c9ac526d377 198 };