Masanori Morita / Mbed 2 deprecated lcdtest

Dependencies:   EthernetNetIf TextLCD mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Hello World! for the TextLCD
00002  
00003 #include "mbed.h"
00004 #include "TextLCD.h"
00005 #include "EthernetNetIf.h"
00006 #include "HTTPServer.h"
00007 #include "ipaddr.h"
00008 #include "RPCVariable.h"
00009 
00010 IpAddr ip(192,168,1,121);
00011 IpAddr netmask(255,255,255,0);
00012 IpAddr gateway(192,168,1,1);
00013 IpAddr dns(192,168,1,1);
00014 
00015 EthernetNetIf eth(ip,netmask,gateway,dns);  
00016 HTTPServer svr;
00017 
00018 DigitalOut led1(LED1);
00019 
00020 InterruptIn geigerIn(p5);
00021 
00022 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7
00023 
00024 unsigned int count = 0;
00025 unsigned int prev_count = 0;
00026 unsigned int i = 0;
00027 
00028 
00029 class MyHandler : public HTTPRequestHandler
00030 {
00031 public:
00032   MyHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
00033   virtual ~MyHandler();
00034 
00035 //protected:
00036   static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new MyHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one
00037 
00038   virtual void doGet();
00039   virtual void doPost();
00040   virtual void doHead();
00041   
00042   virtual void onReadable(); //Data has been read
00043   virtual void onWriteable(); //Data has been written & buf is free
00044   virtual void onClose(); //Connection is closing
00045 };
00046 
00047 MyHandler::MyHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
00048 {
00049 }
00050 
00051 MyHandler::~MyHandler()
00052 {
00053 }
00054 
00055 void MyHandler::doGet()
00056 {
00057   const char* resp = "Hello world !";
00058   char a[64];
00059   sprintf(a, "%d CPM.\n\0", ::prev_count);
00060   //setContentLen( strlen(resp) );
00061   setContentLen( strlen(a) );
00062 
00063   respHeaders()["Connection"] = "close";
00064 
00065   //writeData(resp, strlen(resp));
00066   writeData(a, strlen(a));
00067 
00068 }
00069 
00070 void MyHandler::doPost()
00071 {
00072 
00073 }
00074 
00075 void MyHandler::doHead()
00076 {
00077 
00078 }
00079 
00080   
00081 void MyHandler::onReadable() //Data has been read
00082 {
00083 
00084 }
00085 
00086 void MyHandler::onWriteable() //Data has been written & buf is free
00087 {
00088   close(); //Data written, we can close the connection
00089 }
00090 
00091 void MyHandler::onClose() //Connection is closing
00092 {
00093   //Nothing to do
00094 }
00095 
00096 
00097 void radioactive()
00098 {
00099   count = count + 1;
00100 }
00101 
00102 void count_minutes()
00103 {
00104     lcd.locate(0, 0);
00105     lcd.printf("%04dCPM ", count); lcd.printf("%02dsec", i);
00106     lcd.locate(0, 1);
00107     lcd.printf("Prev. %04dCPM ", prev_count);
00108     
00109     i = i + 1;
00110       
00111     if(i > 59)
00112     {
00113       i = 0;
00114       prev_count = count;
00115       count = 0;
00116     }
00117 }
00118 
00119 int main() {
00120 
00121   // Count variables clear.
00122   count = 0;
00123   prev_count = 0;
00124 
00125   lcd.cls();
00126   lcd.locate(0, 0);
00127 
00128   geigerIn.fall(&radioactive);
00129 
00130   EthernetErr ethErr = eth.setup();
00131   if(ethErr)
00132   {
00133     return -1;
00134   }
00135   
00136   svr.addHandler<SimpleHandler>("/"); //Default handler
00137   svr.addHandler<MyHandler>("/geiger"); 
00138 
00139   svr.bind(80);
00140 
00141   Ticker tick;
00142   tick.attach(count_minutes, 1);   
00143   
00144   Timer tm;
00145   tm.start();
00146   //Listen indefinitely
00147   while(true)
00148   {
00149     Net::poll();
00150     if(tm.read()>.5)
00151     {
00152       led1=!led1; //Show that we are alive
00153       tm.start();
00154     }
00155   }
00156 
00157 }