//
//  status_display.h - status display implementation
//
//  $Id: status_display.cpp 49 2014-02-07 01:52:31Z brian $ 
//
//  Copyright (c) 1999-2014 by Precision Micro Control Corp. All rights reserved.
//
#include "stdafx.h"

IMPLEMENT_DYNCREATE(CStatusDisplay, CFormView)

CStatusDisplay::CStatusDisplay()
	: CFormView(CStatusDisplay::IDD)
{
	TRACE("Constructor: CStatusDisplay\r\n");
	
	m_file_position = "";
	m_program_number = "";
	m_repeat_count = "";
	m_sequence_number = "";
	m_tool_number = "";
}

CStatusDisplay::~CStatusDisplay()
{
	TRACE("Destructor: CStatusDisplay\r\n");
	if (theApp.GetMachine())	
		theApp.GetMachine()->CloseStatusView();
}

void CStatusDisplay::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	
	DDX_Control(pDX, IDC_STATUS_PROGRAMERROR, m_status_program_error);
	DDX_Control(pDX, IDC_STATUS_EMERGENCYSTOP, m_status_estop);
	DDX_Control(pDX, IDC_STATUS_EXTEMERGENCYSTOP, m_status_ext_estop);
	DDX_Control(pDX, IDC_STATUS_DRIVEFAULT, m_status_drive_fault);
	DDX_Control(pDX, IDC_STATUS_MOTORERROR, m_status_motor_error);
	
	DDX_Control(pDX, IDC_STATUS_ABSOLUTE, m_mode_absolute);
	DDX_Control(pDX, IDC_STATUS_INCREMENTAL, m_mode_incremental);
	
	DDX_Control(pDX, IDC_BLOCK_SKIP, m_option_block_skip);
	DDX_Control(pDX, IDC_DRYRUN, m_option_dryrun);

	DDX_Text(pDX, IDC_FILE_POSITION, m_file_position);
	DDX_Text(pDX, IDC_PROGRAM_NUMBER, m_program_number);
	DDX_Text(pDX, IDC_REPEAT_COUNT, m_repeat_count);
	DDX_Text(pDX, IDC_SEQUENCE_NUMBER, m_sequence_number);
	DDX_Text(pDX, IDC_TOOL_NUMBER, m_tool_number);
}

BEGIN_MESSAGE_MAP(CStatusDisplay, CFormView)
	ON_WM_TIMER()
END_MESSAGE_MAP()

void CStatusDisplay::OnTimer(UINT nIDEvent)
{
	OnUpdate(NULL,0,NULL);

	CFormView::OnTimer(nIDEvent);
}

void CStatusDisplay::OnInitialUpdate()
{
	if (theApp.GetMachine() == NULL)
		return;						// Skip if machine doesn't exist
	
	// UpdateData() calls DoDataExchange() which connects controls to member data
	UpdateData(FALSE);
	OnUpdate(NULL, 0, NULL);
	CWnd::SetTimer(100, 100, NULL);
}

void CStatusDisplay::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	CMachine* machine;
	if ((machine = theApp.GetMachine()) == NULL)
		return;					// Skip if machine doesn't exist

	int value = machine->MtnCtrlr.GetRunStatus();
	
	m_status_program_error.SetCheck((value & PROGRAM_ERROR) != 0);
	m_status_estop.SetCheck((value & E_STOP) != 0);
	m_status_ext_estop.SetCheck((value & EE_STOP) != 0);
	m_status_drive_fault.SetCheck((value & DRIVE_FAULT) != 0);
	m_status_motor_error.SetCheck((value & MOTOR_ERROR) != 0);
		
	m_mode_absolute.SetCheck((value & DIM_INPUT_ABS) != 0);
	m_mode_incremental.SetCheck((value & DIM_INPUT_INC) != 0);
	
	m_option_block_skip.SetCheck((value & BLOCK_SKIP) !=0 );
	m_option_dryrun.SetCheck((value & DRYRUN) !=0 );

	char buffer[DOUBLESTRINGLENGTH];

	value = machine->MtnCtrlr.GetProgramNumber();
	_snprintf(buffer, sizeof(buffer), "%3d", value);
	m_program_number = buffer;
	
	value = machine->MtnCtrlr.GetSequenceNumber();
	_snprintf(buffer, sizeof(buffer), "%4d", value);
	m_sequence_number = buffer;
	
	value = machine->MtnCtrlr.GetRepeatCount();
	_snprintf(buffer, sizeof(buffer), "%4d", value);
	m_repeat_count = buffer;
	
	value = machine->MtnCtrlr.GetFilePosition();
	_snprintf(buffer, sizeof(buffer), "%6d", value);
	m_file_position = buffer;
	
	value = machine->MtnCtrlr.GetToolNumber();
	_snprintf(buffer, sizeof(buffer), "%6d", value);
	m_tool_number = buffer;
	
	UpdateData(FALSE);
	
}
