Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.
mbed_osc.cpp@0:439354122597, 2010-03-17 (annotated)
- Committer:
- pehrhovey
- Date:
- Wed Mar 17 03:17:38 2010 +0000
- Revision:
- 0:439354122597
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:439354122597 | 1 | /* |
pehrhovey | 0:439354122597 | 2 | * mbed_osc.cpp |
pehrhovey | 0:439354122597 | 3 | * |
pehrhovey | 0:439354122597 | 4 | * Created on: Mar 8, 2010 |
pehrhovey | 0:439354122597 | 5 | * Author: pehr |
pehrhovey | 0:439354122597 | 6 | * The functions to control the mBed using OSC |
pehrhovey | 0:439354122597 | 7 | * This includes setting up the UDP and OSC subsystem |
pehrhovey | 0:439354122597 | 8 | * And registering handlers to control mBed I/O |
pehrhovey | 0:439354122597 | 9 | */ |
pehrhovey | 0:439354122597 | 10 | /********************************************************************************* |
pehrhovey | 0:439354122597 | 11 | |
pehrhovey | 0:439354122597 | 12 | Copyright 2006-2009 MakingThings |
pehrhovey | 0:439354122597 | 13 | |
pehrhovey | 0:439354122597 | 14 | Licensed under the Apache License, |
pehrhovey | 0:439354122597 | 15 | Version 2.0 (the "License"); you may not use this file except in compliance |
pehrhovey | 0:439354122597 | 16 | with the License. You may obtain a copy of the License at |
pehrhovey | 0:439354122597 | 17 | |
pehrhovey | 0:439354122597 | 18 | http://www.apache.org/licenses/LICENSE-2.0 |
pehrhovey | 0:439354122597 | 19 | |
pehrhovey | 0:439354122597 | 20 | Unless required by applicable law or agreed to in writing, software distributed |
pehrhovey | 0:439354122597 | 21 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
pehrhovey | 0:439354122597 | 22 | CONDITIONS OF ANY KIND, either express or implied. See the License for |
pehrhovey | 0:439354122597 | 23 | the specific language governing permissions and limitations under the License. |
pehrhovey | 0:439354122597 | 24 | |
pehrhovey | 0:439354122597 | 25 | *********************************************************************************/ |
pehrhovey | 0:439354122597 | 26 | #include "mbed_osc.h" |
pehrhovey | 0:439354122597 | 27 | |
pehrhovey | 0:439354122597 | 28 | |
pehrhovey | 0:439354122597 | 29 | //Subsystems |
pehrhovey | 0:439354122597 | 30 | #include "mbed_system_osc.h" |
pehrhovey | 0:439354122597 | 31 | #include "mbed_io_osc.h" |
pehrhovey | 0:439354122597 | 32 | //#include "mbed_analogin_osc.h" |
pehrhovey | 0:439354122597 | 33 | |
pehrhovey | 0:439354122597 | 34 | #include <stdarg.h> |
pehrhovey | 0:439354122597 | 35 | |
pehrhovey | 0:439354122597 | 36 | InterruptIn button(p5); |
pehrhovey | 0:439354122597 | 37 | |
pehrhovey | 0:439354122597 | 38 | DigitalOut got_udp(LED1); |
pehrhovey | 0:439354122597 | 39 | Timeout got_udp_TO; |
pehrhovey | 0:439354122597 | 40 | void got_udp_off(){got_udp=0;} |
pehrhovey | 0:439354122597 | 41 | |
pehrhovey | 0:439354122597 | 42 | DigitalOut sent_udp(LED4); |
pehrhovey | 0:439354122597 | 43 | Timeout sent_udp_TO; |
pehrhovey | 0:439354122597 | 44 | |
pehrhovey | 0:439354122597 | 45 | void sent_udp_off(){sent_udp=0;} |
pehrhovey | 0:439354122597 | 46 | |
pehrhovey | 0:439354122597 | 47 | |
pehrhovey | 0:439354122597 | 48 | DigitalOut green_led(p14); |
pehrhovey | 0:439354122597 | 49 | Timeout green_led_TO; |
pehrhovey | 0:439354122597 | 50 | void green_led_off(){green_led=0;} |
pehrhovey | 0:439354122597 | 51 | DigitalOut green_led2(p18); |
pehrhovey | 0:439354122597 | 52 | |
pehrhovey | 0:439354122597 | 53 | void sent_udp_lights(){ |
pehrhovey | 0:439354122597 | 54 | if(green_led==0){ |
pehrhovey | 0:439354122597 | 55 | green_led = 1; |
pehrhovey | 0:439354122597 | 56 | green_led_TO.attach(&green_led_off, 0.5); |
pehrhovey | 0:439354122597 | 57 | } |
pehrhovey | 0:439354122597 | 58 | if(sent_udp==0){ |
pehrhovey | 0:439354122597 | 59 | sent_udp = 1; |
pehrhovey | 0:439354122597 | 60 | sent_udp_TO.attach(&sent_udp_off, 1.0);//auto turn-off |
pehrhovey | 0:439354122597 | 61 | } |
pehrhovey | 0:439354122597 | 62 | } |
pehrhovey | 0:439354122597 | 63 | |
pehrhovey | 0:439354122597 | 64 | void got_udp_lights(){ |
pehrhovey | 0:439354122597 | 65 | if(got_udp == 0){ |
pehrhovey | 0:439354122597 | 66 | got_udp = 1; //LED |
pehrhovey | 0:439354122597 | 67 | got_udp_TO.attach(&got_udp_off, 1.0);//auto turn-off |
pehrhovey | 0:439354122597 | 68 | } |
pehrhovey | 0:439354122597 | 69 | } |
pehrhovey | 0:439354122597 | 70 | |
pehrhovey | 0:439354122597 | 71 | char scratch1[OSC_SCRATCH_SIZE]; //for building OSC messages |
pehrhovey | 0:439354122597 | 72 | NetServer *osc_net; |
pehrhovey | 0:439354122597 | 73 | |
pehrhovey | 0:439354122597 | 74 | struct udp_pcb *osc_pcb; //Bound to receiving port, listening for messages |
pehrhovey | 0:439354122597 | 75 | |
pehrhovey | 0:439354122597 | 76 | int lights = 0; //flash lights when we send/receive packets? |
pehrhovey | 0:439354122597 | 77 | int echo = 1; //echo received packets? |
pehrhovey | 0:439354122597 | 78 | |
pehrhovey | 0:439354122597 | 79 | NetServer * getNetServer(){ //get the netserver to do things like get hostname |
pehrhovey | 0:439354122597 | 80 | return osc_net; |
pehrhovey | 0:439354122597 | 81 | } |
pehrhovey | 0:439354122597 | 82 | char * getScratch(){ |
pehrhovey | 0:439354122597 | 83 | return scratch1; |
pehrhovey | 0:439354122597 | 84 | } |
pehrhovey | 0:439354122597 | 85 | struct udp_pcb * getUdpPcb(){ |
pehrhovey | 0:439354122597 | 86 | return osc_pcb; |
pehrhovey | 0:439354122597 | 87 | } |
pehrhovey | 0:439354122597 | 88 | |
pehrhovey | 0:439354122597 | 89 | /**UDP recv callback */ |
pehrhovey | 0:439354122597 | 90 | |
pehrhovey | 0:439354122597 | 91 | void udp_recv_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) |
pehrhovey | 0:439354122597 | 92 | { |
pehrhovey | 0:439354122597 | 93 | LWIP_UNUSED_ARG(arg); |
pehrhovey | 0:439354122597 | 94 | // printf("Received UDP Packet on port %d\r\n",port); |
pehrhovey | 0:439354122597 | 95 | |
pehrhovey | 0:439354122597 | 96 | if(lights) |
pehrhovey | 0:439354122597 | 97 | got_udp_lights(); |
pehrhovey | 0:439354122597 | 98 | /* if packet is valid */ |
pehrhovey | 0:439354122597 | 99 | if (p != NULL) { |
pehrhovey | 0:439354122597 | 100 | int length = (int)p->len; |
pehrhovey | 0:439354122597 | 101 | |
pehrhovey | 0:439354122597 | 102 | // printf("UDP Packet Received! Payload:\r\n"); |
pehrhovey | 0:439354122597 | 103 | // printf("-- %s -- length: %d\r\n",static_cast<char *>(p->payload),length); |
pehrhovey | 0:439354122597 | 104 | |
pehrhovey | 0:439354122597 | 105 | char *message = (char *)p->payload; |
pehrhovey | 0:439354122597 | 106 | |
pehrhovey | 0:439354122597 | 107 | if(echo && *message!= '/'){ //Echo if its not OSC |
pehrhovey | 0:439354122597 | 108 | err_t code = udp_sendto(upcb, p, IP_ADDR_BROADCAST, UDP_BROADCAST_PORT); //send it back to port 5555 |
pehrhovey | 0:439354122597 | 109 | printf("Echo'd non-OSC packet, result code is %d\r\n",code); |
pehrhovey | 0:439354122597 | 110 | if(lights) |
pehrhovey | 0:439354122597 | 111 | sent_udp_lights(); |
pehrhovey | 0:439354122597 | 112 | } |
pehrhovey | 0:439354122597 | 113 | |
pehrhovey | 0:439354122597 | 114 | //Do something based on the packet... |
pehrhovey | 0:439354122597 | 115 | |
pehrhovey | 0:439354122597 | 116 | OscChannel *ch = Osc_GetChannel(OSC_CHANNEL_UDP); |
pehrhovey | 0:439354122597 | 117 | |
pehrhovey | 0:439354122597 | 118 | memcpy(ch->incoming, message,length); |
pehrhovey | 0:439354122597 | 119 | //Process packet |
pehrhovey | 0:439354122597 | 120 | |
pehrhovey | 0:439354122597 | 121 | Osc_SetReplyAddress( OSC_CHANNEL_UDP, addr ); |
pehrhovey | 0:439354122597 | 122 | //this would send it back on the same port but we may not be listening on that port |
pehrhovey | 0:439354122597 | 123 | // Osc_SetReplyPort( OSC_CHANNEL_UDP, port ); |
pehrhovey | 0:439354122597 | 124 | Osc_SetReplyPort( OSC_CHANNEL_UDP, UDP_BROADCAST_PORT ); //this never changes |
pehrhovey | 0:439354122597 | 125 | Osc_ReceivePacket( OSC_CHANNEL_UDP, ch->incoming, length ); |
pehrhovey | 0:439354122597 | 126 | |
pehrhovey | 0:439354122597 | 127 | /* must free the pbuf */ |
pehrhovey | 0:439354122597 | 128 | pbuf_free(p); |
pehrhovey | 0:439354122597 | 129 | |
pehrhovey | 0:439354122597 | 130 | } |
pehrhovey | 0:439354122597 | 131 | } |
pehrhovey | 0:439354122597 | 132 | |
pehrhovey | 0:439354122597 | 133 | void osc_sendIPBroadcast(){ |
pehrhovey | 0:439354122597 | 134 | |
pehrhovey | 0:439354122597 | 135 | //broadcast IP and the port we are listening on |
pehrhovey | 0:439354122597 | 136 | |
pehrhovey | 0:439354122597 | 137 | //Make sure it goes to broadcast |
pehrhovey | 0:439354122597 | 138 | |
pehrhovey | 0:439354122597 | 139 | Osc_SetReplyAddress( OSC_CHANNEL_UDP, IP_ADDR_BROADCAST ); |
pehrhovey | 0:439354122597 | 140 | Osc_SetReplyPort( OSC_CHANNEL_UDP, UDP_BROADCAST_PORT ); |
pehrhovey | 0:439354122597 | 141 | |
pehrhovey | 0:439354122597 | 142 | //Ensure that the property numbers have not changed |
pehrhovey | 0:439354122597 | 143 | SystemOsc_PropertyGet( 3, OSC_CHANNEL_UDP ); // /system/ipinfo |
pehrhovey | 0:439354122597 | 144 | Osc_SendPacket(OSC_CHANNEL_UDP); //have to manually send since we didnt go thru normal packet receiving |
pehrhovey | 0:439354122597 | 145 | //printf("Sent /system/ipinfo msg to broadcast IP and port\r\n"); |
pehrhovey | 0:439354122597 | 146 | |
pehrhovey | 0:439354122597 | 147 | if(lights) |
pehrhovey | 0:439354122597 | 148 | sent_udp_lights(); |
pehrhovey | 0:439354122597 | 149 | } |
pehrhovey | 0:439354122597 | 150 | |
pehrhovey | 0:439354122597 | 151 | /* |
pehrhovey | 0:439354122597 | 152 | * Initialize Mbed OSC |
pehrhovey | 0:439354122597 | 153 | * -Set up network interface |
pehrhovey | 0:439354122597 | 154 | * -Register UDP receive callback |
pehrhovey | 0:439354122597 | 155 | * -Set up OSC subsystem to handle received messages |
pehrhovey | 0:439354122597 | 156 | */ |
pehrhovey | 0:439354122597 | 157 | int osc_init(){ |
pehrhovey | 0:439354122597 | 158 | printf("Initializing Network Interface with DHCP\r\n"); |
pehrhovey | 0:439354122597 | 159 | |
pehrhovey | 0:439354122597 | 160 | /*Initialize NetServer which gets us our DHCP address and |
pehrhovey | 0:439354122597 | 161 | gets the network interface ready |
pehrhovey | 0:439354122597 | 162 | and set up UDP |
pehrhovey | 0:439354122597 | 163 | */ |
pehrhovey | 0:439354122597 | 164 | osc_net = NetServer::ready(); |
pehrhovey | 0:439354122597 | 165 | //Initialize UDP |
pehrhovey | 0:439354122597 | 166 | osc_pcb = udp_new(); |
pehrhovey | 0:439354122597 | 167 | |
pehrhovey | 0:439354122597 | 168 | if (osc_pcb != NULL) { |
pehrhovey | 0:439354122597 | 169 | /* we have to be allowed to send broadcast packets! */ |
pehrhovey | 0:439354122597 | 170 | osc_pcb->so_options |= SOF_BROADCAST; |
pehrhovey | 0:439354122597 | 171 | udp_bind(osc_pcb, IP_ADDR_ANY, UDP_RECV_PORT); //Receive from any IP address, on the specified port |
pehrhovey | 0:439354122597 | 172 | udp_recv(osc_pcb,udp_recv_callback, NULL); |
pehrhovey | 0:439354122597 | 173 | }else{ |
pehrhovey | 0:439354122597 | 174 | printf("Could not make UDP pcb\r\n"); |
pehrhovey | 0:439354122597 | 175 | return; |
pehrhovey | 0:439354122597 | 176 | } |
pehrhovey | 0:439354122597 | 177 | |
pehrhovey | 0:439354122597 | 178 | osc_net->setHostname("chapala"); |
pehrhovey | 0:439354122597 | 179 | printf("Network Interface Initialized\r\n"); |
pehrhovey | 0:439354122597 | 180 | |
pehrhovey | 0:439354122597 | 181 | //Now set up OSC system |
pehrhovey | 0:439354122597 | 182 | |
pehrhovey | 0:439354122597 | 183 | //Configure OSC UDP Channel |
pehrhovey | 0:439354122597 | 184 | Osc_SystemInit(osc_pcb); |
pehrhovey | 0:439354122597 | 185 | |
pehrhovey | 0:439354122597 | 186 | button.rise(&osc_sendIPBroadcast); //Callback to broadcast the IP and Port that it is listening on |
pehrhovey | 0:439354122597 | 187 | |
pehrhovey | 0:439354122597 | 188 | //Register handlers for mBed control stuff |
pehrhovey | 0:439354122597 | 189 | |
pehrhovey | 0:439354122597 | 190 | //SystemOsc subsystem -- get system info like IP address |
pehrhovey | 0:439354122597 | 191 | |
pehrhovey | 0:439354122597 | 192 | int reg1 = Osc_RegisterSubsystem( SystemOsc_GetName(), SystemOsc_ReceiveMessage, NULL ); |
pehrhovey | 0:439354122597 | 193 | printf("Registered OscSystem, result is %d \r\n",reg1); |
pehrhovey | 0:439354122597 | 194 | |
pehrhovey | 0:439354122597 | 195 | //IoOsc subsystem - get/set digital I/O pins |
pehrhovey | 0:439354122597 | 196 | int reg2 = Osc_RegisterSubsystem( IoOsc_GetName(), IoOsc_ReceiveMessage, NULL ); |
pehrhovey | 0:439354122597 | 197 | printf("Registered IoSystem, result is %d \r\n",reg2); |
pehrhovey | 0:439354122597 | 198 | |
pehrhovey | 0:439354122597 | 199 | |
pehrhovey | 0:439354122597 | 200 | //LedOsc subsystem - get/set onboard LED |
pehrhovey | 0:439354122597 | 201 | int reg3 = Osc_RegisterSubsystem(LedOsc_GetName(), LedOsc_ReceiveMessage, NULL ); |
pehrhovey | 0:439354122597 | 202 | printf("Registered LedSystem, result is %d \r\n",reg3); |
pehrhovey | 0:439354122597 | 203 | |
pehrhovey | 0:439354122597 | 204 | //AnalogInOsc subsystem - read analog inputs |
pehrhovey | 0:439354122597 | 205 | // int reg4 = Osc_RegisterSubsystem(AnalogInOsc_GetName(),AnalogInOsc_ReceiveMessage, NULL ); |
pehrhovey | 0:439354122597 | 206 | // printf("Registered AnalogInSystem, result is %d \r\n",reg4); |
pehrhovey | 0:439354122597 | 207 | |
pehrhovey | 0:439354122597 | 208 | //Broadcast the IP for anyone that is listening on the right port |
pehrhovey | 0:439354122597 | 209 | printf("Broadcasting IP on port %d\r\n",UDP_BROADCAST_PORT); |
pehrhovey | 0:439354122597 | 210 | osc_sendIPBroadcast(); |
pehrhovey | 0:439354122597 | 211 | } |
pehrhovey | 0:439354122597 | 212 | |
pehrhovey | 0:439354122597 | 213 | /* Poll netserver for received messages |
pehrhovey | 0:439354122597 | 214 | * Must be called during main while(1) loop, very fast |
pehrhovey | 0:439354122597 | 215 | */ |
pehrhovey | 0:439354122597 | 216 | void osc_poll(){ |
pehrhovey | 0:439354122597 | 217 | osc_net->poll(); |
pehrhovey | 0:439354122597 | 218 | } |
pehrhovey | 0:439354122597 | 219 | |
pehrhovey | 0:439354122597 | 220 |