OSC meeseage tranceiver(Sender/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
main.cpp@1:e62251d890c1, 2014-09-03 (annotated)
- Committer:
- xshige
- Date:
- Wed Sep 03 13:12:02 2014 +0000
- Revision:
- 1:e62251d890c1
- Parent:
- 0:eadd1c588343
added supporting FRDM-K64F.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:eadd1c588343 | 1 | /* |
xshige | 0:eadd1c588343 | 2 | CC3000 OSC message tranceiver(Sender/Receiver) |
xshige | 0:eadd1c588343 | 3 | |
xshige | 0:eadd1c588343 | 4 | Sparkfun CC3000 WiFi Shield meets mbed! |
xshige | 0:eadd1c588343 | 5 | |
xshige | 0:eadd1c588343 | 6 | This program supports the following mbed boards with Sparkfun CC3000 Sheild. |
xshige | 0:eadd1c588343 | 7 | '#define' switches board.(refer to source code) |
xshige | 0:eadd1c588343 | 8 | (1)FRDM-KL25Z (K25Z) |
xshige | 0:eadd1c588343 | 9 | (2)ST Nucleo F401RE (STM32F401) |
xshige | 0:eadd1c588343 | 10 | (3)ST Nucleo F030R8 (STM32F030) |
xshige | 0:eadd1c588343 | 11 | (4)LPCXpresso1549 (LPC1549) |
xshige | 0:eadd1c588343 | 12 | (5)Seeduino-Arch-Pro (ARCH_PRO) |
xshige | 1:e62251d890c1 | 13 | (6)FRDM-K64F (K64F) |
xshige | 1:e62251d890c1 | 14 | |
xshige | 0:eadd1c588343 | 15 | Please push RESET button on CC3000 WiFi Shield to start the program. |
xshige | 0:eadd1c588343 | 16 | |
xshige | 0:eadd1c588343 | 17 | reference: |
xshige | 0:eadd1c588343 | 18 | https://www.sparkfun.com/products/12071 for CC300 Shield |
xshige | 0:eadd1c588343 | 19 | http://opensoundcontrol.org/introduction-osc for OSC(Open Sound Control) |
xshige | 0:eadd1c588343 | 20 | |
xshige | 1:e62251d890c1 | 21 | date: 2014/9/3 |
xshige | 1:e62251d890c1 | 22 | added supporting FRDM-K64F by: xshige |
xshige | 1:e62251d890c1 | 23 | |
xshige | 0:eadd1c588343 | 24 | date: 2014/8/31 |
xshige | 0:eadd1c588343 | 25 | written by: xshige |
xshige | 0:eadd1c588343 | 26 | |
xshige | 0:eadd1c588343 | 27 | console out sample: |
xshige | 0:eadd1c588343 | 28 | ========================================== |
xshige | 0:eadd1c588343 | 29 | CC3000 OSC receive&send demo. |
xshige | 0:eadd1c588343 | 30 | IP address: 192.168.0.3 |
xshige | 0:eadd1c588343 | 31 | This OSC msg sent: |
xshige | 0:eadd1c588343 | 32 | /1/fader1 ,f 0.000000 |
xshige | 0:eadd1c588343 | 33 | to: 192.168.0.11 |
xshige | 0:eadd1c588343 | 34 | ------------------- |
xshige | 0:eadd1c588343 | 35 | This OSC msg sent: |
xshige | 0:eadd1c588343 | 36 | /1/fader5 ,f 0.172000 |
xshige | 0:eadd1c588343 | 37 | to: 192.168.0.11 |
xshige | 0:eadd1c588343 | 38 | ------------------- |
xshige | 0:eadd1c588343 | 39 | |
xshige | 0:eadd1c588343 | 40 | Wait for OSC message... |
xshige | 0:eadd1c588343 | 41 | |
xshige | 0:eadd1c588343 | 42 | This OSC msg sent: |
xshige | 0:eadd1c588343 | 43 | /1/fader5 ,f 0.127000 |
xshige | 0:eadd1c588343 | 44 | to: 192.168.0.11 |
xshige | 0:eadd1c588343 | 45 | ------------------- |
xshige | 0:eadd1c588343 | 46 | |
xshige | 0:eadd1c588343 | 47 | Wait for OSC message... |
xshige | 0:eadd1c588343 | 48 | |
xshige | 0:eadd1c588343 | 49 | incomming OSC msg: |
xshige | 0:eadd1c588343 | 50 | /1/fader2 ,f 0.761603 |
xshige | 0:eadd1c588343 | 51 | Received packet from: 192.168.0.2 |
xshige | 0:eadd1c588343 | 52 | ------------------- |
xshige | 0:eadd1c588343 | 53 | This OSC msg sent: |
xshige | 0:eadd1c588343 | 54 | /1/fader5 ,f 0.711000 |
xshige | 0:eadd1c588343 | 55 | to: 192.168.0.2 |
xshige | 0:eadd1c588343 | 56 | ------------------- |
xshige | 0:eadd1c588343 | 57 | |
xshige | 0:eadd1c588343 | 58 | Wait for OSC message... |
xshige | 0:eadd1c588343 | 59 | |
xshige | 0:eadd1c588343 | 60 | incomming OSC msg: |
xshige | 0:eadd1c588343 | 61 | /1/fader2 ,f 0.767932 |
xshige | 0:eadd1c588343 | 62 | Received packet from: 192.168.0.2 |
xshige | 0:eadd1c588343 | 63 | ------------------- |
xshige | 0:eadd1c588343 | 64 | This OSC msg sent: |
xshige | 0:eadd1c588343 | 65 | /1/fader5 ,f 0.286000 |
xshige | 0:eadd1c588343 | 66 | to: 192.168.0.2 |
xshige | 0:eadd1c588343 | 67 | ------------------- |
xshige | 0:eadd1c588343 | 68 | |
xshige | 0:eadd1c588343 | 69 | Wait for OSC message... |
xshige | 0:eadd1c588343 | 70 | |
xshige | 0:eadd1c588343 | 71 | ========================================== |
xshige | 0:eadd1c588343 | 72 | */ |
xshige | 0:eadd1c588343 | 73 | |
xshige | 0:eadd1c588343 | 74 | #include "mbed.h" |
xshige | 0:eadd1c588343 | 75 | #include "cc3000.h" |
xshige | 0:eadd1c588343 | 76 | |
xshige | 0:eadd1c588343 | 77 | #include "UDPSocket.h" |
xshige | 0:eadd1c588343 | 78 | |
xshige | 0:eadd1c588343 | 79 | // define board you like (KL25Z, LPC1549, STM32F401, STM32F030 ...) |
xshige | 0:eadd1c588343 | 80 | //#define KL25Z |
xshige | 1:e62251d890c1 | 81 | //#define STM32F401 |
xshige | 0:eadd1c588343 | 82 | //#define STM32F030 |
xshige | 0:eadd1c588343 | 83 | //#define LPC1549 |
xshige | 0:eadd1c588343 | 84 | //#define ARCH_PRO |
xshige | 1:e62251d890c1 | 85 | #define K64F |
xshige | 0:eadd1c588343 | 86 | |
xshige | 0:eadd1c588343 | 87 | // define SSID, PASSWORD you like |
xshige | 0:eadd1c588343 | 88 | #define SSID "sssid" |
xshige | 0:eadd1c588343 | 89 | #define PASSWORD "password" |
xshige | 0:eadd1c588343 | 90 | |
xshige | 0:eadd1c588343 | 91 | using namespace mbed_cc3000; |
xshige | 0:eadd1c588343 | 92 | |
xshige | 0:eadd1c588343 | 93 | /* cc3000 module declaration specific for user's board. */ |
xshige | 0:eadd1c588343 | 94 | #if defined(KL25Z) |
xshige | 0:eadd1c588343 | 95 | // for KL25Z |
xshige | 0:eadd1c588343 | 96 | cc3000 wifi(PTD4, PTC9, PTD0, SPI(PTD2, PTD3, PTD1), SSID, PASSWORD, WPA2, false); |
xshige | 0:eadd1c588343 | 97 | Serial pc(USBTX, USBRX); |
xshige | 0:eadd1c588343 | 98 | #endif |
xshige | 0:eadd1c588343 | 99 | #if defined(STM32F401) |
xshige | 0:eadd1c588343 | 100 | // for Nucleo STM32F401 |
xshige | 0:eadd1c588343 | 101 | cc3000 wifi(PA_10, PA_8, PB_6, SPI(PA_7, PA_6, PA_5), SSID, PASSWORD, WPA2, false); |
xshige | 0:eadd1c588343 | 102 | // for Nucleo STM32F401 |
xshige | 0:eadd1c588343 | 103 | Serial pc(SERIAL_TX, SERIAL_RX); |
xshige | 0:eadd1c588343 | 104 | #endif |
xshige | 0:eadd1c588343 | 105 | #if defined(STM32F030) |
xshige | 0:eadd1c588343 | 106 | // for Nucleo STM32F030 |
xshige | 0:eadd1c588343 | 107 | cc3000 wifi(PA_10, PA_8, PB_6, SPI(PA_7, PA_6, PA_5), SSID, PASSWORD, WPA2, false); |
xshige | 0:eadd1c588343 | 108 | // for Nucleo STM32F030 |
xshige | 0:eadd1c588343 | 109 | Serial pc(SERIAL_TX, SERIAL_RX); |
xshige | 0:eadd1c588343 | 110 | #endif |
xshige | 0:eadd1c588343 | 111 | #if defined(LPC1549) |
xshige | 0:eadd1c588343 | 112 | // for LPC1549 |
xshige | 0:eadd1c588343 | 113 | cc3000 wifi(P0_29, P0_0, P0_27, SPI(P0_28, P0_12, P0_16), SSID, PASSWORD, WPA2, false); |
xshige | 0:eadd1c588343 | 114 | Serial pc(USBTX, USBRX); |
xshige | 0:eadd1c588343 | 115 | #endif |
xshige | 0:eadd1c588343 | 116 | #if defined(ARCH_PRO) |
xshige | 0:eadd1c588343 | 117 | // for Seeed Studio Arch Pro |
xshige | 0:eadd1c588343 | 118 | cc3000 wifi(P0_4, P2_5, P0_6, SPI(P0_9, P0_8, P0_7), SSID, PASSWORD, WPA2, false); |
xshige | 0:eadd1c588343 | 119 | Serial pc(USBTX, USBRX); |
xshige | 0:eadd1c588343 | 120 | #endif |
xshige | 1:e62251d890c1 | 121 | #if defined(K64F) |
xshige | 1:e62251d890c1 | 122 | // for FRDM-K64F |
xshige | 1:e62251d890c1 | 123 | cc3000 wifi(PTB9, PTC3, PTD0, SPI(PTD2, PTD3, PTD1), SSID, PASSWORD, WPA2, false); |
xshige | 1:e62251d890c1 | 124 | Serial pc(USBTX, USBRX); |
xshige | 1:e62251d890c1 | 125 | #endif |
xshige | 1:e62251d890c1 | 126 | |
xshige | 0:eadd1c588343 | 127 | |
xshige | 0:eadd1c588343 | 128 | // OSC related |
xshige | 0:eadd1c588343 | 129 | #include "OSCmsgCodec.h" |
xshige | 0:eadd1c588343 | 130 | |
xshige | 0:eadd1c588343 | 131 | // set yout IP, PORTs |
xshige | 0:eadd1c588343 | 132 | #define REMOTE_IP "192.168.0.11" // (will be replaced after OSC msg received) |
xshige | 0:eadd1c588343 | 133 | #define OUTGOING_PORT 9000 |
xshige | 0:eadd1c588343 | 134 | #define INCOMMING_PORT 8000 |
xshige | 0:eadd1c588343 | 135 | |
xshige | 0:eadd1c588343 | 136 | #define PACKET_SIZE 512 |
xshige | 0:eadd1c588343 | 137 | #define RECBUF_SIZE 256 |
xshige | 0:eadd1c588343 | 138 | #define PERIOD_BUZZER 10 |
xshige | 0:eadd1c588343 | 139 | |
xshige | 0:eadd1c588343 | 140 | union OSCarg msg[10], outmsg[10]; |
xshige | 0:eadd1c588343 | 141 | char recbuf[RECBUF_SIZE],buff[PACKET_SIZE],packet[PACKET_SIZE]; |
xshige | 0:eadd1c588343 | 142 | int len, tn; |
xshige | 0:eadd1c588343 | 143 | |
xshige | 0:eadd1c588343 | 144 | char sendto_ip[16]; |
xshige | 0:eadd1c588343 | 145 | |
xshige | 0:eadd1c588343 | 146 | int main() { |
xshige | 0:eadd1c588343 | 147 | // set initial IP to send |
xshige | 0:eadd1c588343 | 148 | strcpy(sendto_ip, REMOTE_IP); |
xshige | 0:eadd1c588343 | 149 | |
xshige | 0:eadd1c588343 | 150 | pc.baud(115200); |
xshige | 0:eadd1c588343 | 151 | |
xshige | 0:eadd1c588343 | 152 | printf("\n\nCC3000 OSC receive&send demo. \n"); |
xshige | 0:eadd1c588343 | 153 | wifi.init(); |
xshige | 0:eadd1c588343 | 154 | if (wifi.connect() == -1) { |
xshige | 0:eadd1c588343 | 155 | printf("Failed to connect. Please verify connection details and try again. \n"); |
xshige | 1:e62251d890c1 | 156 | printf("You should check your ssid, password.\n"); |
xshige | 1:e62251d890c1 | 157 | wait(60); |
xshige | 0:eadd1c588343 | 158 | } else { |
xshige | 0:eadd1c588343 | 159 | printf("IP address: %s \n", wifi.getIPAddress()); |
xshige | 0:eadd1c588343 | 160 | } |
xshige | 0:eadd1c588343 | 161 | |
xshige | 0:eadd1c588343 | 162 | UDPSocket udpRec,udpSend; |
xshige | 0:eadd1c588343 | 163 | udpSend.init(); udpRec.bind(INCOMMING_PORT); |
xshige | 0:eadd1c588343 | 164 | |
xshige | 0:eadd1c588343 | 165 | Endpoint target, client; |
xshige | 0:eadd1c588343 | 166 | target.set_address(REMOTE_IP, OUTGOING_PORT); |
xshige | 0:eadd1c588343 | 167 | |
xshige | 0:eadd1c588343 | 168 | // send OSC message with random values (to touchOSC) |
xshige | 0:eadd1c588343 | 169 | // make OSC message for sending |
xshige | 0:eadd1c588343 | 170 | outmsg[0].address="/1/fader1"; |
xshige | 0:eadd1c588343 | 171 | outmsg[1].typeTag=",f"; |
xshige | 0:eadd1c588343 | 172 | outmsg[2].f= 0.0; |
xshige | 0:eadd1c588343 | 173 | memset(packet,0,sizeof(packet)); // clear send buff for OSC msg |
xshige | 0:eadd1c588343 | 174 | len=encOSCmsg(packet,outmsg); |
xshige | 0:eadd1c588343 | 175 | // send it |
xshige | 0:eadd1c588343 | 176 | target.set_address(sendto_ip, OUTGOING_PORT); |
xshige | 0:eadd1c588343 | 177 | udpSend.sendTo(target, packet, len); |
xshige | 0:eadd1c588343 | 178 | pc.printf("This OSC msg sent:\n%s %s %f\n", outmsg[0].address, outmsg[1].typeTag, outmsg[2].f); |
xshige | 0:eadd1c588343 | 179 | pc.printf("to: %s\n", sendto_ip); |
xshige | 0:eadd1c588343 | 180 | pc.printf("-------------------\n"); |
xshige | 1:e62251d890c1 | 181 | //wait_ms(10); |
xshige | 0:eadd1c588343 | 182 | |
xshige | 0:eadd1c588343 | 183 | while (true) { |
xshige | 0:eadd1c588343 | 184 | #if 0 |
xshige | 0:eadd1c588343 | 185 | // xy |
xshige | 0:eadd1c588343 | 186 | // send OSC message with random values (to touchOSC) |
xshige | 0:eadd1c588343 | 187 | // make OSC message for sending |
xshige | 0:eadd1c588343 | 188 | outmsg[0].address="/3/xy"; |
xshige | 0:eadd1c588343 | 189 | outmsg[1].typeTag=",ff"; |
xshige | 0:eadd1c588343 | 190 | outmsg[2].f = rand()%1000/1000.0; |
xshige | 0:eadd1c588343 | 191 | outmsg[3].f = rand()%1000/1000.0; |
xshige | 0:eadd1c588343 | 192 | memset(packet,0,sizeof(packet)); // clear send buff for OSC msg |
xshige | 0:eadd1c588343 | 193 | len=encOSCmsg(packet,outmsg); |
xshige | 0:eadd1c588343 | 194 | target.set_address(sendto_ip, OUTGOING_PORT); |
xshige | 0:eadd1c588343 | 195 | udpSend.sendTo(target, packet, len); |
xshige | 0:eadd1c588343 | 196 | //pc.printf("len:%d\n",len); |
xshige | 0:eadd1c588343 | 197 | pc.printf("This OSC msg sent:\n%s %s %f %f\n", outmsg[0].address, outmsg[1].typeTag, outmsg[2].f, outmsg[3].f); |
xshige | 0:eadd1c588343 | 198 | pc.printf("to: %s\n", sendto_ip); |
xshige | 0:eadd1c588343 | 199 | pc.printf("-------------------\n"); |
xshige | 1:e62251d890c1 | 200 | //wait_ms(10); |
xshige | 0:eadd1c588343 | 201 | #else |
xshige | 0:eadd1c588343 | 202 | // fader |
xshige | 0:eadd1c588343 | 203 | // send OSC message with random values (to touchOSC) |
xshige | 0:eadd1c588343 | 204 | // make OSC message for sending |
xshige | 0:eadd1c588343 | 205 | outmsg[0].address="/1/fader5"; |
xshige | 0:eadd1c588343 | 206 | outmsg[1].typeTag=",f"; |
xshige | 0:eadd1c588343 | 207 | outmsg[2].f= rand()%1000/1000.0; |
xshige | 0:eadd1c588343 | 208 | memset(packet,0,sizeof(packet)); // clear send buff for OSC msg |
xshige | 0:eadd1c588343 | 209 | len=encOSCmsg(packet,outmsg); |
xshige | 0:eadd1c588343 | 210 | // send it |
xshige | 0:eadd1c588343 | 211 | target.set_address(sendto_ip, OUTGOING_PORT); |
xshige | 0:eadd1c588343 | 212 | udpSend.sendTo(target, packet, len); |
xshige | 0:eadd1c588343 | 213 | //pc.printf("len:%d\n",len); |
xshige | 0:eadd1c588343 | 214 | pc.printf("This OSC msg sent:\n%s %s %f\n", outmsg[0].address, outmsg[1].typeTag, outmsg[2].f); |
xshige | 0:eadd1c588343 | 215 | pc.printf("to: %s\n", sendto_ip); |
xshige | 0:eadd1c588343 | 216 | pc.printf("-------------------\n"); |
xshige | 1:e62251d890c1 | 217 | //wait_ms(10); |
xshige | 0:eadd1c588343 | 218 | #endif |
xshige | 0:eadd1c588343 | 219 | |
xshige | 0:eadd1c588343 | 220 | // receive OSC message |
xshige | 0:eadd1c588343 | 221 | printf("\nWait for OSC message...\n\n"); |
xshige | 0:eadd1c588343 | 222 | int n = udpRec.receiveFrom(client, recbuf, sizeof(recbuf)); |
xshige | 0:eadd1c588343 | 223 | if (n >= 0) { |
xshige | 0:eadd1c588343 | 224 | decOSCmsg(recbuf, msg); |
xshige | 0:eadd1c588343 | 225 | pc.printf("incomming OSC msg:\n%s %s", msg[0].address, msg[1].typeTag); |
xshige | 0:eadd1c588343 | 226 | for(int m=0; m < (strlen(msg[1].typeTag)-1); m++) { |
xshige | 0:eadd1c588343 | 227 | if (msg[1].typeTag[m+1]=='f') pc.printf(" %f",msg[m+2].f); |
xshige | 0:eadd1c588343 | 228 | if (msg[1].typeTag[m+1]=='i') pc.printf(" %d",msg[m+2].i); |
xshige | 0:eadd1c588343 | 229 | } |
xshige | 0:eadd1c588343 | 230 | pc.printf("\n"); |
xshige | 0:eadd1c588343 | 231 | printf("Received packet from: %s \n", client.get_address()); |
xshige | 0:eadd1c588343 | 232 | printf("-------------------\n"); |
xshige | 0:eadd1c588343 | 233 | // change IP to send |
xshige | 0:eadd1c588343 | 234 | strcpy(sendto_ip, client.get_address()); |
xshige | 1:e62251d890c1 | 235 | //wait_ms(10); |
xshige | 0:eadd1c588343 | 236 | }; |
xshige | 0:eadd1c588343 | 237 | } // while (true) |
xshige | 0:eadd1c588343 | 238 | } |