/*******************************************************************************
   WebTank.c

   Control Tank using Web interface

*******************************************************************************/

#define TCPCONFIG 1
#define USE_RABBITWEB 1

#use "hobbyist.lib"
#use "dcrtcp.lib"
#use "http.lib"

/* import these web pages */
#ximport "/pages/WebTank.zhtml" WTWebpage
#ximport "/pages/WebTankController.zhtml" CTWebpage
#ximport "/pages/ImageViewer.html" IVWebpage

/* The default mime type for '/' must be first */
SSPEC_MIMETABLE_START
   // This handler enables the ZHTML parser to be used on ZHTML files...
   SSPEC_MIME_FUNC(".zhtml", "text/html", zhtml_handler)
SSPEC_MIMETABLE_END

/* Associate the #ximported files with the web server */
SSPEC_RESOURCETABLE_START
   SSPEC_RESOURCE_XMEMFILE("/index.zhtml", WTWebpage),
   SSPEC_RESOURCE_XMEMFILE("/controller.zhtml", CTWebpage),
   SSPEC_RESOURCE_XMEMFILE("/ImageViewer.html", IVWebpage)
SSPEC_RESOURCETABLE_END

// Define an administration group only
#web_groups admin

/* associate form variables with local variables */
int FWD,REV,LEFT,RIGHT,TLEFT,TRIGHT,FIREMISSLE,FIREGUN;
#web FWD  select("0" = 0, "1" = 1) groups=all(rw)
#web REV  select("0" = 0, "1" = 1) groups=all(rw)
#web LEFT  select("0" = 0, "1" = 1) groups=all(rw)
#web RIGHT  select("0" = 0, "1" = 1) groups=all(rw)
#web TLEFT  select("0" = 0, "1" = 1) groups=all(rw)
#web TRIGHT  select("0" = 0, "1" = 1) groups=all(rw)
#web FIREMISSLE  select("0" = 0, "1" = 1) groups=all(rw)
#web FIREGUN  select("0" = 0, "1" = 1) groups=all(rw)

/* map tank functions to io pins */
#define LT_FWD HB_PA6
#define LT_REV HB_PA2
#define RT_FWD HB_PA5
#define RT_REV HB_PA7
#define TRT_LFT HB_PA3
#define TRT_RGT HB_PA1
#define FIRE HB_PA4
#define GUN HB_PA0

void main(void)
{
	int action,timeout;

   sock_init();   // Initialize socket
   http_init();   // Initialize HTTP server

   http_set_path("/", "index.zhtml"); // Default page as /index.html

   tcp_reserveport(80);   // Port 80 is for the HTTP server

   /* turn everything off */
   FWD=REV=LEFT=RIGHT=TLEFT=TRIGHT=FIREMISSLE=FIREGUN=0;
   action = 0;
   timeout = 0;

   /* loop forever */
	while(1)
   {
      costate
      {
         // This drives the HTTP server.
		   http_handler();

         /* check if a button has been clicked */
         if( FWD|REV|LEFT|RIGHT|TLEFT|TRIGHT|FIREMISSLE|FIREGUN )
         {
           action = 1;
           timeout = 5;
         }
         waitfor(action == 0);
      }
      costate
      {
         waitfor( action );
         action = 0;

         /* set appropriate io pins depending on what button was clicked */
         if( FWD )
         {
           FWD = 0;
           HBpinHigh( LT_FWD );
           HBpinHigh( RT_FWD );
         }
         else if( REV )
         {
           REV = 0;
           HBpinHigh( LT_REV );
           HBpinHigh( RT_REV );
         }
         else if( LEFT )
         {
           LEFT = 0;
           HBpinHigh( LT_REV );
           HBpinHigh( RT_FWD );
         }
         else if( RIGHT )
         {
           RIGHT = 0;
           HBpinHigh( LT_FWD );
           HBpinHigh( RT_REV );
         }
         else if( TRIGHT )
         {
           TRIGHT = 0;
           HBpinHigh( TRT_RGT );
         }
         else if( TLEFT )
         {
           TLEFT = 0;
           HBpinHigh( TRT_LFT );
         }
         else if( FIREMISSLE )
         {
           FIREMISSLE = 0;
           HBpinHigh( FIRE );
         }
         else if( FIREGUN )
         {
           FIREGUN = 0;
           HBpinHigh( GUN );
         }
      }
      costate
      {
         /* decrement timeout after a delay */
         printf("waiting .25 sec\n");
         waitfor(DelayMs(250));
         printf("done waiting\n");
         timeout = (timeout == 0) ? 0 : --timeout;
         printf("timeout = %d\n",timeout);
      }
      costate
      {
         /* if timeout = 1, shut everything off */
         waitfor( timeout == 1 );
         printf("turning off\n");
         HBpinLow( LT_FWD );
         HBpinLow( LT_REV );
         HBpinLow( RT_FWD );
         HBpinLow( RT_REV );
         HBpinLow( TRT_LFT );
         HBpinLow( TRT_RGT );
         HBpinLow( FIRE );
         HBpinLow( GUN );
         timeout = 0;
      }
	}
}


