/**************************************************/
/* Constants and functions for the DISPLAY module */
/**************************************************/

/***** Files to include: *****/
#ifndef DISPLAY_LOADED
#include "display.h"
#endif

/***** Constants *****/

#ifdef SIMUL
#define PM_VALUE 1
#else
#define PM_VALUE 12
#endif

/***** Printing functions for simulation} *****/

#if defined SIMUL || defined SIMULRT

static char* clear_blanks (s) 
char *s; {
   char *res;
   for (res=s; (*res)==' '; res++);
   return res;
}

char* _MAIN_DISPLAY_TYPE_to_text (md)
MAIN_DISPLAY_TYPE md; {
   static char res[14];
   static char seconds[4];
   static timemode[5];
   
   if ( md.seconds[0] != '\0') {
      sprintf(seconds, ":%s", clear_blanks(md.seconds));
   }
   else {
      seconds[0] = '\0';
   }
   if ( md.timemode[0] != '\0' ) {
      sprintf(timemode, " %s",md.timemode);
   }
   else {
      timemode[0] = '\0';
   }

   sprintf (res, 
            "%s:%s%s%s", 
            clear_blanks(md.hours),
            clear_blanks(md.minutes),
            seconds,
            timemode);

   return res;
}

char* _MINI_DISPLAY_TYPE_to_text (md)
MINI_DISPLAY_TYPE md; {
   static char res[6];
   sprintf (res, "%s%s%s", 
                 clear_blanks(md.fst),
                 clear_blanks(md.sep), 
                 clear_blanks(md.snd));
   return res;
}

static char *position_name [7] = {"seconds", "hours", "10 minutes",
                                  "minutes", "month", "day", "day name"};

char* _DISPLAY_POSITION_to_text (dp)
DISPLAY_POSITION dp; {
   static char res[11];
   sprintf (res, "%s", position_name[dp]);
   return res;
}

#endif

/***** The WATCH_TIME_TO_MAIN_DISPLAY function *****/

MAIN_DISPLAY_TYPE WATCH_TIME_TO_MAIN_DISPLAY (wt)
WATCH_TIME_TYPE wt; {
   static MAIN_DISPLAY_TYPE maindt;
   static hours;
   if (wt.mode12h) {
     hours = wt.hours % PM_VALUE;
     if (!hours) hours=PM_VALUE;
   }
   else {
     hours=wt.hours;
   }
   sprintf(maindt.hours,"%2d",hours);
   sprintf(maindt.minutes,"%02d",wt.minutes);
   sprintf(maindt.seconds,"%02d",wt.seconds);
   if (wt.mode12h) {
      if (wt.hours>(PM_VALUE-1))
         strcpy(maindt.timemode,"PM");
      else strcpy(maindt.timemode,"");
   } else strcpy(maindt.timemode,"24H");

   return(maindt);
}
                           
/***** The WATCH_TIME_TO_MINI_DISPLAY function *****/

MINI_DISPLAY_TYPE WATCH_TIME_TO_MINI_DISPLAY (wt)
WATCH_TIME_TYPE wt; {
   static MINI_DISPLAY_TYPE minidt;
   sprintf(minidt.fst,"%2d",wt.hours);
   strcpy(minidt.sep,":");
   sprintf(minidt.snd,"%02d",wt.minutes);
   return(minidt);
}

/***** The WATCH_DATE_TO_MINI_DISPLAY function *****/

MINI_DISPLAY_TYPE WATCH_DATE_TO_MINI_DISPLAY (wt)
WATCH_TIME_TYPE wt; {
   static MINI_DISPLAY_TYPE minidt;
   sprintf(minidt.fst,"%2d",wt.month+1);
   strcpy(minidt.sep,"-");
   sprintf(minidt.snd,"%2d",wt.day);
   return(minidt);
}


/***** The WATCH_DAY_TO_ALPHABETIC_DISPLAY function *****/

static char *day_names [7] = {"SU","MO","TU","WE","TH","FR","SA"};

char * WATCH_DAY_TO_ALPHABETIC_DISPLAY (wt)
WATCH_TIME_TYPE wt; {
   return(day_names[wt.day_in_week]);
}

/***** The WATCH_DISPLAY_POSITION function *****/

DISPLAY_POSITION WATCH_DISPLAY_POSITION (wtp)
WATCH_TIME_POSITION wtp; {
   return(wtp);
}

/***** The STOPWATCH_TIME_TO_MAIN_DISPLAY function *****/

MAIN_DISPLAY_TYPE STOPWATCH_TIME_TO_MAIN_DISPLAY (st)
STOPWATCH_TIME_TYPE st; {
   static MAIN_DISPLAY_TYPE maindt;
   sprintf(maindt.hours,"%2d",st.minutes);
   sprintf(maindt.minutes,"%02d",st.seconds);
   sprintf(maindt.seconds,"%02d",st.hundredths);
   strcpy(maindt.timemode,"");
   return(maindt);
}

/***** The ALARM_TIME_TO_MAIN_DISPLAY function *****/

MAIN_DISPLAY_TYPE ALARM_TIME_TO_MAIN_DISPLAY (at)
ALARM_TIME_TYPE at; {
   static MAIN_DISPLAY_TYPE maindt;
   static hours;
   if (at.mode12h) {
     hours = at.hours % PM_VALUE;
     if (!hours) hours = PM_VALUE;
   }
   else {
     hours=at.hours;
   }
   sprintf(maindt.hours,"%2d",hours);
   sprintf(maindt.minutes,"%02d",at.minutes);
   sprintf(maindt.seconds,"");
   if (at.mode12h) {
     if (at.hours > PM_VALUE-1)
        strcpy(maindt.timemode,"PM");
     else strcpy(maindt.timemode,"");
   } else strcpy(maindt.timemode,"24H");
   
   return(maindt);
}

/***** The ALARM_DISPLAY_POSITION function *****/

ALARM_DISPLAY_POSITION (atp)
ALARM_TIME_POSITION atp; {
   return(atp+1);
}

