Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832 EthernetInterface mbed-rtos mbed ConfigFile
Diff: main.cpp
- Revision:
- 1:8efef658d90b
- Parent:
- 0:bef69e35f486
- Child:
- 2:a8eebf64cd3e
--- a/main.cpp Thu Mar 05 10:10:22 2015 +0000 +++ b/main.cpp Thu Mar 05 13:18:28 2015 +0000 @@ -1,8 +1,12 @@ #include "mbed.h" #include "EthernetInterface.h" #include "C12832.h" +#include "Log.h" +#include "TcpDaemon.h" -#define ECHO_SERVER_PORT 6666 +#define MAX_BACKLOG 1 +#define TCP_SERVER_PORT 6666 + #define LCD_LINE_HEIGHT 12 #define DELAY_INDICATION 0 @@ -11,17 +15,21 @@ #define STR_CLEAR "CLEAR" Serial pc(USBTX,USBRX); -DigitalOut myled(LED1); +DigitalOut error_led(LED1); C12832 lcd(p5, p7, p6, p8, p11); PwmOut rOut (p23); PwmOut gOut (p24); PwmOut bOut (p25); +using namespace MachineVision; + void setLcdServerInfo(char * ip) { lcd.cls(); lcd.locate(0,0); - lcd.printf("IP: %s - Port: %d", ip, ECHO_SERVER_PORT); + lcd.printf("IP: %s", ip); + lcd.locate(0,14); + lcd.printf("Port: %d", TCP_SERVER_PORT); } void setRGB(int r, int g, int b) { @@ -55,20 +63,7 @@ initRgb(); pc.baud(115200); - EthernetInterface eth; - printf("Requesting IP address from DHCP\r\n"); - eth.init(); //Use DHCP - eth.connect(); - printf("IP Address is %s\r\n", eth.getIPAddress()); - printf("Listening on port %d\r\n", ECHO_SERVER_PORT); - - // Set ip on LCD - setLcdServerInfo(eth.getIPAddress()); - - TCPSocketServer server; - server.bind(ECHO_SERVER_PORT); - server.listen(); - + // Status check for (int i = 0; i < 4; i++) { indicateOk(); @@ -76,43 +71,30 @@ } clearRGB(); - while (true) { - printf("Awaiting client connection ...\r\n"); - setLcdServerInfo(eth.getIPAddress()); - lcd.locate(0, LCD_LINE_HEIGHT); - lcd.printf("Awaiting connection ..."); + EthernetInterface eth; + Log::v("Requesting IP address from DHCP\r\n"); + eth.init(); //Use DHCP + + if (eth.connect() < 0) { + Log::w("Could not retrieve IP address from DHCP\r\n"); + setLcdServerInfo("No ip address"); + } else { + Log::v("IP Address is %s\r\n", eth.getIPAddress()); - TCPSocketConnection client; - server.accept(client); - //client.set_blocking(false, 5000); // Timeout after (1.5)s - - printf("Client connected: %s\r\n", client.get_address()); + // Set ip on LCD setLcdServerInfo(eth.getIPAddress()); - lcd.locate(0, LCD_LINE_HEIGHT); - lcd.printf("Client: %s", client.get_address()); - - char buffer[256]; - while (true) { - int n = client.receive(buffer, sizeof(buffer)); - if (n <= 0) { - break; - } else { - if (strcmp(STR_FAIL, buffer) == 0) { - printf("Received FAIL\r\n"); - indicateFail(); - } else if (strcmp(STR_OK, buffer) == 0) { - printf("Received OK\r\n"); - indicateOk(); - } else if (strcmp(STR_CLEAR, buffer) == 0) { - printf("Clearing\r\n"); - clearRGB(); - } else { - printf("Unknown message %s\r\n", buffer); - } - } - } - - printf("Client disconnected\r\n"); - client.close(); + + // Start the daemon + TcpDaemon daemon(TCP_SERVER_PORT); + Log::v("TCP daemon listening @ TCP_SERVER_PORT = %d\r\n", TCP_SERVER_PORT); + daemon.startListening(); + } + + // Should never be reached + while (true) { + error_led = 1; + wait(0.25); + error_led = 0; + wait(0.25); } }