Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NetServices TextLCD mbed
OSCReceiver.cpp
00001 // 00002 // OSC Receiver 00003 // 00004 // date: 2011/1/10 00005 // version: 0.7 00006 // written by: xshige 00007 // 00008 // please find OSC Sender program at the bottom of this file 00009 // 00010 /* 00011 The followings are supported: 00012 00013 Transport Type: 00014 UDP 00015 00016 Features: 00017 Packet Parsing (Client) 00018 Not Supported:Packet Construction (Server) 00019 Bundle NOT Support 00020 Timetag NOT Support 00021 00022 Type Support: 00023 i: int32 00024 b: blob 00025 s: string 00026 f: float32 00027 m: MIDI message(port id, status byte, data1, data2) // I don't know the detail 00028 00029 */ 00030 00031 00032 #include "mbed.h" 00033 #include "OSCReceiver.h" // Add by RadioJunkBox 2012/05/16 00034 00035 /* comment out by RadioJunkBox 2012/05/16 00036 00037 #include "EthernetNetIf.h" 00038 #include "UDPSocket.h" 00039 00040 #define DHCP 00041 00042 //#define INPUT_PORT 12000 00043 #define INPUT_PORT 12345 00044 00045 #ifdef DHCP 00046 EthernetNetIf eth; 00047 #else 00048 EthernetNetIf eth( 00049 IpAddr(192,168,0,25), //IP Address 00050 IpAddr(255,255,255,0), //Network Mask 00051 IpAddr(192,168,0,1), //Gateway 00052 IpAddr(192,168,0,1) //DNS 00053 ); 00054 #endif 00055 //--- OSC related stuff --- 00056 union OSCarg { 00057 // char*, int and float are assumed four bytes 00058 char *address; 00059 char *typeTag; 00060 int i; 00061 float f; 00062 char *s; 00063 struct { 00064 int len; // is "int i" 00065 char *p; 00066 } blob; 00067 char m[4]; // for MIDI 00068 char _b[4]; // endian conversion temp variable 00069 }; 00070 */ 00071 void getOSCmsg(char *packet , union OSCarg *msg){ 00072 // Caution: the returned result points to packet as blobs or strings (not newly allocatd) 00073 char *p, *typeTag; char c; 00074 00075 msg[0].address = packet; // address 00076 msg[1].typeTag = packet+4*(strlen(msg[0].s)/4+1);//typeTag 00077 typeTag=msg[1].s+1; // skip ',' 00078 p= msg[1].s+4*(strlen(msg[1].s)/4+1); 00079 for(int n=0; n<strlen(typeTag); n++){ 00080 c = typeTag[n]; 00081 if (('s'==c)) { 00082 msg[n+2].s=p; 00083 p += 4*(strlen(msg[n+2].s)/4+1); 00084 } else if (('i'==c)||('f'==c)) { 00085 // chang endian (big to little) 00086 msg[n+2]._b[3]=p[0]; 00087 msg[n+2]._b[2]=p[1]; 00088 msg[n+2]._b[1]=p[2]; 00089 msg[n+2]._b[0]=p[3]; 00090 p +=4; 00091 } else if ('b'==c) { 00092 // chang endian (big to little) 00093 // get lenth of blog (copy to msg[n].blog.len) 00094 msg[n+2]._b[3]=p[0]; 00095 msg[n+2]._b[2]=p[1]; 00096 msg[n+2]._b[1]=p[2]; 00097 msg[n+2]._b[0]=p[3]; 00098 p +=4; 00099 // get ponter of blog (copy to msg[n].blog.p) 00100 msg[n+2].blob.p=p; 00101 p += 4*(msg[n+2].blob.len/4+1); 00102 } else if ('m'==c) { 00103 // get midi data (copy to msg[n].m[]) 00104 msg[n+2].m[0]=p[0]; 00105 msg[n+2].m[1]=p[1]; 00106 msg[n+2].m[2]=p[2]; 00107 msg[n+2].m[3]=p[3]; 00108 p +=4; 00109 } else { 00110 printf("*** Not Supported TypeTag:%s ****\n",typeTag); 00111 } 00112 }; 00113 00114 } 00115 //------------------------------------------- 00116 00117 /* comment out RadioJunkBox 2012/05/06 00118 00119 UDPSocket udp; 00120 00121 00122 void onUDPSocketEvent(UDPSocketEvent e) 00123 { 00124 union OSCarg msg[10]; 00125 00126 switch(e) 00127 { 00128 case UDPSOCKET_READABLE: //The only event for now 00129 char buf[256] = {0}; 00130 Host host; 00131 while( int len = udp.recvfrom( buf, 256, &host ) ) 00132 { 00133 if( len <= 0 ) 00134 break; 00135 printf("\r\nFrom %d.%d.%d.%d:\r\n", 00136 host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]); 00137 00138 getOSCmsg(buf,msg); 00139 // address pattern samples 00140 if (strcmp(msg[0].address,"/test")==0) { 00141 printf("OSCmsg: %s %s %d %f %s %d\n", 00142 msg[0].address, msg[1].typeTag,msg[2].i, msg[3].f, msg[4].s, msg[5].blob.len); 00143 printf("blob content:\n"); 00144 char *p=msg[5].blob.p; 00145 for(int n=0; n<msg[5].blob.len; p++,n++) printf(" %02X",(unsigned char)(*p)); 00146 printf("\n"); 00147 break; 00148 } 00149 if (strcmp(msg[0].address,"/kb/m")==0) { 00150 printf("OSCmsg: %s %s %d %d %d\n", 00151 msg[0].address, msg[1].typeTag,msg[2].i, msg[3].i, msg[4].i); 00152 break; 00153 } 00154 if (strcmp(msg[0].address,"/cc/m")==0) { 00155 printf("OSCmsg: %s %s %d %d %d\n", 00156 msg[0].address, msg[1].typeTag,msg[2].i, msg[3].i, msg[4].i); 00157 break; 00158 } 00159 if (strcmp(msg[0].address,"/osc/padx")==0) { 00160 printf("OSCmsg: %s %s %f\n", 00161 msg[0].address, msg[1].typeTag,msg[2].f); 00162 break; 00163 } 00164 if (strcmp(msg[0].address,"/osc/pady")==0) { 00165 printf("OSCmsg: %s %s %f\n", 00166 msg[0].address, msg[1].typeTag, msg[2].f); 00167 break; 00168 } 00169 if (strcmp(msg[0].address,"/osc/button1")==0) { 00170 printf("OSCmsg: %s %s %i\n", 00171 msg[0].address, msg[1].typeTag, msg[2].i); 00172 break; 00173 } 00174 if (strcmp(msg[0].address,"/osc/button2")==0) { 00175 printf("OSCmsg: %s %s %i\n", 00176 msg[0].address, msg[1].typeTag, msg[2].i); 00177 break; 00178 } 00179 if (strcmp(msg[0].address,"/mouse/dragged")==0) { 00180 printf("OSCmsg: %s %s %i %i\n", 00181 msg[0].address, msg[1].typeTag, msg[2].i, msg[3].i); 00182 break; 00183 } 00184 if (strcmp(msg[0].address,"/mouse/pressed")==0) { 00185 printf("OSCmsg: %s %s %i %i\n", 00186 msg[0].address, msg[1].typeTag, msg[2].i, msg[3].i); 00187 break; 00188 } 00189 if (strcmp(msg[0].address,"/1/xy")==0) { 00190 printf("OSCmsg: %s %s %f %f %d\n", 00191 msg[0].address, msg[1].typeTag, msg[2].f, msg[3].f, msg[4].i); 00192 break; 00193 } 00194 printf("undefined OSCmsg:%s %s\n",msg[0].address, msg[1].typeTag); 00195 } // while 00196 break; 00197 } // case 00198 } 00199 00200 int main() { 00201 // make debug port Fast 00202 // Serial pc(USBTX, USBRX); 00203 // pc.baud(9600); 00204 // pc.baud(115200); 00205 // pc.baud(230400); 00206 00207 printf("Setting up...\r\n"); 00208 EthernetErr ethErr = eth.setup(); 00209 if(ethErr) 00210 { 00211 printf("Error %d in setup.\r\n", ethErr); 00212 return -1; 00213 } 00214 printf("Setup OK\r\n"); 00215 00216 00217 // port setup 00218 // Host recHost(IpAddr(192, 168, 0, 7), INPUT_PORT, NULL); 00219 Host broadcast(IpAddr(eth.getIp()[0], eth.getIp()[1], eth.getIp()[2], 255), INPUT_PORT, NULL); 00220 udp.setOnEvent(&onUDPSocketEvent); 00221 udp.bind(broadcast); 00222 00223 Timer tmr; 00224 tmr.start(); 00225 while(true) 00226 { 00227 Net::poll(); 00228 if(tmr.read() > 5) 00229 { 00230 tmr.reset(); 00231 } 00232 } 00233 00234 } 00235 */
Generated on Tue Jul 12 2022 17:52:31 by
1.7.2