// FalconCLI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define APPNAME "FalconCLI"

void siginthandler(int param)
{
	printf("Program Terminating...\n");
	exit(param);
}

void print_usage() {
	fprintf(stdout, "Usage: %s options\n", "spoggle");
	fprintf(stdout, "  -h --help                  Display this usage information.\n");
	fprintf(stdout, "  -d --device				  Serial device to trigger through (i.e. COM1) \n");

	exit(0);
}

void print_menu() {
	fprintf(stdout, "Please enter a command\n"
		"(1) Read in All Analog Inputs\n"
		"(2) Toggle ALL DIO LOW\n"
		"(3) Toggle ALL DIO HIGH\n"
		"(4) Read all DIO\n"
		"What is your selection? ");
}

int _tmain(int argc, _TCHAR* argv[])
{
	signal(SIGINT, siginthandler);

	char buffer[256];
	VersalogicMMPEEAX * adc = new VersalogicMMPEEAX();
	
	if (!adc->isConnected()) {
		printf("%s: Failed to connect to VersalogicMMPEEAX Analog Input Module\n", APPNAME);
		return -1;
	}

	adc->printVersion(buffer);

	printf("%s: Successfully Connected, Welcome to the MBARI MMPEEAX Command Line Utility.\n", APPNAME);
	printf("%s: %s\n", APPNAME, buffer);


	//set all inputs 0-5
	adc->setAIRange(PCI_AI_CHANNEL_1, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_2, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_3, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_4, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_5, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_6, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_7, SPI_RANGE_0_5V);
	adc->setAIRange(PCI_AI_CHANNEL_8, SPI_RANGE_0_5V);

	//adc->setDIODirection(DIO_AX_CHANNEL_1, DIO_OUTPUT);
	//adc->setDIODirection(DIO_AX_CHANNEL_2, DIO_OUTPUT);
	//adc->setDIODirection(DIO_AX_CHANNEL_3, DIO_OUTPUT);
	//adc->setAllDIOLevels(DIO_CHANNEL_LOW);

	//Enter command loop
	unsigned char level = 0x00;
	int gc;
	while (1) {
		print_menu();
		gc = getchar();

		if (gc == EOF) {
			fprintf(stdout, "Incorrect input, try again, q to quit.\n\n");
			continue;
		}
		switch (gc) {
		case '1': //Read In all analog inputs. 
			adc->readAllAIChannels();
			//print results
			fprintf(stdout, "AIN VALUES: %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f\n",
				adc->dAINValues[0], adc->dAINValues[1], adc->dAINValues[2], adc->dAINValues[3], 
				adc->dAINValues[4], adc->dAINValues[5], adc->dAINValues[6], adc->dAINValues[7]);
			break;
		case '2':
			adc->setDIODirection(DIO_AX_CHANNEL_1, DIO_OUTPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_2, DIO_OUTPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_3, DIO_OUTPUT); 
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_1, DIO_CHANNEL_LOW);
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_2, DIO_CHANNEL_LOW); 
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_3, DIO_CHANNEL_LOW); 
			break;
		case '3':
			adc->setDIODirection(DIO_AX_CHANNEL_1, DIO_OUTPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_2, DIO_OUTPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_3, DIO_OUTPUT); 
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_1, DIO_CHANNEL_HIGH);
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_2, DIO_CHANNEL_HIGH);
			adc->setDIOOutputLevel(DIO_AX_CHANNEL_3, DIO_CHANNEL_HIGH);
			break;
		case '4':
			adc->setDIODirection(DIO_AX_CHANNEL_1, DIO_INPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_2, DIO_INPUT);
			adc->setDIODirection(DIO_AX_CHANNEL_3, DIO_INPUT);
			adc->getDIOChannelLevel(DIO_AX_CHANNEL_1, level);
			fprintf(stdout, "CHANNEL_1 LEVEL 0x%02X\n", level);
			adc->getDIOChannelLevel(DIO_AX_CHANNEL_2, level);
			fprintf(stdout, "CHANNEL_2 LEVEL 0x%02X\n", level);
			adc->getDIOChannelLevel(DIO_AX_CHANNEL_2, level);
			fprintf(stdout, "CHANNEL_3 LEVEL 0x%02X\n", level);
			break;

		case 'q':
			siginthandler(0);
			break;

		}
	}

	adc->close();

	return 0;
}

