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: EthernetNetIf HTTPServer TextLCD a101 mbed
Fork of ethernet_test_http by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "TCPSocket.h" 00004 00005 #include "a101.h" 00006 #include "string.h" 00007 00008 #define LED_LENGTH 24 00009 #define TAPE_LENGTH 10 00010 #define TCP_LISTENING_PORT 50505 00011 00012 00013 char color[] = "0x000000"; 00014 char *error; 00015 int ledColors[LED_LENGTH*TAPE_LENGTH] = {0}; 00016 a101 ledTape(p13, p14, LED_LENGTH, TAPE_LENGTH); 00017 00018 void onTCPSocketEvent(TCPSocketEvent e) ; 00019 void onConnectedTCPSocketEvent(TCPSocketEvent e) ; 00020 00021 EthernetNetIf eth; 00022 TCPSocketErr tcpErr ; 00023 TCPSocket tcpSock ; 00024 TCPSocket* pConnectedSock ; 00025 Host client ; 00026 00027 int main() { 00028 printf("Setting up...\r\n"); 00029 EthernetErr ethErr = eth.setup() ; 00030 if ( ethErr != ETH_OK ) { 00031 printf("Error %d in setup.\r\n", ethErr ) ; 00032 return -1 ; 00033 } 00034 printf("Setup OK\r\n") ; 00035 IpAddr ip = eth.getIp() ; 00036 printf("mbed IP Address is [%d.%d.%d.%d]\r\n", ip[0], ip[1], ip[2], ip[3] ) ; 00037 00038 tcpSock.setOnEvent(&onTCPSocketEvent) ; 00039 00040 printf("Bindding...\r\n") ; 00041 tcpErr = tcpSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)) ; 00042 if ( tcpErr != ETH_OK ){ 00043 printf("Bindding Error.\r\n") ; 00044 return -1 ; 00045 } 00046 00047 printf("Listen...\r\n"); 00048 tcpErr = tcpSock.listen() ; 00049 if ( tcpErr != ETH_OK ){ 00050 printf("Listen Error.\r\n") ; 00051 return -1 ; 00052 } 00053 00054 while(1) { 00055 Net::poll() ; 00056 } 00057 } 00058 00059 void onTCPSocketEvent(TCPSocketEvent e) { 00060 if (e != TCPSOCKET_ACCEPT) return; 00061 00062 printf("Listening: TCP Socket Accepted\r\n") ; 00063 00064 tcpErr = tcpSock.accept(&client, &pConnectedSock) ; 00065 if (tcpErr != TCPSOCKET_OK) { 00066 printf("onTCPSocketEvent Error\r\n") ; 00067 return ; 00068 } 00069 00070 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent) ; 00071 00072 IpAddr clientIp = client.getIp(); 00073 printf("Controler IP Address is [%d.%d.%d.%d]. \r\n", clientIp[0], clientIp[1], clientIp[2], clientIp[3]); 00074 } 00075 00076 void onConnectedTCPSocketEvent(TCPSocketEvent e) { 00077 char buf[3000]; 00078 00079 switch(e) { 00080 case TCPSOCKET_CONNECTED: 00081 printf("Connected to host.\r\n"); 00082 break ; 00083 case TCPSOCKET_WRITEABLE: 00084 printf("Can write data to buf.\r\n"); 00085 break ; 00086 case TCPSOCKET_READABLE: 00087 pConnectedSock->recv(buf, sizeof(buf)); 00088 for (int i = 0; i < LED_LENGTH*TAPE_LENGTH; i++) { 00089 for (int j = 0; j < 6; j++) { 00090 color[j+2] = buf[i*6+j]; 00091 } 00092 ledColors[i] = strtol(color, &error, 0); 00093 } 00094 ledTape.post(ledColors); 00095 break ; 00096 case TCPSOCKET_CONTIMEOUT: 00097 printf("Connection timed out.\r\n"); 00098 break; 00099 case TCPSOCKET_CONRST: 00100 printf("Connection was reset by remote host.\r\n"); 00101 break; 00102 case TCPSOCKET_CONABRT: 00103 printf("Connection was aborted.\r\n"); 00104 break; 00105 case TCPSOCKET_ERROR: 00106 printf("Unknown error.\r\n"); 00107 break; 00108 case TCPSOCKET_DISCONNECTED: 00109 printf("TCP Socket Disconnected\r\n"); 00110 pConnectedSock->close() ; 00111 break; 00112 } 00113 } 00114
Generated on Sat Jul 16 2022 19:04:21 by
1.7.2
