#include  <stdio.h>
#include  <conio.h>
#include  <stdlib.h>
#include <graphics.h>

#include  "ISO_LD.H"

#define ZERO 204
#define WORD int

WORD wBase=0x220,wActive=0,wIrq=15,wBuf[512];
int   Buffer[2000];

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

main()
{
char c;

load_configuration();
for(;;)
{
printf("\n**********************************************************");
show_configuration();
printf("\n**********************************************************");
printf("\n* 0 : Software Library Test                              *");
printf("\n* 1 : Hardware Board Configuration Setting               *");
printf("\n* 2 : DI configuration test                              *");
printf("\n* 3 : DIO test                                           *");
printf("\n* 4 : general test                                       *");
printf("\n* 5 : AD polling test                                    *");
printf("\n* 6 : AD Pacer trigger test                              *");
printf("\n* 7 : DA bias test                                       *");
printf("\n* 8 : AD polling long time test                          *");
printf("\n* 9 : AD pacer trigger and show wave                     *");
printf("\n* Q : Quit                                               *");
printf("\n**********************************************************");
printf("\nPress key to select function : ");

c=getch();
switch	(c)
	{
	case '0' : fun_0(); break;
	case '1' : fun_1(); break;
	case '2' : fun_2(); break;
	case '3' : fun_3(); break;
	case '4' : fun_4(); break;
	case '5' : fun_5(); break;
	case '6' : fun_6(); break;
	case '7' : fun_7(); break;
	case '8' : fun_8(); break;
	case '9' : fun_9(); break;
	case 'q' :
	case 'Q' : goto ret_label;
	}
}
ret_label:
return;
}

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

load_configuration()
{
FILE *stream;
int wRetVal;

wActive=0;
stream=fopen("ISO_LD.DAT","r");
if (stream)
   {
   fscanf(stream,"%x",&wBase);
   fscanf(stream,"%d",&wIrq);
   printf("\nLOAD ISO_LD.DAT OK");

   wRetVal=LD_CheckBoard(0,wBase,wIrq);
   if (wRetVal==NoError)
      {
      wActive=1;
      printf("\nSet Configuration OK");
      }
   else printf("\nSet Configuration Error, Error Code=%d",wRetVal);

   fclose(stream);
   }
else printf("\nOPEN ISO_LD.DAT ERROR");
}

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

show_configuration()
{
printf("\n");
printf("* wBase=%x",wBase);
printf(", wIrq=%d",wIrq);
printf("                                     *");
}

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

save_configuration()
{
FILE *stream;

stream=fopen("ISO_LD.DAT","w");
if (stream)
   {
   fprintf(stream,"%x\n",wBase);
   fprintf(stream,"%d\n",wIrq);
   fprintf(stream,"%d");
   printf("\nSAVE ISO_LD.DAT OK");
   fclose(stream);
   }
else printf("\nOPEN ISO_LD.DAT ERROR");
}

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

fun_0()
{
int i;
float f;

i=LD_ShortSub2(1,2);
printf("\nLibrary Test : LD_ShortSub2(1,2)=%d",i);
if (i==-1) printf(" --> OK"); else printf(" --> Error");
f=LD_FloatSub2(1.0,2.0);
printf("\nLibrary Test : LD_FloatSub2(1.0,2.0)=%f",f);
if (f==-1.0) printf(" --> OK"); else printf(" --> Error");
i=LD_GetVersion();
printf("\nSoftware Version=%x",i);
}

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

fun_1()
{
int i,j,k,l;

printf("\nwBASE   (ex. 220) = "); scanf("%x",&i);
printf("wIrq      (ex. 15) = "); scanf("%d",&j);
l=LD_CheckBoard(0,i,j);
if (l==0)
   {
   wBase=i; wIrq=j;
   wActive=1;
   printf("\nSet Configuration OK");
   save_configuration();
   return;
   }
printf("\nSet Configuration Error, Error Code=%d",l);
wActive=0;
}

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

fun_2()
{
int i,j,ok,err;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

ok=err=0;
for (i=0; i<256; i++)
    {
    LD_WriteDiMode(i);
    LD_ReadDiMode(&j);
    if (i==j) ok++; else err++;
    }
printf("\nok=%d, err=%d",ok,err);
}

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

fun_3()
{
int i,j,ok,err;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_WriteDiMode(0xff);	/* All DI Active High */
ok=err=0;
for (i=0; i<128; i++)
    {
    LD_WriteDo(i);
    delay(20);
    LD_ReadDi(&j);
    if (i==j) ok++; else {err++; printf("(%x,%x)",i,j);}
    }
printf("\nok=%d, err=%d",ok,err);
}

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

fun_4()
{
int i,j,ok,err,wChannel,wGain,wCount,wLedBase,wLedDelta,wDa;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

for (i=0; i<4; i++)    /* test AD channel 0 : normal AD */
    {
    LD_SetChannelGain(0,i);
    LD_ReadChannelGain(&wChannel, &wGain);
    printf("\nwChannel=%d, wGain=%d",wChannel,wGain);
    }
printf("\n");
for (i=0; i<4; i++)   /* test AD channel 1 : loadcell input AD */
    {
    LD_SetChannelGain(1,i);
    LD_ReadChannelGain(&wChannel, &wGain);
    printf("\nwChannel=%d, wGain=%d",wChannel,wGain);
    }
printf("\n");


LD_SetPacerTimer(99);	/* 20K */
LD_ReadPacerTimer(&wCount); printf("\n20K : wCount=%d",wCount);
LD_SetPacerTimer(999);	 /* 1K */
LD_ReadPacerTimer(&wCount); printf("\n1K  : wCount=%d",wCount);

LD_SetLedConfiguration(0x01, 0x23);
LD_ReadLedConfiguration(&wLedBase, &wLedDelta);
printf("\nwLedBase=%x, wLedDelta=%x",wLedBase,wLedDelta);
LD_SetLedConfiguration(0x34, 0x12);
LD_ReadLedConfiguration(&wLedBase, &wLedDelta);
printf("\nwLedBase=%x, wLedDelta=%x",wLedBase,wLedDelta);
printf("\n");

ok=err=0;
for (i=0; i<0xfff; i+=0x10)
    {
    LD_WriteDaBias(i);
    LD_ReadDaBias(&wDa);
    if (i==wDa) ok++; else err++;
    }
printf("\nDaBias : ok=%d, err=%d",ok,err);
}

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

fun_5()
{
int i,j,ok,err,wAd;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_WriteDaBias(0);	/* DA BIAS value=0 */
LD_SetPacerTimer(999);	/* 1K */
LD_SetChannelGain(0,0); /* channel_0, gain=1 */
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_0 : AD DATA=%3d",wAd);

LD_SetChannelGain(1,0); /* channel_1, gain=1 */
delay(1000);		/* delay settling time */
for (i=0; i<20; i++)
{
LD_ReadAd(&wAd); printf("\nChannel_1 : AD DATA=%3d",wAd);
}
printf("\npress any key to continue"); getch();
}

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

fun_6()
{
int i,j,ok,err,wAd,wFifoData;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_ClearFifo();
LD_WriteDaBias(0);	/* DA BIAS value=0 */
LD_SetPacerTimer(999);	/* 1K */
LD_SetChannelGain(1,0); /* channel_1, gain=1 */
delay(1000);		/* delay settling time */

LD_StartAdsPacer(1);	/* 256 counts */
i=0;
for (;;)
    {
    j=LD_ReadFifo(&wFifoData);
    if (j==NoError)
       {
       printf("[%3d]",wFifoData); i++;
       }
    if (i==256) goto ret_label;
    }

ret_label:
printf("\npress any key to continue"); getch();
}

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

fun_7()
{
int i,j,ok,err,wAd;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_SetPacerTimer(999);	/* 1K */
LD_SetChannelGain(1,0); /* channel_1, gain=1 */
LD_WriteDaBias(0x100);
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_1 : AD DATA=%x",wAd);
LD_WriteDaBias(0x300);
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_1 : AD DATA=%x",wAd);
LD_WriteDaBias(0x500);
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_1 : AD DATA=%x",wAd);
LD_WriteDaBias(0x700);
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_1 : AD DATA=%x",wAd);
}

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

fun_8()
{
int i,j,ok,err,wAd;
float min,max,sum,ave,count;

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_WriteDaBias(0);	/* DA BIAS value=0 */
LD_SetPacerTimer(999);	/* 1K */
LD_SetChannelGain(0,0); /* channel_0, gain=1 */
delay(1000);		/* delay settling time */
LD_ReadAd(&wAd); printf("\nChannel_0 : AD DATA=%3d",wAd);

LD_SetChannelGain(1,0); /* channel_1, gain=1 */
delay(1000);		/* delay settling time */

min=4096; max=0; count=0;
for(;;)
{
sum=0.0; count+=1.0;
for (i=0; i<20; i++)
    {
    if (LD_ReadAd(&wAd)==NoError) sum+=wAd;
    else {printf("\nError in ReadAd"); goto ret_label;}
    }
    ave=sum/20.0;
    if (ave<min) min=ave;
    if (ave>max) max=ave;
    printf("\nave=%.1f, min=%.1f, max=%.1f, count=%.0f",ave,min,max,count);
    if (kbhit()!=0) {getch(); goto ret_label;}
}
ret_label:
    printf("\npress any key to continue"); getch();
}

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

fun_9()
{
int i,j,ok,err,wAd,wFifoData;
int  x1,x2,y1,y2,io_base;
char buf[80];

init();

if (wActive==0)
   {
   i=LD_CheckBoard(0,wBase,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

LD_ClearFifo();
printf("\nDA Bias="); scanf("%d",&i);
LD_WriteDaBias(i);	/* DA BIAS value */
LD_ReadDaBias(&j);	/* DA BIAS value read back*/
printf("DA BIAS value readback=%d",j);

printf("\nPacer clk="); scanf("%d",&i);
LD_SetPacerTimer(i);
LD_ReadPacerTimer(&j);
printf("Pacer clock readback=%d",j);

printf("\nGain_code of channel 1="); scanf("%d",&i);
LD_SetChannelGain(1,i); /* channel_1, gain code=i */
LD_ReadChannelGain(1,&j);
printf("Gain_code readback=%d",j);
printf("\npress any key to start testing"); getch();

delay(1000);		/* delay settling time */
try_again:
LD_ClearFifo();
for (i=0; i<512; i++) Buffer[i]=0;
LD_StartAdsPacer(1);	/* 256*1=256 counts */
i=0;
for (;;)
    {
    j=LD_ReadFifo(&wFifoData);
    if (j==NoError)
       {
       Buffer[i]=wFifoData;
       i++;
       }
    else
       {
       printf("ReadFifo Error, Ret=%d",j); getch();
       goto ret_label;
       }
    if (i==256) goto show_label;
    }

show_label:
	x1=0;
	j=Buffer[0]&0x0fff;
	y1=j>>5;
	y1+=ZERO;
	for(i=1; i<127;  i++)
		{
		j=Buffer[i]&0x0fff;
		j=j>>5;
		j+=ZERO;
		x2=i*5; y2=j;
		line(x1,y1,x2,y2);
		x1=x2; y1=y2;
	}

	for(i=0; i<127;  i++)
		{
		j=Buffer[i]&0x0fff;
		j=j>>5;
		j+=ZERO;
		putpixel(i*5,j,RED);
		putpixel(i*5,j+1,RED);
		putpixel(i*5+1,j,RED);
		putpixel(i*5+1,j+1,RED);
		}

	j=ZERO+64;
	for (i=0; i<639; i++)  putpixel(i,j,GREEN);
	for (i=0; i<639; i++)  putpixel(i,j-65,GREEN);
	for (i=0; i<639; i++)  putpixel(i,j+65,GREEN);
	for (j=ZERO-65; j<ZERO+65; j++) putpixel(0,j+64,GREEN);
	for (j=ZERO-65; j<ZERO+65; j++) putpixel(639,j+64,GREEN);

	for(i=0; i<16;	i++)
	{
	Buffer[i]=Buffer[i]&0x0fff;
	sprintf(buf,"POLLING : [%02d] --> [%03xH]",i,Buffer[i]);
	outtextxy(5,5+10*(i+3),buf);
	}
	delay(1000);
	if (kbhit()!=0) {getch(); goto ret_label;}
	cleardevice();
	goto try_again;
ret_label:
printf("\npress any key to continue"); getch();
cleardevice();
closegraph();
}

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

init()
{
int driver,mode,char_size;

driver=EGA;
mode=EGAHI;
initgraph(&driver,&mode,"");
setactivepage(0);
setvisualpage(0);
setcolor(15);
setbkcolor(0);
settextstyle(SMALL_FONT,HORIZ_DIR,6);
}

