/*
 * hifi.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: hifi.c,v $
 * Revision 1.4  2000/10/07 18:30:06  root
 * #
 *
 * Revision 1.3  2000/10/06 19:52:20  root
 * #
 *
 * Revision 1.2  2000/09/24 01:12:43  root
 * #
 *
 * Revision 1.1  2000/09/24 01:11:47  root
 * #
 *
 * Revision 1.5  2000/02/21 04:11:27  root
 * #
 *
 * Revision 1.4  2000/02/21 04:01:00  root
 * #
 *
 * Revision 1.3  2000/02/20 03:03:57  root
 * #
 *
 * Revision 1.2  2000/02/20 03:03:53  root
 * #
 *
 * Revision 1.1  2000/02/20 02:52:13  root
 * #
 *
 *
 */

static char rcsid[] = "$Id: hifi.c,v 1.4 2000/10/07 18:30:06 root Exp $";

#include "project.h"


void
system_st_message (Byte unit, Word cmd, Byte * data, Word len)
{
}

void
cd_st_message (Byte unit, Word cmd, Byte * data, Word len)
{
}
void
tuner_st_message (Byte unit, Word cmd, Byte * data, Word len)
{
  Hifi_Tuner_State s;
  char buf[1024], *ptr = buf;

  data = hifipkt_dtuner_state (data, &s);

  printf ("Soup %d\n", s.f.freq);

  if (s.f.freq > 1000000)
    {
      DWord f = s.f.freq / 1000000;
      DWord r = s.f.freq - (f * 1000000);
      r = r / 1000;

      ptr += sprintf (ptr, "Freq %3d.%03dMHz ", (int) f, (int) r);
    }
  else
    {
      DWord f = s.f.freq / 1000;
      DWord r = s.f.freq - (f * 1000);
      r = r / 100;
      ptr += sprintf (ptr, "Freq %3d.%1dkHz ", (int) f, (int) r);
    }




  if (s.lock)
    {
      ptr += sprintf (ptr, "[ locked  ] ");
    }
  else
    {
      ptr += sprintf (ptr, "[no signal] ");
    }

  if (s.stereo)
    {
      ptr += sprintf (ptr, "[stereo]");
    }
  else
    {
      ptr += sprintf (ptr, "[ mono ]");
    }

  lcd_wr_buf (lcd, buf, strlen (buf));


}

void
hifi_valid_cmd (Byte sclass, Byte sunit, Byte dclass, Byte dunit, Word cmd,
                Byte * data, Word len)
{

  if ((dclass != HIFI_CLASS_ALL) && (dclass != HIFI_CLASS_DISPLAY))
    return;
  if ((dunit != HIFI_UNIT_ALL) && (dunit != lcdunit))
    return;


  printf ("Fish\n");
  switch (sclass)
    {
    case HIFI_CLASS_TUNER:
      tuner_st_message (sunit, cmd, data, len);
      break;
    case HIFI_CLASS_CD:
      cd_st_message (sunit, cmd, data, len);
      break;
    case HIFI_CLASS_SYSTEM:
      system_st_message (sunit, cmd, data, len);
      break;
    }

}

void
hifi_dispatch ()
{
  extern void *msglib;

  Byte buf[1024];
  int len;
  int i;

  Word w, l;
  Byte class, unit, d;

  len = hifimsg_recv (msglib, buf, sizeof (buf));
  if (len <= 0)
    return;


#define DEBUG
#ifdef DEBUG
  {
    putchar ('>');
    for (i = 0; i < len; ++i)
      {
        printf (" %02x", buf[i]);
      }
    putchar ('\n');
    fflush (stdout);
  }
#endif

  hifi_new_packet (msglib, buf, len, hifi_valid_cmd);

}
