
// MultiZebrasDemoDlg.h : header file
//

#pragma once

#include "mipiCntrlDef.h"

#define WM_MIPI_DEVICEARRIVAL					WM_USER+1
#define WM_MIPI_DEVICEREMOVECOMPLETE			WM_USER+2
#define WM_MIPI_PROGRESS_MONITOR				WM_USER+3

#define WM_MIPI_THREAD_ACTION					WM_USER+4

#define Act_ZebraReadForNew						0
#define Act_LoadRegisterFile					1
#define Act_RunTestStepOrRegister				2
#define Act_GrabImageData						3
#define Act_RunTestStepScript					4
#define Act_I2CReadWrite						5
#define Act_ResetZebraBrd						6
#define Act_GetBrdTestSteps						7


#define	__ZebraAppMax__							4	// base 1



// For average the image
#define	__GRAB_IMG_ACT_GOING__					0


#define	__GRAB_IMG_GOT__						0
#define	__GRAB_IMG_NON_PIC__					-1
#define	__GRAB_IMG_START_INDX_TOO_LOW__			-2
#define	__GRAB_IMG_LAST_INDX_OVER__				-3
#define __GRAB_IMG_RUN_STEP_ERR__				-4

#define  __GRAB_IMAGE_BUFFER_COUNT__			16


typedef  int		__HZHANDLE__;

//typedef struct
//{
//	int				nZebraBoardId;			// set in dip-switch or port - id when without dip-switch on carrier board (<0 port and locked)
//	int				nZebraType;				// Zebra4=0, Zebra5=1, ...
//	int				nZebraStatus;			// __Zebra_Open_usable__(2), __Zebra_Open_DrvNoApp__(1), __Zebra_NotYet_Init__(0), __Zebra_InValid__(-1), __Zebra_Removed__(-2)
//} __ZEBRA_BOARD_INFO__, *__PZEBRA_BOARD_INFO__;



typedef struct {
	// Application using variables
	int		m_AppZebraHandle;						// The initiated Zebra handle
	int		m_AppZebraId;							// Zebra card id (it may from dip-switch setting or USB port assignment).
	int		m_ZebraActionStatus;					// Current zebra status

	int		m_TestStepCnt;							// How many test-steps(register steps) count when just download to Zebra
	TCHAR   m_LoadStepRegName[MAX_PATH];			// Assigned the download configuration 
	
	int		m_ResetByApp;							// flag to reset by App or USB driver

	int	    m_TestStepImgCnt;						// run step has pictures image count
	int	    m_RunAsStreaming;								// StepId, Aliase or Register file name
	int	    m_RunType;								// StepId, Aliase or Register file name
	int		m_TestStepId;							// Just run test step id - for restore only
	TCHAR   m_AliasTestRegisterName[MAX_PATH];		// An alias or test-step(register) name

	short   m_GrabStart;							// Grab image start index
	short   m_GrabEnd;								// Grab image ending index
	TCHAR   m_GrabImgSaveName[MAX_PATH];			// Grab image save to ...
	int     m_ImgRaw8To10;

	int		m_ScriptDontSaveImage;						// if this item is turn on, without save image to disk - for USB speed test
	int     m_ScriptImgRaw8To10;
	TCHAR   m_ScriptName[MAX_PATH];					// script name would be execute

	TCHAR   m_ZebraAppInfo[128];					// Only for restore thread info

	int		m_I2C_cfgByte;							// I2c operation configuration byte
	int	    m_I2C_ReadWriteLen;						// A read write data length
	int	    m_I2C_Addr;								// I2c address
	UCHAR   m_I2C_SubAddr[4];						// I2c sub-address (device offset, how many bytes are defined in configuration byte
	UCHAR   m_I2C_RdWrData[256];					// read/write data buffer

	// Extra for thread context
	TCHAR   m_I2cAddrTypeTxt[16];
	TCHAR   m_I2cSubAddrByteCntTxt[16];
	TCHAR   m_I2cSubAddrContext[32];

	char    m_GotZebraTestStepName[256];

	// thread for image average 
	DWORD	dwGrabThrdId;
	HANDLE	hGrabThrd;
	HANDLE	hGrabThrdCmd;
	HANDLE	hGrabThrdRslt;
	BOOL	bGrabExit;
	int		GrabAct;

	int nGrabStart;
	int nGrabLast;

	int nGrabbeResult;
	int nQueHead, nQueTail;
	DWORD dwImageSize[__GRAB_IMAGE_BUFFER_COUNT__];
	UCHAR *pGrabBuffer[__GRAB_IMAGE_BUFFER_COUNT__];
} __sOneZebraAppPrams__, *__psOneZebraAppPrams__;

// CMultiZebrasDemoDlg dialog
class CMultiZebrasDemoDlg : public CDialogEx
{
// Construction
public:
	CMultiZebrasDemoDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_MULTIZEBRASDEMO_DIALOG };

	// Thread
	HANDLE		hThrdStartEvt;					// Used to wait previous thread start then to start next thread!
	HANDLE		m_ExitEvent[__ZebraAppMax__ + 1];
	HANDLE		m_ThrdHandle[__ZebraAppMax__ + 1];
	DWORD		m_ThrdId[__ZebraAppMax__ + 1];
	static DWORD WINAPI ZebraHandleThrd(LPVOID pParam);

	int			m_StatusPercent[__ZebraAppMax__ + 1];

	CButton m_RDO_AppReset;
	CButton m_RDO_UsbReset;

	CButton m_RDO_AlaisRun;
	CButton m_RDO_FileRun;

	CButton	  m_chk_RunAsStreaming;
	CButton	  m_chk_ScriptDontSavImg;
	CButton	  m_chkRaw8To10;
	CButton	  m_scriptchkRaw8To10;

	CButton	  m_chk_I2CIgnoreAck;
	CButton	  m_RDO_I2CRead;
	CButton   m_RDO_I2CWrite;
	CComboBox m_Combo_I2CAddrBits;
	CComboBox m_Combo_I2CRdSubAddrBytes;

	CComboBox m_Combo_RunNames;

	CSpinButtonCtrl m_SPIN_ZebraIndx;

	CStatic			m_stc_Z1_Id_Port_Info;
	CStatic			m_stc_Z1_ActionStatus;
	CProgressCtrl	m_Z1_progress;

	CStatic			m_stc_Z2_Id_Port_Info;
	CStatic			m_stc_Z2_ActionStatus;
	CProgressCtrl	m_Z2_progress;

	CStatic			m_stc_Z3_Id_Port_Info;
	CStatic			m_stc_Z3_ActionStatus;
	CProgressCtrl	m_Z3_progress;

	CStatic			m_stc_Z4_Id_Port_Info;
	CStatic			m_stc_Z4_ActionStatus;
	CProgressCtrl	m_Z4_progress;

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


	DWORD	m_dwMSVer, m_dwLSVer;

	char    m_szProgVersion[64];

	public:
	int		m_ZebraInfoDlgOpenMode;
	int     m_FoundZebras;

	int		m_SelectIndx;

	__sOneZebraAppPrams__	m_sZebraUI[__ZebraAppMax__ + 1];


	void UpdateProgressMsg(int nZId, int PrgCnt, TCHAR *pMsg);
	void UpdateRunStepCmbList(__HZHANDLE__ hZhandle, int nZebraApIndex);

	void saveCurrentContext2Thread(int ThrdIndex);
	void restoreThreadContext(int ThrdIndex);
// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
//	afx_msg void OnIdclose();
	afx_msg void OnClose();
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	afx_msg void OnBnClickedBtnSelect();
	afx_msg void OnBnClickedBtnTestCfg();
	afx_msg void OnBnClickedBtnLoadTestStep();
	afx_msg void OnChangeEdtSelectZebraBrdIndx();
	afx_msg void OnBnClickedRdoAliasRun();
	afx_msg void OnBnClickedRdoFilenameRun();
	afx_msg void OnBnClickedBtnGrabImg();
	afx_msg void OnBnClickedBtnRunStep();
	afx_msg void OnBnClickedBtnSelectScriptFile();
	afx_msg void OnBnClickedBtnRunScript();
	afx_msg void OnBnClickedBtnI2cReadWrite();
	afx_msg void OnBnClickedRdoI2cRead();
	afx_msg void OnBnClickedRdoI2cWrite();
	afx_msg void OnBnClickedBtnReset();
	afx_msg void OnBnClickedBtnGetZebraSteps();
	afx_msg void OnBnClickedChkGetUnsave();
};


// Zebra info dialog box open type
#define	__zInfoDlgOpen_View__		0
#define	__zInfoDlgOpen_Select__		1


// Zebra status
#define  __Zebra_Open_usable__		2	// a Zebra open by me and usable now
#define  __Zebra_Open_DrvNoApp__	1	// a Zebra open but application (sensor) not ready
#define  __Zebra_NotYet_Init__		0	// doesn't init Zebra or a new valid Zebra insert to USB hub-port
#define  __Zebra_InValid__			-1	// No valid Zebra or they have locked by other applications
#define  __Zebra_Removed__			-2	// that open Zebra has removed by user

// Zebra type
#define __ZebraType_Z4__			0
#define __ZebraType_Z5__			1
#define __ZebraType_Z6__			2			// to be later
#define __ZebraType_Z4U3__			16

// Zebra select error
#define __USB_HANDLE_NOT_INIT__			-20
#define __USB_HANDLE_NOT_FOUND__		-21
#define __USB_DRV_OK_APP_NOT_READY__	-22


#define	__Run_Step_by_Id			0
#define	__Run_Step_by_AliasName		1
#define	__Run_Step_by_FileName		2