#pragma once

#include "robbase.h"






#define  DIFFERENT                  1
#define  CAMERROR                  -1
//#define  MAX_CAMERA_NAME_LEN       31
//#define  WM_INSPECTFINFINISH      WM_USER + 10

//CAMERA HANDLE , IMAGE DATA, IMAGE SIZE, USERDATA
typedef void ( *FS_FRAME_ARRIVAL_FUNC)(HANDLE, void*, UINT, void*);

//旋转
#define CR_NONE     0
#define CR_TOLEFT   1
#define CR_TORIGHT  2


//
class ROBBASE_API CCamera
{
public:
	//trigger type
	enum{
		CTT_NONE =-1,
		CTT_SOFT=0,
		CTT_WIRE=1,
	};

	CCamera(void);
	~CCamera(void);

	BOOL IsCamValid(){return (m_hCam!=NULL);};
	int GetCurCameraIndex(){ return m_nCamIndex; };

	//获得可用相机数量和名称
	int  GetDeviceCount();
	const char* GetDeviceName(int index);
	BOOL        SetDeviceName(int index, const char* strName); // 保留接口

	const char* GetDeviceName();
	HANDLE GetCameraHandle(){ return m_hCam; };

public:
	//strModule: 输入带绝对路径的相机模块名称
	//colorDepth: 输入-1自动选择
	BOOL Create(const char* strModule, int colorDepth);
	void Destroy();

	BOOL StartCam(int nTriggerType);
	void StopCam();

	BOOL Attach(HANDLE hCamera);
	void Detach(void);

	BOOL IsCamStarted();

	BOOL OpenCam(const char* strCamName, CUtString& strError);
	BOOL OpenCam(int camIndex,  CUtString& strError);

	//该实力函数,需要在上面的任意函数调用以后进行(请使用ReopenCam代替)
	BOOL OpenCam(CUtString& strError);

	BOOL ReopenCam(CUtString& strError);

	void CloseCam();
	void CloseCam(BOOL bThrough);

	 
	BOOL IsCamOpened();

	BOOL  TriggerCam(int timeout);
	BOOL  WaitCam(RvImage image, int timeout);

	////不需要调用TriggerCam
	//里面自动调用TriggerCam
	//get started before fetch
	BOOL Fetch(RvImage image, int timeout);

	BOOL Capture(RvImage image);

	BOOL GetDepth(int* pValue) ;
	BOOL GetWidth(int* pValue) ;
	BOOL GetHeight(int* pValue) ;

	void SetFrameLoadedCallback(FS_FRAME_ARRIVAL_FUNC pFunc, void* pUserData)
	{
		m_pFrameLoadFunc = pFunc;
		m_pUserData = pUserData;
	}

	FS_FRAME_ARRIVAL_FUNC GetFrameLoadedCallback(void** ppUserData);
    int  GetTriggerType();

	 
	int GetRotateType();
	void SetRotateType(int type);

public:
	BOOL  SetCamParamter(const char* strName,int value);
	BOOL  GetCamParamter(const char*  strName, int * pValue);
	BOOL  GetCamParamterMax(const char*  strName, int * pValue);
	BOOL  GetCamParamterMin(const char*  strName, int * pValue);

	BOOL SetRoi(int offsetx, int offsety, int width, int height);

	//枚举类分辨率
	BOOL SetResolution(int width, int height);

	BOOL AutoBalanceWhite();

	CUtString GetSerialNum(void);
	static CUtString GetSerialNum(HANDLE hCam, int index );
	
	BOOL CheckImageFormat(void);

	int GetLastError(){ return m_nLastError; };
	const TCHAR* GetErrorDesc(int errCode);

public:
	void HandleFrameArrival (void* pImageData, UINT nDataSize);

private:
	int    m_nCamIndex;
	int    m_nResoIndex;

	HANDLE m_hCam;

	BOOL   m_bCopyToImage;
	HANDLE m_hRetainLiveEvent ;

	FS_FRAME_ARRIVAL_FUNC m_pFrameLoadFunc;
	void* m_pUserData;

	
private:

	int  m_nLastError ; //该错误码请参照RVB错误码，多线程安全
	//int m_nImageWidth, m_nImageHeight;

	RvImage   m_image;
	BOOL      m_bOpen;
	CUtString m_strName ;

	int     GetCamIndex(CUtString strCamName);

	HANDLE  m_hCamThread;
	HANDLE  m_hCamEvent;
	BOOL    m_bStarted;
	CRITICAL_SECTION m_hCritial;

	//BOOL    m_bTrigle;
	int   m_nRot90;
	 
	BOOL  m_bCamStop;
	 


	  
};

