/*********
 * Technologic Systems 
 * 08.27.2002	liberty young liberty@embeddedx86.com
 * example application that uses the analog to digital driver 
 * of the TS-3300
 */


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>

#include "a2d_driver.h"

int main () {
	unsigned char command = 0;
	int response;
	signed int datavalue;
	int fd = open ("/dev/AtoD/0", O_RDWR); 
				//make sure that this device
				//has been created..
	if (fd < 0) {
		printf ("failed opening /dev/AtoD/0\n");
		return -1;
	}

	printf("\n\nstarting!\n");
	
	command = (0x00 | A2D_UNIPOLAR| A2D_RANGE1 ); 
	printf ("sending command of  0x%02x\n", command);

	//test ioctls
	ioctl (fd, TSA2D_CONFIG, command);
	response = ioctl (fd, TSA2D_SHOWCONFIG);

	printf ("/dev/AtoD/0 config byte is %d\n", response);
	printf ("/dev/AtoD/0 is %spolar, and uses Range%d\n",
			response & A2D_BIPOLAR ? "bi" : "uni", 
			response & A2D_RANGE2 ? 2 : 1);

	//test write operation
	datavalue=0;
	response = write(fd, "trigger", sizeof("trigger"));
	if (response < 0){
		printf("write returned %d\n", response);
		perror("error:");
	}

	do {
		response = read (fd, &datavalue, sizeof(signed int));
		printf("a2d returns: 0x%x\n", datavalue);
		printf("\tresponse was %d\n", response);
	} while (response > 0) ;

printf("now we try this again!\n");
	write(fd, "trigger", sizeof("trigger"));
	response = read(fd, &datavalue, sizeof(signed int));
		printf("a2d returns: 0x%x\n", datavalue);
		printf("\tresponse was %d\n", response);

	

	close (fd);
	return 0;
}
