Ethernet Voice Streaming code

Dependencies:   WIZnetInterface mbed

Fork of UDPEchoServer_TEST by eunkyoung kim

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 //#define UDPServer 1
00005 #define UDPClient 1
00006 
00007 #if defined (UDPServer)
00008 #define MAC     "\x00\x08\xDC\x11\x34\x78"
00009 #define IP      "192.168.99.100"
00010 #define MASK    "255.255.255.0"
00011 #define GATEWAY "192.168.99.1"
00012 
00013 #else // UDPClient
00014 #define MAC     "\x00\x08\xDC\x11\x34\x90"
00015 #define IP      "192.168.99.101"
00016 #define MASK    "255.255.255.0"
00017 #define GATEWAY "192.168.99.1"
00018 #endif
00019 
00020 #define MAX_BUF_SIZE 128
00021 
00022 AnalogIn mic(A5);
00023 PwmOut trigger_o (D14);
00024 InterruptIn trigger_i (D15);
00025 DigitalIn button (A0);
00026 DigitalOut toggle (D0);
00027 DigitalOut sent (D1);
00028 PwmOut aout (D12);
00029 
00030 Serial pc (USBTX, USBRX);
00031 
00032 char ad_data_buf0[MAX_BUF_SIZE];
00033 char ad_data_buf1[MAX_BUF_SIZE];
00034 int i = 0;
00035 char buf_sel = 0;
00036 char send_udp = 0;
00037 
00038 int n; 
00039 
00040 const char* ECHO_SERVER_ADDRESS = "192.168.99.100";
00041 const int ECHO_SERVER_PORT = 3000;
00042 const int ECHO_CLIENT_PORT = 2000;
00043 
00044 void adcTickfunc() {
00045 # if defined (UDPServer)
00046     if (buf_sel) aout.write((float)ad_data_buf1[i]/256*1.32);
00047     else         aout.write((float)ad_data_buf0[i]/256*1.32);
00048     i++;
00049     
00050     if (i == MAX_BUF_SIZE) i = MAX_BUF_SIZE - 1;
00051 # else
00052     if (buf_sel == 0) ad_data_buf0[i] = mic.read_u16() >> 4;
00053     else              ad_data_buf1[i] = mic.read_u16() >> 4;
00054     i++;
00055     
00056     if (i == MAX_BUF_SIZE) { 
00057         i = 0;
00058         toggle = !toggle;
00059         if (buf_sel) buf_sel = 0; else buf_sel = 1;
00060         send_udp = 1;
00061         sent = 1;
00062     }
00063 #endif
00064 }    
00065 
00066 int main (void) {
00067     // baud setting
00068     pc.baud(460800);    
00069     trigger_i.rise (&adcTickfunc);
00070     
00071     EthernetInterface eth;
00072     eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
00073     printf("\r\nConnecting");
00074     eth.connect();
00075 #if defined UDPServer
00076     while (true) {  
00077         UDPSocket server;
00078         server.bind(ECHO_SERVER_PORT);
00079         Endpoint client;
00080         
00081         // Start acquisition (interval 100usec)
00082         trigger_o.period_us(100);
00083         trigger_o.write(0.50f);
00084         
00085         aout.period_us(50);
00086              
00087         while (true) {
00088             if (buf_sel) n = server.receiveFrom(client, ad_data_buf0, sizeof(ad_data_buf0));
00089             else         n = server.receiveFrom(client, ad_data_buf1, sizeof(ad_data_buf1));  
00090             printf ("\r\n%d",i);
00091             toggle = buf_sel;
00092             //receive_udp = receive_udp + 1;
00093             if (buf_sel) buf_sel = 0; else buf_sel = 1;
00094             i = 0;
00095             if (n <= 0) break;
00096         }
00097     }
00098 #else
00099     printf("\r\nClient IP Address is %s", eth.getIPAddress());
00100     UDPSocket sock;
00101     sock.init();
00102     sock.bind(ECHO_CLIENT_PORT);
00103     Endpoint echo_server;
00104     echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00105     printf("\r\nip= %s,port=%d",echo_server.get_address(),echo_server.get_port());
00106     
00107     // Start acquisition (interval 100usec)
00108     trigger_o.period_us(100);
00109     trigger_o.write(0.50f);
00110     
00111     while (1) {
00112         if (send_udp) {
00113             if (buf_sel == 1) sock.sendTo(echo_server, ad_data_buf0, MAX_BUF_SIZE);
00114             else              sock.sendTo(echo_server, ad_data_buf1, MAX_BUF_SIZE);
00115             send_udp = 0;
00116             sent = 0;
00117         }
00118     }
00119 #endif
00120 }