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

#include  "ISO_AD32.H"
#define WORD int
#define ZERO 204

WORD wBase=0x220,wActive=0,wSingDiff=1,wIrq=15;
WORD Buffer[512];

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

main()
{
char c;

init();
load_configuration();
for(;;)
{
clear_screen(); set_pos(0,0);
printf("\n**********************************************************");
show_configuration();
printf("\n**********************************************************");
printf("\n* 1 : Hardware Board Configuration Setting               *");
printf("\n* 2 : Read JP1 Setting (Single-ended or Differential)    *");
printf("\n* 3 : Read AD (fixed channel) (channel_0) Waveform       *");
printf("\n* 4 : Read AD (channel scan) (0->1->2->3->0...) Waveform *");
printf("\n* Q : Quit                                               *");
printf("\n**********************************************************");
printf("\nPress key to select function : ");

c=getch();
switch	(c)
	{
	case '1' : fun_1(); break;
	case '2' : fun_2(); break;
	case '3' : fun_3(); break;
	case '4' : fun_4(); break;
	case 'q' :
	case 'Q' : goto ret_label;
	}
}
ret_label:
closegraph();
return;
}

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

set_pos(int x, int y)
{
union REGS regs;
regs.h.ah=2;
regs.h.dh=y;
regs.h.dl=x;
regs.h.bh=0;
int86(0x10,&regs,&regs);
}

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

clear_screen(void)
{
union REGS regs;
regs.h.ah=15;
int86(0x10,&regs,&regs);
regs.h.ah=0;
int86(0x10,&regs,&regs);

}

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

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


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

load_configuration()
{
FILE *stream;
int wRetVal;

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

   wRetVal=AD32_CheckBoard(0,wBase,wSingDiff,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_AD32.DAT ERROR");
}

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

show_configuration()
{
printf("\n");
printf("* wBase=%x",wBase);
printf(", wIrq=%d",wIrq);
if (wSingDiff == 0) printf(", Single-ended");
else printf(", Differential");
printf(" Type Analog Input     *");
}

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

save_configuration()
{
FILE *stream;

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

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

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

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

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

fun_2()
{
int i;

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

AD32_ReadSingDiff(&i);

printf("\nAD32_ReadSingDiff=%d",i);
if (i==0) printf(" --> Single-ended"); printf(" --> Differential");
}

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

fun_3()
{
int wBoard,wBase,wIrq,wRetVal,i,j,k,flag,l;
int wChannel,wMode,wConfig,wSettle,wData;
int wLow,wHigh,wMin,wMax;
float f,fMax,fMin;
int  x1,x2,y1,y2,io_base,irq_no;

wRetVal=AD32_ReadActive(&wBoard,&wBase,&wIrq);
if (wRetVal==NoError) printf("\nboard=%d, base=%x, irq=%d",wBoard,wBase,wIrq);
else
     {
     printf("\nAD32_ReadActive(...) Error");
     return;
     }

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }
else printf("\nFixed Channel_0, bipolar, +/-5V range");

wRetVal=AD32_StartAdsNoScan(FAST_NO_INT,1);
if (wRetVal!=NoError)
   {
   printf("\nAD32_StartAdsNoScan(...) Error");
   return;
   }

for (i=0; i<512; i++) Buffer[i]=0;
j=0; flag=0; l=0;
for (k=0; k<10; k++)	      /* read until time-out */
for (i<0; i<32766; i++)       /* in real word application, */
    {			      /* read the expected data and break to next */
    AD32_ReadSystemStatus(&wData);
    if ((wData&0x02)!=0)
       {
       AD32_ReadFIFO(&wData);
       if (flag==0) {wLow=wData; flag=1;}
       else
	    {
	    wHigh=wData; flag=0; Buffer[j]=wLow+(wHigh<<8); j++;
	    }
       }
    }

wMin=0x7fff; wMax=0;
for (i=0; i<j; i++)
    {
    if (Buffer[i]>wMax) wMax=Buffer[i];
    if (Buffer[i]<wMin) wMin=Buffer[i];
    }
fMax=(float)(wMax-2048)/2048.0*5.0;
fMin=(float)(wMin-2048)/2048.0*5.0;
printf("\nwMin=%x=%f,  wMax=%x=%f, j=%d",wMin,fMin,wMax,fMax,j);

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);
getch();
cleardevice();
}

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

fun_4()
{
int wBoard,wBase,wIrq,wRetVal,i,j,k,flag,l,p;
int wChannel,wMode,wConfig,wSettle,wData;
int wLow,wHigh,wMin,wMax;
float f,fVal,fMax,fMin;
int  x1,x2,y1,y2,io_base,irq_no,scan;

wRetVal=AD32_ReadActive(&wBoard,&wBase,&wIrq);
if (wRetVal==NoError) printf("\nboard=%d, base=%x, irq=%d",wBoard,wBase,wIrq);
else
     {
     printf("\nAD32_ReadActive(...) Error");
     return;
     }

wRetVal=AD32_SetSampleRate((float)90000.0);
if (wRetVal==NoError) printf("\nSetSampleRate 90K s/s OK");
else printf("\nSetSampleRate Error");

wRetVal=AD32_ReadSampleRate(&fVal);
if (wRetVal==NoError) printf(" --> ReadSampleRate=%.0f s/s",fVal);
else printf("\nReadSampleRate Error");

/* Scan sequence 0->1->2->3->0.... */
wRetVal=AD32_SetChannelScanStatus(0,0,0,CONFIG_1);
wRetVal += AD32_SetChannelScanStatus(1,1,0,CONFIG_2);
wRetVal += AD32_SetChannelScanStatus(2,2,0,CONFIG_3);
wRetVal += AD32_SetChannelScanStatus(3,3,0,CONFIG_4);
if (wRetVal!=NoError) printf("\nSet Channel Scan TEST2 Error");

/* Conversion=FAST, scan=4 channel, total Ads=4*256=1K WORD=2K BYTE */
/* Scan sequence : 0->1->2->3->0...			*/
wRetVal=AD32_StartChannelScanAds(SAMPLE_RATE_NO_INT,4,256);
if (wRetVal!=NoError) printf("\nStart Channel Scan Error");

for (i=0; i<512; i++) Buffer[i]=0;
j=0; flag=0; l=0; scan=0; p=0;
for (k=0; k<10; k++)		/* read until time-out */
for (i<0; i<32766; i++) 	/* in real word application, */
    {				/* read the expected data and break to next */
    AD32_ReadSystemStatus(&wData);
    if ((wData&0x02)!=0)
       {
       p++;
       AD32_ReadFIFO(&wData);

	if (flag==0) {wLow=wData; flag=1;}
       else
	    {
	    wHigh=wData; flag=0;
	    if (scan==0) {Buffer[j]=wLow+(wHigh<<8); j++;}
	    scan++; if (scan>3) scan=0;
	    }
       }
    }

wMin=0x7fff; wMax=0;
for (i=0; i<j; i++)
    {
    if (Buffer[i]>wMax) wMax=Buffer[i];
    if (Buffer[i]<wMin) wMin=Buffer[i];
    }
fMax=(float)(wMax-2048)/2048.0*5.0;
fMin=(float)(wMin-2048)/2048.0*5.0;
printf("\nwMin=%x=%f,  wMax=%x=%f, p=%d",wMin,fMin,wMax,fMax,p);

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);
getch();
cleardevice();
}

