![](/media/cache/group/WIZnet_logo_001.jpg.50x50_q85.jpg)
Ethernet Voice Streaming code
Dependencies: WIZnetInterface mbed
Fork of UDPEchoServer_TEST by
main.cpp@9:2593d6010881, 2015-08-25 (annotated)
- Committer:
- WizLeo
- Date:
- Tue Aug 25 12:55:31 2015 +0000
- Revision:
- 9:2593d6010881
- Parent:
- 8:47e9d7df0582
Voice Streaming Project source code
; MIC / SPK / PWMOUT / UDP / Ethernet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:3e54841149df | 1 | #include "mbed.h" |
emilmont | 1:3f409cd0bede | 2 | #include "EthernetInterface.h" |
eunkyoungkim | 7:345b078c448a | 3 | |
eunkyoungkim | 7:345b078c448a | 4 | //#define UDPServer 1 |
WizLeo | 9:2593d6010881 | 5 | #define UDPClient 1 |
WizLeo | 9:2593d6010881 | 6 | |
WizLeo | 9:2593d6010881 | 7 | #if defined (UDPServer) |
WizLeo | 9:2593d6010881 | 8 | #define MAC "\x00\x08\xDC\x11\x34\x78" |
WizLeo | 9:2593d6010881 | 9 | #define IP "192.168.99.100" |
WizLeo | 9:2593d6010881 | 10 | #define MASK "255.255.255.0" |
WizLeo | 9:2593d6010881 | 11 | #define GATEWAY "192.168.99.1" |
WizLeo | 9:2593d6010881 | 12 | |
WizLeo | 9:2593d6010881 | 13 | #else // UDPClient |
WizLeo | 9:2593d6010881 | 14 | #define MAC "\x00\x08\xDC\x11\x34\x90" |
WizLeo | 9:2593d6010881 | 15 | #define IP "192.168.99.101" |
WizLeo | 9:2593d6010881 | 16 | #define MASK "255.255.255.0" |
WizLeo | 9:2593d6010881 | 17 | #define GATEWAY "192.168.99.1" |
WizLeo | 9:2593d6010881 | 18 | #endif |
WizLeo | 9:2593d6010881 | 19 | |
WizLeo | 9:2593d6010881 | 20 | #define MAX_BUF_SIZE 128 |
eunkyoungkim | 7:345b078c448a | 21 | |
WizLeo | 9:2593d6010881 | 22 | AnalogIn mic(A5); |
WizLeo | 9:2593d6010881 | 23 | PwmOut trigger_o (D14); |
WizLeo | 9:2593d6010881 | 24 | InterruptIn trigger_i (D15); |
WizLeo | 9:2593d6010881 | 25 | DigitalIn button (A0); |
WizLeo | 9:2593d6010881 | 26 | DigitalOut toggle (D0); |
WizLeo | 9:2593d6010881 | 27 | DigitalOut sent (D1); |
WizLeo | 9:2593d6010881 | 28 | PwmOut aout (D12); |
WizLeo | 9:2593d6010881 | 29 | |
WizLeo | 9:2593d6010881 | 30 | Serial pc (USBTX, USBRX); |
WizLeo | 9:2593d6010881 | 31 | |
WizLeo | 9:2593d6010881 | 32 | char ad_data_buf0[MAX_BUF_SIZE]; |
WizLeo | 9:2593d6010881 | 33 | char ad_data_buf1[MAX_BUF_SIZE]; |
WizLeo | 9:2593d6010881 | 34 | int i = 0; |
WizLeo | 9:2593d6010881 | 35 | char buf_sel = 0; |
WizLeo | 9:2593d6010881 | 36 | char send_udp = 0; |
WizLeo | 9:2593d6010881 | 37 | |
WizLeo | 9:2593d6010881 | 38 | int n; |
WizLeo | 9:2593d6010881 | 39 | |
eunkyoungkim | 7:345b078c448a | 40 | const char* ECHO_SERVER_ADDRESS = "192.168.99.100"; |
eunkyoungkim | 8:47e9d7df0582 | 41 | const int ECHO_SERVER_PORT = 3000; |
eunkyoungkim | 8:47e9d7df0582 | 42 | const int ECHO_CLIENT_PORT = 2000; |
eunkyoungkim | 7:345b078c448a | 43 | |
WizLeo | 9:2593d6010881 | 44 | void adcTickfunc() { |
WizLeo | 9:2593d6010881 | 45 | # if defined (UDPServer) |
WizLeo | 9:2593d6010881 | 46 | if (buf_sel) aout.write((float)ad_data_buf1[i]/256*1.32); |
WizLeo | 9:2593d6010881 | 47 | else aout.write((float)ad_data_buf0[i]/256*1.32); |
WizLeo | 9:2593d6010881 | 48 | i++; |
WizLeo | 9:2593d6010881 | 49 | |
WizLeo | 9:2593d6010881 | 50 | if (i == MAX_BUF_SIZE) i = MAX_BUF_SIZE - 1; |
WizLeo | 9:2593d6010881 | 51 | # else |
WizLeo | 9:2593d6010881 | 52 | if (buf_sel == 0) ad_data_buf0[i] = mic.read_u16() >> 4; |
WizLeo | 9:2593d6010881 | 53 | else ad_data_buf1[i] = mic.read_u16() >> 4; |
WizLeo | 9:2593d6010881 | 54 | i++; |
WizLeo | 9:2593d6010881 | 55 | |
WizLeo | 9:2593d6010881 | 56 | if (i == MAX_BUF_SIZE) { |
WizLeo | 9:2593d6010881 | 57 | i = 0; |
WizLeo | 9:2593d6010881 | 58 | toggle = !toggle; |
WizLeo | 9:2593d6010881 | 59 | if (buf_sel) buf_sel = 0; else buf_sel = 1; |
WizLeo | 9:2593d6010881 | 60 | send_udp = 1; |
WizLeo | 9:2593d6010881 | 61 | sent = 1; |
WizLeo | 9:2593d6010881 | 62 | } |
WizLeo | 9:2593d6010881 | 63 | #endif |
WizLeo | 9:2593d6010881 | 64 | } |
WizLeo | 9:2593d6010881 | 65 | |
WizLeo | 9:2593d6010881 | 66 | int main (void) { |
WizLeo | 9:2593d6010881 | 67 | // baud setting |
WizLeo | 9:2593d6010881 | 68 | pc.baud(460800); |
WizLeo | 9:2593d6010881 | 69 | trigger_i.rise (&adcTickfunc); |
WizLeo | 9:2593d6010881 | 70 | |
emilmont | 1:3f409cd0bede | 71 | EthernetInterface eth; |
eunkyoungkim | 7:345b078c448a | 72 | eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway |
WizLeo | 9:2593d6010881 | 73 | printf("\r\nConnecting"); |
emilmont | 1:3f409cd0bede | 74 | eth.connect(); |
WizLeo | 9:2593d6010881 | 75 | #if defined UDPServer |
WizLeo | 9:2593d6010881 | 76 | while (true) { |
eunkyoungkim | 7:345b078c448a | 77 | UDPSocket server; |
eunkyoungkim | 7:345b078c448a | 78 | server.bind(ECHO_SERVER_PORT); |
eunkyoungkim | 7:345b078c448a | 79 | Endpoint client; |
WizLeo | 9:2593d6010881 | 80 | |
WizLeo | 9:2593d6010881 | 81 | // Start acquisition (interval 100usec) |
WizLeo | 9:2593d6010881 | 82 | trigger_o.period_us(100); |
WizLeo | 9:2593d6010881 | 83 | trigger_o.write(0.50f); |
WizLeo | 9:2593d6010881 | 84 | |
WizLeo | 9:2593d6010881 | 85 | aout.period_us(50); |
WizLeo | 9:2593d6010881 | 86 | |
eunkyoungkim | 7:345b078c448a | 87 | while (true) { |
WizLeo | 9:2593d6010881 | 88 | if (buf_sel) n = server.receiveFrom(client, ad_data_buf0, sizeof(ad_data_buf0)); |
WizLeo | 9:2593d6010881 | 89 | else n = server.receiveFrom(client, ad_data_buf1, sizeof(ad_data_buf1)); |
WizLeo | 9:2593d6010881 | 90 | printf ("\r\n%d",i); |
WizLeo | 9:2593d6010881 | 91 | toggle = buf_sel; |
WizLeo | 9:2593d6010881 | 92 | //receive_udp = receive_udp + 1; |
WizLeo | 9:2593d6010881 | 93 | if (buf_sel) buf_sel = 0; else buf_sel = 1; |
WizLeo | 9:2593d6010881 | 94 | i = 0; |
eunkyoungkim | 7:345b078c448a | 95 | if (n <= 0) break; |
eunkyoungkim | 7:345b078c448a | 96 | } |
emilmont | 1:3f409cd0bede | 97 | } |
WizLeo | 9:2593d6010881 | 98 | #else |
WizLeo | 9:2593d6010881 | 99 | printf("\r\nClient IP Address is %s", eth.getIPAddress()); |
eunkyoungkim | 7:345b078c448a | 100 | UDPSocket sock; |
eunkyoungkim | 7:345b078c448a | 101 | sock.init(); |
eunkyoungkim | 8:47e9d7df0582 | 102 | sock.bind(ECHO_CLIENT_PORT); |
eunkyoungkim | 7:345b078c448a | 103 | Endpoint echo_server; |
eunkyoungkim | 7:345b078c448a | 104 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
WizLeo | 9:2593d6010881 | 105 | printf("\r\nip= %s,port=%d",echo_server.get_address(),echo_server.get_port()); |
eunkyoungkim | 7:345b078c448a | 106 | |
WizLeo | 9:2593d6010881 | 107 | // Start acquisition (interval 100usec) |
WizLeo | 9:2593d6010881 | 108 | trigger_o.period_us(100); |
WizLeo | 9:2593d6010881 | 109 | trigger_o.write(0.50f); |
eunkyoungkim | 7:345b078c448a | 110 | |
WizLeo | 9:2593d6010881 | 111 | while (1) { |
WizLeo | 9:2593d6010881 | 112 | if (send_udp) { |
WizLeo | 9:2593d6010881 | 113 | if (buf_sel == 1) sock.sendTo(echo_server, ad_data_buf0, MAX_BUF_SIZE); |
WizLeo | 9:2593d6010881 | 114 | else sock.sendTo(echo_server, ad_data_buf1, MAX_BUF_SIZE); |
WizLeo | 9:2593d6010881 | 115 | send_udp = 0; |
WizLeo | 9:2593d6010881 | 116 | sent = 0; |
WizLeo | 9:2593d6010881 | 117 | } |
WizLeo | 9:2593d6010881 | 118 | } |
eunkyoungkim | 7:345b078c448a | 119 | #endif |
emilmont | 1:3f409cd0bede | 120 | } |