//mmcapdis1 - cjc - 04/04/2009
//Read mm data on D, retain C for control purposes. Buffer the data and format a web page display.
//Display raw processor data on html page for cutpaste formatting.
//Adapted from:  /Samples/SERIAL/RelayChr.c
//          and  /Samples/RCM3720/Tcpip/BROWSELED.C
//          and  /projects08/ubuntudl/lcap2007cr.c
#class auto
#define DINBUFSIZE  63
#define DOUTBUFSIZE 63
#define CINBUFSIZE  63
#define COUTBUFSIZE 63
#define DS1 0x40	//led, port F bit 6 bitmask
#define DS2 0x80	//led, port F bit 7 bitmask
#define TCPCONFIG 1
#define TCP_BUF_SIZE 2048
#define HTTP_MAXSERVERS 2
#define MAX_TCP_SOCKET_BUFFERS 2
#define REDIRECTHOST		_PRIMARY_STATIC_IP
#define REDIRECTTO 		"http://" REDIRECTHOST "/index.shtml"
#memmap xmem
#use "dcrtcp.lib"
#use "http.lib"
#ximport "logo.gif"               			logo_gif
#ximport "mmcapdis1.shtml"  				index_html
#ximport "samples/rcm3700/tcpip/pages/rabbit1.gif"    	rabbit1_gif
#ximport "samples/rcm3700/tcpip/pages/ledon.gif"      	ledon_gif
#ximport "samples/rcm3700/tcpip/pages/ledoff.gif"     	ledoff_gif
#ximport "samples/rcm3700/tcpip/pages/button.gif"     	button_gif
//#ximport "samples/rcm3700/tcpip/pages/showsrc.shtml" 	showsrc_shtml
//#ximport "samples/rcm3700/tcpip/browseled.c"         	browseled_c
SSPEC_MIMETABLE_START
	SSPEC_MIME_FUNC(".shtml", "text/html", shtml_handler),
	SSPEC_MIME(".html", "text/html"),
	SSPEC_MIME(".gif", "image/gif"),
	SSPEC_MIME(".cgi", "")
SSPEC_MIMETABLE_END

// these are global variables
char led1[15];
char led2[15];
int chr;
char ch2[350];
char str3[350];char str4[350];
char str5[350];char str6[350];
char sg01[50],sg02[50],sg03[50],sg04[50],sg05[50],sg06[50],sg07[50],sg08[50];
char sg09[50],sg10[50],sg11[50],sg12[50],sg13[50],sg14[50],sg15[50],sg16[50];
int h;int sw1;
//....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8

//-------------------------
int led1toggle(HttpState* state)
{
   if (strcmp(led1,"ledon.gif")==0)
      strcpy(led1,"ledoff.gif");
   else
      strcpy(led1,"ledon.gif");

   cgi_redirectto(state,REDIRECTTO);
   return 0;
}

int led2toggle(HttpState* state)
{
   if (strcmp(led2,"ledon.gif")==0)
      strcpy(led2,"ledoff.gif");
   else
      strcpy(led2,"ledon.gif");

   cgi_redirectto(state,REDIRECTTO);
   return 0;
}

int led3toggle(HttpState* state)
{
   if (sw1>0){ //show only after all are populated the 1st time
    cgi_redirectto(state,REDIRECTTO);//refresh and show global variables
   }
   return 0;
}

	SSPEC_RESOURCETABLE_START
	SSPEC_RESOURCE_XMEMFILE("/", index_html),
	SSPEC_RESOURCE_XMEMFILE("/index.shtml", index_html),
 //	SSPEC_RESOURCE_XMEMFILE("/showsrc.shtml", showsrc_shtml),
	SSPEC_RESOURCE_XMEMFILE("/rabbit1.gif", rabbit1_gif),
  	SSPEC_RESOURCE_XMEMFILE("/logo.gif", logo_gif),
	SSPEC_RESOURCE_XMEMFILE("/ledon.gif", ledon_gif),
	SSPEC_RESOURCE_XMEMFILE("/ledoff.gif", ledoff_gif),
	SSPEC_RESOURCE_XMEMFILE("/button.gif", button_gif),
 //	SSPEC_RESOURCE_XMEMFILE("browseled.c", browseled_c),
	SSPEC_RESOURCE_ROOTVAR("led1", led1, PTR16, "%s"),
	SSPEC_RESOURCE_ROOTVAR("led2", led2, PTR16, "%s"),
  	SSPEC_RESOURCE_ROOTVAR("ch2", ch2, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg01", sg01, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg02", sg02, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg03", sg03, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg04", sg04, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg05", sg05, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg06", sg06, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg07", sg07, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg08", sg08, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg09", sg09, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg10", sg10, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg11", sg11, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg12", sg12, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg13", sg13, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg14", sg14, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg15", sg15, PTR16, "%s"),
   SSPEC_RESOURCE_ROOTVAR("sg16", sg16, PTR16, "%s"),
	SSPEC_RESOURCE_FUNCTION("/led1tog.cgi", led1toggle),
	SSPEC_RESOURCE_FUNCTION("/led2tog.cgi", led2toggle),
   SSPEC_RESOURCE_FUNCTION("/led3tog.cgi", led3toggle),

SSPEC_RESOURCETABLE_END


void update_outputs()
{
	auto int value;

	value=PFDRShadow&0x3F;   //on state for leds

	/* update O0 */
	if (strcmp(led1,"ledon.gif"))
		value|=DS1;

	/* update O1 */
	if (strcmp(led2,"ledon.gif"))
		value|=DS2;

	WrPortI(PFDR, &PFDRShadow, value);
}

main()
{
   int i,j,k;
	brdInit();				//initialize board for this demo
//....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
   printf("mmcapdis1 v1.0 for Rabbit Core 3720\n\r");
   h=0;sw1=0;
   serDopen(300);
   serCopen(300);

   strcpy(led1,"ledon.gif");
   strcpy(led2,"ledoff.gif");
   strcpy(str5,"txt0");
   sock_init();
   http_init();
   tcp_reserveport(80);
   i=0;
   strcpy(str4,"");//clear buffer
   strcpy(str3,"");//clear buffer

   while (1)
   {
   	update_outputs();
      http_handler();
//   while (chr != 27) {  // Exit on Esc    (won't happen here)
//This happens for every byte from Mm. Add logic to build, print, and clear
//a string for each channel.
      if ((chr = serDgetc()) != -1 && chr != 27) {     //27 decimal = escape
         	serCputc(chr);
            i++;
            sprintf(str3,"%c",chr); strcat(str4,str3); //append char
            if ( chr < 27) {    //detect end of line and process channel
            //This happens when the channel string is complete and ready to print.
              k=strlen(str4);
              if (k>10){
              strcpy(str5,"");
              k-=1;
              h+=1;
              if (h>16) {h=1;sw1=1;}//1-16
              //printf("%d h1 ",h);
              strncat(str5,str4,k);//save it    \n clears it
              //printf("%s",str5); // ok
              strcpy(str4,"");
              i=0;
              }//k>10
              //Not necessarily starting w channel 1, just hold 16
              if (h==1){strcpy(sg01,str5);}
              if (h==2){strcpy(sg02,str5);}
              if (h==3){strcpy(sg03,str5);}
              if (h==4){strcpy(sg04,str5);}
              if (h==5){strcpy(sg05,str5);}
              if (h==6){strcpy(sg06,str5);}
              if (h==7){strcpy(sg07,str5);}
              if (h==8){strcpy(sg08,str5);}
              if (h==9){strcpy(sg09,str5);}
              if (h==10){strcpy(sg10,str5);}
              if (h==11){strcpy(sg11,str5);}
              if (h==12){strcpy(sg12,str5);}
              if (h==13){strcpy(sg13,str5);}
              if (h==14){strcpy(sg14,str5);}
              if (h==15){strcpy(sg15,str5);}
              if (h==16){strcpy(sg16,str5);}
              //printf("%s",str5);
              //printf("%d h2 ",h);
            }
      }
	}

   serCputs("Done");
   while (serCwrFree() != COUTBUFSIZE) ; // allow tx to complete before closing
   serCclose();
   serDclose();
}

#nodebug


