Minecraft extension

Dependencies:   NySDFileSystem libMiMic mbed-rtos mbed registers

Fork of MiMicSimpleHttpd by Ryo Iizuka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mimic.h"
00002 #include "mbed.h"
00003 #include "InOut.h"
00004 #include "fsdata.h"
00005 
00006 LocalFileSystem2 lf("local");
00007 
00008 
00009 class RedIoPin: public InOut
00010 {
00011 private:
00012     bool _v_pin;
00013     bool _v_rs;
00014 public:
00015     RedIoPin(char pin):InOut(pin,0)
00016     {
00017         //pull down
00018         this->mode(1);//pull down
00019         //read from pin
00020         this->setDirection(true);
00021         this->write(0);
00022         this->setDirection(false);        
00023         //initialize pin and rs
00024         this->_v_pin=this->read()!=0;
00025         this->_v_rs=false;
00026     }
00027     /**
00028      * set RedStoneValue RedStone value
00029      * @param v
00030      * red stone value
00031      * @return
00032      * current pin value
00033      */
00034     bool setRedValue(int v)
00035     {
00036         //update red stone pattern
00037         this->_v_rs=(v!=0);
00038         
00039         //check pin value
00040         this->setDirection(true);
00041         this->write(0);
00042         this->setDirection(false);
00043         this->_v_pin=(this->read()!=0);
00044         
00045         //mix two values
00046         if(this->_v_pin){
00047             // set pin to Lv=H,dir=in
00048             if(this->_v_rs){
00049                 //pin==1 && rs==1
00050                 this->setDirection(true);
00051                 this->write(1);
00052             }else{
00053                 //pin==1 && rs==0
00054                 this->write(0);
00055             }
00056         }else{
00057             if(this->_v_rs){
00058                 //pin==0 && rs==1
00059                 this->setDirection(true);
00060                 this->write(1);
00061             }else{
00062                 //pin==0 && rs==0
00063                 this->write(0);
00064             }
00065         }
00066         //actual pin value
00067         return this->_v_pin;
00068     }
00069 };
00070 class RedIo
00071 {
00072 public:
00073     RedIoPin* io[30];
00074     RedIo()
00075     {
00076         const char d[]={LED1,LED2,LED3,LED4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
00077         for(int i=0;i<30;i++){
00078             this->io[i]=new RedIoPin(d[i]);
00079         }
00080     }
00081     virtual ~RedIo()
00082     {
00083         for(int i=0;i<30;i++){
00084             delete this->io[i];
00085         }
00086     }
00087     unsigned int update(unsigned int v)
00088     {
00089         unsigned int ret=0;
00090         for(int i=0;i<30;i++){
00091             ret=ret<<1;
00092             ret=ret | ((this->io[29-i]->setRedValue((v>>(29-i))&0x00000001))?1:0);
00093         }
00094         return ret;
00095     }
00096 };
00097 
00098 Net* net;
00099 
00100 class RedWireBridge:public MiMic::Httpd
00101 {
00102 private:
00103     ModUrl modurl; //basic URL parser
00104     ModRomFiles modromfs; //ROM file module
00105     ModUPnPDevice modupnp;
00106     RedIo rsio;
00107 public:
00108     RedWireBridge(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
00109     {
00110         this->modromfs.setParam("rom",FSDATA,3);
00111         //bind upnp service to module.
00112         this->modupnp.setParam(*net);
00113     }
00114     virtual void onRequest(HttpdConnection& i_connection)
00115     {
00116         char url[64];
00117         int method;
00118         //try to ModRomFS module. for icon,images.
00119         if(this->modromfs.execute(i_connection)){
00120             return;
00121         }        
00122         //try to UPnP service. for descriptions.
00123         if(this->modupnp.execute(i_connection)){
00124             return;
00125         }
00126         //rsb CGI 
00127         
00128         
00129         //call ModUrl module.
00130         if(!this->modurl.execute(i_connection,url,64,&method)){
00131             i_connection.sendHeader(400,"text/html",NULL);
00132             i_connection.sendBodyF("<html><body>Bad Request.</body></html>",url);
00133             return;
00134         }
00135         UrlReader r(url);
00136         if(!r.isPathEqual("/rsb/")){
00137             i_connection.sendHeader(403,"text/html",NULL);
00138             i_connection.sendBodyF("<html><body>Path must be '/rsb/?p=[:unsigned int:]'</body></html>",url);
00139             return;
00140         }
00141         unsigned int rsv;
00142         if(!r.getQueryUInt("p",rsv)){
00143             i_connection.sendHeader(400,"text/html",NULL);
00144             i_connection.sendBodyF("<html><body>p val must be unsigned int</body></html>",url);
00145             return;
00146         }
00147         i_connection.sendHeader(200,"text/html",NULL);
00148         i_connection.sendBodyF("%u",rsio.update(rsv));
00149         return;
00150     }
00151 };
00152 
00153 int main()
00154 {
00155     net=new Net();  //create a net instance.
00156     NetConfig cfg; //create network configulation
00157     //Prepare configulation.
00158     cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");//set upnp icon address
00159     cfg.setUPnPUdn(0x0c9720e0,0x031e,0x11e3,0); //set application timebase-uuid time and sequence field.
00160     cfg.setFriendlyName("RedWireBridge"); //set friendly name
00161     cfg.setUPnPPresentationURL("/rom/index.html"); //set presentationURL
00162     cfg.setZeroconf(true);//AutoIP enable
00163  
00164     //try to override setting by local file.
00165     cfg.loadFromFile("/local/mimic.cfg");
00166 
00167     RedWireBridge httpd(cfg); //create a httpd instance.
00168     net->start(cfg);
00169     httpd.loop();  //start httpd loop.
00170     return 0;
00171 }