/*************************************************************************\
* "$RCSfile: _main.cpp,v $"                                               *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2010-10-04 14:03:19 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.1 $                                 *
\*************************************************************************/
/*! \file _main.cpp
*	\brief Main function CameraLink Example
*	\author (c) 2010 - 2013 IMAGO Technologies GmbH
**************************************************************************/

#include "vision_box.h"
#include "GUI_Lib.h"
#include "Example.h"
#include "CNetwork.hpp"
#include "pfASCII.h"

extern "C"
{
void task_main(void);
}

/*************************************************************************/
int main()
/*************************************************************************/
/*! @brief Main function.

	Initializes hardware and then jumps to idle loop.
**************************************************************************/
{
	// *** Init VisionBox hardware ***
	if (VIB_Init()<0)
	{
		printf("Error in VIB_Init!\n");
		exit(-1);
	}
	return 0; // Jump into idle loop
}

/*************************************************************************/
void task_main(void)
/*************************************************************************/
/*! @brief Main task.
**************************************************************************/
{
	int bDVIDisplay, bGBitDisplay;
	CIMAGE DisplayPic;
	CNetwork CNet;
	FILE *fKB;

	printf("Example V: "__DATE__"\n");

	// *** Initialize display ***
	CNet.InitDisplay(&DisplayPic, &bDVIDisplay, &bGBitDisplay, 1024, 768);
	GUI_Init();
	GUI_MBGradient = 1;
	GUI_MBShadow = 1;

	GUI_Fill(&DisplayPic, &rgbDkBlue);
	GUI_Display(&DisplayPic, 0, 0);
	GUI_Display(&DisplayPic, 0, 0);

	// *** Init Keyboard ***
	fKB = fopen("VIB:KEYB", "r");


	//pfASCII (Communication interface to the camera head)
	//Init of pfASCII
	int numOfPorts, error, baudrate, mBytes, nBytes, version, type, port, ival;
	char manu[25], name[25], sval[16];
	float fval;
	pfASCII_PropertyType propertyType;

	error = -1;
	port = 2; //CameraHead is connected on COM2

	//Init and print port information
	pfASCII_PortInit(&numOfPorts);
	pfASCII_PortInfo(port, manu, &mBytes, name, &nBytes, &version, &type);
	printf("Manu: %s, %s, mBytes %d, nBytes %d, ver %d, type %d\n", manu, name, mBytes, nBytes, version, type);

	// Open camera port
	error = pfASCII_DeviceOpen(port);
	if (error != 0)
	{
		printf("pfDeviceOpen: %d\n", error);
	}

	baudrate = 0;
	error = pfASCII_SetBaudRate(port, 115200);
	error = pfASCII_GetBaudRate(port, &baudrate);
	printf("BaudRate: %d\n", baudrate);

	//DigitalGain has type PF_INT
	error = pfASCII_SetPropertyInt(port, "DigitalGain", 0);
	error = pfASCII_GetPropertyInt(port, "DigitalGain", &ival);
	printf("DigitalGain: %d\n", ival);

	//ExposureTime has type PF_FLOAT
	error = pfASCII_SetPropertyFloat(port, "ExposureTime", 12.126);
	error = pfASCII_GetPropertyFloat(port, "ExposureTime", &fval);
	printf("ExposureTime: %f\n", fval);

	//FineGain has type FineGain
	error = pfASCII_SetPropertyString(port, "FineGain", "1.0");
	error = pfASCII_GetPropertyString(port, "FineGain", sval);
	printf("FineGain: %s\n", sval);

	//DataOutMode has type PF_MODE
	error = pfASCII_SetPropertyString(port, "DataOutMode", "0");
	error = pfASCII_GetPropertyString(port, "DataOutMode", sval);
	printf("DataOutMode: %s\n", sval);

	//Trigger.CFR has type PF_BOOL
	error = pfASCII_SetPropertyInt(port, "Trigger.CFR", 1);
	error = pfASCII_GetPropertyInt(port, "Trigger.CFR", &ival);
	printf("Trigger.CFR: %d\n", ival);

	//FrameRateCFR has type PF_FLOAT
	error = pfASCII_SetPropertyFloat(port, "FrameRateCFR", 5);
	error = pfASCII_GetPropertyFloat(port, "FrameRateCFR", &fval);
	printf("FrameRateCFR: %f\n", fval);

	//get Type of property (PF_INT, PF_FLOAT, PF_MODE, ...)
	error = pfASCII_GetPropertyType("ExposureTime", &propertyType);

	//Close pfASCII
	pfASCII_DeviceClose(port);

	// *** Main loop ***
	CLLive(&DisplayPic, fKB);

	// *** Clean up ***
	fclose(fKB);
	GUI_MessageBox(&DisplayPic, "\nThe end\n\n", NULL, GUI_MBSIMPLE);
	GUI_Display(&DisplayPic, 0, 0);
	GUI_FreeCImage(&DisplayPic);
	if (bGBitDisplay)
		CNet.Stop();

	// *** Reboot ***
	if (!strncmp(getenv("OS"), "Windows", 7)) // Check for JTAG connection
		exit(0);
	VIB_Reboot();
}

/*************************************************************************/
