#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <rtl_fifo.h>
#include <rtl_time.h>


#define BUFSIZE 70

char buf[BUFSIZE];

int main()
{

	int fd0;
	FILE* fd1,*fd2,*fd3,*fd4,*fd5;
	int n;
	int count=0;
	struct dump_data {
	  int cmd;
	  int u;
	  int setpt;
	  int pos;
	  int e_d;
	  int e_delta;
	} dump;
	
	if ((fd0 = open("/dev/rtf6", O_RDONLY)) < 0) {
		fprintf(stderr, "Error opening /dev/rtf1\n");
		exit(1);
	}

	if ((fd1 = fopen("u.dat", "w+")) < 0) {
		fprintf(stderr, "Error opening u\n");
		exit(1);
	}
	if ((fd2 = fopen("pos.dat", "w+")) < 0) {
		fprintf(stderr, "Error opening pos\n");
		exit(1);
	}
	if ((fd3 = fopen("setpt.dat", "w+")) < 0) {
		fprintf(stderr, "Error opening setpt\n");
		exit(1);
	}
	if ((fd4 = fopen("e_d.dat", "w+")) < 0) {
		fprintf(stderr, "Error opening e_d\n");
		exit(1);
	}
	if ((fd5 = fopen("e_delta.dat", "w+")) < 0) {
		fprintf(stderr, "Error opening e_delta\n");
		exit(1);
	}

	fprintf(fd1,"color = blue\n");
	fprintf(fd2,"color = red\n");
	fprintf(fd3,"color = yellow\n");
	fprintf(fd4,"color = green\n");
	fprintf(fd5,"color = white\n");


	dump.cmd=1;

	while (dump.cmd != 0) {
	   n = read(fd0, &dump, sizeof(dump));
	   fprintf(fd1,"%d\t%d\n",count,dump.u);
	   fprintf(fd2,"%d\t%d\n",count,dump.pos);
	   fprintf(fd3,"%d\t%d\n",count,dump.setpt);
	   fprintf(fd4,"%d\t%d\n",count,dump.e_d);
	   fprintf(fd5,"%d\t%d\n",count++,dump.e_delta);
	}
	fclose(fd1);
	fclose(fd2);
	fclose(fd3);
	fclose(fd4);
	fclose(fd5);

return (0);
}
