/*
 * RTC  Version 2.0           Author :  Vincent Hayward
 *                                      School of Electrical Engineering
 *                                      Purdue University
 *      Dir     : rtc
 *      File    : mkenc.c
 *      Remarks : Utility, convert angles to playable encoders values.
 *      Usage   : make mkenc
 */

#include <stdio.h>

main(argc, argv)
int argc;
char **argv;
{
	int fo = creat("@@@.out", 0644);
	int code;
	double a[6];
	unsigned short e[7];
	FILE *file;

	switch (argc) {
	case 1 :
		file = stdin;
		break;

	case 2 :
		file = fopen(*(argv + 1), "r");
		if (file == NULL) {
			fprintf(stderr, "can't open %s\n", *(argv + 1));
			exit (1);
		}
		break;

	default :
		fprintf(stderr, "usage : mkenc [file]\n");
		exit(2);
	}
	while (
fscanf(file, "%f%f%f%f%f%f", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) == 6) {
		if (code = angtoenc(e, a)) {
			fprintf(stderr,
			"Joints %c%c%c%c%c%c%c limited\n",
			(code & 01) ? '1' : ' ', (code & 02) ? '2' : ' ' ,
			(code & 04) ? '3' : ' ', (code & 010) ? '4' : ' ' ,
			(code & 020) ? '5' : ' ', (code & 040) ? '6' : ' ' ,
			(code & 0400) ? 'D' : ' ');
		}
		write(fo, e, sizeof(e));
	}
}
