void changeAllToInput(int cport_nr, char commands[32][13], int inputToSelect){
	int i = 0;
	while(i < 4){
		changeInput(cport_nr, commands, inputToSelect, i);
		usleep(500000);  /* sleep for 1/2 Second */
		i++;
	}
}
int main(int argc, char* argv[])
{
	int cport_nr=34,        /* /dev/cuaU0 (first serial on FreeBSD) */
	    bdrate=115200,        /* 115200 baud */
	    tvToSelect=-1,
	    inputToSelect=-1;

	char mode[]={'8','N','1',0};
	char commands[32][13]={
        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF9}, // Switch Input 1, to Output 1
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}, // Switch Input 2, to Output 1
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 3, to Output 1
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 4, to Output 1

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}, // Switch Input 1, to Output 2
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 2, to Output 2
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 3, to Output 2
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 4, to Output 2

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 1, to Output 3
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 2, to Output 3
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 3, to Output 3
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF4}, // Switch Input 4, to Output 3

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 1, to Output 4
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 2, to Output 4
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF4}, // Switch Input 3, to Output 4
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3}, // Switch Input 4, to Output 4

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF9}, // Switch Input 1, to Output 5
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}, // Switch Input 2, to Output 5
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 3, to Output 5
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 4, to Output 5

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8}, // Switch Input 1, to Output 6
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 2, to Output 6
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 3, to Output 6
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 4, to Output 6

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7}, // Switch Input 1, to Output 7
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 2, to Output 7
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 3, to Output 7
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF4}, // Switch Input 4, to Output 7

        {0xA5, 0x5B, 0x02, 0x03, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6}, // Switch Input 1, to Output 8
        {0xA5, 0x5B, 0x02, 0x03, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF5}, // Switch Input 2, to Output 8
        {0xA5, 0x5B, 0x02, 0x03, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF4}, // Switch Input 3, to Output 8
        {0xA5, 0x5B, 0x02, 0x03, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3}  // Switch Input 4, to Output 8

	};

	if(argc <= 1){
		printf("Requires at least one input.\n");
		return(1);
	}

	if(argc == 2){
		inputToSelect = atoi(argv[1]) - 1;
	}

	if(argc >= 3){
		tvToSelect    = atoi(argv[1]) - 1;
		inputToSelect = atoi(argv[2]) - 1;
		if(tvToSelect < 0 || tvToSelect > 3){
			printf("Only TV values of 1-4 are supported.\n");
			return(1);
		}
	}

	if(inputToSelect < 0 || inputToSelect > 3){
		printf("Only input values of 1-4 are supported.\n");
		return(1);
	}

	if(RS232_OpenComport(cport_nr, bdrate, mode)){
		printf("Cannot open the serial port.\n");
		return(0);
	}

	if(tvToSelect >= 0)
		changeInput(cport_nr, commands, inputToSelect, tvToSelect);
	else
		changeAllToInput(cport_nr, commands, inputToSelect);

#ifdef _WIN32
    Sleep(1000);
#else
    usleep(500000);  /* sleep for 1/2 Second */
#endif

  return(0);
}
int main(int argc, char **argv)
{
	if (!bcm2835_init())
		return 1;

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
	
	softReset();
	/*
	printf("status: %d \n", readStatus());
	printf("config: %d \n", readConfig());
	printf("mode: %d \n", readMode());
	printf("gain: %d \n", gainSetting);
	*/
	
	//changeInput(0);
	//changeGain(AD7794_GAIN_8);
	//delay(changeInputDelay); //Time to value is ready
	
	long fullData;
	int readAmount = 10;
	int delayMilis=20;
	while(1){
		long input0 = readAvgValueFromInput(0, readAmount, delayMilis);
		printf("Value at input %d: %d / %06X\n", currentInput, input0, input0);
		long input1 = readAvgValueFromInput(1, readAmount, delayMilis);
		printf("Value at input %d: %d / %06X\n", currentInput, input1, input1);
		long input2 = readAvgValueFromInput(2, readAmount, delayMilis);
		printf("Value at input %d: %d / %06X\n", currentInput, input2, input2);
		long input3 = readAvgValueFromInput(3, readAmount, delayMilis);
		printf("Value at input %d: %d / %06X\n", currentInput, input3, input3);
		
		long avg = (input0 + input1 + input2 + input3) / 4;
		printf("avg: %d / %06X\n", avg, avg);
	}
	
	
	int loopc=0;
	while(1){
		fullData = readSingleValue();
		printf("Value at input %d: %d / %06X\n", currentInput, fullData, fullData);
		
		printf("status: %d \n", readStatus());
		++loopc;
		if (loopc>=10){
			if (currentInput<3){
				changeInput(currentInput+1);
			} else {
				changeInput(0);
			}
			delay(changeInputDelay);
			loopc=0;
		} else {
			delay(delayMilis);
		}
	}
	
	bcm2835_spi_end();
	return 0;
}
long readAvgValueFromInput(int input, int avgOver, int delayMilis){
	changeInput(input);
	delay(changeInputDelay);
	return readMultiValueAVG(avgOver, delayMilis);
}
long readSingleValueFromInput(int input){
	changeInput(input);
	delay(changeInputDelay);
	return readSingleValue();
}
//Constructors:
ProjectFilenamesDialog::ProjectFilenamesDialog(ftk::ProjectFiles * files, QWidget * parent)
: QDialog(parent)
{
	pFiles = files;

	QString buttonText = tr("Change...");
	int labelWidths = 120;

	QVBoxLayout *masterLayout = new QVBoxLayout;

	/*
	QHBoxLayout *pLayout = new QHBoxLayout;
	projectLabel = new QLabel(tr("Project File: "));
	projectLabel->setFixedWidth(labelWidths);
	projectFile = new QLabel(projfile);
	projectFile->setFrameShadow(QFrame::Sunken);
	projectFile->setFrameShape(QFrame::StyledPanel);
	pLayout->addWidget(projectLabel);
	pLayout->addWidget(projectFile);
	//pLayout->addStretch(5);
	masterLayout->addLayout(pLayout);
	*/

	QHBoxLayout *pathLayout = new QHBoxLayout;
	pathLabel = new QLabel(tr("Project Path: "));
	pathLabel->setFixedWidth(labelWidths);
	inputPath = new QLabel(QString::fromStdString(files->path));
	inputPath->setFrameShadow(QFrame::Sunken);
	inputPath->setFrameShape(QFrame::StyledPanel);
	pathButton = new QPushButton(buttonText);
	pathButton->setVisible(false);
	connect(pathButton, SIGNAL(clicked()), this, SLOT(changePath()));
	pathLayout->addWidget(pathLabel);
	pathLayout->addWidget(inputPath);
	pathLayout->addWidget(pathButton);
	masterLayout->addLayout(pathLayout);

	QHBoxLayout *nLayout = new QHBoxLayout;
	nameLabel = new QLabel(tr("Project Name: "));
	nameLabel->setFixedWidth(labelWidths);
	nameText = new QLabel(QString::fromStdString(files->name));
	nameText->setFrameShadow(QFrame::Sunken);
	nameText->setFrameShape(QFrame::StyledPanel);
	nameButton = new QPushButton(buttonText);
	connect(nameButton, SIGNAL(clicked()), this, SLOT(changeName()));
	nLayout->addWidget(nameLabel);
	nLayout->addWidget(nameText);
	nLayout->addWidget(nameButton);
	masterLayout->addLayout(nLayout);

	//masterLayout->addStretch(5);
	masterLayout->addSpacerItem(new QSpacerItem(labelWidths,5));

	QHBoxLayout *iLayout = new QHBoxLayout;
	inputLabel = new QLabel(tr("Input: "));
	inputLabel->setFixedWidth(labelWidths);
	inputFile = new QLabel(QString::fromStdString(files->input));
	inputFile->setFrameShadow(QFrame::Sunken);
	inputFile->setFrameShape(QFrame::StyledPanel);
	inputButton = new QPushButton(buttonText);
	inputButton->setVisible(false);
	connect(inputButton, SIGNAL(clicked()), this, SLOT(changeInput()));
	iLayout->addWidget(inputLabel);
	iLayout->addWidget(inputFile);
	iLayout->addWidget(inputButton);
	masterLayout->addLayout(iLayout);

	QHBoxLayout *oLayout = new QHBoxLayout;
	outputLabel = new QLabel(tr("Output: "));
	outputLabel->setFixedWidth(labelWidths);
	outputFile = new QLabel(QString::fromStdString(files->output));
	outputFile->setFrameShadow(QFrame::Sunken);
	outputFile->setFrameShape(QFrame::StyledPanel);
	outputButton = new QPushButton(buttonText);
	outputButton->setVisible(false);
	connect(outputButton, SIGNAL(clicked()), this, SLOT(changeOutput()));
	oLayout->addWidget(outputLabel);
	oLayout->addWidget(outputFile);
	oLayout->addWidget(outputButton);
	masterLayout->addLayout(oLayout);

	QHBoxLayout *lLayout = new QHBoxLayout;
	logLabel = new QLabel(tr("Edit Log: "));
	logLabel->setFixedWidth(labelWidths);
	logFile = new QLabel(QString::fromStdString(files->log));
	logFile->setFrameShadow(QFrame::Sunken);
	logFile->setFrameShape(QFrame::StyledPanel);
	logButton = new QPushButton(buttonText);
	logButton->setEnabled(false);					//Cannot change the log file here!!
	logButton->setVisible(false);
	connect(logButton, SIGNAL(clicked()), this, SLOT(changeLog()));
	lLayout->addWidget(logLabel);
	lLayout->addWidget(logFile);
	lLayout->addWidget(logButton);
	masterLayout->addLayout(lLayout);

	QHBoxLayout *dLayout = new QHBoxLayout;
	definitionLabel = new QLabel(tr("Project Definition: "));
	definitionLabel->setFixedWidth(labelWidths);
	definitionFile = new QLabel(QString::fromStdString(files->definition));
	definitionFile->setFrameShadow(QFrame::Sunken);
	definitionFile->setFrameShape(QFrame::StyledPanel);
	definitionButton = new QPushButton(buttonText);
	definitionButton->setVisible(false);
	connect(definitionButton, SIGNAL(clicked()), this, SLOT(changeDefinition()));
	dLayout->addWidget(definitionLabel);
	dLayout->addWidget(definitionFile);
	dLayout->addWidget(definitionButton);
	masterLayout->addLayout(dLayout);

	QHBoxLayout *tLayout = new QHBoxLayout;
	tableLabel = new QLabel(tr("Table: "));
	tableLabel->setFixedWidth(labelWidths);
	tableFile = new QLabel(QString::fromStdString(files->table));
	tableFile->setFrameShadow(QFrame::Sunken);
	tableFile->setFrameShape(QFrame::StyledPanel);
	tableButton = new QPushButton(buttonText);
	tableButton->setVisible(false);
	connect(tableButton, SIGNAL(clicked()), this, SLOT(changeTable()));
	tLayout->addWidget(tableLabel);
	tLayout->addWidget(tableFile);
	tLayout->addWidget(tableButton);
	masterLayout->addLayout(tLayout);

	QHBoxLayout *a_tLayout = new QHBoxLayout;
	adjTablesLabel = new QLabel(tr("Adj_Tables: "));
	adjTablesLabel->setFixedWidth(labelWidths);
	adjTablesFile = new QLabel(QString::fromStdString(files->adjTables));
	adjTablesFile->setFrameShadow(QFrame::Sunken);
	adjTablesFile->setFrameShape(QFrame::StyledPanel);
	adjTablesButton = new QPushButton(buttonText);
	adjTablesButton->setVisible(false);
	connect(adjTablesButton, SIGNAL(clicked()), this, SLOT(changeAdjTables()));
	a_tLayout->addWidget(adjTablesLabel);
	a_tLayout->addWidget(adjTablesFile);
	a_tLayout->addWidget(adjTablesButton);
	masterLayout->addLayout(a_tLayout);

	masterLayout->addStretch(5);

	QHBoxLayout *okLayout = new QHBoxLayout;

	quitButton = new QPushButton(tr("Cancel"));
	connect(quitButton, SIGNAL(clicked()), this, SLOT(reject()));
	okButton = new QPushButton(tr("Done"));
	connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
	okLayout->addStretch(20);
	okLayout->addWidget(quitButton);
	okLayout->addWidget(okButton);
	masterLayout->addLayout(okLayout);

	this->setLayout(masterLayout);

	//this->setupDefaults();
}