#include "forms.h"
#include "rtty_f.h"
#include "common.h"

extern FD_rtty_form *fd_rtty_form;

/*** callbacks and freeobj handles for form rtty_form ***/

void display_mode(FL_OBJECT *ob, long data)
{
	disdp->mode=data;
	redisp=1;
}

/*********************************************************/

void set_data_bits(FL_OBJECT *ob, long data)
{
	fskdp->nbit=data;
        if (data==5) {fskdp->nstop05=1; update_rtty_form();}
}

void set_stop_bits(FL_OBJECT *ob, long data)
{
	fskdp->nstop=data;
	if(data==3) {
		fskdp->nstop=1;
		fskdp->nstop05=1;
	} else  fskdp->nstop05=0;
}

void set_parity(FL_OBJECT *ob, long data)
{
	fskdp->parity=data;
}

/*------------------------------------------------------*/

const int baudlist[]=BAUD_LIST;
static int baudp=0;
void adj_baudp(int dr)
{
	for (baudp=0;;baudp++) {
		if (baudlist[baudp]==0 || baudlist[baudp]>dr){baudp--; break;}
	}
	if (baudp<0) baudp=0;
}

void set_baud(FL_OBJECT *ob, long data)
{
	unsigned int dr;
	dr=atoi(fl_get_input(ob));
	fskdp->dr=dr;
	fskdp->phs=(dr<<16)/FS;	/* bit phase / sample */
	set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
	adj_baudp(dr);
	redisp=1;
}

void set_baud_st(FL_OBJECT *ob, long data)
{
	char buf[40];
	
	baudp+=data;
	if (baudp<0) baudp=0;
	if (baudlist[baudp]==0) baudp--;
	sprintf(buf,"%d",baudlist[baudp]);
	fl_set_input(fd_rtty_form->sbaud,buf);
	fskdp->dr=baudlist[baudp];
	fskdp->phs=(baudlist[baudp]<<16)/FS;
	set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
	redisp=1;
}

void set_space(FL_OBJECT *ob, long data)
{
	fskdp->spacef=fl_get_counter_value(ob);
	set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
	redisp=1;
}

void set_mark(FL_OBJECT *ob, long data)
{
	fskdp->markf=fl_get_counter_value(ob);
	set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
	redisp=1;
}

void swap_but(FL_OBJECT *ob, long data)
{
        float fs,fm;
        fs=fl_get_counter_value(fd_rtty_form->sspace);
        fm=fl_get_counter_value(fd_rtty_form->smark);
        fl_set_counter_value(fd_rtty_form->sspace,fm);
        fl_set_counter_value(fd_rtty_form->smark,fs);
        fskdp->spacef=fm;
        fskdp->markf=fs;
        set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
        redisp=1;
}

void set_mode(FL_OBJECT *ob, long data)
{
        static float cw_f=1000.,cw_cwt=10.;
        static float rtty_f1=1000.,rtty_f0=1170.;
        static float hdlc_f1=1000.,hdlc_f0=1300.;
        static int rtty_dr=45,hdlc_dr=300,nbit=5,nstop=1,nstop05=1;

        //saving current mode settings
        switch(fskdp->mode) {
        case RTTY: rtty_f0=fskdp->spacef;
                   rtty_f1=fskdp->markf;
                   rtty_dr=fskdp->dr;
                   nbit=fskdp->nbit;
                   nstop=fskdp->nstop;
                   nstop05=fskdp->nstop05;
                   break;
        case HDLC: hdlc_f0=fskdp->spacef;
                   hdlc_f1=fskdp->markf;
                   hdlc_dr=fskdp->dr;
                   break;
        case CW:   cw_f=fskdp->markf;
                   cw_cwt=fskdp->cwt;
                   break;
        }

        fskdp->mode=data;

        switch(data) {
        case CW:   fskdp->markf=cw_f;
                   fskdp->dr=30; fskdp->phs=(fskdp->dr<<16)/FS;
                   fskdp->cwt=cw_cwt;
                   set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
                   break;
        case RTTY: fskdp->spacef=rtty_f0;
                   fskdp->markf=rtty_f1;
                   fskdp->dr=rtty_dr;
                   fskdp->nbit=nbit;
                   fskdp->nstop=nstop;
                   fskdp->nstop05=nstop05;
                   fskdp->phs=(fskdp->dr<<16)/FS;
                   set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
                   break;
        case HDLC: fskdp->spacef=hdlc_f0;
                   fskdp->markf=hdlc_f1;
                   fskdp->dr=hdlc_dr;
                   fskdp->phs=(fskdp->dr<<16)/FS;
                   set_filters(fskdp->spacef,fskdp->markf,fskdp->dr);
                   break;
        }
        update_rtty_form();

        redisp=1;
}

void atim_but(FL_OBJECT *ob, long data)
{
        fskdp->cwatim=fl_get_button(ob);
}

void tim_sl(FL_OBJECT *ob, long data)
{
        fskdp->cwt=fl_get_slider_value(ob);
}

void update_tim()
{
        fl_set_slider_value(fd_rtty_form->tim_sl,fskdp->cwt);
}

void save_but(FL_OBJECT *ob, long data)
{
        save_settings();
}
