//*****************************************************************************
// "$RCSfile: Analog.h,v $"
//*****************************************************************************
// Information by CVS or VSS:
//     Last check in on: $Date: 2011-02-17 15:59:45 $
//     by                $Author: goebel $
//     Revision        : $Revision: 1.7 $
//
// CVS: $Source: /Daten/Projekte/CVS/Lib/Analog_Lib/src/Analog.h,v $
// VSS: $Archive:$
//
// $NoKeywords: $ expanding by VSS -->
//
// CVS: $Revision: 1.7 $
//
//*****************************************************************************
//! \file Analog.h
//! \brief Header for analog camera functions
//! \author (c) 2004 - 2013 IMAGO Technologies GmbH
//!
//! $Revision: 1.7 $
//! $Date: 2011-02-17 15:59:45 $
//! \addtogroup AnalogMPS
//! \{
//******************************************************************************

#ifndef __ANALOG_H__
#define __ANALOG_H__

#include "vision_box.h"

/******************************************************************************\
* Defines                                                                      *
\******************************************************************************/

//! Analog camera types
enum eCAM_TYPE
{
	ANALOG_PAL_Y =		100, //!< PAL monochrome camera
	ANALOG_PAL_YUYV	=	101, //!< PAL color camera
	ANALOG_SONY_HR50 =	200, //!< Sony XC-HR50
	ANALOG_SONY_XC56 =	201  //!< Sony XC-56BB
};

//!@cond SHOWALL

enum eSAT_REGISTER
{
	SATREGA_PMS_H0 = 0x30,		//!< Register address Packet maker Header0
	SATREGA_PMS_H1 = 0x34,		//!< Register address Packet maker Header1
	SATREGA_STATUS = 0x40,		//!< Register address status register
	SATREGA_LR = 0x41,			//!< Register address crop image left/right
	SATREGA_TB = 0x42,			//!< Register address crop image top/bottom
	SATREGA_MASK = 0x43,		//!< Register address scale-mask
	SATREGA_FC = 0x44,			//!< Register address frame counter
	SATREGA_START = 0x45,		//!< Register address start register
	SATREGA_SEL = 0x46,			//!< Register address sync-source-select
	SATREGA_DPM = 0x47,			//!< Register address data path-mode
	SATREGA_GENLIN = 0x48,		//!< Register address sync generator line parameter
	SATREGA_TRG = 0x49,			//!< Register address trigger pulse width
	SATREGA_GENFLD = 0x4A		//!< Register address sync generator field parameter
};

enum eSAT_VALUE
{
	SATREGV_START = 0x1,		//!< Start register value for start
	SATREGV_RESTART = 0x4,		//!< Start register value for restart
	SATREGV_STOP = 0x20			//!< Start register value for stop
};

enum eSAT_STAT
{
	STATVAL_LINELENTH = 0x0,	//!< Status register sub-address brutto line length
	STATVAL_F0HEIGHT = 0x1,		//!< Status register sub-address frame0 line count
	STATVAL_F1HEIGHT = 0x2,		//!< Status register sub-address frame1 line count
	STATVAL_CURRENT_FLD = 0x3,	//!< Status register sub-address current field
	STATVAL_DEBUG = 0x4,		//!< Status register sub-address debug values
	STATVAL_STATE = 0x5			//!< Status register sub-address acquisition state
};

/******************************************************************************\
* Structures                                                                   *
\******************************************************************************/

typedef union sSatan_LR
{
	unsigned int Register;
	struct
	{
		unsigned int left	:12;
		unsigned int right	:12;
		unsigned int rsvd	:8;
	} Bits;
} SATAN_LR;

typedef union sSatan_TB
{
	unsigned int Register;
	struct
	{
		unsigned int top	:12;
		unsigned int bottom	:12;
		unsigned int rsvd	:8;
	} Bits;
} SATAN_TB;

typedef union sSatan_Mask
{
	unsigned int Register;
	struct
	{
		unsigned int scalmask1		:4;
		unsigned int scalmask2		:4;
		unsigned int enable_field	:2;
		unsigned int start_field	:3;
		unsigned int enable_counter	:1;
		unsigned int rsvd			:18;
	} Bits;
} SATAN_MASK;

typedef union sSatan_FC
{
	unsigned int Register;
	struct
	{
		unsigned int fieldcount	:16;
		unsigned int rsvd	:16;
	} Bits;
} SATAN_FC;

typedef union sSatan_Sel
{
	unsigned int Register;
	struct
	{
		unsigned int wen		:2;
		unsigned int sync		:2;
		unsigned int extsync	:1;
		unsigned int rsvd		:27;
	} Bits;
} SATAN_SEL;

typedef union sSatan_Dpm
{
	unsigned int Register;
	struct
	{
		unsigned int datamode	:2;
		unsigned int rsvd		:30;
	} Bits;
} SATAN_DPM;

typedef union sSatan_GenFld
{
	unsigned int Register;
	struct
	{
		unsigned int blank_lines	:11;
		unsigned int rsvd1			:1;
		unsigned int active_lines	:11;
		unsigned int rsvd2			:9;
	} Bits;
} SATAN_GENFLD;

typedef union sSatan_GenLin
{
	unsigned int Register;
	struct
	{
		unsigned int pixels			:12;
		unsigned int active_pixels	:12;
		unsigned int rsvd			:8;
	} Bits;
} SATAN_GENLIN;

//! Trigger pulse width
typedef union sSatan_Trg
{
	unsigned int Register;
	struct
	{
		unsigned int position		:12;
		unsigned int shuttertime	:16; //!< 1=10 µs
		unsigned int mode			:2;
		unsigned int rsvd1			:2;
	} Bits;
} SATAN_TRG;

//!@endcond

//! Analog camera parameter structure
typedef struct sVIB_AnalogParameter
{
	int CameraNumber;				//!< Camera number
	unsigned int SatAnPort;			//!< 0 for Y-Port, 1 for UV-Port
	int DmaChannel;					//!< DMA-Channel
	int DmaChannelDI;				//!< DMA-Channel for De-interlacer
	int DmaIsOpen;					// 
	int DIFrame;					//
	int Board;						//!< 0:SatA, 1:SatB, 2:SatC
	int IICAddress;					//!< ADC IIC-Address [0..1]
	int ADCMode;					//!< PAL/NTSC, ...
	int PacketSize;					//!< Packet size for DMA/VIB_SunSystem
	int PacketCount;				//!< \brief Number of packets for one image or one line,
									//!< depending on grabbing mode
									//!<
									//!< (a. one DMA/image\n b. one DMA/line)
	int LineOffset;					//!< Offset between two lines
	int PSize;						//!< Number of bytes per pixel
	int XSize;						//!< Width of image
	int FrameHeight;				//!< Height of frame
	int ImageHeight;				//!< Height of image 
	int FrameOffset[2];				//!< \brief Offset between the last line of a frame and the
									//!< first line of the next frame
									//!<
									//!< [0] for even frames\n [1] for odd frames
	int FrameCount;					//!< Number of frames to grab.
	int CurrentLine;				//!< Current line in current frame (Interlaced mode only)
	int CurrentFrame;				//!< Current frame (Interlaced mode only)
	int XOffset;					//!< Horizontal offset from HSync (2 * pixel clock-cycles)
	int YOffset;					//!< Vertical offset from VSync
	int Running;					//!< Flag indicates if automatic grabbing is still running
//	int AutoRestart;				//!< Enables automatic restart for circular buffer
	unsigned char *pImageBuffer;	//!< Memory location to grab into
	unsigned char *pDstDeInt;		//!< Memory destination for de-interlacing
	unsigned char *pSrcDeInt;		//!< Memory source for de-interlacing
	SATAN_LR lr;					//!< Crop image left/right
	SATAN_TB tb;					//!< Crop image top/bottom
	SATAN_SEL sel;					//!< Synchronization mode selection
	SATAN_MASK mask;				//!< Byte/word selection mask
	SATAN_FC fc;					//!< Number of fields to grab
	SATAN_DPM dpm;					//!< Data path mode
	SATAN_GENLIN genlin;			//!< Sync-generator line parameters
	SATAN_GENFLD genfld;			//!< Sync-generator field parameters
	SATAN_TRG trg;					//!< Trigger/Shutter time
	int Lasterror;					//!< Initialization error flag
	int bSwap;						//!< Swap frames
	void *hSem;						//!< Handle for Semaphore
//	void *hSemDeInt;				//!< Handle for De-interlacing function
} ANALOG_PARAMETER;

//!@cond SHOWALL
typedef struct sVIB_AnalogParameter *pACPARAM;
typedef struct sVIB_AnalogParameter ACPARAM;

/******************************************************************************\
* Globals                                                                      *
\******************************************************************************/

extern ANALOG_PARAMETER grParam[2];
extern unsigned char VIB_TVP5145mtable[];
extern int VIB_TVP5145mtable_size[];
extern unsigned int VIB_AnalogGrabTimestamp[];

/******************************************************************************\
* Prototypes                                                                   *
\******************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

int  TVP5145_SetMode(unsigned int IICAddress, int Mode);
int  TVP5145_ReadAll(void);
void VIB_SetRegAn(ANALOG_PARAMETER *pParam, unsigned int reg, unsigned int val);
unsigned int VIB_GetRegAn(ANALOG_PARAMETER *pParam, unsigned int reg);
int  VIB_TVP5145_Read(int Board, unsigned char MAdr, unsigned char *pData, int usTimeout);
void VIB_AnalogTriggerISR(unsigned int DinValue);
void VIB_AnalogRestartTrigger(unsigned int DinValue);
int  VIB_AnalogInitCamera(ANALOG_PARAMETER *pParam);
int  VIB_AnalogCloseCamera(ANALOG_PARAMETER *pParam);
int  VIB_AnalogSetSat(ANALOG_PARAMETER *pParam);
ANALOG_PARAMETER *VIB_AnalogNewParameter(void);
void VIB_AnalogSetDefaultParameter(ANALOG_PARAMETER *pParam, int BoardID, int CamPort, int IICAddress, int CamType, int Resolution);
int  VIB_AnalogSetBoard(ANALOG_PARAMETER *pParam, int Board, int IICAddress);
int  VIB_AnalogDeleteParameter(ANALOG_PARAMETER *pParam);
int  VIB_AnalogCloseDMA(ANALOG_PARAMETER *pParam);
int  VIB_SetAnCamPara(ANALOG_PARAMETER *pParam, int XSize, int YSize, int XOffset, int YOffset);
void VIB_AnalogStartGrab(ANALOG_PARAMETER *pParam, unsigned char *pDst);
int  VIB_AnalogWait4FrameEnd(ANALOG_PARAMETER *pParam, unsigned int usTimeout);
int  VIB_AnalogWait4Idle(ANALOG_PARAMETER *pParam, unsigned int usTimeout);
void VIB_AnalogDeInterlace(ANALOG_PARAMETER *pParam, void *pSrc, void *pDst);
void VIB_AnalogDeInterlaceWait(ANALOG_PARAMETER *param);

int  VIB_AnalogReadParameter(int Device, char *pFilename, ANALOG_PARAMETER *pParam);
void VIB_AnalogWriteParameter(int Device, char *pFilename, ANALOG_PARAMETER *pParam);

void AnalogSetTrigger(void);

unsigned int VIB_AnalogGetCurrentFrame(ANALOG_PARAMETER *pParam);

#ifdef __cplusplus
}
#endif

//!@endcond

#endif // __ANALOG_H__
//! \}
/******************************************************************************/
