Machine Vision Status TCP Server

Dependencies:   C12832 EthernetInterface mbed-rtos mbed ConfigFile

Files at this revision

API Documentation at this revision

Comitter:
dwini
Date:
Thu Mar 05 10:10:22 2015 +0000
Child:
1:8efef658d90b
Commit message:
Basic status display using RGB led

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Thu Mar 05 10:10:22 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Mar 05 10:10:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#d1ccbed7687a
--- /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();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Mar 05 10:10:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5448826aa700
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 05 10:10:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file