#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <alloc.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <graphics.h>
#include "A826.h"

int ZERO;
int Yrange;

void init(void);
int Buffer[2000];
int DesireCount=2000;  /* The count which We desire to transfer */
int MaxX,MaxY;
int incidx;

main()
{
int key,c1,c2,t;
FILE *stream;
char c,buf[80];

   int	DesireCount=2000;     /* The count which We desire to transfer */
   int	NowCount;	      /* The count which is transfering now */
   int	ErrorCode;
   int	i,j,x1,x2,y1,y2,io_base,irq_no;
   float  volt;

   printf("\nio base  (in hex format, 220) = "); scanf("%x",&io_base);
   printf("IRQ number (in int format, 15)  = "); scanf("%d",&irq_no);
   printf("(100K --> C1=4) --> C1= "); scanf("%d",&c1);
   printf("(100K --> C2=5) --> C2= "); scanf("%d",&c2);
   A826_Initialize(0,io_base,-1,irq_no);
   A826_AD_SetChGainMode(0,A826_BI_1,INTERRUPT_MODE);

   init();
   for(;;) {
	cleardevice();

	sprintf(buf,"C1=%d C2=%d --> Sampling Rate=%5.1fK",c1,c2,2000.0/(float)(c1*c2));
	outtextxy(5,5,buf);
	sprintf(buf,"Press Any Key Twice To Stop");
	outtextxy(5,5+10,buf);

	if( A826_AD_INT_Start(Buffer,DesireCount,c1,c2)!=0 ) {
	    putch(0x07);
	    printf("A826_AD_INT_Start() error !!!\n");
	    exit(1);
	}

	do {
	    NowCount=A826_AD_INT_Count();
	    if (kbhit()!=0) {
                break;
            }
	}while( NowCount<DesireCount );

	A826_AD_INT_Stop( );

        setcolor(WHITE);
	for(i=0; i<16;	i++) {
	   volt=(float)(Buffer[i])*10.0/32768.0;
	   sprintf(buf,"Interrupt: [%02d] --> [%03xH]=%+5.3f Volt",i,Buffer[i],volt);
	   outtextxy(5,5+10*(i+3),buf);
	}

        setcolor(GREEN);
	rectangle(0,ZERO-Yrange,MaxX,ZERO+Yrange);
	line(0,ZERO,MaxX,ZERO);

	x1=1;
	y1=ZERO-(float)Buffer[0]*Yrange/32768.0;
	for(i=incidx; i<DesireCount && x1<MaxX ;  i+=incidx){
	    y2=ZERO-(float)Buffer[i]*Yrange/32768.0;
	    line(x1,y1,x1+1,y2);
	    putpixel(x1,y1,RED);
	    x1++; y1=y2;
	}
	delay(1000);
	if (kbhit()!=0) {
            getch();
            break;
        }
   }
   getch();
   cleardevice();
   closegraph();
}

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

void init(void)
{
int driver,mode,char_size;

    driver=DETECT;
    initgraph(&driver,&mode,"");
    setactivepage(0);
    setvisualpage(0);
    setcolor(15);
    setbkcolor(0);
    settextstyle(SMALL_FONT,HORIZ_DIR,6);
    MaxX=getmaxx();
    MaxY=getmaxy();
    incidx=DesireCount/(MaxX-1);
    Yrange=(MaxY-20*10)/2;
    ZERO=200+Yrange;

}


