// ClockOverride.cpp : implementation file
//

#include "stdafx.h"
#include "digifwrk.h"
#include "resource.h"
#include "ClockOverride.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

enum ClockModes
{
	CLOCK_ODD = 0,
	CLOCK_EVEN,
	CLOCK_BRG
};

#define SET_TCLOK_ODD   0x00010000L
#define SET_TCLOK_EVEN  0x00020000L
#define SET_TCLOK_BRG   0x00040000L
#define SET_PHASE       0x00080000L
#define SET_RCLOK_ODD   0x00001000L
#define SET_RCLOK_EVEN  0x00002000L
#define SET_RCLOK_BRG   0x00004000L

/////////////////////////////////////////////////////////////////////////////
// CClockOverride dialog

IMPLEMENT_DYNCREATE(CClockOverride, CConfigWizPage)

CClockOverride::CClockOverride()
{
	//{{AFX_DATA_INIT(CClockOverride)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bInit = FALSE;
	CConfigWizPage::Construct(IDD_CLOCK_OVERRIDE);
}

CClockOverride::CClockOverride(CConfigObject const * const pOwner, int const nState)
{
	m_bInit = FALSE;
	CConfigWizPage::Construct(IDD_WIZ_CLOCK_OVERRIDE,0,pOwner,nState);
}

CClockOverride::~CClockOverride()
{
}

void CClockOverride::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClockOverride)
	DDX_Control(pDX, IDC_CLOCK_TXOVERRIDE, m_TxOverrideCombo);
	DDX_Control(pDX, IDC_CLOCK_TXOVERRIDE_TEXT, m_TxOverrideComboText);
	DDX_Control(pDX, IDC_CLOCK_RXOVERRIDE, m_RxOverrideCombo);
	DDX_Control(pDX, IDC_CLOCK_RXOVERRIDE_TEXT, m_RxOverrideComboText);
	DDX_Control(pDX, IDC_INVERT_PHASE, m_InvertPhase);
	DDX_Control(pDX, IDC_ENABLE_OVERRIDE, m_EnableOverride);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClockOverride, CConfigWizPage)
	//{{AFX_MSG_MAP(CClockOverride)
	ON_BN_CLICKED(IDC_ENABLE_OVERRIDE, OnEnableOverride)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// called by the owner of the dialog object. provide the 3 arrays holding IO,memory
// and Interrupt #'s along with current IO,MEM and IRQ choice.
void CClockOverride::SetChoices(DWORD dwOverrideValue)
{	
	m_WorkOverrideValue   = dwOverrideValue;
	m_BackupOverrideValue = dwOverrideValue;	
}


BOOL CClockOverride::GetChoices(DWORD& dwClockOverride)
{

	dwClockOverride = m_BackupOverrideValue;

	return TRUE;
}

void CClockOverride::UpdateOverride( void )
{	
	m_WorkOverrideValue = 0;

	if ( m_EnableOverride.GetCheck() == BST_UNCHECKED ) return;

	switch ( m_TxOverrideCombo.GetCurSel() )
	{
		case CLOCK_ODD:  m_WorkOverrideValue |= SET_TCLOK_ODD;  break;
		case CLOCK_EVEN: m_WorkOverrideValue |= SET_TCLOK_EVEN; break;
		case CLOCK_BRG:  m_WorkOverrideValue |= SET_TCLOK_BRG;  break;
	}

	switch ( m_RxOverrideCombo.GetCurSel() )
	{
		case CLOCK_ODD:  m_WorkOverrideValue |= SET_RCLOK_ODD;  break;
		case CLOCK_EVEN: m_WorkOverrideValue |= SET_RCLOK_EVEN; break;
		case CLOCK_BRG:  m_WorkOverrideValue |= SET_RCLOK_BRG;  break;
	}
	
	if ( m_InvertPhase.GetCheck() == BST_CHECKED )
	{
		m_WorkOverrideValue |= SET_PHASE;
	}
}

void CClockOverride::UpdateControlState( void )
{
	if ( m_ShallOverride == TRUE )
	{		
		m_EnableOverride.SetCheck(TRUE);

		m_TxOverrideCombo.EnableWindow( TRUE );
		m_TxOverrideComboText.EnableWindow( TRUE );
		m_RxOverrideCombo.EnableWindow( TRUE );
		m_RxOverrideComboText.EnableWindow( TRUE );
		m_InvertPhase.EnableWindow( TRUE );


		m_TxOverrideCombo.AddString("ODD");
		m_TxOverrideCombo.AddString("EVEN");
		m_TxOverrideCombo.AddString("BRG");

		m_RxOverrideCombo.AddString("ODD");
		m_RxOverrideCombo.AddString("EVEN");
		m_RxOverrideCombo.AddString("BRG");

		//
		// Preset invert phase control:
		//
		if ( m_WorkOverrideValue & SET_PHASE )
		{
			m_InvertPhase.SetCheck( BST_CHECKED );
		}
		else
		{
			m_InvertPhase.SetCheck( BST_UNCHECKED );
		}

		//
		// Preset transmit clock control:
		//
		if ( m_WorkOverrideValue & SET_TCLOK_ODD )
		{
			m_TxOverrideCombo.SetCurSel( CLOCK_ODD );
		}
		else if ( m_WorkOverrideValue & SET_TCLOK_EVEN )
		{
			m_TxOverrideCombo.SetCurSel( CLOCK_EVEN );
		}
		else if ( m_WorkOverrideValue & SET_TCLOK_BRG )
		{
			m_TxOverrideCombo.SetCurSel( CLOCK_BRG );
		}
		else
		{
			// default:
			m_TxOverrideCombo.SetCurSel( CLOCK_EVEN );
		}

		//
		// Preset receive clock control:
		//
		if ( m_WorkOverrideValue & SET_RCLOK_ODD )
		{
			m_RxOverrideCombo.SetCurSel( CLOCK_ODD );
		}
		else if ( m_WorkOverrideValue & SET_RCLOK_EVEN )
		{
			m_RxOverrideCombo.SetCurSel( CLOCK_EVEN );
		}
		else if ( m_WorkOverrideValue & SET_RCLOK_BRG )
		{
			m_RxOverrideCombo.SetCurSel( CLOCK_BRG );
		}
		else
		{
			// default:
			m_RxOverrideCombo.SetCurSel( CLOCK_EVEN );
		}
	}
	else
	{			
		m_EnableOverride.SetCheck(FALSE);
		m_TxOverrideCombo.EnableWindow( FALSE );
		m_TxOverrideComboText.EnableWindow( FALSE );
		m_RxOverrideCombo.EnableWindow( FALSE );
		m_RxOverrideComboText.EnableWindow( FALSE );
		m_InvertPhase.EnableWindow( FALSE );	
	}
}

BOOL CClockOverride::OnInitDialog() 
{
	CConfigWizPage::OnInitDialog();	

	m_ShallOverride = ( m_WorkOverrideValue != 0 ) ? TRUE : FALSE;
	UpdateControlState();
	UpdateOverride();

	UpdateData(FALSE);	
	m_bInit = TRUE;
	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CClockOverride::OnOK() 
{
	m_bInit = FALSE;

	UpdateOverride(); // update override value

	m_BackupOverrideValue = m_WorkOverrideValue;

	CConfigWizPage::OnOK();
}

/////////////////////////////////////////////////////////////////////////////
// CClockOverride message handlers

void CClockOverride::OnEnableOverride() 
{	
	m_ShallOverride = ( m_EnableOverride.GetCheck() == BST_CHECKED ) ? TRUE : FALSE;

	UpdateOverride(); // update override value
	UpdateControlState();
}
