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.
mbed_osc.cpp
00001 /* 00002 * mbed_osc.cpp 00003 * 00004 * Created on: Mar 8, 2010 00005 * Author: pehr 00006 * The functions to control the mBed using OSC 00007 * This includes setting up the UDP and OSC subsystem 00008 * And registering handlers to control mBed I/O 00009 */ 00010 /********************************************************************************* 00011 00012 Copyright 2006-2009 MakingThings 00013 00014 Licensed under the Apache License, 00015 Version 2.0 (the "License"); you may not use this file except in compliance 00016 with the License. You may obtain a copy of the License at 00017 00018 http://www.apache.org/licenses/LICENSE-2.0 00019 00020 Unless required by applicable law or agreed to in writing, software distributed 00021 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 00022 CONDITIONS OF ANY KIND, either express or implied. See the License for 00023 the specific language governing permissions and limitations under the License. 00024 00025 *********************************************************************************/ 00026 #include "mbed_osc.h" 00027 00028 00029 //Subsystems 00030 #include "mbed_system_osc.h" 00031 #include "mbed_io_osc.h" 00032 //#include "mbed_analogin_osc.h" 00033 00034 #include <stdarg.h> 00035 00036 InterruptIn button(p5); 00037 00038 DigitalOut got_udp(LED1); 00039 Timeout got_udp_TO; 00040 void got_udp_off(){got_udp=0;} 00041 00042 DigitalOut sent_udp(LED4); 00043 Timeout sent_udp_TO; 00044 00045 void sent_udp_off(){sent_udp=0;} 00046 00047 00048 DigitalOut green_led(p14); 00049 Timeout green_led_TO; 00050 void green_led_off(){green_led=0;} 00051 DigitalOut green_led2(p18); 00052 00053 void sent_udp_lights(){ 00054 if(green_led==0){ 00055 green_led = 1; 00056 green_led_TO.attach(&green_led_off, 0.5); 00057 } 00058 if(sent_udp==0){ 00059 sent_udp = 1; 00060 sent_udp_TO.attach(&sent_udp_off, 1.0);//auto turn-off 00061 } 00062 } 00063 00064 void got_udp_lights(){ 00065 if(got_udp == 0){ 00066 got_udp = 1; //LED 00067 got_udp_TO.attach(&got_udp_off, 1.0);//auto turn-off 00068 } 00069 } 00070 00071 char scratch1[OSC_SCRATCH_SIZE]; //for building OSC messages 00072 NetServer *osc_net; 00073 00074 struct udp_pcb *osc_pcb; //Bound to receiving port, listening for messages 00075 00076 int lights = 0; //flash lights when we send/receive packets? 00077 int echo = 1; //echo received packets? 00078 00079 NetServer * getNetServer(){ //get the netserver to do things like get hostname 00080 return osc_net; 00081 } 00082 char * getScratch(){ 00083 return scratch1; 00084 } 00085 struct udp_pcb * getUdpPcb(){ 00086 return osc_pcb; 00087 } 00088 00089 /**UDP recv callback */ 00090 00091 void udp_recv_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) 00092 { 00093 LWIP_UNUSED_ARG(arg); 00094 // printf("Received UDP Packet on port %d\r\n",port); 00095 00096 if(lights) 00097 got_udp_lights(); 00098 /* if packet is valid */ 00099 if (p != NULL) { 00100 int length = (int)p->len; 00101 00102 // printf("UDP Packet Received! Payload:\r\n"); 00103 // printf("-- %s -- length: %d\r\n",static_cast<char *>(p->payload),length); 00104 00105 char *message = (char *)p->payload; 00106 00107 if(echo && *message!= '/'){ //Echo if its not OSC 00108 err_t code = udp_sendto(upcb, p, IP_ADDR_BROADCAST, UDP_BROADCAST_PORT); //send it back to port 5555 00109 printf("Echo'd non-OSC packet, result code is %d\r\n",code); 00110 if(lights) 00111 sent_udp_lights(); 00112 } 00113 00114 //Do something based on the packet... 00115 00116 OscChannel *ch = Osc_GetChannel(OSC_CHANNEL_UDP); 00117 00118 memcpy(ch->incoming, message,length); 00119 //Process packet 00120 00121 Osc_SetReplyAddress( OSC_CHANNEL_UDP, addr ); 00122 //this would send it back on the same port but we may not be listening on that port 00123 // Osc_SetReplyPort( OSC_CHANNEL_UDP, port ); 00124 Osc_SetReplyPort( OSC_CHANNEL_UDP, UDP_BROADCAST_PORT ); //this never changes 00125 Osc_ReceivePacket( OSC_CHANNEL_UDP, ch->incoming, length ); 00126 00127 /* must free the pbuf */ 00128 pbuf_free(p); 00129 00130 } 00131 } 00132 00133 void osc_sendIPBroadcast(){ 00134 00135 //broadcast IP and the port we are listening on 00136 00137 //Make sure it goes to broadcast 00138 00139 Osc_SetReplyAddress( OSC_CHANNEL_UDP, IP_ADDR_BROADCAST ); 00140 Osc_SetReplyPort( OSC_CHANNEL_UDP, UDP_BROADCAST_PORT ); 00141 00142 //Ensure that the property numbers have not changed 00143 SystemOsc_PropertyGet( 3, OSC_CHANNEL_UDP ); // /system/ipinfo 00144 Osc_SendPacket(OSC_CHANNEL_UDP); //have to manually send since we didnt go thru normal packet receiving 00145 //printf("Sent /system/ipinfo msg to broadcast IP and port\r\n"); 00146 00147 if(lights) 00148 sent_udp_lights(); 00149 } 00150 00151 /* 00152 * Initialize Mbed OSC 00153 * -Set up network interface 00154 * -Register UDP receive callback 00155 * -Set up OSC subsystem to handle received messages 00156 */ 00157 int osc_init(){ 00158 printf("Initializing Network Interface with DHCP\r\n"); 00159 00160 /*Initialize NetServer which gets us our DHCP address and 00161 gets the network interface ready 00162 and set up UDP 00163 */ 00164 osc_net = NetServer::ready(); 00165 //Initialize UDP 00166 osc_pcb = udp_new(); 00167 00168 if (osc_pcb != NULL) { 00169 /* we have to be allowed to send broadcast packets! */ 00170 osc_pcb->so_options |= SOF_BROADCAST; 00171 udp_bind(osc_pcb, IP_ADDR_ANY, UDP_RECV_PORT); //Receive from any IP address, on the specified port 00172 udp_recv(osc_pcb,udp_recv_callback, NULL); 00173 }else{ 00174 printf("Could not make UDP pcb\r\n"); 00175 return; 00176 } 00177 00178 osc_net->setHostname("chapala"); 00179 printf("Network Interface Initialized\r\n"); 00180 00181 //Now set up OSC system 00182 00183 //Configure OSC UDP Channel 00184 Osc_SystemInit(osc_pcb); 00185 00186 button.rise(&osc_sendIPBroadcast); //Callback to broadcast the IP and Port that it is listening on 00187 00188 //Register handlers for mBed control stuff 00189 00190 //SystemOsc subsystem -- get system info like IP address 00191 00192 int reg1 = Osc_RegisterSubsystem( SystemOsc_GetName(), SystemOsc_ReceiveMessage, NULL ); 00193 printf("Registered OscSystem, result is %d \r\n",reg1); 00194 00195 //IoOsc subsystem - get/set digital I/O pins 00196 int reg2 = Osc_RegisterSubsystem( IoOsc_GetName(), IoOsc_ReceiveMessage, NULL ); 00197 printf("Registered IoSystem, result is %d \r\n",reg2); 00198 00199 00200 //LedOsc subsystem - get/set onboard LED 00201 int reg3 = Osc_RegisterSubsystem(LedOsc_GetName(), LedOsc_ReceiveMessage, NULL ); 00202 printf("Registered LedSystem, result is %d \r\n",reg3); 00203 00204 //AnalogInOsc subsystem - read analog inputs 00205 // int reg4 = Osc_RegisterSubsystem(AnalogInOsc_GetName(),AnalogInOsc_ReceiveMessage, NULL ); 00206 // printf("Registered AnalogInSystem, result is %d \r\n",reg4); 00207 00208 //Broadcast the IP for anyone that is listening on the right port 00209 printf("Broadcasting IP on port %d\r\n",UDP_BROADCAST_PORT); 00210 osc_sendIPBroadcast(); 00211 } 00212 00213 /* Poll netserver for received messages 00214 * Must be called during main while(1) loop, very fast 00215 */ 00216 void osc_poll(){ 00217 osc_net->poll(); 00218 } 00219 00220
Generated on Wed Jul 13 2022 01:51:44 by
1.7.2