#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,t;
FILE *stream;
char c,buf[80];

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

   init();
   printf("\nio base (in hex format) ="); scanf("%x",&io_base);
   A826_Initialize(0,io_base,-1,-1);
   A826_AD_SetChGainMode(0,A826_BI_1,POLLING_MODE);

   for(;;)
	{
	cleardevice();

	sprintf(buf,"Press Any Key Twice To Stop");
	outtextxy(5,5+10,buf);

	A826_AD_PollingArray(Buffer,DesireCount);

        setcolor(WHITE);
	for(i=0; i<16;	i++) {
	   volt=(float)(Buffer[i])*10.0/32768.0;
	   sprintf(buf,"Polling : [%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(); goto ret_label;}
	}
ret_label:
	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;

}
