// 4133_MultiROIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "4133_MultiROI.h"
#include "4133_MultiROIDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Set of controls for each ROI

struct ROI_CONTROLS Roi11Controls;
struct ROI_CONTROLS Roi12Controls;
struct ROI_CONTROLS Roi2Controls;
struct ROI_CONTROLS Roi3Controls;
struct ROI_CONTROLS Roi4Controls;


struct symbolic_list {
	char *name;
};
static struct symbolic_list shutterModesList[] = {
    { "Rolling" },
    { "Global reset" },
	{ "Global shutter" },
	{ NULL }
};

int nIDTimerEvent=1;
int g_iCurCam=0;
int g_iTriggerMode	= TRIG_SW_START;
bool bVideostopped = true;
bool bReady=false;

ROI_PROPERTY        roi_prop_1_1;
ROI_RANGE_PROPERTY	roi_range_prop_1_1;
ROI_PROPERTY        roi_prop_1_2;
ROI_RANGE_PROPERTY	roi_range_prop_1_2;
ROI_PROPERTY        roi_prop_2;
ROI_RANGE_PROPERTY	roi_range_prop_2;
ROI_PROPERTY        roi_prop_3;
ROI_RANGE_PROPERTY	roi_range_prop_3;
ROI_PROPERTY        roi_prop_4;
ROI_RANGE_PROPERTY	roi_range_prop_4;

//---------------------------------------------------------------------------

void SaveRaw(BYTE * pBuffer, long lBufferSize,char* cName)
{
    FILE *fp;
	char fname[40];
	sprintf_s(fname, cName);
	fopen_s(&fp, fname, "wb");
	fwrite(pBuffer, sizeof(BYTE), lBufferSize, fp);
	fclose(fp);
}
//---------------------------------------------------------------------------
// One callback for all rois. Its also possible to use separate callbacks
// for each roi. In that case no context parameter is needed in
// the ICubeSDK_SetCallback-function.
//
long CALLBACK MyCallbackFunc(BYTE * pBuffer, long lBufferSize,PVOID pContext)
{
	int nContext=reinterpret_cast<int>(pContext);

    if(pBuffer==NULL)
        return -1;

  switch(nContext)
    {
    case 1:         // Callback from roi1
    if(Roi11Controls.bGrabCallback==true)
    {
	    Roi11Controls.bGrabCallback=false;
        SaveRaw(pBuffer,lBufferSize,"Roi1.raw");
	}
    break;
    case 2:         // Callback from roi2
    if(Roi2Controls.bGrabCallback==true)
    {
	    Roi2Controls.bGrabCallback=false;
        SaveRaw(pBuffer,lBufferSize,"Roi2.raw");
	}
    break;
    case 3:         // Callback from roi3
    if(Roi3Controls.bGrabCallback==true)
    {
	    Roi3Controls.bGrabCallback=false;
        SaveRaw(pBuffer,lBufferSize,"Roi3.raw");
	}
    break;
    case 4:         // Callback from roi4
    if(Roi4Controls.bGrabCallback==true)
    {
	    Roi4Controls.bGrabCallback=false;
        SaveRaw(pBuffer,lBufferSize,"Roi4.raw");
	}
    break;
    default:
    break;
    }

return 42;
}
//---------------------------------------------------------------------------
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

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

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMy4133_MultiROIDlg dialog

CMy4133_MultiROIDlg::CMy4133_MultiROIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy4133_MultiROIDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CMy4133_MultiROIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CAM, m_ComboCam);
	DDX_Control(pDX, IDC_COMBO_SHUTTER, m_ComboShutter);
}

BEGIN_MESSAGE_MAP(CMy4133_MultiROIDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_CLOSE()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CMy4133_MultiROIDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI1, &CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi1)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI1, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi1)
	ON_BN_CLICKED(IDC_BUTTON_GRAB_ROI1, &CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi1)
	ON_BN_CLICKED(IDC_BUTTON_BMP_ROI1, &CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi1)
	ON_BN_CLICKED(IDC_BUTTON_AVI_ROI1, &CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi1)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI12, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi12)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI12, &CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi12)
	ON_BN_CLICKED(IDC_BUTTON_GRAB_ROI2, &CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi2)
	ON_BN_CLICKED(IDC_BUTTON_BMP_ROI2, &CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi2)
	ON_BN_CLICKED(IDC_BUTTON_AVI_ROI2, &CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi2)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI2, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi2)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI2, &CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi2)
	ON_BN_CLICKED(IDC_BUTTON_GRAB_ROI3, &CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi3)
	ON_BN_CLICKED(IDC_BUTTON_BMP_ROI3, &CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi3)
	ON_BN_CLICKED(IDC_BUTTON_AVI_ROI3, &CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi3)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI3, &CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi3)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI3, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi3)
	ON_BN_CLICKED(IDC_BUTTON_SET_ROI4, &CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi4)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_ROI4, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi4)
	ON_BN_CLICKED(IDC_BUTTON_GRAB_ROI4, &CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi4)
	ON_BN_CLICKED(IDC_BUTTON_BMP_ROI4, &CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi4)
	ON_BN_CLICKED(IDC_BUTTON_AVI_ROI4, &CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi4)
	ON_BN_CLICKED(IDC_CHECK_DEFECT_COR, &CMy4133_MultiROIDlg::OnBnClickedCheckDefectCor)
	ON_CBN_SELCHANGE(IDC_COMBO_SHUTTER, &CMy4133_MultiROIDlg::OnCbnSelchangeComboShutter)
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_COMBO_CAM, &CMy4133_MultiROIDlg::OnCbnSelchangeComboCam)
	ON_EN_CHANGE(IDC_EDIT_EXPOSURE_ROI1, &CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi1)
	ON_EN_CHANGE(IDC_EDIT_EXPOSURE_ROI2, &CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi2)
	ON_EN_CHANGE(IDC_EDIT_EXPOSURE_ROI3, &CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi3)
	ON_EN_CHANGE(IDC_EDIT_EXPOSURE_ROI4, &CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi4)
	ON_BN_CLICKED(IDC_BUTTON_DO_SW_TRIGGER, &CMy4133_MultiROIDlg::OnBnClickedButtonDoSwTrigger)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_TRIGGER, &CMy4133_MultiROIDlg::OnBnClickedCheckEnableTrigger)
	ON_BN_CLICKED(IDC_RADIO_SOFTWARE_TRIGGER, &CMy4133_MultiROIDlg::OnBnClickedRadioSoftwareTrigger)
	ON_BN_CLICKED(IDC_RADIO_HARDWARE_TRIGGER, &CMy4133_MultiROIDlg::OnBnClickedRadioHardwareTrigger)
	ON_BN_CLICKED(IDC_BUTTON_SET_ALL_ROIS, &CMy4133_MultiROIDlg::OnBnClickedButtonSetAllRois)
END_MESSAGE_MAP()


// CMy4133_MultiROIDlg message handlers

BOOL CMy4133_MultiROIDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	CheckRadioButton( IDC_RADIO_SOFTWARE_TRIGGER,IDC_RADIO_HARDWARE_TRIGGER, IDC_RADIO_SOFTWARE_TRIGGER);

	// Load roi controls
    Roi11Controls.nROI_ID = ROI_ID_1_1;
    Roi11Controls.SliderXPos = IDC_SLIDER_XPOS1;
    Roi11Controls.SliderYPos = IDC_SLIDER_YPOS1;
    Roi11Controls.SliderXRes = IDC_SLIDER_XRES1;
    Roi11Controls.SliderYRes = IDC_SLIDER_YRES1;
    Roi11Controls.EditXPos = IDC_EDIT_XPOS1;
    Roi11Controls.EditYPos = IDC_EDIT_YPOS1;
    Roi11Controls.EditXRes = IDC_EDIT_XRES1;
    Roi11Controls.EditYRes = IDC_EDIT_YRES1;
    Roi11Controls.SliderRepetitions = IDC_SLIDER_REPETITIONS_ROI1;
    Roi11Controls.EditRepetitions = IDC_EDIT_REPETITIONS_ROI1;
    Roi11Controls.SliderExposure = IDC_SLIDER_EXPOSURE_ROI1;
    Roi11Controls.EditExposure = IDC_EDIT_EXPOSURE_ROI1;
    Roi11Controls.SliderGain = IDC_SLIDER_GAIN_ROI1;
    Roi11Controls.EditGain = IDC_EDIT_GAIN_ROI1;
	Roi11Controls.ButtonSet = IDC_BUTTON_SET_ROI1;
	Roi11Controls.CheckEnable = IDC_CHECK_ENABLE_ROI1;
	Roi11Controls.ButtonAvi = IDC_BUTTON_AVI_ROI1;
	Roi11Controls.ButtonBmp = IDC_BUTTON_BMP_ROI1;
	Roi11Controls.ButtonGrab = IDC_BUTTON_GRAB_ROI1;
	Roi11Controls.bGrabCallback=false;

	Roi12Controls.nROI_ID = ROI_ID_1_2;				// SIMR Controls
    Roi12Controls.SliderXPos = IDC_SLIDER_XPOS12;
    Roi12Controls.SliderYPos = IDC_SLIDER_YPOS12;
    Roi12Controls.SliderXRes = IDC_SLIDER_XRES12;
    Roi12Controls.SliderYRes = IDC_SLIDER_YRES12;
    Roi12Controls.EditXPos = IDC_EDIT_XPOS12;
    Roi12Controls.EditYPos = IDC_EDIT_YPOS12;
    Roi12Controls.EditXRes = IDC_EDIT_XRES12;
    Roi12Controls.EditYRes = IDC_EDIT_YRES12;
    Roi12Controls.ButtonSet = IDC_BUTTON_SET_ROI12;
	Roi12Controls.CheckEnable = IDC_CHECK_ENABLE_ROI12;

    Roi2Controls.nROI_ID = ROI_ID_2;
    Roi2Controls.SliderXPos = IDC_SLIDER_XPOS2;
    Roi2Controls.SliderYPos = IDC_SLIDER_YPOS2;
    Roi2Controls.SliderXRes = IDC_SLIDER_XRES2;
    Roi2Controls.SliderYRes = IDC_SLIDER_YRES2;
    Roi2Controls.EditXPos = IDC_EDIT_XPOS2;
    Roi2Controls.EditYPos = IDC_EDIT_YPOS2;
    Roi2Controls.EditXRes = IDC_EDIT_XRES2;
    Roi2Controls.EditYRes = IDC_EDIT_YRES2;
    Roi2Controls.SliderRepetitions = IDC_SLIDER_REPETITIONS_ROI2;
    Roi2Controls.EditRepetitions = IDC_EDIT_REPETITIONS_ROI2;
    Roi2Controls.SliderExposure = IDC_SLIDER_EXPOSURE_ROI2;
    Roi2Controls.EditExposure = IDC_EDIT_EXPOSURE_ROI2;
    Roi2Controls.SliderGain = IDC_SLIDER_GAIN_ROI2;
    Roi2Controls.EditGain = IDC_EDIT_GAIN_ROI2;
    Roi2Controls.ButtonSet = IDC_BUTTON_SET_ROI2;
	Roi2Controls.CheckEnable = IDC_CHECK_ENABLE_ROI2;
	Roi2Controls.ButtonAvi = IDC_BUTTON_AVI_ROI2;
	Roi2Controls.ButtonBmp = IDC_BUTTON_BMP_ROI2;
	Roi2Controls.ButtonGrab = IDC_BUTTON_GRAB_ROI2;
	Roi2Controls.bGrabCallback=false;

    Roi3Controls.nROI_ID = ROI_ID_3;
    Roi3Controls.SliderXPos = IDC_SLIDER_XPOS3;
    Roi3Controls.SliderYPos = IDC_SLIDER_YPOS3;
    Roi3Controls.SliderXRes = IDC_SLIDER_XRES3;
    Roi3Controls.SliderYRes = IDC_SLIDER_YRES3;
    Roi3Controls.EditXPos = IDC_EDIT_XPOS3;
    Roi3Controls.EditYPos = IDC_EDIT_YPOS3;
    Roi3Controls.EditXRes = IDC_EDIT_XRES3;
    Roi3Controls.EditYRes = IDC_EDIT_YRES3;
    Roi3Controls.SliderRepetitions = IDC_SLIDER_REPETITIONS_ROI3;
    Roi3Controls.EditRepetitions = IDC_EDIT_REPETITIONS_ROI3;
    Roi3Controls.SliderExposure = IDC_SLIDER_EXPOSURE_ROI3;
    Roi3Controls.EditExposure = IDC_EDIT_EXPOSURE_ROI3;
    Roi3Controls.SliderGain = IDC_SLIDER_GAIN_ROI3;
    Roi3Controls.EditGain = IDC_EDIT_GAIN_ROI3;
    Roi3Controls.ButtonSet = IDC_BUTTON_SET_ROI3;
	Roi3Controls.CheckEnable = IDC_CHECK_ENABLE_ROI3;
	Roi3Controls.ButtonAvi = IDC_BUTTON_AVI_ROI3;
	Roi3Controls.ButtonBmp = IDC_BUTTON_BMP_ROI3;
	Roi3Controls.ButtonGrab = IDC_BUTTON_GRAB_ROI3;
	Roi3Controls.bGrabCallback=false;

    Roi4Controls.nROI_ID = ROI_ID_4;
    Roi4Controls.SliderXPos = IDC_SLIDER_XPOS4;
    Roi4Controls.SliderYPos = IDC_SLIDER_YPOS4;
    Roi4Controls.SliderXRes = IDC_SLIDER_XRES4;
    Roi4Controls.SliderYRes = IDC_SLIDER_YRES4;
    Roi4Controls.EditXPos = IDC_EDIT_XPOS4;
    Roi4Controls.EditYPos = IDC_EDIT_YPOS4;
    Roi4Controls.EditXRes = IDC_EDIT_XRES4;
    Roi4Controls.EditYRes = IDC_EDIT_YRES4;
    Roi4Controls.SliderRepetitions = IDC_SLIDER_REPETITIONS_ROI4;
    Roi4Controls.EditRepetitions = IDC_EDIT_REPETITIONS_ROI4;
    Roi4Controls.SliderExposure = IDC_SLIDER_EXPOSURE_ROI4;
    Roi4Controls.EditExposure = IDC_EDIT_EXPOSURE_ROI4;
    Roi4Controls.SliderGain = IDC_SLIDER_GAIN_ROI4;
    Roi4Controls.EditGain = IDC_EDIT_GAIN_ROI4;
    Roi4Controls.ButtonSet = IDC_BUTTON_SET_ROI4;
	Roi4Controls.CheckEnable = IDC_CHECK_ENABLE_ROI4;
	Roi4Controls.ButtonAvi = IDC_BUTTON_AVI_ROI4;
	Roi4Controls.ButtonBmp = IDC_BUTTON_BMP_ROI4;
	Roi4Controls.ButtonGrab = IDC_BUTTON_GRAB_ROI4;
	Roi4Controls.bGrabCallback=false;

	CString s;
	m_ComboShutter.ResetContent();
	for(int i=0; i<3; i++)
	{
		s.Format("%d. %s", i+1, shutterModesList[i].name);
		m_ComboShutter.AddString(s);
	}

	int numDevices = 0;
	int nStat;

#ifndef LIBFILE
	if ( LoadICubeApi("ICubeSDK.dll") != IC_SUCCESS )
	{
		AfxMessageBox("Can not find ICubeSDK.dll");
		PostMessage(WM_QUIT, NULL, NULL);
		return FALSE;
	}
#endif

    numDevices=ICubeSDK_Init ();
	if(numDevices<1){
		AfxMessageBox("No Devices!");
		return FALSE;
	}

	char CamName[NETCAM_NAME_LENGTH];
	
	m_ComboCam.ResetContent();
	for(int i=0; i<numDevices; i++)
	{
		nStat = ICubeSDK_GetName(i, CamName);
		if ( nStat != IC_SUCCESS )
		{
			AfxMessageBox("Error: getting model name ");
			return FALSE;
		}
		s.Format("%d. %s", i+1, CamName);
		m_ComboCam.AddString(s);
	}
	m_ComboCam.SetCurSel(0);
	g_iCurCam = 0;

	if(!((strcmp(CamName,"NET 4133 CU")==0)||(strcmp(CamName,"NET 4133 BU")==0)||
		 (strcmp(CamName,"NET3 4133 CU")==0)||(strcmp(CamName,"NET3 4133 BU")==0)||
		 (strcmp(CamName,"NET 4203 CU")==0)||(strcmp(CamName,"NET 4203 BU")==0)||
		 (strcmp(CamName,"NET3 4203 CU")==0)||(strcmp(CamName,"NET3 4203 BU")==0)
		 ))
    {
      AfxMessageBox("This example only works with NET4133CU/BU cameras!");
      return FALSE;
    }

	nStat=OpenCam(); 
	if(nStat!=TRUE) 
		return FALSE;

	// Set defaults (optional)
	int XRes=512;
	int YRes=100;
	CSliderCtrl *pSlider;

	pSlider = (CSliderCtrl*)GetDlgItem(Roi11Controls.SliderXRes);		
	pSlider->SetPos(XRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi11Controls.EditXRes,XRes);
	pSlider = (CSliderCtrl*)GetDlgItem(Roi11Controls.SliderYRes);		
	pSlider->SetPos(YRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi11Controls.EditYRes,YRes);
	roi_prop_1_1.nXRes=XRes;
	roi_prop_1_1.nYRes=YRes;

	pSlider = (CSliderCtrl*)GetDlgItem(Roi12Controls.SliderXRes);		
	pSlider->SetPos(XRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi12Controls.EditXRes,XRes);
	pSlider = (CSliderCtrl*)GetDlgItem(Roi12Controls.SliderYRes);		
	pSlider->SetPos(YRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi12Controls.EditYRes,YRes);
	roi_prop_1_2.nXRes=XRes;
	roi_prop_1_2.nYRes=YRes;

	pSlider = (CSliderCtrl*)GetDlgItem(Roi2Controls.SliderXRes);		
	pSlider->SetPos(XRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi2Controls.EditXRes,XRes);
	pSlider = (CSliderCtrl*)GetDlgItem(Roi2Controls.SliderYRes);		
	pSlider->SetPos(YRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi2Controls.EditYRes,YRes);
	roi_prop_2.nXRes=XRes;
	roi_prop_2.nYRes=YRes;

	pSlider = (CSliderCtrl*)GetDlgItem(Roi3Controls.SliderXRes);		
	pSlider->SetPos(XRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi3Controls.EditXRes,XRes);
	pSlider = (CSliderCtrl*)GetDlgItem(Roi3Controls.SliderYRes);		
	pSlider->SetPos(YRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi3Controls.EditYRes,YRes);
	roi_prop_3.nXRes=XRes;
	roi_prop_3.nYRes=YRes;

	pSlider = (CSliderCtrl*)GetDlgItem(Roi4Controls.SliderXRes);		
	pSlider->SetPos(XRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi4Controls.EditXRes,XRes);
	pSlider = (CSliderCtrl*)GetDlgItem(Roi4Controls.SliderYRes);		
	pSlider->SetPos(YRes/ROI_STEP_SIZE);
	SetDlgItemInt(Roi4Controls.EditYRes,YRes);
	roi_prop_4.nXRes=XRes;
	roi_prop_4.nYRes=YRes;
	
    SetTimer(nIDTimerEvent,100,NULL);  // Starting the Timer

	return TRUE;  // return TRUE  unless you set the focus to a control
}
//---------------------------------------------------------------------------
int CMy4133_MultiROIDlg::OpenCam(void)
{
    bReady=false;

    if(ICubeSDK_Open(g_iCurCam)!= IC_SUCCESS ){
        AfxMessageBox("Error: Open ");
        return FALSE;
    }
    unsigned long tmp;
    if(ICubeSDK_GetCamParameter(g_iCurCam,REG_SHUTTER,&tmp)!= IC_SUCCESS ){
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");
        return FALSE;
    }    
	m_ComboShutter.SetCurSel(tmp);

    if(ICubeSDK_GetCamParameter(g_iCurCam,REG_DEFECT_COR,&tmp)!= IC_SUCCESS ){
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");
        return FALSE;
    }
	CheckDlgButton(IDC_CHECK_DEFECT_COR,tmp);
    
    // Init ROI1 Controls
    GetRoiRange(Roi11Controls,&roi_prop_1_1,&roi_range_prop_1_1);
    GetRoiRange(Roi12Controls,&roi_prop_1_2,&roi_range_prop_1_2);	// SIMR
    GetRoiGain(Roi11Controls);
    GetRoiExposure(Roi11Controls);
    GetRoiRepetitions(Roi11Controls);

    // Init ROI2 Controls
	GetRoiRange(Roi2Controls,&roi_prop_2,&roi_range_prop_2);
    GetRoiGain(Roi2Controls);
    GetRoiExposure(Roi2Controls);
    GetRoiRepetitions(Roi2Controls);

    // Init ROI3 Controls
	GetRoiRange(Roi3Controls,&roi_prop_3,&roi_range_prop_3);
    GetRoiGain(Roi3Controls);
    GetRoiExposure(Roi3Controls);
    GetRoiRepetitions(Roi3Controls);

    // Init ROI4 Controls
	GetRoiRange(Roi4Controls,&roi_prop_4,&roi_range_prop_4);
    GetRoiGain(Roi4Controls);
    GetRoiExposure(Roi4Controls);
    GetRoiRepetitions(Roi4Controls);

    bReady=true;

return TRUE;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnCbnSelchangeComboCam()
{
	if(ICubeSDK_IsOpen(g_iCurCam))
        ICubeSDK_Close(g_iCurCam);

	g_iCurCam = m_ComboCam.GetCurSel();

	int nStat=OpenCam(); 
	if(nStat!=TRUE)
		AfxMessageBox("Error: Open Cam ");
}
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy4133_MultiROIDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy4133_MultiROIDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnClose()
{
  if(ICubeSDK_IsOpen(g_iCurCam))
        ICubeSDK_Close(g_iCurCam);
#ifndef LIBFILE
	UnloadICubeApi();
#endif

	KillTimer(nIDTimerEvent);  // Killing the Timer

	CDialog::OnClose();
}
//---------------------------------------------------------------------------
// Switch between the different ROIs
void CMy4133_MultiROIDlg::SetRoiID(int nRoiID)
{
    if(ICubeSDK_SetCamParameter(g_iCurCam,REG_ROI_ID,nRoiID)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCamParameter ");
}
//---------------------------------------------------------------------------
// Gain,Exposure
void CMy4133_MultiROIDlg::SetRoiParameter(int nRoiID,int nParam,int nVal)
{
    SetRoiID(nRoiID);
    if(ICubeSDK_SetCamParameter(g_iCurCam,nParam,nVal)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCamParameter ");
}
//---------------------------------------------------------------------------
// Set new Roi Window
void CMy4133_MultiROIDlg::SetRoiWindow(int nRoiID,ROI_PROPERTY roi_prop,ROI_CALLBACK roiCallback)
{
	int * context = (int*)(nRoiID);   // set context parameter for callback

     SetRoiID(nRoiID);

     // nMode = NULL, because it has to be the same as in StartRoi1()
	 // All rois must have the same mode (CALLBACK_RGB | CALLBACK_RAW)
     if(ICubeSDK_SetCallback(g_iCurCam,NULL,roiCallback,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");

     if(ICubeSDK_SetResolutionParam(g_iCurCam,NULL,true,true)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolutionParam ");
  
     if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolution ");

}
//---------------------------------------------------------------------------
// optional: disable controls to avoid error messages
//
void CMy4133_MultiROIDlg::SetControls(BOOL IsEnabled, ROI_CONTROLS roiControl)
{
	CEdit		*pEdit;
	pEdit = (CEdit *)GetDlgItem(roiControl.ButtonSet);	
	pEdit->EnableWindow(IsEnabled);
	pEdit = (CEdit *)GetDlgItem(roiControl.CheckEnable);	
	pEdit->EnableWindow(IsEnabled);
	pEdit = (CEdit *)GetDlgItem(IDC_BUTTON_SET_ALL_ROIS);	
	pEdit->EnableWindow(0);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::SetControlCheck(BOOL IsEnabled, ROI_CONTROLS roiControl)
{
	CEdit		*pEdit;
	pEdit = (CEdit *)GetDlgItem(roiControl.CheckEnable);	
	pEdit->EnableWindow(IsEnabled);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::SetSaveControls(BOOL IsEnabled, ROI_CONTROLS roiControl)
{
	CEdit		*pEdit;
	pEdit = (CEdit *)GetDlgItem(roiControl.ButtonAvi);	
	pEdit->EnableWindow(IsEnabled);
	pEdit = (CEdit *)GetDlgItem(roiControl.ButtonBmp);	
	pEdit->EnableWindow(IsEnabled);
	pEdit = (CEdit *)GetDlgItem(roiControl.ButtonGrab);	
	pEdit->EnableWindow(IsEnabled);
}
//---------------------------------------------------------------------------
// Get Exposure Time in [msec]
float CMy4133_MultiROIDlg::GetRoiExposureMSec(int nRoiID)
{
    float value;
    if(ICubeSDK_GetExposure(g_iCurCam,&value)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetExposure ");
return value;
}
//---------------------------------------------------------------------------
// Set Exposure Time in [msec]
void CMy4133_MultiROIDlg::SetRoiExp(int nRoiID,char* fVal)
{
    if(!bReady)
        return;

    double fExp = atof(fVal);
    SetRoiID(nRoiID);

    if(ICubeSDK_SetExposure(g_iCurCam,(float)fExp)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetExposure ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonStop()
{
	if(ICubeSDK_Stop(g_iCurCam)!= IC_SUCCESS ){
         AfxMessageBox("Error: Stop ");
         return;
         }

	// Back to default (disable all rois)
	SetControls(true,Roi11Controls);
	SetControls(false,Roi12Controls);      // SIMR mode
    SetSaveControls(false,Roi11Controls);
	SetControls(false,Roi2Controls);
	SetSaveControls(false,Roi2Controls);
	SetControls(false,Roi3Controls);
	SetSaveControls(false,Roi3Controls);
	SetControls(false,Roi4Controls);
	SetSaveControls(false,Roi4Controls);

	CheckDlgButton(IDC_CHECK_ENABLE_ROI1,0);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI12,0);	// SIMR mode
	CheckDlgButton(IDC_CHECK_ENABLE_ROI2,0);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI3,0);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI4,0);

	roi_prop_1_1.bEnabled=FALSE;
	roi_prop_1_2.bEnabled=FALSE;
	roi_prop_2.bEnabled=FALSE;
	roi_prop_3.bEnabled=FALSE;
	roi_prop_4.bEnabled=FALSE;

	CEdit		*pEdit;
	pEdit = (CEdit *)GetDlgItem(IDC_CHECK_DEFECT_COR);	
	pEdit->EnableWindow(1);	// enable defect correction
	pEdit = (CEdit *)GetDlgItem(IDC_COMBO_CAM);	
	pEdit->EnableWindow(1);	// enable camera selection
	pEdit = (CEdit *)GetDlgItem(IDC_BUTTON_SET_ALL_ROIS);	
	pEdit->EnableWindow(1);

	bVideostopped=true;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::StartRoi1(void)
{
	int * context = (int*)(ROI_ID_1_1);

    SetRoiID(ROI_ID_1_1);

    if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_1_1)!= IC_SUCCESS )
         AfxMessageBox("Error: ICubeSDK_SetResolution ");

    if(ICubeSDK_SetCallback(g_iCurCam,CALLBACK_RAW,MyCallbackFunc,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");

    if(ICubeSDK_Start(g_iCurCam,NULL,true,true)!= IC_SUCCESS ){
        AfxMessageBox("Error: Start ");
        return;
        }

	// Enable next roi controls
    SetControls(roi_prop_1_1.bEnabled,Roi2Controls);
	SetSaveControls(roi_prop_1_1.bEnabled,Roi11Controls);
    SetControls(roi_prop_1_1.bEnabled,Roi12Controls);

    // Change Defect correction only if not started 
    CEdit		*pEdit;
	pEdit = (CEdit *)GetDlgItem(IDC_CHECK_DEFECT_COR);	
	pEdit->EnableWindow(0);

	// Disable Camera selection after start
	pEdit = (CEdit *)GetDlgItem(IDC_COMBO_CAM);	
	pEdit->EnableWindow(0);

    bVideostopped=false;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	CString s;
	float value;
	CSliderCtrl *pSlider = (CSliderCtrl *)pScrollBar;
	int nNewVal=pSlider->GetPos();

	switch(pSlider->GetDlgCtrlID())
	{
	case IDC_SLIDER_XPOS1:						// ROI11
		roi_prop_1_1.nXPos=nNewVal;
		SetDlgItemInt(Roi11Controls.EditXPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_YPOS1:
		roi_prop_1_1.nYPos=nNewVal;
		SetDlgItemInt(Roi11Controls.EditYPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_XRES1:
		roi_prop_1_1.nXRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi11Controls.EditXRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_YRES1:
		roi_prop_1_1.nYRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi11Controls.EditYRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_XPOS12:						// ROI12
		roi_prop_1_2.nXPos=nNewVal;
		SetDlgItemInt(Roi12Controls.EditXPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_YPOS12:
		roi_prop_1_2.nYPos=nNewVal;
		SetDlgItemInt(Roi12Controls.EditYPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_XRES12:
		roi_prop_1_2.nXRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi12Controls.EditXRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_YRES12:
		roi_prop_1_2.nYRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi12Controls.EditYRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;	
	case IDC_SLIDER_REPETITIONS_ROI1:
		SetDlgItemInt(Roi11Controls.EditRepetitions,nNewVal,FALSE);
		SetRoiParameter(Roi11Controls.nROI_ID,REG_ROI_CYCLE,nNewVal);
	break;
	case IDC_SLIDER_GAIN_ROI1:
		SetDlgItemInt(Roi11Controls.EditGain,nNewVal,FALSE);
	    SetRoiParameter(Roi11Controls.nROI_ID,REG_GAIN,nNewVal);
	break;
	case IDC_SLIDER_EXPOSURE_ROI1:
	    SetRoiParameter(Roi11Controls.nROI_ID,REG_EXPOSURE_TIME,nNewVal);
		value=GetRoiExposureMSec(Roi11Controls.nROI_ID);
		s.Format("%.3f",value);
		SetDlgItemText(Roi11Controls.EditExposure,s);
	break;

	case IDC_SLIDER_XPOS2:						// ROI2
		roi_prop_2.nXPos=nNewVal;
		SetDlgItemInt(Roi2Controls.EditXPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_YPOS2:
		roi_prop_2.nYPos=nNewVal;
		SetDlgItemInt(Roi2Controls.EditYPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_XRES2:
		roi_prop_2.nXRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi2Controls.EditXRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_YRES2:
		roi_prop_2.nYRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi2Controls.EditYRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;	
	case IDC_SLIDER_REPETITIONS_ROI2:
		SetDlgItemInt(Roi2Controls.EditRepetitions,nNewVal,FALSE);
		SetRoiParameter(ROI_ID_2,REG_ROI_CYCLE,nNewVal);
	break;
	case IDC_SLIDER_GAIN_ROI2:
		SetDlgItemInt(Roi2Controls.EditGain,nNewVal,FALSE);
	    SetRoiParameter(ROI_ID_2,REG_GAIN,nNewVal);
	break;
	case IDC_SLIDER_EXPOSURE_ROI2:
	    SetRoiParameter(Roi2Controls.nROI_ID,REG_EXPOSURE_TIME,nNewVal);
		value=GetRoiExposureMSec(Roi2Controls.nROI_ID);
		s.Format("%.3f",value);
		SetDlgItemText(Roi2Controls.EditExposure,s);
	break;

	case IDC_SLIDER_XPOS3:						// ROI3
		roi_prop_3.nXPos=nNewVal;
		SetDlgItemInt(Roi3Controls.EditXPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_YPOS3:
		roi_prop_3.nYPos=nNewVal;
		SetDlgItemInt(Roi3Controls.EditYPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_XRES3:
		roi_prop_3.nXRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi3Controls.EditXRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_YRES3:
		roi_prop_3.nYRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi3Controls.EditYRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;	
	case IDC_SLIDER_REPETITIONS_ROI3:
		SetDlgItemInt(Roi3Controls.EditRepetitions,nNewVal,FALSE);
		SetRoiParameter(ROI_ID_3,REG_ROI_CYCLE,nNewVal);
	break;
	case IDC_SLIDER_GAIN_ROI3:
		SetDlgItemInt(Roi3Controls.EditGain,nNewVal,FALSE);
	    SetRoiParameter(ROI_ID_3,REG_GAIN,nNewVal);
	break;
	case IDC_SLIDER_EXPOSURE_ROI3:
	    SetRoiParameter(ROI_ID_3,REG_EXPOSURE_TIME,nNewVal);
		value=GetRoiExposureMSec(ROI_ID_3);
		s.Format("%.3f",value);
		SetDlgItemText(Roi3Controls.EditExposure,s);
	break;

	case IDC_SLIDER_XPOS4:						// ROI4
		roi_prop_4.nXPos=nNewVal;
		SetDlgItemInt(Roi4Controls.EditXPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_YPOS4:
		roi_prop_4.nYPos=nNewVal;
		SetDlgItemInt(Roi4Controls.EditYPos,nNewVal,FALSE);
		break;
	case IDC_SLIDER_XRES4:
		roi_prop_4.nXRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi4Controls.EditXRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;
	case IDC_SLIDER_YRES4:
		roi_prop_4.nYRes=nNewVal* ROI_STEP_SIZE;
		SetDlgItemInt(Roi4Controls.EditYRes,nNewVal* ROI_STEP_SIZE,FALSE);
		break;	
	case IDC_SLIDER_REPETITIONS_ROI4:
		SetDlgItemInt(Roi4Controls.EditRepetitions,nNewVal,FALSE);
		SetRoiParameter(ROI_ID_4,REG_ROI_CYCLE,nNewVal);
	break;
	case IDC_SLIDER_GAIN_ROI4:
		SetDlgItemInt(Roi4Controls.EditGain,nNewVal,FALSE);
	    SetRoiParameter(ROI_ID_4,REG_GAIN,nNewVal);
	break;
	case IDC_SLIDER_EXPOSURE_ROI4:
	    SetRoiParameter(ROI_ID_4,REG_EXPOSURE_TIME,nNewVal);
		value=GetRoiExposureMSec(ROI_ID_4);
		s.Format("%.3f",value);
		SetDlgItemText(Roi4Controls.EditExposure,s);
	break;
	default:
		break;
	}	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::GetRoiRange(ROI_CONTROLS roiControl,
                        ROI_PROPERTY *roi_prop,ROI_RANGE_PROPERTY *roi_range_prop)
{
    SetRoiID(roiControl.nROI_ID);

   if(ICubeSDK_GetResolutionRange(g_iCurCam,roi_range_prop)!= IC_SUCCESS ){
        AfxMessageBox("Error: ICubeSDK_GetResolutionRange ");
        return;
    }
    if(ICubeSDK_GetResolution(g_iCurCam,roi_prop)!= IC_SUCCESS ){
        AfxMessageBox("Error: ICubeSDK_GetResolution ");
        return;
    }
	CSliderCtrl *pSlider;
	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderXPos);		// XPos 11
	pSlider->SetRange(0,roi_range_prop->nXMax);
	pSlider->SetPos(roi_prop->nXPos);
	SetDlgItemInt(roiControl.EditXPos,roi_prop->nXPos,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderYPos);		// YPos 11
	pSlider->SetRange(0,roi_range_prop->nYMax);
	pSlider->SetPos(roi_prop->nYPos);
	SetDlgItemInt(roiControl.EditYPos,roi_prop->nYPos,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderXRes);		// XRes 11
	pSlider->SetRange(roi_range_prop->nXMin/ROI_STEP_SIZE,roi_range_prop->nXMax/ROI_STEP_SIZE);
	pSlider->SetPos(roi_prop->nXRes/ROI_STEP_SIZE);
	SetDlgItemInt(roiControl.EditXRes,roi_prop->nXRes,FALSE);

	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderYRes);		// YRes 11
	pSlider->SetRange(roi_range_prop->nYMin/ROI_STEP_SIZE,roi_range_prop->nYMax/ROI_STEP_SIZE);
	pSlider->SetPos(roi_prop->nYRes/ROI_STEP_SIZE);
	SetDlgItemInt(roiControl.EditYRes,roi_prop->nYRes,FALSE);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::GetRoiGain(ROI_CONTROLS roiControl)
{
PARAM_PROPERTY param_prop;
unsigned long nVal;

    SetRoiID(roiControl.nROI_ID);

    if(ICubeSDK_GetCamParameterRange(g_iCurCam,REG_GAIN,&param_prop)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameterRange ");

    if(ICubeSDK_GetCamParameter(g_iCurCam,REG_GAIN,&nVal)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");

	CSliderCtrl *pSlider;
	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderGain);		
	pSlider->SetRange(param_prop.nMin,param_prop.nMax);
	pSlider->SetPos(nVal);
	SetDlgItemInt(roiControl.EditGain,nVal,FALSE);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::GetRoiExposure(ROI_CONTROLS roiControl)
{
PARAM_PROPERTY param_prop;
unsigned long nVal;

    SetRoiID(roiControl.nROI_ID);

    if(ICubeSDK_GetCamParameterRange(g_iCurCam,REG_EXPOSURE_TIME,&param_prop)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameterRange ");

    if(ICubeSDK_GetCamParameter(g_iCurCam,REG_EXPOSURE_TIME,&nVal)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");

	CString s;
    float value=GetRoiExposureMSec(roiControl.nROI_ID);
	s.Format("%.3f",value);

	CSliderCtrl *pSlider;
	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderExposure);		
	pSlider->SetRange(param_prop.nMin,param_prop.nMax);
	pSlider->SetPos(nVal);
	SetDlgItemText(roiControl.EditExposure,s);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::GetRoiRepetitions(ROI_CONTROLS roiControl)
{
PARAM_PROPERTY param_prop;
unsigned long nVal;

    SetRoiID(roiControl.nROI_ID);

    if(ICubeSDK_GetCamParameterRange(g_iCurCam,REG_ROI_CYCLE,&param_prop)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameterRange ");

    if(ICubeSDK_GetCamParameter(g_iCurCam,REG_ROI_CYCLE,&nVal)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");

	CSliderCtrl *pSlider;
	pSlider = (CSliderCtrl*)GetDlgItem(roiControl.SliderRepetitions);		
	pSlider->SetRange(param_prop.nMin,param_prop.nMax);
	pSlider->SetPos(nVal);
	SetDlgItemInt(roiControl.EditRepetitions,nVal,FALSE);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckDefectCor()
{
	unsigned long tmp=IsDlgButtonChecked(IDC_CHECK_DEFECT_COR);
    if(ICubeSDK_SetCamParameter(g_iCurCam,REG_DEFECT_COR,tmp)!= IC_SUCCESS ){
		AfxMessageBox("Error: ICubeSDK_GetCamParameter ");
        return;
    }
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnCbnSelchangeComboShutter()
{
	unsigned long tmp=m_ComboShutter.GetCurSel();
    if(ICubeSDK_SetCamParameter(g_iCurCam,REG_SHUTTER,tmp)!= IC_SUCCESS ){
        AfxMessageBox("Error: ICubeSDK_GetCamParameter ");
        return;
    }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ROI 1
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi1()
{
	if(ICubeSDK_IsStarted(g_iCurCam))
	{
        SetRoiID(ROI_ID_1_1);
		if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_1_1)!= IC_SUCCESS )
            AfxMessageBox("Error: ICubeSDK_SetResolution ");

        if(!roi_prop_1_2.bEnabled)
        {
            if(!roi_prop_2.bEnabled)
                SetControls(roi_prop_1_1.bEnabled,Roi2Controls);
        }
	}
    else
        StartRoi1();    	
}
//---------------------------------------------------------------------------
// Single Image Multiple ROi mode (SMIR-Mode)
void CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi12()
{
	if(ICubeSDK_IsStarted(g_iCurCam))
	{
        SetRoiID(ROI_ID_1_2);
		if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_1_2)!= IC_SUCCESS )
            AfxMessageBox("Error: ICubeSDK_SetResolution ");
       // in SIMR mode, MIMR mode is not allowed
       SetControls(!roi_prop_1_2.bEnabled,Roi2Controls);
	}
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi1()
{
	roi_prop_1_1.bEnabled = IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI1);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi12()
{
	roi_prop_1_2.bEnabled = IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI12);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi1()
{
    Roi11Controls.bGrabCallback = true;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi1()
{
   SetRoiID(ROI_ID_1_1);
   if(ICubeSDK_SaveToFile(g_iCurCam,"roi1.bmp")!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi1()
{
   SetRoiID(ROI_ID_1_1);
   if(ICubeSDK_SaveAvi(g_iCurCam,"roi1.avi",AVI_DIB,1)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi1()
{
	char sBuf[10];
	GetDlgItemText(IDC_EDIT_EXPOSURE_ROI1,sBuf,10);
	SetRoiExp(ROI_ID_1_1,sBuf);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ROI 2
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi2()
{
	if(ICubeSDK_IsStarted(g_iCurCam))
	{
        SetRoiWindow(ROI_ID_2,roi_prop_2,MyCallbackFunc);
		// Disabling of roiN is only allowed when roiN+X is disabled
		SetControlCheck(!roi_prop_2.bEnabled,Roi11Controls);
        // Enable next roi controls
        if(!roi_prop_3.bEnabled)
        {
            SetSaveControls(roi_prop_2.bEnabled,Roi2Controls);
            SetControls(roi_prop_2.bEnabled,Roi3Controls);
        }

        // in MIMR mode, SIMR mode is not allowed
        SetControls(!roi_prop_2.bEnabled,Roi12Controls);
	}
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi2()
{
	roi_prop_2.bEnabled = IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI2);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi2()
{
    Roi2Controls.bGrabCallback = true;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi2()
{
   SetRoiID(ROI_ID_2);
   if(ICubeSDK_SaveToFile(g_iCurCam,"roi2.bmp")!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi2()
{
   SetRoiID(ROI_ID_2);
   if(ICubeSDK_SaveAvi(g_iCurCam,"roi2.avi",AVI_DIB,1)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi2()
{
	char sBuf[10];
	GetDlgItemText(IDC_EDIT_EXPOSURE_ROI2,sBuf,10);
	SetRoiExp(ROI_ID_2,sBuf);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ROI 3
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi3()
{
if(ICubeSDK_IsStarted(g_iCurCam))
	{
       SetRoiWindow(ROI_ID_3,roi_prop_3,MyCallbackFunc);
       // Disabling of roiN is only allowed when roiN+X is disabled
	   SetControlCheck(!roi_prop_3.bEnabled,Roi2Controls);
       // Enable next roi controls
       SetSaveControls(roi_prop_3.bEnabled,Roi3Controls);
       SetControls(roi_prop_3.bEnabled,Roi4Controls);
	}
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi3()
{
	roi_prop_3.bEnabled = IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI3);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi3()
{
    Roi3Controls.bGrabCallback = true;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi3()
{
   SetRoiID(ROI_ID_3);
   if(ICubeSDK_SaveToFile(g_iCurCam,"roi3.bmp")!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi3()
{
   SetRoiID(ROI_ID_3);
   if(ICubeSDK_SaveAvi(g_iCurCam,"roi3.avi",AVI_DIB,1)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi3()
{
char sBuf[10];
	GetDlgItemText(IDC_EDIT_EXPOSURE_ROI3,sBuf,10);
	SetRoiExp(ROI_ID_3,sBuf);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ROI 4
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedButtonSetRoi4()
{
 if(ICubeSDK_IsStarted(g_iCurCam))
	{
        SetRoiWindow(ROI_ID_4,roi_prop_4,MyCallbackFunc);
        // Disabling of roiN is only allowed when roiN+X is disabled
		SetControlCheck(!roi_prop_4.bEnabled,Roi3Controls);
        // Enable roi4 controls
        SetSaveControls(roi_prop_4.bEnabled,Roi4Controls);
        SetControls(roi_prop_4.bEnabled,Roi4Controls);
	}
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableRoi4()
{
	roi_prop_4.bEnabled = IsDlgButtonChecked(IDC_CHECK_ENABLE_ROI4);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonGrabRoi4()
{
    Roi4Controls.bGrabCallback = true;
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonBmpRoi4()
{
   SetRoiID(ROI_ID_4);
   if(ICubeSDK_SaveToFile(g_iCurCam,"roi4.bmp")!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonAviRoi4()
{
   SetRoiID(ROI_ID_4);
   if(ICubeSDK_SaveAvi(g_iCurCam,"roi4.avi",AVI_DIB,1)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SaveToFile ");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnTimer(UINT_PTR nIDEvent)
{
	if(!bVideostopped)
    {                          // get the frame counters
        int badframes;
        ICubeSDK_GetBrokenFrames(g_iCurCam,&badframes);
        SetDlgItemInt(IDC_STATIC_BAD_FRAMES,badframes,FALSE);

        int goodframes;
        ICubeSDK_GetGoodFrames(g_iCurCam,&goodframes);
		SetDlgItemInt(IDC_STATIC_GOOD_FRAMES,goodframes,FALSE);
    }
	CDialog::OnTimer(nIDEvent);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnEnChangeEditExposureRoi4()
{
	char sBuf[10];
	GetDlgItemText(IDC_EDIT_EXPOSURE_ROI4,sBuf,10);
	SetRoiExp(ROI_ID_4,sBuf);
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedButtonDoSwTrigger()
{
	if(g_iTriggerMode==TRIG_HW_START)
	{
		AfxMessageBox("Wrong Trigger mode!");
		return;
	}

	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_TRIGGER ))
	{
		int nStat=ICubeSDK_SetTrigger(g_iCurCam,TRIG_SW_DO);
		if ( nStat != IC_SUCCESS )
		{
				AfxMessageBox("Error SetTrigger");
				return;
		}
	}
	else
	{
		AfxMessageBox("Enable Trigger mode first!");
	}
}
//---------------------------------------------------------------------------
void CMy4133_MultiROIDlg::OnBnClickedCheckEnableTrigger()
{
int nStat;

	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_TRIGGER ))
	{
		nStat=ICubeSDK_SetTrigger(g_iCurCam,g_iTriggerMode);
		if ( nStat != IC_SUCCESS )
		{
				AfxMessageBox("Error SetTrigger");
				return;
		}
	}
	else
	{
		nStat=ICubeSDK_SetTrigger(g_iCurCam,TRIG_STOP);
 		if ( nStat != IC_SUCCESS )
		{
				AfxMessageBox("Error SetTrigger");
				return;
		}
	}
}
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedRadioSoftwareTrigger()
{
	g_iTriggerMode=TRIG_SW_START;
}
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedRadioHardwareTrigger()
{
	g_iTriggerMode=TRIG_HW_START;
}
//---------------------------------------------------------------------------

void CMy4133_MultiROIDlg::OnBnClickedButtonSetAllRois()
{
int * context;
	
	if(!ICubeSDK_IsStarted(g_iCurCam))
	{
	// enable all rois
	roi_prop_1_1.bEnabled=TRUE;
	roi_prop_2.bEnabled=TRUE;
	roi_prop_4.bEnabled=TRUE;
	roi_prop_3.bEnabled=TRUE;

	// set on all mimr-rois the roi parameter
	SetRoiID(ROI_ID_1_1);
    if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_1_1)!= IC_SUCCESS )
         AfxMessageBox("Error: ICubeSDK_SetResolution ");
	SetRoiID(ROI_ID_2);
    if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_2)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolution ");   
	SetRoiID(ROI_ID_3);
    if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_3)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolution "); 
	SetRoiID(ROI_ID_4);
    if(ICubeSDK_SetResolution(g_iCurCam,&roi_prop_4)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolution ");   

	// set on all mimr-rois the callback, if needed
	SetRoiID(ROI_ID_1_1);			
	context = (int*)(ROI_ID_1_1);
    if(ICubeSDK_SetCallback(g_iCurCam,CALLBACK_RAW,MyCallbackFunc,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");
	SetRoiID(ROI_ID_2);
	context = (int*)(ROI_ID_2);	
    if(ICubeSDK_SetCallback(g_iCurCam,NULL,MyCallbackFunc,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");
	SetRoiID(ROI_ID_3);
	context = (int*)(ROI_ID_3);	
    if(ICubeSDK_SetCallback(g_iCurCam,NULL,MyCallbackFunc,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");
	SetRoiID(ROI_ID_4);
	context = (int*)(ROI_ID_4);	
    if(ICubeSDK_SetCallback(g_iCurCam,NULL,MyCallbackFunc,context)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetCallback ");

	// start all mimr-rois 
	SetRoiID(ROI_ID_1_1);
    if(ICubeSDK_Start(g_iCurCam,NULL,true,true)!= IC_SUCCESS )
        AfxMessageBox("Error: Start ");
	SetRoiID(ROI_ID_2);
    if(ICubeSDK_SetResolutionParam(g_iCurCam,NULL,true,true)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolutionParam ");
	SetRoiID(ROI_ID_3);
    if(ICubeSDK_SetResolutionParam(g_iCurCam,NULL,true,true)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolutionParam ");
	SetRoiID(ROI_ID_4);
    if(ICubeSDK_SetResolutionParam(g_iCurCam,NULL,true,true)!= IC_SUCCESS )
        AfxMessageBox("Error: ICubeSDK_SetResolutionParam ");

	bVideostopped=false;

	// enable all roi controls
	SetSaveControls(true,Roi11Controls);
	SetControls(true,Roi2Controls);
	SetSaveControls(true,Roi2Controls);
	SetControls(true,Roi3Controls);
	SetSaveControls(true,Roi3Controls);
	SetControls(true,Roi4Controls);
	SetSaveControls(true,Roi4Controls);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI1,1);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI2,1);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI3,1);
	CheckDlgButton(IDC_CHECK_ENABLE_ROI4,1);

	}

}
