
/****************************************************/
/*  Filename	 : EXAMPLE5.C	(For Turbo/Borland C Only)*/
/*  I/O	Hardware : None				    */
/*						    */
/*						    */
/*  Description	 : This	function is to test the	user*/
/*		   RAM of the RTC and battery-backup*/
/*		   RAM to implement a data-logger.  */
/*		   The data logger can store the    */
/*		   time	stamp of each data record.  */
/****************************************************/

#include "5510drv.h"

unsigned char GetRTCtime(unsigned char Time);

void	get_module_data();
void	set_module_data();
void	get_log_time()	 ;
void	start_log()	 ;
void	dump_log()	 ;


long t_i;  /*--- global	variable to keep the time interval of the data logger */

int flash_idx=0; /*global variable to keep the current index of	the user action	*/

void main()
{
   char	c;
  int idx;
  /*---- Disable the watch-dog timer ----*/
  WDT_disable();

  /*---	Initialize the timer --------*/
  Timer_Init();

  /*---	Forever	loop */
  while(1)
  {
   /*--- Main menu */
    printf("---------- Main Menu ---------\n");
    printf("0: Exit.\n");
    printf("1: Get module data.\n");
    printf("2: Set module data.\n");
    printf("3: Get current time. \n");
    printf("4: Start data logging.\n");
    printf("5: Show data.\n");
    printf("\n Please select a item to implement...\n");

    /*--- Get the user's action       */
    c=getch();

    /*--- User's actions ---*/
    switch(c)
    {
	case '0':
		return;
	case '1':
		/*--- Get the contents from the	user RAM of the	Real-time Clock	*/
		get_module_data();
		break;

	case '2':
		/*--- Set the contents of the user RAM of the Real-time	Clock */
		set_module_data();
		break;

	case '3':
		/*--- Get the time stamp from the RTC--*/
		get_log_time();
		break;

	case '4':
		/*--- Start data logging --*/
		start_log();
		break;

	case '5':
		/*-- Show the contents of the data logger.--*/
		dump_log();
		break;

	  }
  }

  /*---	Release	all timer resources --*/
  Release_All();
}

/*----------------------------------------------*/
/* Show	the data of the	user RAM in RTC		*/
/*  byte0: null					*/
/*  byte1: time	interval low byte		*/
/*  byte2: time	interval  high byte		*/
/*  byte3: null					*/
/*----------------------------------------------*/

void	get_module_data()
{
unsigned char c;

    printf("---------- Show the setting of the data logger ---------\n");

    c=Get_SysMem(2);
    t_i=c;
    t_i*=0x100;
    c=Get_SysMem(1);
    t_i+=c;
    printf("1.Time interval of the data logger is %lu sec.\n",t_i);
}
/*----------------------------------------------------*/
/*  Change time	interval data and  store in	      */
/*  the	RTC memory				      */
/*  The	memory map refers to the description of		  */
/*  get_module_data()								  */
/*----------------------------------------------------*/
void	set_module_data()
{
  long newt;
  unsigned int data;
  unsigned char	b;


	get_module_data();
	printf("Please input time interval(1 to 60)=");
	scanf("%d",&data);
	if( data <0 || data > 60)
	{
	  printf("Input error, press any key to return main menu.\n");
	  getch();
	  return;
	}
	b=data & 0xff;
	Set_SysMem(1,b);
	b=data/0x100;
	Set_SysMem(2,b);


}

/*----------------------------------------------------*/
/*  A routine to translate part	of string to integer. */
/*  Called by get_log_time() function.		      */
/*----------------------------------------------------*/
unsigned char  get_data(char *s, int *i2, unsigned char	sign)
{
  int i=*i2;
  unsigned char	data;
  int flag=1;

   /*---- get month value ---*/
    if(	s[i] <'0' || s[i] > '9')
    {
      flag=0;
    }
    if((s[i+1] <'0' || s[i+1] >	'9')&& s[i+1] !=sign &&	s[i+1] !=0)
    {
      flag=0;
    }
    if(	s[i+1]==sign)
    {
      data=s[i]-'0';
      i+=2;
    }
    else
    {
      if( s[i+2] == sign ||s[i+2] == 0	)
	data=(s[i]-'0')*10 + (s[i+1]-'0');
      else
	flag=0;
      i+=3;
    }
    *i2=i;
    if(	flag ==0)
      data=0xff;
    return(data);
}
/*----------------------------------------------------*/
/*  Show current date and time , and then the users   */
/*  may	input the new date and time.		      */
/*----------------------------------------------------*/
void	get_log_time()
{
 char s[40];
 unsigned char month,day,year,hour,minute,sec;
 int year2;
 char c;
 int i;

  /*-------- Show current date and time-------*/
  month=GetRTCtime(RTC_month);

  day=GetRTCtime(RTC_day);

  year=GetRTCtime(RTC_year);

  if( year < 80)
    year2=2000+year;
  else
    year2=1900+year;

  printf("Current Date :%2d-%2d-%4d \n",month ,day,year2);

  hour=GetRTCtime(RTC_hour);

  minute=GetRTCtime(RTC_min);

  sec=GetRTCtime(RTC_sec);

  printf("Current time  = %2d:%2d:%4d \n",hour ,minute,sec);

  /*----- Input	new date -----*/
  printf("New date(MM-DD-YY) =");scanf("%s",s);
  if( s[0] == 0)
    return;
  /*---	Initialize the variables...*/
  month=day=year=0xff;
  i=0;

   /*---- Get month value ---*/
   month=get_data( s, &i, '-');
   if( month==0xff)
   {
     printf("Input month error, press any key to continue...\n");
     getch();
     return;
   }
   /*---- Get day value	---*/
   day=get_data( s, &i,	'-');
   if( day==0xff)
   {
     printf("Input day error, press any key to continue...\n");
     getch();
     return;
   }
   /*---- Get year value ---*/
   year=get_data( s, &i, '-');
   if( year==0xff)
   {
     printf("Input year error, press any key to continue...\n");
     getch();
     return;
   }

   if( year < 80)
     year2=2000+year;
   else
     year2=1900+year;


  /*----- Input	new time -----*/
  printf("New time(HH:MM:SS) =");scanf("%s",s);
  if( s[0] == 0)
    return;
  /*---	Initialize the variables...*/
  hour=minute=sec=0xff;
  i=0;

   /*---- Get month value ---*/
   hour=get_data( s, &i, ':');
   if( hour==0xff)
   {
     printf("Input hour error, press any key to continue...\n");
     getch();
     return;
   }
   /*---- Get day value	---*/
   minute=get_data( s, &i, ':');
   if( minute==0xff)
   {
     printf("Input minute error, press any key to continue...\n");
     getch();
     return;
   }
   /*---- Get year value ---*/
   sec=get_data( s, &i,	':');
   if( sec==0xff)
   {
     printf("Input second error, press any key to continue...\n");
     getch();
     return;
   }

  /*---	Show new date and time */
  printf("New data  :%2d-%2d-%4d \n",month ,day,year2);
  printf("New time  = %2d:%2d:%2d \n",hour ,minute,sec);
  printf("press <Y> to save ...\n");

  /*---	Get the	user command */
  c=getch();

  if( c	== 'y' || c == 'Y')
  {
    /*--- Set RTC ---*/

    SetRTCtime(RTC_month,month);

    SetRTCtime(RTC_day,day);

    SetRTCtime(RTC_year,year);

    SetRTCtime(RTC_hour,hour);

    SetRTCtime(RTC_min,minute);

    SetRTCtime(RTC_sec,sec);
  }

}
/*----------------------------------------------------*/
/*  Start to data logging, first save date and time,  */ 
/* then	save the data. The record length is saved in  */
/* byte	0 and 1	.				      */
/*----------------------------------------------------*/
void	start_log()
{

 int length=1;
 unsigned char month,day,year,hour,minute, sec,	mark;

 long count;
 char c;
 int data;
 int tmidx;

   /*--	Get the	time interval of the data logger ---*/
   get_module_data();

   tmidx=Timer_Set(1000);/*set timer is	1 sec --*/

   /*--- reset timer ---*/
   Timer_Reset(tmidx);

   printf("Press 'q' to stop log...\n");

   /*--- Get the time interval of the data logger */
   count=t_i;

   /*--- Initialize the	data ---*/
   data=0;
   mark=0;

   while(1)
   {

	       /*--- The timer arrives the preset time interval	*/
	       if( count <= tmArriveCnt[tmidx])
	       {
		  /*---	reset timer */
		  Timer_Reset(tmidx);

		  /*---	get time and date --*/
		   month=GetRTCtime(RTC_month);

		   day=GetRTCtime(RTC_day);

		   year=GetRTCtime(RTC_year);

		   hour=GetRTCtime(RTC_hour);

		   minute=GetRTCtime(RTC_min);

		   sec=GetRTCtime(RTC_sec);


		   /*--- Write the Date/Time to	the battery backup memory --*/
		   write_backup_ram(length*9+0,	month);
		   write_backup_ram(length*9+1,	day);
		   write_backup_ram(length*9+2,	year);
		   write_backup_ram(length*9+3,	hour);
		   write_backup_ram(length*9+4,	minute);
		   write_backup_ram(length*9+5,	sec);
		   write_backup_ram(length*9+6,	mark);
		   write_backup_ram(length*9+7,	data &0xff);
		   write_backup_ram(length*9+8,	(data>>8) &0xff);

		   /*--- update	data ---*/
		   data	+=10;
		   mark+=1;


		   if( length >	6000)
		   {
		     printf("memory is full, press any key to continue..\n");
		     getch();
		     break;
		   }
		   length++;
		   printf("current records number is %d , id=%d, count=%d.\n",length,tmidx,tmArriveCnt[tmidx]);
	       }





      if( kbhit())
      {
	c= getch();
	if( c == 'q' ||	c == 'Q')
	  break;
      }
   }
	/*--- Save record length	*/
	    write_backup_ram(0,	length &0xff);
	    write_backup_ram(1,	(length>>8) &0xff);

}
/*----------------------------------------------------*/
/*  Dump the stored data.			      */
/*						      */
/*----------------------------------------------------*/
void	dump_log()
{
  int index=0, total;
  static unsigned char	m1,d1,y1,h1,min1,s1,mark,dd1,dd2;
  int val;
  int length,i;
  char c;


	/*--- Read the record length stored in battery backup RAM --*/
	 dd1=	   read_backup_ram(0);
	 dd2=	   read_backup_ram(1);
	 total=dd2;
	 total *=0x100;
	 total+=dd1;

  while(1)
  {
    for(i=0;i<10;i++)
    {

       length=index*10+i+1;
       if( length < total)
       {
	 m1=   read_backup_ram(length*9+0);
	 d1 =  read_backup_ram(length*9+1);
	 y1 =  read_backup_ram(length*9+2);
	 h1 =  read_backup_ram(length*9+3);
	 min1= read_backup_ram(length*9+4);
	 s1 =  read_backup_ram(length*9+5);
	 mark =read_backup_ram(length*9+6);
	 dd1=  read_backup_ram(length*9+7);
	 dd2=  read_backup_ram(length*9+8);
	 val=dd2;
	 val *=0x100;
	 val+=dd1;
	 printf("Record %d,Date=%d-%d-%d Time=%d:%d:%d Value=%d \n",length,m1,d1,y1,h1,min1,s1,mark,val);
       }
    }
     printf(" 1:next 10 , 2: prev 10 , 0:exit");
     c=getch();
     printf("\n");
     if( c == '1')
     {
       if( index < (total/10))
	 index++;
     }
     if( c == '2')
     {
       if( index  > 0)
	 index--;
     }
     if( c == '0')
       break;
  }

}

