Dependencies:   mbed lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HTTPServer.h"
00003 #include "HTTPRPC.h"
00004 #include "HTTPFS.h"
00005 
00006 #include "TextLCD.h"  //kb
00007 #include "myrpc.h"
00008 
00009 
00010 HTTPServer http;
00011 
00012 DigitalOut led1(LED1, "led1");
00013 DigitalOut led2(LED2, "led2");
00014 DigitalOut led3(LED3, "led3");
00015 DigitalOut led4(LED4, "led4");
00016 
00017 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
00018 
00019 myrpc myrpc1(LED1, "myrpc1");
00020 myrpc myrpc2(LED2, "myrpc2");
00021 myrpc myrpc3(LED3, "myrpc3");
00022 myrpc myrpc4(LED4, "myrpc4");
00023 
00024 LocalFileSystem local("local");
00025 Ethernet eth2;
00026 
00027 float const DISP_SLOW(2.0);
00028 float const DISP_FAST(0.5);
00029 
00030 // Once running, try this web page ->  http://192.168.1.102/rpc/led3/write+1
00031 
00032 /* Display the MAC address on the Text LCD */
00033 void disp_mac() {
00034     lcd.cls();
00035     lcd.printf("MAC: ");
00036     lcd.locate(0,1);         //column(0-15), row(0-1)
00037         char dispchar, mac_addr[6], i;
00038         eth2.address(mac_addr);        
00039         for(i = 0; i < 3; i++) { 
00040             dispchar = mac_addr[i]; 
00041                 if(dispchar < 16) { lcd.printf("0", dispchar);
00042                 }
00043             lcd.printf("%x", dispchar); 
00044         }
00045         for(i = i; i < 6; i++) { 
00046             dispchar = mac_addr[i]; 
00047                 lcd.printf(":", dispchar); 
00048                 if(dispchar < 16) { lcd.printf("0", dispchar);
00049                 }
00050             lcd.printf("%x", dispchar); 
00051         }  
00052 /* Display done */  
00053 }
00054 
00055 /* Display Waiting for Link */
00056 void disp_linkStat() {
00057     lcd.cls();
00058     while(eth2.link() == 0) {
00059         led2 = !led2;
00060         wait(DISP_FAST);
00061         lcd.cls();
00062         lcd.printf("Link DOWN");  
00063     }
00064     led2 = 0;
00065     lcd.cls();
00066     lcd.printf("Link UP");     
00067 /* Display done */
00068 }    
00069 
00070 /* Display the IP Address on the Text LCD */
00071 void disp_ipAddr() {
00072     NetServer *net = NetServer::get();
00073     struct ip_addr ip = net->getIPAddr();
00074     lcd.cls();
00075     lcd.printf("IP:");
00076     lcd.locate(0,1);         //column(0-15), row(0-1)
00077     lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
00078 /* Display done */
00079 }        
00080 
00081 /* Display the IP Address Mask on the Text LCD */
00082 void disp_ipMask() {
00083     NetServer *net = NetServer::get();
00084     struct ip_addr nm = net->getNetmask();
00085     lcd.cls();
00086     lcd.printf("Mask:");
00087     lcd.locate(0,1);         //column(0-15), row(0-1)
00088     lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (nm.addr)&0xFF, (nm.addr>>8)&0xFF, (nm.addr>>16)&0xFF, (nm.addr>>24)&0xFF);
00089 /* Display done */
00090 }    
00091   
00092 /* Display the IP Gateway Address on the Text LCD */
00093 void disp_ipGateway() {
00094     NetServer *net = NetServer::get();
00095     struct ip_addr gw = net->getGateway();
00096     lcd.cls();
00097     lcd.printf("GW:");
00098     lcd.locate(0,1);         //column(0-15), row(0-1)
00099     lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (gw.addr)&0xFF, (gw.addr>>8)&0xFF, (gw.addr>>16)&0xFF, (gw.addr>>24)&0xFF);
00100 /* Display done */
00101 }
00102 
00103 /* Display the DNS IP Address on the Text LCD */
00104 void disp_ipDNS() {
00105     NetServer *net = NetServer::get();
00106     struct ip_addr dns = net->getDNS1();
00107     lcd.cls();
00108     lcd.printf("DNS:");
00109     lcd.locate(0,1);         //column(0-15), row(0-1)
00110     lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (dns.addr)&0xFF, (dns.addr>>8)&0xFF, (dns.addr>>16)&0xFF, (dns.addr>>24)&0xFF);
00111 /* Display done */
00112 }
00113 
00114 
00115 int main(void) {
00116     
00117     /* Init the LCD */
00118     lcd.cls();
00119     lcd.locate(0,0);         //column(0-15), row(0-1)
00120     lcd.printf("Init HTTP Serv 9");
00121     lcd.locate(0,1);         //column(0-15), row(0-1)
00122     lcd.printf("K Braun");
00123     wait(DISP_SLOW);
00124     /* Display done */
00125     
00126     char time_buff[32];
00127     lcd.cls();
00128     time_t seconds = time(NULL);
00129     strftime(time_buff, 32, "%a %m/%d/%y\n" /*%I:%M:%S %p\n"*/, localtime(&seconds));
00130     lcd.printf("%s", time_buff);
00131     strftime(time_buff, 32, /*"%a %m/%d/%y\n"*/ "%I:%M:%S %p\n", localtime(&seconds));
00132     lcd.printf("%s", time_buff);
00133     wait(DISP_SLOW);
00134     
00135     Base::add_rpc_class<AnalogIn>();
00136     Base::add_rpc_class<AnalogOut>();
00137     Base::add_rpc_class<BusIn>();
00138     Base::add_rpc_class<BusInOut>();
00139     Base::add_rpc_class<BusOut>();
00140 //    Base::add_rpc_class<CAN>();
00141     Base::add_rpc_class<DigitalIn>();
00142     Base::add_rpc_class<DigitalInOut>();    
00143     Base::add_rpc_class<DigitalOut>();
00144 //    Base::add_rpc_class<Ethernet>();
00145 //    Base::add_rpc_class<I2C>();
00146 //    Base::add_rpc_class<InterruptIn>();
00147 //    Base::add_rpc_class<LocalFileSystem>();
00148     Base::add_rpc_class<PwmOut>();
00149     Base::add_rpc_class<SPI>();
00150     Base::add_rpc_class<Serial>();
00151 //    Base::add_rpc_class<Ticker>();
00152 //    Base::add_rpc_class<Timeout>();
00153     Base::add_rpc_class<Timer>();
00154 //    Base::add_rpc_class<error>(); 
00155 //    Base::add_rpc_class<rtc_time>();
00156 
00157     Base::add_rpc_class<myrpc>();
00158                
00159     disp_mac();  //put MAC address on LCD
00160     wait (DISP_SLOW); 
00161     disp_linkStat();  //show link status on LCD, wait for valid link
00162     wait (DISP_FAST); 
00163     lcd.cls();
00164     lcd.printf("Looking for...\n");
00165     lcd.printf("DHCP Server");
00166     http.addHandler(new HTTPRPC());
00167     http.addHandler(new HTTPFileSystemHandler("/", "/local/"));
00168     http.bind();
00169     
00170 //    server.addHandler(new HTTPFS("/", "/local/"));
00171 //    server.bind();
00172 //    NetServer *net = NULL;    
00173     NetServer *net = NetServer::get();
00174 //    struct ip_addr ip = net->getIPAddr();
00175 //    struct ip_addr gw = net->getGateway();
00176 //    struct ip_addr nm = net->getNetmask();
00177 //    struct ip_addr dns = net->getDNS1();
00178 //    struct ip_addr hn = net->getHostname();
00179     // ...
00180 
00181     /* Display the Host Name on the Text LCD */
00182     lcd.cls();
00183     lcd.printf("Host:");
00184     lcd.locate(0,1);         //column(0-15), row(0-1)
00185 //    hn((char *)net->getHostname);
00186     lcd.printf("mbed-c3p0");  //<<until I can find out "how-to", force feed "mbed-c3p0"
00187 //    lcd.printf("%s", (const char)net->getHostname));
00188     wait(DISP_FAST);
00189     /* Display done */
00190 
00191     disp_ipAddr();    //put IP address on LCD
00192     wait (DISP_FAST); 
00193     disp_ipMask();    //put IP Mask on LCD
00194     wait (DISP_FAST); 
00195     disp_ipGateway(); //put Gateway address on LCD
00196     wait (DISP_FAST);
00197     disp_ipDNS();     //put DNS address on LCD
00198     wait (DISP_FAST);  
00199     disp_ipAddr();    //put IP address on LCD...again
00200     lcd.printf("mbed-c3p0");  //<<until I can find out "how-to", force feed "mbed-c3p0"       
00201 
00202     while(1) {
00203         http.poll();
00204         disp_ipAddr();    //put IP address on LCD...again
00205         lcd.printf("mbed-c3p0");  //<<until I can find out "how-to", force feed "mbed-c3p0"  
00206         time_t seconds = time(NULL);
00207         strftime(time_buff, 32, /*"%a %m/%d/%y\n"*/ "  %H:%M\n", localtime(&seconds));
00208         lcd.printf("%s", time_buff);
00209         wait(0.01);
00210     }
00211 }
00212