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:
- 0:bef69e35f486
- Child:
- 1:8efef658d90b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 05 10:10:22 2015 +0000 @@ -0,0 +1,118 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "C12832.h" + +#define ECHO_SERVER_PORT 6666 +#define LCD_LINE_HEIGHT 12 +#define DELAY_INDICATION 0 + +#define STR_FAIL "FAIL" +#define STR_OK "OK" +#define STR_CLEAR "CLEAR" + +Serial pc(USBTX,USBRX); +DigitalOut myled(LED1); +C12832 lcd(p5, p7, p6, p8, p11); + +PwmOut rOut (p23); +PwmOut gOut (p24); +PwmOut bOut (p25); + +void setLcdServerInfo(char * ip) { + lcd.cls(); + lcd.locate(0,0); + lcd.printf("IP: %s - Port: %d", ip, ECHO_SERVER_PORT); +} + +void setRGB(int r, int g, int b) { + rOut = r; + gOut = g; + bOut = b; +} + +void clearRGB(void) { + setRGB(255, 255, 255); +} + +void initRgb(void) { + rOut.period(0.001); // set pwm period + gOut.period(0.001); // set pwm period + bOut.period(0.001); // set pwm period + + setRGB(255, 255, 255); +} + +void indicateOk(void) { + setRGB(255, 0, 255); +} + +void indicateFail(void) { + setRGB(0, 255, 255); +} + +int main (void) { + // Init RGB led + 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(); + indicateFail(); + } + clearRGB(); + + while (true) { + printf("Awaiting client connection ...\r\n"); + setLcdServerInfo(eth.getIPAddress()); + lcd.locate(0, LCD_LINE_HEIGHT); + lcd.printf("Awaiting connection ..."); + + TCPSocketConnection client; + server.accept(client); + //client.set_blocking(false, 5000); // Timeout after (1.5)s + + printf("Client connected: %s\r\n", client.get_address()); + 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(); + } +}