/*
 * ogg.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id: tuner.c,v 1.7 2002/04/02 14:12:42 root Exp $";

/*
 * $Log: tuner.c,v $
 * Revision 1.7  2002/04/02 14:12:42  root
 * *** empty log message ***
 *
 * Revision 1.6  2001/01/30 06:16:33  root
 * #
 *
 * Revision 1.5  2000/12/01 04:42:23  root
 * #
 *
 * Revision 1.4  2000/11/23 23:45:30  root
 * #
 *
 * Revision 1.3  2000/10/07 18:30:11  root
 * #
 *
 * Revision 1.2  2000/10/06 19:52:46  root
 * #
 *
 * Revision 1.1  2000/10/06 06:21:36  root
 * #
 *
 */

#include "project.h"


static Panel_item volumeitem, freqitem, freqlitem, statusitem;

static void
tuner_tune (DWord f, int band, int rel)
{
  Hifi_Radio_Frequency rf;
  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  if (rel)
    {
      ptr =
        hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit,
                         HIFI_CLASS_TUNER, tunerunit);
    }
  else
    {
      ptr =
        hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_TUNER,
                     tunerunit);

    }
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_TUNER_TUNE);
  rf.band = band;
  rf.freq = f;
  ptr = hifipkt_eradio_frequency (ptr, rf);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

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

}
static void
rbut (Panel_item i)
{
  int j = (int) xv_get (i, PANEL_CLIENT_DATA);
  tuner_tune (j, HIFI_TUNER_Band_FM, 1);
}
static void
tcmd (Panel_item i)
{
  Word c = (Word) (int) xv_get (i, PANEL_CLIENT_DATA);

  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_TUNER,
                tunerunit, c);
}


static void
lproc (void)
{
  char *buf = (char *) xv_get (freqlitem, PANEL_VALUE);
  int i = atoi (buf);

  xv_set (freqitem, PANEL_VALUE, i / 10, 0);


  tuner_tune (i, HIFI_TUNER_Band_FM, 0);

}

static void
dofreq (Panel_item p, int i)
{
  char buf[80];
  i *= 10;
  tuner_tune (i, HIFI_TUNER_Band_FM, 0);

  sprintf (buf, "%d", i);
  xv_set (freqlitem, PANEL_VALUE, buf, 0);

}


static void
dovol (Panel_item p, int i)
{
  char *buf, *ptr, *cptr;


  buf = ptr = hifi_getbuf ();

  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_TUNER,
                 tunerunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_VOL);
  ptr = hifipkt_ebyte (ptr, i);
  ptr = hifipkt_ebyte (ptr, i);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

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


void
tuner_cmdst (Byte class, Byte unit, Word cmd, unsigned char *byte, int len)
{
  if (unit != tunerunit)
    return;

  switch (cmd)
    {
    case HIFI_ST_VOL:
      xv_set (volumeitem, PANEL_VALUE, (int) *byte, 0);
      break;

    case HIFI_ST_TUNER_STATE:
      {
        Hifi_Tuner_State s;
        char buf[80], *ptr = buf;


        byte = hifipkt_dtuner_state (byte, &s);

        sprintf (buf, "%d", s.f.freq);
        xv_set (freqlitem, PANEL_VALUE, buf, 0);
        xv_set (freqitem, PANEL_VALUE, s.f.freq / 10, 0);

        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 ]");
          }


        xv_set (statusitem, PANEL_VALUE, buf, 0);


      }
      break;

    }

}

void
tuner_makepanel (Panel p)
{
  int y = 5;

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Up",
             PANEL_CLIENT_DATA, HIFI_CMD_TUNER_SEARCH_UP,
             PANEL_NOTIFY_PROC, tcmd, XV_X, 5, XV_Y, y, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Down",
             PANEL_CLIENT_DATA, HIFI_CMD_TUNER_SEARCH_DOWN,
             PANEL_NOTIFY_PROC, tcmd, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Mono",
             PANEL_CLIENT_DATA, HIFI_CMD_TUNER_MONO,
             PANEL_NOTIFY_PROC, tcmd, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Stereo",
             PANEL_CLIENT_DATA, HIFI_CMD_TUNER_STEREO,
             PANEL_NOTIFY_PROC, tcmd, 0);

  y += 25;

  xv_create (p, PANEL_BUTTON, XV_X, 5, XV_Y, y,
             PANEL_LABEL_STRING, "Off",
             PANEL_CLIENT_DATA, 0, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "R1",
             PANEL_CLIENT_DATA, 98500000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "R2",
             PANEL_CLIENT_DATA, 88900000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "R3",
             PANEL_CLIENT_DATA, 91100000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "R4",
             PANEL_CLIENT_DATA, 93300000, PANEL_NOTIFY_PROC, rbut, 0);
  y += 25;

  xv_create (p, PANEL_BUTTON, XV_X, 5, XV_Y, y,
             PANEL_LABEL_STRING, "Camb",
             PANEL_CLIENT_DATA, 96000000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Q103",
             PANEL_CLIENT_DATA, 103000000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Vibe",
             PANEL_CLIENT_DATA, 105600000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Cafe",
             PANEL_CLIENT_DATA, 107900000, PANEL_NOTIFY_PROC, rbut, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "CFM",
             PANEL_CLIENT_DATA, 101525000, PANEL_NOTIFY_PROC, rbut, 0);
  y += 25;


  statusitem = xv_create (p, PANEL_TEXT,
                          PANEL_VALUE_DISPLAY_LENGTH, 40,
                          PANEL_READ_ONLY, TRUE,
                          PANEL_LABEL_STRING, "Status:", XV_X, 5, XV_Y, y, 0);
  y += 25;


  freqlitem = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 20,
                         PANEL_LABEL_STRING, "Frequency",
                         PANEL_NOTIFY_PROC, lproc, XV_X, 5, XV_Y, y, 0);
  y += 25;
  freqitem = xv_create (p, PANEL_SLIDER,
                        PANEL_MIN_VALUE, 8800000,
                        PANEL_MAX_VALUE, 10800000, PANEL_SLIDER_WIDTH, SLW,
                        PANEL_SHOW_RANGE, FALSE,
                        PANEL_SHOW_VALUE, FALSE,
                        PANEL_SLIDER_END_BOXES, TRUE,
                        PANEL_NOTIFY_PROC, dofreq,
                        PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;
  y += SLSEP;

  volumeitem = xv_create (p, PANEL_SLIDER,
                          PANEL_MIN_VALUE, 0,
                          PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                          PANEL_LABEL_STRING, "Vol:",
                          PANEL_SHOW_VALUE, FALSE,
                          PANEL_SHOW_RANGE, FALSE,
                          PANEL_SLIDER_END_BOXES, TRUE,
                          PANEL_NOTIFY_PROC, dovol,
                          PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;
}
