Daniel Bromand
/
CatDoorProject
Revision 0:990d96bbd6d0, committed 2011-08-22
- Comitter:
- bromand
- Date:
- Mon Aug 22 01:48:39 2011 +0000
- Commit message:
- 1
Changed in this revision
diff -r 000000000000 -r 990d96bbd6d0 CatDoorProject.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CatDoorProject.cpp Mon Aug 22 01:48:39 2011 +0000 @@ -0,0 +1,123 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPServer.h" +#include "HTTPRequestHandler.h" +#include "dbg/dbg.h" +#include <string> + +EthernetNetIf eth; +HTTPServer svr; + +DigitalOut led1(LED1); + +struct system +{ + bool bOverrideSystem; + bool bIsDoorClosed; +} system1; + +struct cat +{ + int id; + string name; +} cat1; + + +class CatDoorHandler : public HTTPRequestHandler +{ +public: +CatDoorHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket): HTTPRequestHandler(rootPath, path, pTCPSocket){}; +virtual ~CatDoorHandler(){ DBG("\r\nCatDoorHandler destroyed\r\n");}; +static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new CatDoorHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one +virtual void doGet() +{ +string name = cat1.name; +DBG("\r\nIn CatDoorHandler::doGet()\r\n"); +string header = "<html><body><h1>Cat Door System</h1>"; +string p1 = "<h2><u>System Status</u></h2>"; +string p2 = system1.bOverrideSystem == true ? "Yes" : "No"; +string p3 = "<p>Override System: " + p2 + "</p>"; +string p4 = system1.bIsDoorClosed == true ? "Yes" : "No"; +string p5 = "<p>Door Status: " + p4 + "</p>"; +string p6 = "<h2><u>Cats Status</u></h2>"; +string p7 = "<p>Is " + cat1.name + " Inside: Yes</p>"; +string footer = "</body></html>"; +string resp = header + p1 + p3 + p5 + p6 + p7 + footer; +setContentLen( resp.length()); +respHeaders()["Connection"] = "close"; +writeData(resp.c_str(), resp.length()); +DBG("\r\nExit CatDoorHandler::doGet()\r\n"); +} +virtual void doPost(){}; +virtual void doHead(){}; + +virtual void onReadable(){}; //Data has been read +virtual void onWriteable() +{ +DBG("\r\nCatDoorHandler::onWriteable() event\r\n"); +close(); //Data written, we can close the connection +} +virtual void onClose(){}; //Connection is closing + +virtual void SetOverrideSystem(bool bOverride) +{ + bOverrideSystem = bOverride; +}; + +virtual void SetDoorStatus(bool bLocked) +{ + bDoorStatus = bLocked; +}; + +virtual void SetIsCatInside(bool bIsInside) +{ + bIsCatInside = bIsInside; +}; + +protected: +bool bOverrideSystem; +bool bDoorStatus; +bool bIsCatInside; + +}; + +int main() +{ + //Initialize system parameters + system1.bOverrideSystem = false; + system1.bIsDoorClosed = true; //Initialize default here read from sensor later + + //Initialize cat 1 parameters + cat1.id = 1; + cat1.name = "Garfield"; + + printf("Setting up...\n"); + EthernetErr ethErr = eth.setup(); + if(ethErr) + { + printf("Error %d in setup.\n", ethErr); + return -1; + } + printf("Setup OK\n"); + + svr.addHandler<CatDoorHandler>("/"); //Default handler + svr.bind(80); + + printf("Listening...\n"); + + Timer tm; + tm.start(); + //Listen indefinitely + while(true) + { + + Net::poll(); + if(tm.read()>.5) + { + led1=!led1; //Show that we are alive + tm.start(); + } + } + + return 0; +}
diff -r 000000000000 -r 990d96bbd6d0 EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Mon Aug 22 01:48:39 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r 990d96bbd6d0 HTTPServer.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPServer.lib Mon Aug 22 01:48:39 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r 990d96bbd6d0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Aug 22 01:48:39 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da