Internet piano with Updated SDFileSystem
Dependencies: SDFileSystem WIZnetInterface httpServer mbed
Fork of HTTPWebServer-WIZwiki-W750023 by
main.cpp@13:a09c9ff30460, 2015-06-29 (annotated)
- Committer:
- hjjeon
- Date:
- Mon Jun 29 09:40:10 2015 +0000
- Revision:
- 13:a09c9ff30460
- Parent:
- 12:d1e2995ca5f6
- Child:
- 14:9202a7785974
revert
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 13:a09c9ff30460 | 1 | |
justinkim | 8:f837e0d255e8 | 2 | #include "mbed.h" |
justinkim | 8:f837e0d255e8 | 3 | #include "EthernetInterface.h" |
hjjeon | 9:a63ff95c354b | 4 | |
hjjeon | 12:d1e2995ca5f6 | 5 | #define ECHO_SERVER_PORT 7 |
hjjeon | 9:a63ff95c354b | 6 | |
hjjeon | 12:d1e2995ca5f6 | 7 | int main (void) |
justinkim | 8:f837e0d255e8 | 8 | { |
justinkim | 8:f837e0d255e8 | 9 | printf("Wait a second...\r\n"); |
hjjeon | 12:d1e2995ca5f6 | 10 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; |
hjjeon | 12:d1e2995ca5f6 | 11 | EthernetInterface eth; |
hjjeon | 12:d1e2995ca5f6 | 12 | eth.init(mac_addr); //Use DHCP |
justinkim | 8:f837e0d255e8 | 13 | eth.connect(); |
justinkim | 8:f837e0d255e8 | 14 | printf("Server IP Address is %s\r\n", eth.getIPAddress()); |
hjjeon | 12:d1e2995ca5f6 | 15 | |
hjjeon | 12:d1e2995ca5f6 | 16 | TCPSocketServer server; |
hjjeon | 12:d1e2995ca5f6 | 17 | server.bind(ECHO_SERVER_PORT); |
hjjeon | 12:d1e2995ca5f6 | 18 | server.listen(); |
hjjeon | 12:d1e2995ca5f6 | 19 | |
hjjeon | 12:d1e2995ca5f6 | 20 | while (true) |
hjjeon | 12:d1e2995ca5f6 | 21 | { |
hjjeon | 12:d1e2995ca5f6 | 22 | printf("Wait for new connection...\r\n"); |
hjjeon | 12:d1e2995ca5f6 | 23 | TCPSocketConnection client; |
hjjeon | 12:d1e2995ca5f6 | 24 | server.accept(client); |
hjjeon | 12:d1e2995ca5f6 | 25 | client.set_blocking(false, 15000); // Timeout after (1.5)s |
hjjeon | 12:d1e2995ca5f6 | 26 | |
hjjeon | 12:d1e2995ca5f6 | 27 | printf("Connection from: %s\r\n", client.get_address()); |
hjjeon | 12:d1e2995ca5f6 | 28 | char buffer[256]; |
hjjeon | 12:d1e2995ca5f6 | 29 | while (true) { |
hjjeon | 12:d1e2995ca5f6 | 30 | int n = client.receive(buffer, sizeof(buffer)); |
hjjeon | 12:d1e2995ca5f6 | 31 | if (n <= 0) break; |
hjjeon | 12:d1e2995ca5f6 | 32 | |
hjjeon | 12:d1e2995ca5f6 | 33 | // print received message to terminal |
hjjeon | 12:d1e2995ca5f6 | 34 | buffer[n] = '\0'; |
hjjeon | 12:d1e2995ca5f6 | 35 | printf("Received message from Client :'%s'\r\n",buffer); |
hjjeon | 12:d1e2995ca5f6 | 36 | |
hjjeon | 12:d1e2995ca5f6 | 37 | // reverse the message |
hjjeon | 12:d1e2995ca5f6 | 38 | char temp; |
hjjeon | 12:d1e2995ca5f6 | 39 | for(int f = 0, l = n-1; f<l; f++,l--){ |
hjjeon | 12:d1e2995ca5f6 | 40 | temp = buffer[f]; |
hjjeon | 12:d1e2995ca5f6 | 41 | buffer[f] = buffer[l]; |
hjjeon | 12:d1e2995ca5f6 | 42 | buffer[l] = temp; |
hjjeon | 12:d1e2995ca5f6 | 43 | } |
hjjeon | 12:d1e2995ca5f6 | 44 | |
hjjeon | 12:d1e2995ca5f6 | 45 | // print reversed message to terminal |
hjjeon | 12:d1e2995ca5f6 | 46 | printf("Sending message to Client: '%s'\r\n",buffer); |
hjjeon | 12:d1e2995ca5f6 | 47 | |
hjjeon | 12:d1e2995ca5f6 | 48 | // Echo received message back to client |
hjjeon | 12:d1e2995ca5f6 | 49 | client.send_all(buffer, n); |
hjjeon | 12:d1e2995ca5f6 | 50 | if (n <= 0) break; |
hjjeon | 12:d1e2995ca5f6 | 51 | } |
hjjeon | 12:d1e2995ca5f6 | 52 | |
hjjeon | 12:d1e2995ca5f6 | 53 | client.close(); |
hjjeon | 9:a63ff95c354b | 54 | } |
justinkim | 8:f837e0d255e8 | 55 | } |
justinkim | 8:f837e0d255e8 | 56 |