www.pudn.com > DalsaNetlink.rar > CyApp.cpp


// CyApp.cpp : Defines the class behaviors for the application. 
// 
 
#include "stdafx.h" 
#include "CyApp.h" 
#include "CyPropertySheet.h" 
#include "CyDeviceDlg.h" 
#include "CyAcquisitionPage.h" 
#include "CyDiagnosticPage.h" 
#include "CyExternalProgramPage.h" 
#include "iport_version.h" 
#include "CyAboutDlg.h" 
 
#include "CyDevice.h" 
#include "CyGrabber.h" 
#include "CyCameraInterface.h" 
#include "CyDisplay.h" 
#include "CyXMLDocument.h" 
 
#include  
using namespace std; 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// Internal Constants 
///////////////////////////////////////////////////////////////////////////// 
 
// ----- Formats ----- 
static const char	*IMAGE_EXT			= "bmp"; 
static const char	*IMAGE_FILE_TYPE	= "Bitmap (*.bmp)|*.bmp|RAW image (*.raw)|*.raw|All files (*.*)|*.*||"; 
static const char	*CONFIG_EXT			= "XML"; 
static const char	*CONFIG_FILE_TYPE	= "Configuration Files (*.xml)|*.xml|All files (*.*)|*.*||"; 
 
 
///////////////////////////////////////////////////////////////////////////// 
// Utilities 
 
class CyCommandLine : public CCommandLineInfo 
{ 
public: 
    bool    mShowDiagnosticPage; 
 
    CyCommandLine() 
        : mShowDiagnosticPage( false ) 
    { 
    } 
 
    virtual void ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast ) 
    { 
        static CString lDiag1( "diag" ); 
        static CString lDiag2( "-diag" ); 
        static CString lDiag3( "--diag" ); 
        if ( ( lDiag1.CompareNoCase( lpszParam ) == 0 ) || 
             ( lDiag2.CompareNoCase( lpszParam ) == 0 ) || 
             ( lDiag3.CompareNoCase( lpszParam ) == 0 ) ) 
        { 
            mShowDiagnosticPage = true; 
        } 
        else 
            CCommandLineInfo::ParseParam( lpszParam, bFlag, bLast ); 
    } 
}; 
 
 
unsigned long CyApp::mCallbackCounter = 0; 
unsigned long CyApp::mCallbackMask = 0; 
unsigned long CyApp::mCallbackTimestamp = 0; 
 
CyResult CallbackFunction( CyApp *aApp, CyDevice::CallbackId aId, unsigned long aTimeStamp, CyDevice::InterruptStatus aStatus ) 
{ 
    CyAssert( aApp != NULL ); 
    CyAssert( aId == CyDevice::CB_ON_INTERRUPT_GPIO ); 
 
    aApp->mCallbackCounter++; 
    aApp->mCallbackMask = aStatus.mGPIOLUTInputs; 
    aApp->mCallbackTimestamp = aTimeStamp; 
 
    return CY_RESULT_OK; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CyApp 
 
BEGIN_MESSAGE_MAP(CyApp, CWinApp) 
	//{{AFX_MSG_MAP(CyApp) 
	ON_COMMAND(ID_APP_ABOUT, OnAbout) 
	ON_COMMAND(ID_CAMERA_RESET, OnCameraReset) 
	ON_COMMAND(ID_FILE_SAVE_ADAPTER_INDEX, OnFileSaveAdapterIndex) 
	//}}AFX_MSG_MAP 
 
	ON_COMMAND(ID_FILE_SAVE, OnFileSave) 
	ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveas) 
	ON_COMMAND(ID_HELP, CWinApp::OnHelp) 
    ON_COMMAND(ID_BUFFER_QUEUE_1, OnBufferQueue1) 
    ON_COMMAND(ID_BUFFER_QUEUE_2, OnBufferQueue2) 
    ON_COMMAND(ID_BUFFER_QUEUE_4, OnBufferQueue4) 
    ON_COMMAND(ID_BUFFER_QUEUE_8, OnBufferQueue8) 
    ON_COMMAND(ID_BUFFER_QUEUE_16, OnBufferQueue16) 
    ON_COMMAND(ID_BUFFER_QUEUE_32, OnBufferQueue32) 
    ON_COMMAND(ID_BUFFER_QUEUE_64, OnBufferQueue64) 
    ON_COMMAND(ID_BUFFER_QUEUE_128, OnBufferQueue128) 
    ON_COMMAND(ID_BUFFER_QUEUE_256, OnBufferQueue256) 
    ON_COMMAND(ID_BUFFER_QUEUE_REMOVE_WHEN_FULL, OnBufferQueueModeReplace) 
    ON_COMMAND(ID_BUFFER_QUEUE_DROP_NEW_WHEN_FULL, OnBufferQueueModeDrop) 
    ON_COMMAND(ID_RESET_HOST_PC_INTERRUPT_COUNT, OnResetInterruptCount) 
END_MESSAGE_MAP() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CyApp construction 
 
CyApp::CyApp() 
    : CWinApp( "Camera Interface Application" ) 
    , mGrabber( 0 ) 
    , mCamera( 0 ) 
    , mPropertySheet( "Camera Interface Application" ) 
    , mSaveAdapterAsIndex( false ) 
    , mMulticastMasterNoData( false ) 
    , mConfigChanged( false ) 
    , mDetectOnStartup( false ) 
    , mSelectCameraAfterDetect( false ) 
    , mConnectAfterCameraSelection( false ) 
    , mAcquisitionAfterConnect( false ) 
    , mDefaultAnswerTimeout( CyConfig::DEFAULT_ANSWER_TIMEOUT ) 
    , mDefaultFirstPacketTimeout( CyConfig::DEFAULT_FIRST_PACKET_TIMEOUT ) 
    , mDefaultPacketTimeout( CyConfig::DEFAULT_PACKET_TIMEOUT ) 
    , mDefaultRequestTimeout( CyConfig::DEFAULT_REQUEST_TIMEOUT ) 
    , mDefaultPacketSize( CyConfig::DEFAULT_PACKET_SIZE_DRV ) 
    , mBayerFlag ( 0 ) 
#ifdef _HIGH_MEMORY_ 
    , mHighMemoryManager( 2, 260 ) 
#endif // _HIGH_MEMORY_ 
{ 
    mGrabber = new CyGrabber; 
 
    GetDevice().RegisterCallback2( CyDevice::CB_ON_INTERRUPT_GPIO, 
                                  (CyDevice::Callback2)CallbackFunction, 
                                   reinterpret_cast( this ) ); 
} 
 
CyApp::~CyApp() 
{ 
    // stop any running displays 
    std::map< CyChannelID, ChannelData* >::iterator lItr; 
    for ( lItr = mChannels.begin(); lItr != mChannels.end(); ++lItr ) 
    { 
        delete lItr->second; 
    } 
    mDisplay.StopDisplaying(); 
 
    delete mCamera; 
    mCamera = 0; 
 
    delete mGrabber; 
    mGrabber = 0; 
 
    // clear the channel data 
    mChannels.clear(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only CyApp object 
 
CyApp theApp; 
 
///////////////////////////////////////////////////////////////////////////// 
// CyApp initialization 
 
BOOL CyApp::InitInstance() 
{ 
    // Change the help file 
    string lHelp( m_pszHelpFilePath ); 
    string::size_type lPos = lHelp.find_last_of( "\\/" ); 
    CyAssert( lPos != string::npos ); 
    lHelp.erase( lPos + 1 ); 
    lHelp += "Coyote.hlp"; 
    free( (void*)m_pszHelpFilePath ); 
    m_pszHelpFilePath = strdup( lHelp.c_str() ); 
 
	// Change the registry key under which our settings are stored. 
	SetRegistryKey( IPORT_VERSION_COMPANY_NAME ); 
     
    // Read some registry settings 
    mDetectOnStartup = GetProfileInt( "", "DetectOnStartup", false ) != 0; 
    mSelectCameraAfterDetect = GetProfileInt( "", "SelectCameraAfterDetect", false ) != 0; 
    mConnectAfterCameraSelection = GetProfileInt( "", "ConnectAfterCameraSelection", false ) != 0; 
    mAcquisitionAfterConnect = GetProfileInt( "", "AcquisitionAfterConnect", false ) != 0; 
    mDefaultAnswerTimeout = GetProfileInt( "", "DefaultAnswerTimeout", CyConfig::DEFAULT_ANSWER_TIMEOUT ); 
    mDefaultFirstPacketTimeout = GetProfileInt( "", "DefaultFirstPacketTimeout", CyConfig::DEFAULT_FIRST_PACKET_TIMEOUT ); 
    mDefaultPacketTimeout = GetProfileInt( "", "DefaultPacketTimeout", CyConfig::DEFAULT_PACKET_TIMEOUT ); 
    mDefaultRequestTimeout = GetProfileInt( "",  "DefaultRequestTimeout", CyConfig::DEFAULT_REQUEST_TIMEOUT ); 
    mDefaultPacketSize = GetProfileInt( "",  "DefaultPacketSize", CyConfig::DEFAULT_PACKET_SIZE_DRV ); 
 
    // Analyze the command-line for a config file 
    CyCommandLine lCommandLine; 
    ParseCommandLine( lCommandLine ); 
    if ( lCommandLine.m_strFileName.GetLength() > 0 ) 
    { 
        CyXMLDocument lDocument( (LPCTSTR)lCommandLine.m_strFileName ); 
        if ( lDocument.LoadDocument() == CY_RESULT_OK ) 
        { 
	        // Connect the default camera from config file 
            CyResult lResult = mConfig.LoadFromXML( lDocument ); 
	        if ( lResult == CY_RESULT_OK ) 
	        { 
		        // Name of the default device to load 
		        if ( mConfig.FindDeviceByName( "Default" ) != CY_RESULT_OK ) 
                { 
                    mConfig.SetIndex( 0 ); 
                } 
	        } 
            else if ( lResult != CY_RESULT_CANNOT_OPEN_FILE ) 
            { 
                CyErrorInfo lError; 
                mConfig.GetErrorInfo( &lError ); 
 
                ostringstream lMessage; 
                lMessage << "Could not read camera information because " << lError.mMessage; 
                AfxMessageBox( lMessage.str().c_str() ); 
            } 
        } 
    } 
 
    // Load the accelerators 
    mAccelerators = LoadAccelerators( m_hInstance, MAKEINTRESOURCE( IDR_MAINFRAME ) ); 
 
    // Create and display the prop sheet 
    CyDeviceDlg lDevice; 
    mFileName = m_lpCmdLine; 
    mPropertySheet.AddPage( &lDevice ); 
    CyAcquisitionPage lAcquisition; 
    mPropertySheet.AddPage( &lAcquisition ); 
    CyDiagnosticPage lDiag; 
    if ( lCommandLine.mShowDiagnosticPage ) 
        mPropertySheet.AddPage( &lDiag ); 
    CyExternalProgramPage lExternal; 
    mPropertySheet.AddPage( &lExternal ); 
    mPropertySheet.DoModal(); 
 
	// Since the dialog has been closed, return FALSE so that we exit the 
	//  application, rather than start the application's message pump. 
	return FALSE; 
} 
 
void CyApp::OnAbout()  
{ 
    CyAboutDlg().DoModal(); 
} 
 
void CyApp::OnCameraReset()  
{ 
	if ( HasCamera() ) 
        GetCamera().ResetCamera(); 
} 
 
void CyApp::OnFileSave()  
{ 
    CyConfig& lConfig = GetConfig(); 
    CyErrorInfo lError; 
 
    if ( mFileName == "" ) 
    { 
        OnFileSaveas(); 
    } 
    else 
    { 
	    try 
	    { 
            // Create the XML document 
            CyXMLDocument lDocument( (LPCTSTR) mFileName ); 
            CyResult lResult = lDocument.LoadDocument(); 
            if ( lResult != CY_RESULT_OK )  
            { 
                if ( lResult == CY_RESULT_CANNOT_OPEN_FILE ) 
                { 
                    lResult = lDocument.CreateDocument( "Camera_Interface_Application" ); 
                    if ( lResult != CY_RESULT_OK ) 
                    { 
                        lDocument.GetErrorInfo( &lError ); 
                        AfxMessageBox( lError.mMessage ); 
                        return; 
                    } 
                } 
                else 
                { 
                    lDocument.GetErrorInfo( &lError ); 
                    AfxMessageBox( lError.mMessage ); 
                    return; 
                } 
            } 
 
            // Save the config 
		    if ( ( lConfig.SaveToXML( lDocument, mSaveAdapterAsIndex ) != CY_RESULT_OK ) && 
                 ( lDocument.GetErrorInfo( &lError ) != CY_RESULT_OK ) ) 
            { 
                AfxMessageBox( lError.mMessage ); 
                return; 
            }             
 
            // save the current camera 
            if ( HasCamera() && 
                 ( GetCamera().SaveToXML( lDocument ) != CY_RESULT_OK ) && 
                 ( lDocument.GetErrorInfo( &lError ) != CY_RESULT_OK ) ) 
            { 
                AfxMessageBox( lError.mMessage ); 
                return; 
            } 
 
            // Save the file 
            if ( ( lDocument.WriteToFile() != CY_RESULT_OK ) && 
                 ( lDocument.GetErrorInfo( &lError ) != CY_RESULT_OK ) ) 
            { 
                AfxMessageBox( lError.mMessage ); 
                return; 
            } 
 
            mConfigChanged = false; 
	    } 
	    catch ( ... ) 
	    { 
		    CString lStr = "Unexpected exception"; 
		    AfxMessageBox( lStr ); 
	    } 
    } 
} 
 
void CyApp::OnFileSaveas()  
{ 
    CyConfig& lConfig = GetConfig(); 
    CyErrorInfo lError; 
 
	CFileDialog	ConfigSaveDialog( false, CONFIG_EXT, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CONFIG_FILE_TYPE ); 
	if ( ConfigSaveDialog.DoModal() == IDOK ) 
    { 
        mFileName = ConfigSaveDialog.GetPathName(); 
        OnFileSave(); // should not come back, because mFileName is not "" 
    } 
} 
 
BOOL CyApp::PreTranslateMessage(MSG* pMsg)  
{ 
	// allow tooltip messages to be filtered 
	if (CWinApp::PreTranslateMessage(pMsg)) 
		return TRUE; 
 
	if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST) 
	{ 
		// finally, translate the message 
		return ::TranslateAccelerator(mPropertySheet.GetSafeHwnd(), mAccelerators, pMsg); 
	} 
	return FALSE; 
} 
 
void CyApp::OnFileSaveAdapterIndex()  
{ 
    mSaveAdapterAsIndex = !mSaveAdapterAsIndex; 
 
    UINT lFlags = MF_BYCOMMAND; 
    if ( mSaveAdapterAsIndex ) 
        lFlags |= MF_CHECKED; 
    else 
        lFlags |= MF_UNCHECKED; 
    mPropertySheet.GetMenu()->GetSubMenu( 0 )->CheckMenuItem( ID_FILE_SAVE_ADAPTER_INDEX, lFlags ); 
} 
 
 
 
void CyApp::SetBufferQueueSize( unsigned long aSize ) 
{ 
    std::map< CyChannelID, ChannelData* >::iterator lItr; 
    for ( lItr = mChannels.begin(); lItr != mChannels.end(); ++lItr ) 
    { 
        (*lItr).second->mBuffer.SetQueueSize( aSize ); 
    } 
} 
 
#define SET_BUFFER_QUEUE( aSize ) \ 
    void CyApp::OnBufferQueue##aSize() \ 
    { \ 
        SetBufferQueueSize( aSize ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_1, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_2, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_4, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_8, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_16, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_32, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_64, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_128, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_256, MF_BYCOMMAND | MF_UNCHECKED ); \ 
        mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 1 )->CheckMenuItem( ID_BUFFER_QUEUE_##aSize, MF_BYCOMMAND | MF_CHECKED ); \ 
    } 
 
SET_BUFFER_QUEUE(   1 ) 
SET_BUFFER_QUEUE(   2 ) 
SET_BUFFER_QUEUE(   4 ) 
SET_BUFFER_QUEUE(   8 ) 
SET_BUFFER_QUEUE(  16 ) 
SET_BUFFER_QUEUE(  32 ) 
SET_BUFFER_QUEUE(  64 ) 
SET_BUFFER_QUEUE( 128 ) 
SET_BUFFER_QUEUE( 256 ) 
 
void CyApp::OnBufferQueueModeReplace() 
{ 
    mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 2 )->CheckMenuItem( ID_BUFFER_QUEUE_REMOVE_WHEN_FULL, MF_BYCOMMAND | MF_CHECKED ); 
    mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 2 )->CheckMenuItem( ID_BUFFER_QUEUE_DROP_NEW_WHEN_FULL, MF_BYCOMMAND | MF_UNCHECKED ); 
  
    std::map< CyChannelID, ChannelData* >::iterator lItr; 
    for ( lItr = mChannels.begin(); lItr != mChannels.end(); ++lItr ) 
    { 
        (*lItr).second->mBuffer.SetQueueMode( false ); 
    } 
} 
 
void CyApp::OnBufferQueueModeDrop() 
{ 
    mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 2 )->CheckMenuItem( ID_BUFFER_QUEUE_REMOVE_WHEN_FULL, MF_BYCOMMAND | MF_UNCHECKED ); 
    mPropertySheet.GetMenu()->GetSubMenu( 2 )->GetSubMenu( 2 )->CheckMenuItem( ID_BUFFER_QUEUE_DROP_NEW_WHEN_FULL, MF_BYCOMMAND | MF_CHECKED ); 
 
    std::map< CyChannelID, ChannelData* >::iterator lItr; 
    for ( lItr = mChannels.begin(); lItr != mChannels.end(); ++lItr ) 
    { 
        (*lItr).second->mBuffer.SetQueueMode( true ); 
    } 
} 
 
void CyApp::OnResetInterruptCount() 
{ 
    mCallbackCounter = 0; 
	mCallbackMask = 0; 
	mCallbackTimestamp = 0; 
}