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

  File: C.PLOTCOMM

  Inhoud: int XscaleCi(char *p), int CopyCi(char *p),int ImagCi (char *p),
          int RealCi (char *p), int BodeCi (char *p),int DoffCi (char *p),
          int DonCi (char *p), int DisplayCi(char *p), int InfoCi(char *p),
          int PrintCi(char *p),int ListCi(char *p)

  Schrijvers: Edwin Zoer & Erwin Waterlander
              HTS Enschede
  Datum:  juni 1993

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


#include <string.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include "allegro.h"  /* only for TRUE definition */
#include "image.h"
#include "parse.h"
#include "commnd.h"
#include "errors.h"
#include "imagwind.h"
#include "asm.h"
#include "plotutil.h"

extern BOOL don;
extern BOOL bon;
extern VENSTER* vp[MAXSIG];
extern unsigned short MAX_LENGTH;
extern unsigned short SAMPLE_RATE;
extern int number_of_signals;
extern int current_signal;  /* The actual signal drawn to screen */


/*****************************************************************************
*   XscaleCi    schaalfactor van de horizontale as aanpassen                 *

    Input:   -Signaalnaam
             -Horizontale schaalfaktor
    Output:  -Maakt een nieuw drawobject voor het signaal.
    Returns: -ERR_SIGNAL: signaal bestaat niet.
             - 0: geen error.
******************************************************************************/

int XscaleCi(char *p)
{
	int im;      /*Venster-nummer */
	char *signalname;
	char buffer[128];
	double hscale;

	signalname = strarg(&p,NULL,"Signal","a",buffer);
	hscale = doublearg(&p,NULL,"Scalefactor",0,10,1);

	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (hscale == 0.0)
	 {
		asm_wprintf("Illegal scale value: %f",hscale);
		return 0;
	 }

	Window_HScale(vp[im]) = hscale;


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im]) = TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
	return 0;

}


/********************************************************************************
*   CopyCi copieert een signaal naar een ander venster (+data)
*********************************************************************************/

int CopyCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal","b",buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */


	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

	out_im = Find_Signal_Venster(out_signalname);      /* Vind het juiste venster */
	if (out_im == -1)                                  /* Signaal bestond nog niet */
	{
		out_im = Create_Venster(out_signalname,alloc_size);
		if (out_im == -1) return ERR_WINDOWS;             /* Te veel displaywindows */
	}
	else if (in_im != out_im) Realloc_Venster(vp[out_im],alloc_size);

	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=Window_Record(vp[in_im]);
	Window_Channel(vp[out_im])=Window_Channel(vp[in_im]);
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);


	for (i=0;i < DataLength(vp[out_im])*DataRecords(vp[out_im])*DataChannels(vp[out_im]);i++)
	{

		real_data_out[i] = real_data_in[i];
		imag_data_out[i] = imag_data_in[i];
	}

	if (don)
	{
		make_plot(vp[out_im]);
		current_signal = out_im;
		Window_InUse(vp[out_im]) = TRUE;
	}
	else
		Window_InUse(vp[out_im]) = FALSE;
	return 0;
}


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


int ImagCi (char *p)
{
	int im;
	char *signalname;
	char buffer[128];
	unsigned short record_nr, channel_nr;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1) return ERR_SIGNAL;        /* Signaal bestaat niet */

	channel_nr = (unsigned short) intarg(&p,NULL,"Channel",0,DataChannels(vp[im])-1,0);
	record_nr  = (unsigned short) intarg(&p,NULL,"Record",0,DataRecords(vp[im])-1,0);

	if ((DataType(vp[im]) == REAL) && (Window_Mode(vp[im]) == REAL_M))
	{
		return ERR_NO_IMAG;
	}

	if( !( (DataDomain(vp[im]) == TIME) || (DataDomain(vp[im]) == FREQ) ) )
		return ERR_DOMAIN;    /* Geen tijd of frequentie domein */

	Window_Record(vp[im]) = record_nr;
	Window_Channel(vp[im]) = channel_nr;
	Window_Mode(vp[im]) = IMAG_M;


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im]) = TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
	return 0;
}


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

int RealCi (char *p)
{
	int im;
	char *signalname;
	char buffer[128];
	unsigned short record_nr,channel_nr;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1) return ERR_SIGNAL;        /* Signaal bestaat niet */

	channel_nr = (unsigned short) intarg(&p,NULL,"Channel",0,DataChannels(vp[im])-1,0);
	record_nr  = (unsigned short) intarg(&p,NULL,"Record",0,DataRecords(vp[im])-1,0);


	if ((DataType(vp[im]) == IMAG) && (Window_Mode(vp[im]) == IMAG_M))
	{
		return ERR_NO_REAL;
	}

	if( !( (DataDomain(vp[im]) == TIME) || (DataDomain(vp[im]) == FREQ) ) )
		return ERR_DOMAIN;    /* Geen tijd of frequentie domein */

	Window_Record(vp[im]) = record_nr;
	Window_Channel(vp[im]) = channel_nr;

	Window_Mode(vp[im]) = REAL_M;

	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im]) = TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
	return 0;
}


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

int BodeCi (char *p)
{
	int im,mid_type,channel_nr;
	char *signalname;
	char buffer[128];

	signalname = strarg(&p,NULL,"Signal","a",buffer);


	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1) return ERR_SIGNAL;        /* Signaal bestaat niet */

	if (DataDomain(vp[im]) != FREQ) return ERR_DOMAIN;

	channel_nr = (unsigned short) intarg(&p,NULL,"Channel",0,DataChannels(vp[im])-1,0);
	mid_type   = intarg(&p,NULL,"Average-type",0,1,0);


	Window_Mode(vp[im]) = BODE_M;
	Window_Channel(vp[im]) = channel_nr;

	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im]) = TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;

	return 0;

}


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

int DoffCi (char *p)
{
	don=FALSE;
	asm_wprintf("Display is OFF");
	return 0;
}

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

int DonCi (char *p)
{
	don=TRUE;
	asm_wprintf("Display is ON");
	return 0;
}

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

int BoffCi (char *p)
{
	bon=FALSE;
	asm_wprintf("Bargraph is OFF");
	return 0;
}

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

int BonCi (char *p)
{
	bon=TRUE;
	asm_wprintf("Bargraph is ON");
	return 0;
}


/********************************************************************************
*   DisplayCi displayed een signaal (+data)
*********************************************************************************/

int DisplayCi(char *p)
{
	int im;  /* Vensternummer */
	char *signalname;
	char buffer[128];

	signalname = strarg(&p,NULL,"Input Signal","a",buffer);

	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	Window_Channel(vp[im]) = (unsigned short) intarg(&p,NULL,"Channel",0,DataChannels(vp[im])-1,0);
	Window_Record(vp[im])  = (unsigned short) intarg(&p,NULL,"Record",0,DataRecords(vp[im])-1,0);

	make_plot(vp[im]);
	current_signal = im;
	Window_InUse(vp[im]) = TRUE;

	return 0;
}

/********************************************************************************
*   InfoCi geeft de informatie uit de header in het commando-window
*********************************************************************************/

int InfoCi(char *p)
{
	int im;  /* Vensternummer */
	char *signalname;
	char buffer[128];

	signalname = strarg(&p,NULL,"Input Signal","a",buffer);

	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	asm_wprintf("PixelFormat        : %d \n",DataPixelFormat(vp[im]));
	asm_wprintf("RecordLength       : %d \n",DataLength(vp[im]));
	asm_wprintf("Number of Records  : %d \n",DataRecords(vp[im]));
	asm_wprintf("Number of Channels : %d \n",DataChannels(vp[im]));
	asm_wprintf("Fileseq. Nr.       : %d \n",DataFileSeq(vp[im]));
	asm_wprintf("Bits per sample    : %d \n",DataBits_p_s(vp[im]));
	asm_wprintf("Samplerate         : %d \n",DataSampleRate(vp[im]));
	asm_wprintf("Domain             : %d \n",DataDomain(vp[im]));
	asm_wprintf("Type               : %d \n",DataType(vp[im]));
	asm_wprintf("ASM-Id String      : %s \n",DataIdString(vp[im]));
	asm_wprintf("Signal-Name        : %s \n",DataSignalName(vp[im]));
	asm_wprintf("UserText           : %s \n",DataUserText(vp[im]));
	asm_wprintf("Date               : %s \n",DataDate(vp[im]));
	asm_wprintf("Description        : %s \n",DataDescription(vp[im]));
	asm_wprintf("X Scale            : %f \n",Window_HScale(vp[im]));

	return 0;
}

/********************************************************************************
*   PrintCi print de data numeriek in het commando-window
*********************************************************************************/

int PrintCi(char *p)
{
	int im,i;  /* Vensternummer */
	char *signalname;
	char buffer[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;
	unsigned short channel,record;
	unsigned int offset;

	signalname = strarg(&p,NULL,"Input Signal","a",buffer);

	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	channel = (unsigned short) intarg(&p,NULL,"Channel",0,DataChannels(vp[im])-1,0);
	record  = (unsigned short) intarg(&p,NULL,"Record",0,DataRecords(vp[im])-1,0);

	real_data  = DataReal(vp[im]);
	imag_data  = DataImag(vp[im]);

	offset=(channel*DataRecords(vp[im]) + record)*DataLength(vp[im]);

	for (i=0;i < DataLength(vp[im]);i++)
	{

		asm_wprintf("%6d) Re: %g  Im: %g \n",i,real_data[i+offset],imag_data[i+offset]);
	}
	return 0;
}



/********************************************************************************
*   PrintCi print de data numeriek in het commando-window
*********************************************************************************/

int ListCi(char *p)
{
	int im;  /* Vensternummer */

	for (im=0;im < number_of_signals;im++)
	{
		asm_wprintf("Signal    : %s\n",DataSignalName(vp[im]));
		asm_wprintf(" Type     : %5d,",DataType(vp[im]));
		asm_wprintf(" Domain   : %5d\n",DataDomain(vp[im]));
		asm_wprintf(" Channels : %5d,",DataChannels(vp[im]));
		asm_wprintf(" Records  : %5d,",DataRecords(vp[im]));
		asm_wprintf(" Length   : %5d\n",DataLength(vp[im]));
	}
	return 0;
}
