/*
   LCDCLK2.C

   Any board that has a native LCD port or native PLC-bus
   and a real-time clock.

   Make a very expensive digital clock.
   Shows how to read the real-time clock and output strings to
   the KDM
*/

#use "default.h"
#ifdef LK_COLS
#undef LK_COLS
#endif
#define LK_COLS 20

#ifdef LK_LINES
#undef LK_LINES
#endif
#define LK_LINES 4
#define KEYPAD_SIZE 24

/**   Use for KDM-4X40
#ifdef LK_COLS
#undef LK_COLS
#endif
#define LK_COLS 40
#define KEYPAD_SIZE 40
*/

#use "kdm.lib"

/* RTC information structure */

struct tm dt;

// structure already defined in drivers.lib
/*
{
   char tm_sec;
   char tm_min;
   char tm_hour;
   char tm_mday;
   char tm_mon;
   char tm_year;
   char tm_wday;
} dt;       
*/

char *days[]={"Sunday", "Monday", "Tuesday","Wednesday","Thursday",
               "Friday","Saturday"};

char *months[]={"January", "Febuary", "March", "April", "May", "June", 
                "July", "August","September", "October", "November", 
                "December"};

main ()
{
    int k, ok, start_pos, led_on, last_key;
    char old_day, old_sec, old_hour;
    char strbuf [LK_COLS+1];

    outport (DCNTL, 0x30);    /* set I/O wait states to 4 */
    old_day = 0;     /* init to zero to get first display */
    old_hour = 0;
    old_sec = 0;
    led_on = 0;
    ok = 1;
    k = -2;

    lk_init_keypad ();    /* initialize and blank LCD */
    lk_printf ("\x1b0");  /* turn off cursor */

    while (ok)
    {
      if ((tm_rd (&dt)) != 0) /* make sure the clock is there */
      {
          lk_printf ("Clock not available");
          lk_setbeep (1000);
          ok = 0;
      }
      else
      {
          if (old_day != dt.tm_mday)   /* has the date changed? */
          {
             start_pos = (LK_COLS - strlen (days [dt.tm_wday])) / 2;
             lk_pos (0, start_pos);
             lk_printf ("\x1bc%s", days [dt.tm_wday]);

             sprintf (strbuf, "%s %d, %d", months [dt.tm_mon - 1], 
                        dt.tm_mday, dt.tm_year + 1900);
             start_pos = (LK_COLS - strlen (strbuf)) / 2;
             lk_pos (1, start_pos);
             lk_printf ("\x1bc%s", strbuf);  

             old_day = dt.tm_mday;
         }

         if (old_sec != dt.tm_sec)       /* has the time changed? */
         {
             start_pos = (LK_COLS - 8) / 2;
             lk_pos (2, start_pos);
             lk_printf ("\x1bc"); /* position - line 3 col 6, EOL */
             lk_printf ("%d:%02d:%02d", dt.tm_hour, dt.tm_min, dt.tm_sec);
             old_sec = dt.tm_sec;
    
             if (old_hour != dt.tm_hour)
             {
               lk_setbeep (500);
               old_hour = dt.tm_hour;
             }
             else
               lk_setbeep (2);
    
            lk_led (led_on);
            led_on = (led_on == 1) ? 2 : 1;
    
            if ((k = lk_kxget (0) - 1) != last_key) /* test for new key */
            {
               if (last_key >= 0)                   /* clear last block? */
               {
                  if (LK_COLS <= KEYPAD_SIZE && last_key >= LK_COLS)
                     lk_pos (3, last_key - LK_COLS);  /* did it wrap? */
                  else
                     lk_pos (3, last_key);            /* no, just do it */
                  lk_putc (' ');
               }
            
               if (k >= 0)                         /* it it a key? */
               {
                  if (LK_COLS <= KEYPAD_SIZE && k >= LK_COLS)
                     lk_pos (3, k - LK_COLS);
                  else
                     lk_pos (3, k);
                  lk_putc ('\xff');                      /* output a block */
               }
               last_key = k;
            }
         }
      }     /* end if clock */
      hitwd();
    }       /* end of while loop */

   while (1) hitwd();
}

/*************************************************End of LCDCLK2.C */
