This is FlexLEDTape on TCP/IP Program

Dependencies:   EthernetNetIf HTTPServer TextLCD a101 mbed

Fork of ethernet_test_http by Yasushi TAUCHI

Files at this revision

API Documentation at this revision

Comitter:
hototogisu
Date:
Fri Dec 28 10:31:58 2012 +0000
Parent:
0:7dcfd77d344d
Commit message:
This is FlexLEDTape on TCP/IP Program

Changed in this revision

a101.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
diff -r 7dcfd77d344d -r a505df26eceb a101.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/a101.lib	Fri Dec 28 10:31:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hototogisu/code/a101/#28d3f6703e22
diff -r 7dcfd77d344d -r a505df26eceb main.cpp
--- a/main.cpp	Sun May 08 09:03:08 2011 +0000
+++ b/main.cpp	Fri Dec 28 10:31:58 2012 +0000
@@ -1,55 +1,114 @@
 #include "mbed.h"
-#include "TextLCD.h"
 #include "EthernetNetIf.h"
-#include "HTTPServer.h"
+#include "TCPSocket.h"
+
+#include "a101.h"
+#include "string.h"
+
+#define LED_LENGTH 24
+#define TAPE_LENGTH 10
+#define TCP_LISTENING_PORT 50505
 
-DigitalOut led1(LED1,"led1");
-DigitalOut led2(LED2,"led2");
-DigitalOut led3(LED3,"led3");
-DigitalOut led4(LED4,"led4");
+            
+char color[] = "0x000000";
+char *error;
+int ledColors[LED_LENGTH*TAPE_LENGTH] = {0};
+a101 ledTape(p13, p14, LED_LENGTH, TAPE_LENGTH);
 
-TextLCD lcd(p24, p26, p27, p28, p29, p30);
+void onTCPSocketEvent(TCPSocketEvent e) ;
+void onConnectedTCPSocketEvent(TCPSocketEvent e) ;
+
+EthernetNetIf eth;
+TCPSocketErr tcpErr ;
+TCPSocket tcpSock ;
+TCPSocket* pConnectedSock ;
+Host client ;
 
-#if 1
-/*
- * Use DHCP
- */
-        EthernetNetIf ethif;
-#else
-/*
- * Use "static IP address" (Parameters:IP, Subnet mask, Gateway, DNS)
- */
-        EthernetNetIf ethif(IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx));
-#endif
+int main() {
+    printf("Setting up...\r\n");
+    EthernetErr ethErr = eth.setup() ;
+    if ( ethErr != ETH_OK ) {
+        printf("Error %d in setup.\r\n", ethErr ) ;
+    return -1 ;
+    }
+    printf("Setup OK\r\n") ;
+    IpAddr ip = eth.getIp() ;
+    printf("mbed IP Address is [%d.%d.%d.%d]\r\n", ip[0], ip[1], ip[2], ip[3] ) ;
+
+    tcpSock.setOnEvent(&onTCPSocketEvent) ;
+
+    printf("Bindding...\r\n") ;
+    tcpErr = tcpSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)) ;
+    if ( tcpErr != ETH_OK ){
+        printf("Bindding Error.\r\n") ;
+        return -1 ;
+    }
     
-    HTTPServer server;
-    LocalFileSystem local("local");
+    printf("Listen...\r\n");
+    tcpErr = tcpSock.listen() ;
+    if ( tcpErr != ETH_OK ){
+        printf("Listen Error.\r\n") ;
+        return -1 ;
+    }
 
-int main(void) {
-
-    Base::add_rpc_class<DigitalOut>();
+    while(1) {
+        Net::poll() ;
+    }
+}
 
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("Program init..  ");
-
-    if (ethif.setup()) {
-        error("Ethernet setup failed.");
-        return 1;
-    }
-    IpAddr ethIp=ethif.getIp();
+void onTCPSocketEvent(TCPSocketEvent e) {
+    if (e != TCPSOCKET_ACCEPT) return;
     
-    lcd.locate(0,1);
-    lcd.printf("%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
-    led1=1;
-    wait(1);
-    server.addHandler<SimpleHandler>("/hello");
-    server.addHandler<RPCHandler>("/rpc");
-    FSHandler::mount("/local", "/");
-    server.addHandler<FSHandler>("/");
-    server.bind(80);
-    while (1) {
-        Net::poll();
+    printf("Listening: TCP Socket Accepted\r\n") ;
+
+    tcpErr = tcpSock.accept(&client, &pConnectedSock) ;
+    if (tcpErr != TCPSOCKET_OK) {
+        printf("onTCPSocketEvent Error\r\n") ;
+        return ;
     }
-    return 0;
+
+    pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent) ;
+        
+    IpAddr clientIp = client.getIp();
+        printf("Controler IP Address is [%d.%d.%d.%d]. \r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
 }
+
+void onConnectedTCPSocketEvent(TCPSocketEvent e) {
+    char buf[3000];
+
+    switch(e) {
+        case TCPSOCKET_CONNECTED:
+            printf("Connected to host.\r\n");
+            break ;
+        case TCPSOCKET_WRITEABLE:
+            printf("Can write data to buf.\r\n");
+            break ;
+        case TCPSOCKET_READABLE:
+            pConnectedSock->recv(buf, sizeof(buf));
+            for (int i = 0; i < LED_LENGTH*TAPE_LENGTH; i++) {
+                for (int j = 0; j < 6; j++) {
+                    color[j+2] = buf[i*6+j];
+                }
+                ledColors[i] = strtol(color, &error, 0);
+            }
+            ledTape.post(ledColors);
+            break ;
+        case TCPSOCKET_CONTIMEOUT:
+            printf("Connection timed out.\r\n");
+            break;
+        case TCPSOCKET_CONRST:
+            printf("Connection was reset by remote host.\r\n");
+            break;
+        case TCPSOCKET_CONABRT:
+            printf("Connection was aborted.\r\n");
+            break;
+        case TCPSOCKET_ERROR:
+            printf("Unknown error.\r\n");
+            break;
+        case TCPSOCKET_DISCONNECTED:
+            printf("TCP Socket Disconnected\r\n");
+            pConnectedSock->close() ;
+            break;
+    }
+}
+