// SensAmteoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SensAmteo.h"
#include "SensAmteoDlg.h"
#include "runmain.h"
#include "appdebug.h"

#include <iolib.h>
#include <afxwin.h>
#include "datadef.h"
#include "appiodef.h"
#include "hwboard.h"
#include "gvariables.h"
#include "pccomm.h"

#define SLIDER_MAXIMUM	65535


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 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()


// CSensAmteoDlg dialog




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

void CSensAmteoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_PIC_CU_LED, picBoardLed);
	DDX_Control(pDX, IDC_PIC_CU_LED2, picBoardLed2);
	DDX_Control(pDX, IDC_CB_POWER_ON, cbPowerOn);

	DDX_Control(pDX, IDC_CB_DI_0, BTN_DI_0);
	DDX_Control(pDX, IDC_CB_DI_1, BTN_DI_1);
	DDX_Control(pDX, IDC_CB_DI_2, BTN_DI_2);
	DDX_Control(pDX, IDC_CB_DI_3, BTN_DI_3);
	DDX_Control(pDX, IDC_CB_DI_4, BTN_DI_4);
	DDX_Control(pDX, IDC_CB_DI_5, BTN_DI_5);
	DDX_Control(pDX, IDC_CB_DI_6, BTN_DI_6);
	DDX_Control(pDX, IDC_CB_DI_7, BTN_DI_7);
	DDX_Control(pDX, IDC_CB_DI_8, BTN_DI_STS);

	DDX_Control(pDX, IDC_CB_DO_0, BTN_DO_0);
	DDX_Control(pDX, IDC_CB_DO_1, BTN_DO_1);
	DDX_Control(pDX, IDC_CB_DO_2, BTN_DO_2);
	DDX_Control(pDX, IDC_CB_DO_3, BTN_DO_3);
	DDX_Control(pDX, IDC_CB_DO_4, BTN_DO_4);
	DDX_Control(pDX, IDC_CB_DO_5, BTN_DO_5);
	DDX_Control(pDX, IDC_CB_DO_6, BTN_DO_6);
	DDX_Control(pDX, IDC_CB_DO_7, BTN_DO_7);
	DDX_Control(pDX, IDC_CB_DO_8, BTN_DO_OUT_EN);
	DDX_Control(pDX, IDC_CB_DO_9, BTN_DO_LAMBDA_POWER);


	DDX_Control(pDX, IDC_SL_AI_0, SL_AI_0);
	DDX_Control(pDX, IDC_SL_AI_1, SL_AI_1);
	DDX_Control(pDX, IDC_SL_AI_2, SL_AI_2);
	DDX_Control(pDX, IDC_SL_AI_3, SL_AI_3);
	DDX_Control(pDX, IDC_SL_AI_4, SL_AI_4);
	DDX_Control(pDX, IDC_SL_AI_5, SL_AI_5);
	DDX_Control(pDX, IDC_SL_AI_6, SL_AI_6);

	DDX_Control(pDX, IDC_ED_AI_0, ED_AI_0);
	DDX_Control(pDX, IDC_ED_AI_1, ED_AI_1);
	DDX_Control(pDX, IDC_ED_AI_2, ED_AI_2);
	DDX_Control(pDX, IDC_ED_AI_3, ED_AI_3);
	DDX_Control(pDX, IDC_ED_AI_4, ED_AI_4);
	DDX_Control(pDX, IDC_ED_AI_5, ED_AI_5);
	DDX_Control(pDX, IDC_ED_AI_6, ED_AI_6);

	DDX_Control(pDX, IDC_SL_AO_0, SL_AO_0);
	DDX_Control(pDX, IDC_ED_AO_0, ED_AO_0);

}

BEGIN_MESSAGE_MAP(CSensAmteoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CB_POWER_ON, &CSensAmteoDlg::OnBnClickedCbPowerOn)

END_MESSAGE_MAP()


// CSensAmteoDlg message handlers

BOOL CSensAmteoDlg::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

	SL_AI_0.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_1.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_2.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_3.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_4.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_5.SetRange(0, SLIDER_MAXIMUM, true);
	SL_AI_6.SetRange(0, SLIDER_MAXIMUM, true);

	SL_AO_0.SetRange(0, SLIDER_MAXIMUM, true);

	// TODO: Add extra initialization here
	SetTimer( 1, 100, 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

BOOL CSensAmteoDlg::DestroyWindow()
{
	//! finalizace dat simulatoru

	if(cbPowerOn.GetCheck() != 0)
	{
		// Ukonceni ulohy
		StopMain();
	}

//	delete imageMotor;

	return CDialog::DestroyWindow();
}


void CSensAmteoDlg::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 CSensAmteoDlg::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 CSensAmteoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


bool flReceived;
char TextMessage[80];

void CSensAmteoDlg::OnTimer(UINT_PTR nIDEvent)
{
	if(cbPowerOn.GetCheck() != 0)
	{
		CString s;
		CBitmap bmpLedOff, bmpLedGreen;

		bmpLedOff.LoadBitmapW(IDB_LED_OFF);
		bmpLedGreen.LoadBitmapW(IDB_LED_GREEN);

		//! LED desky ridici jednotky
		picBoardLed.SetBitmap(lLed & 0x01 ? bmpLedGreen : bmpLedOff);
		picBoardLed2.SetBitmap(lLed & 0x02 ? bmpLedGreen : bmpLedOff);

		uint8_t tmpDig = 0;

		tmpDig = (BTN_DI_0.GetCheck() == BST_CHECKED);
		tmpDig |= (BTN_DI_1.GetCheck() == BST_CHECKED) << 1;
		tmpDig |= (BTN_DI_2.GetCheck() == BST_CHECKED) << 2;
		tmpDig |= (BTN_DI_3.GetCheck() == BST_CHECKED) << 3;
		tmpDig |= (BTN_DI_4.GetCheck() == BST_CHECKED) << 4;
		tmpDig |= (BTN_DI_5.GetCheck() == BST_CHECKED) << 5;
		tmpDig |= (BTN_DI_6.GetCheck() == BST_CHECKED) << 6;
		tmpDig |= (BTN_DI_7.GetCheck() == BST_CHECKED) << 7;

		gIOBoardCtx.gDInp[0] = tmpDig;
		gIOBoardCtx.gDInp[1] = (BTN_DI_STS.GetCheck() == BST_CHECKED);


		tmpDig = gIOBoardCtx.gDOutp[0];
		BTN_DO_0.SetCheck( tmpDig & 0x01 );		
		BTN_DO_1.SetCheck( tmpDig & 0x02 );		
		BTN_DO_2.SetCheck( tmpDig & 0x04 );		
		BTN_DO_3.SetCheck( tmpDig & 0x08 );		
		BTN_DO_4.SetCheck( tmpDig & 0x10 );		
		BTN_DO_5.SetCheck( tmpDig & 0x20 );		
		BTN_DO_6.SetCheck( tmpDig & 0x40 );		
		BTN_DO_7.SetCheck( tmpDig & 0x80 );	

		tmpDig = gIOBoardCtx.gDOutp[1];
		BTN_DO_OUT_EN.SetCheck( tmpDig & 0x01 );
		BTN_DO_LAMBDA_POWER.SetCheck( tmpDig & 0x02 );

		gIOBoardCtx.gAInp[0] = SL_AI_0.GetPos();
		gIOBoardCtx.gAInp[1] = SL_AI_1.GetPos();
		gIOBoardCtx.gAInp[2] = SL_AI_2.GetPos();
		gIOBoardCtx.gAInp[3] = SL_AI_3.GetPos();
		gIOBoardCtx.gAInp[4] = SL_AI_4.GetPos();
		gIOBoardCtx.gAInp[5] = SL_AI_5.GetPos();
		gIOBoardCtx.gAInp[6] = SL_AI_6.GetPos();


		s.Format(_T("% 00 4d"), sGetAInp( AI_THERMOCOUPLE ) );
		ED_AI_0.SetWindowTextW( s );
		s.Format(_T("% 00 4d"), sGetAInp(AI_CONNTEMP) );
		ED_AI_1.SetWindowTextW( s );
		s.Format(_T("% 00 4d"), sGetAInp(AI_LAMBDA_CURRENT) );
		ED_AI_2.SetWindowTextW( s );
		s.Format(_T("% 00 4d"), sGetAInp(AI_RESERVED) );
		ED_AI_3.SetWindowTextW( s );
		s.Format(_T("% 00 4d"), sGetAInp(AI_LAMBDA) );
		ED_AI_4.SetWindowTextW( s );
		s.Format(_T("% 00 4d"), sGetAInp(AI_PHOTORES) );
		ED_AI_5.SetWindowTextW( s );

		s.Format(_T("% 00 4d"), modbusL2.CPT1 );
		//		s.Format(_T("% 00 4d"), sGetAInp(AI_PHOTOTRANS) );
		ED_AI_6.SetWindowTextW( s );

		SL_AO_0.SetPos( gIOBoardCtx.gAOutp[0] );
		
		s.Format(_T("% 00 4X"), sGetAOutp(AO_0) );
		ED_AO_0.SetWindowTextW( s );
		

	}
}


void CSensAmteoDlg::OnBnClickedCbPowerOn()
{
	if(cbPowerOn.GetCheck() != 0)
	{
		// spusteni aplikace
		RunMain();
	}
	else
	{
		// Ukonceni ulohy
		StopMain();
	}
}
