#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 "A822.h"

#define ZERO 204
int   Buffer[2000];
int  wCardMode;
main()
{
int key;
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);
   wCardMode=A822_SetCardModel(A822_Mode);
   A822_Initialize(0,io_base,-1,-1);
   
   /*Warring:A8112 only support Single-End*/
   A822_AD_SetChGainMode(0,A822_BI_1,POLLING_MODE);

   for(;;)
	{
	cleardevice();

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

	A822_AD_PollingArray(Buffer,DesireCount);
	A822_Delay(200);
	x1=1;
	j=Buffer[0]&0x0fff;
	y1=j>>5;
	y1=ZERO+129-y1;
	//line(x1,y1,x1,y1);

	for(i=1; i<127;  i++)
		{

		j=Buffer[i]&0x0fff;
		j=j>>5;

		j=ZERO+129-j;
		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+129-j;
		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;
	volt=(float)(Buffer[i]-2048)/2048.0*5.0;
	sprintf(buf,"POLLING : [%02d] --> [%03xH]=%+5.3f Volt",i,Buffer[i],volt);
	outtextxy(5,5+10*(i+3),buf);
	}

	delay(1000);
	if (kbhit()!=0) {getch(); goto ret_label;}
	}
ret_label:
	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);
}

