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: EthernetNetIf LPD8806 mbed
Fork of OSCReceiver by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "UDPSocket.h" 00004 #include "Control.h" 00005 00006 extern "C" void mbed_reset(); 00007 00008 //#define 00009 #define INPUT_PORT 5678 00010 00011 #ifdef DHCP 00012 EthernetNetIf eth; 00013 #else 00014 EthernetNetIf eth( 00015 IpAddr(192,168,12,210), //IP Address 00016 IpAddr(255,255,255,0), //Network Mask 00017 IpAddr(), //Gateway 00018 IpAddr() //DNS 00019 ); 00020 #endif 00021 00022 Serial pc(USBTX, USBRX); 00023 00024 LedControl ledControl; 00025 00026 DigitalOut led1(LED1); 00027 00028 //--- OSC related stuff --- 00029 union OSCarg { 00030 // char*, int and float are assumed four bytes 00031 char *address; 00032 char *typeTag; 00033 int i; 00034 float f; 00035 char *s; 00036 struct { 00037 int len; // is "int i" 00038 char *p; 00039 } blob; 00040 char m[4]; // for MIDI 00041 char _b[4]; // endian conversion temp variable 00042 }; 00043 00044 void getOSCmsg(char *packet , union OSCarg *msg){ 00045 // Caution: the returned result points to packet as blobs or strings (not newly allocatd) 00046 char *p, *typeTag; char c; 00047 00048 msg[0].address = packet; // address 00049 msg[1].typeTag = packet+4*(strlen(msg[0].s)/4+1);//typeTag 00050 typeTag=msg[1].s+1; // skip ',' 00051 p= msg[1].s+4*(strlen(msg[1].s)/4+1); 00052 for(int n=0; n<strlen(typeTag); n++){ 00053 c = typeTag[n]; 00054 if (('s'==c)) { 00055 msg[n+2].s=p; 00056 p += 4*(strlen(msg[n+2].s)/4+1); 00057 } else if (('i'==c)||('f'==c)) { 00058 // chang endian (big to little) 00059 msg[n+2]._b[3]=p[0]; 00060 msg[n+2]._b[2]=p[1]; 00061 msg[n+2]._b[1]=p[2]; 00062 msg[n+2]._b[0]=p[3]; 00063 p +=4; 00064 } else if ('b'==c) { 00065 // chang endian (big to little) 00066 // get lenth of blog (copy to msg[n].blog.len) 00067 msg[n+2]._b[3]=p[0]; 00068 msg[n+2]._b[2]=p[1]; 00069 msg[n+2]._b[1]=p[2]; 00070 msg[n+2]._b[0]=p[3]; 00071 p +=4; 00072 // get ponter of blog (copy to msg[n].blog.p) 00073 msg[n+2].blob.p=p; 00074 p += 4*(msg[n+2].blob.len/4+1); 00075 } else if ('m'==c) { 00076 // get midi data (copy to msg[n].m[]) 00077 msg[n+2].m[0]=p[0]; 00078 msg[n+2].m[1]=p[1]; 00079 msg[n+2].m[2]=p[2]; 00080 msg[n+2].m[3]=p[3]; 00081 p +=4; 00082 } else { 00083 printf("*** Not Supported TypeTag:%s ****\n",typeTag); 00084 } 00085 }; 00086 } 00087 //------------------------------------------- 00088 00089 UDPSocket udp; 00090 00091 void onUDPSocketEvent(UDPSocketEvent e) 00092 { 00093 union OSCarg msg[10]; 00094 00095 printf("receive."); 00096 00097 switch(e) 00098 { 00099 case UDPSOCKET_READABLE: //The only event for now 00100 char buf[256] = {0}; 00101 Host host; 00102 while( int len = udp.recvfrom( buf, 256, &host ) ) 00103 { 00104 if( len <= 0 ) 00105 break; 00106 printf("\r\nFrom %d.%d.%d.%d:\r\n", 00107 host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]); 00108 00109 getOSCmsg(buf,msg); 00110 00111 if(msg[2].i==101) 00112 { 00113 ledControl.colorChase(127,127,127,3);//(r,g,b,delay) 00114 00115 // colorChase(strip.Color(127,127,127), 3);//white 00116 // led = 1; 00117 // wait(0.2); 00118 // led = 0; 00119 printf("led blink complete."); 00120 } 00121 00122 printf("OSCmsg >>> %s %s %d\n", 00123 msg[0].address, msg[1].typeTag,msg[2].i); 00124 } 00125 break; 00126 } 00127 } 00128 00129 int main() 00130 { 00131 printf("Ethernet Setup..............................\r\n"); 00132 EthernetErr ethErr = eth.setup(); 00133 if(ethErr) 00134 { 00135 printf("Error %d in setup.\r\n", ethErr); 00136 return -1; 00137 } 00138 printf("Ethernet Setup Complete..............................\r\n"); 00139 00140 printf("Address&Port Setup..............................\r\n"); 00141 // port setup 00142 Host recHost(IpAddr(192, 168, 12, 210), INPUT_PORT, NULL); 00143 udp.setOnEvent(&onUDPSocketEvent); 00144 udp.bind(recHost); 00145 printf("Address&Port Setup Complete..............................\r\n"); 00146 00147 while(true) 00148 { 00149 Net::poll(); 00150 00151 printf("."); 00152 } 00153 }
Generated on Mon Aug 1 2022 22:54:19 by
