
static char rcsid[] = "$Id: clock.c,v 1.7 2001/01/30 06:16:24 root Exp $";

/*
 * $Log: clock.c,v $
 * Revision 1.7  2001/01/30 06:16:24  root
 * #
 *
 * Revision 1.6  2000/12/01 04:42:12  root
 * #
 *
 * Revision 1.5  2000/11/23 23:45:23  root
 * #
 *
 * Revision 1.4  2000/10/07 18:30:06  root
 * #
 *
 * Revision 1.3  2000/10/06 19:52:19  root
 * #
 *
 * Revision 1.2  2000/09/24 01:46:05  root
 * #
 *
 * Revision 1.1  2000/09/24 01:12:42  root
 * #
 *
 * Revision 1.4  1999/10/26 17:46:46  root
 * #
 *
 * Revision 1.3  1999/10/26 17:46:41  root
 * #
 *
 * Revision 1.2  1999/09/21 23:01:38  root
 * #
 *
 * Revision 1.1  1999/09/21 22:56:11  root
 * Initial revision
 *
 */


#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include "lcd.h"
#include "hifi.h"
#include "hifimsg.h"

#include "prototypes.h"

#include <sys/time.h>

Lcd l;
void *msglib;

int clockunit;

void
do_stuff (void)
{
  char *c;
  struct tm *tm;
  time_t t;

  time (&t);
  tm = localtime (&t);

  //lcd_pos (l, 0, 0);
  //lcd_wr (l, LCD_HOME, 0);
  //l->mpos = 0;
  //l->spos = 0;

  c = asctime (tm);
  //lcd_wr_buf (l, c, 24);

}


void
do_otherstuff (void)
{
  char *c;
  struct tm *tm;
  time_t t;
  char *buf, *ptr, *cptr;

  time (&t);
  tm = localtime (&t);

  c = asctime (tm);

  buf = hifi_getbuf ();
  ptr = buf;

  ptr = hifipkt_new_brd (ptr, HIFI_CLASS_CLOCK, clockunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_ST_CLOCK_TIME);
  bcopy (c, ptr, 24);
  ptr += 24;
  *ptr = 0;
  ptr++;
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (msglib, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}

main (int argc, char **argv)
{
  struct Lcd_struct ls = LCD_H2571;
  struct timeval tv, last, last2, sub, sep, sep2;
  fd_set readfds;


  clockunit = hifi_getunit ("clock");

  msglib = hifi_open (HIFI_CLASS_CLOCK, clockunit);

  //l = lcd_open (ls);

  //lcd_reset (l);

  //lcd_dpy_ctrl (l, 1, 0, 0);


  //lcd_cls (l);

  FD_ZERO (&readfds);

  timerclear (&last);
  timerclear (&last2);

  sep.tv_sec = 0;
  sep.tv_usec = 100000;
  sep2.tv_sec = 0;
  sep2.tv_usec = 1000000;

  while (1)
    {
      int fd = hifimsg_fd (msglib);

      FD_SET (fd, &readfds);

      tv.tv_usec = 100000;
      tv.tv_sec = 0;

      select (fd + 1, &readfds, NULL, NULL, &tv);

      if (FD_ISSET (fd, &readfds))
        {
          char buf[1024];

          (void) hifi_recv (msglib, buf, sizeof (buf));

        }



      gettimeofday (&tv, NULL);

      timersub (&tv, &last, &sub);
      if (timercmp (&sub, &sep, >))
        {
          last = tv;
          do_stuff ();
        }

      timersub (&tv, &last2, &sub);
      if (timercmp (&sub, &sep2, >))
        {
          last2 = tv;
          do_otherstuff ();
        }

    }



}
