Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

408 lines
11 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DeviceVMs.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DeviceVMs.cpp
//----------------------------------------------------------------------------
// DeviceVideoMatrix.cpp: implementation of the CDeviceVMs class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "chicago.h"
#include "DeviceVMs.h"
#include "SocketVM.h"
#include "..\..\LgLib\Version.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const CDeviceVMs::COMMAND CDeviceVMs::CMD_INIT = { 2, 0, FALSE, FALSE, { { STAGE_SET_LOGICAL_ID, 50, 0, 0 }, { STAGE_GET_CONNECT, 30, 3, 0 } } };
const CDeviceVMs::COMMAND CDeviceVMs::CMD_AUTO_INIT = { 3, 0, FALSE, FALSE, { { STAGE_GET_HARDWARE_ID, 50, 0, 0 }, { STAGE_SET_LOGICAL_ID, 50, 0, 0 }, { STAGE_GET_CONNECT, 30, 3, 0 } } };
const CDeviceVMs::COMMAND CDeviceVMs::CMD_GET_STATE = { 1, 0, FALSE, FALSE, { { STAGE_GET_CONNECT, 30, 3, 0 } } };
const CDeviceVMs::COMMAND CDeviceVMs::CMD_CONNECT = { 2, 0, FALSE, FALSE, { { STAGE_SET_CONNECT, 0, 0, 0 }, { STAGE_GET_CONNECT, 30, 3, 0 } } };
CDeviceVMs::CDeviceVMs()
{
m_pVideoMatrix = NULL;
CDeviceVM::SetIOCount( INPUT_COUNT, OUTPUT_COUNT );
::ZeroMemory( &m_Command, sizeof( m_Command ) );
::ZeroMemory( m_bCommand, sizeof( m_bCommand ) );
for( int i = 0; i < OUTPUT_COUNT; i++ )
m_DataConnect.pOutput[ i ] = VI_UNDEFINED;
m_bAutoInit = FALSE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CDeviceVMs::CDeviceVMs") );
}
CDeviceVMs::~CDeviceVMs()
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CDeviceVMs::~CDeviceVMs") );
}
void CDeviceVMs::PostConnect(int nInput, int nOutput )
{
CDeviceVM::PostConnect( nInput, nOutput );
if( m_pSocket )
((CSocketVM*)m_pSocket)->Resume();
}
void CDeviceVMs::PostGetState()
{
CDeviceVM::PostGetState();
if( m_pSocket )
((CSocketVM*)m_pSocket)->Resume();
}
void CDeviceVMs::SetLogicalID( WORD wID )
{
m_pLogicalID[0] = LOBYTE( wID );
m_pLogicalID[1] = HIBYTE( wID );
}
BOOL CDeviceVMs::GetCommand( LPBYTE pBuffer, DWORD &dwPause, BOOL &bPause )
{
if( m_Command.nStage == m_Command.nCount )
{
DVMEVENT event;
while( m_Queue.Pop( event ) )
{
m_bCommand[ event.id ] = TRUE;
switch( event.id )
{
case DVMC_INIT:
break;
case DVMC_GET_STATE:
break;
case DVMC_CONNECT:
m_DataConnect.pOutput[ event.nOutput ] = event.nInput;
break;
}
}
if( m_bCommand[ DVMC_INIT ] )
{
m_bCommand[ DVMC_INIT ] = FALSE;
if( m_bAutoInit )
m_Command = CMD_AUTO_INIT;
else
m_Command = CMD_INIT;
}
else
if( m_bCommand[ DVMC_CONNECT ] )
{
m_bCommand[ DVMC_CONNECT ] = FALSE;
m_Command = CMD_CONNECT;
}
else
if( m_bCommand[ DVMC_GET_STATE ] )
{
m_bCommand[ DVMC_GET_STATE ] = FALSE;
m_Command = CMD_GET_STATE;
}
else
{
return FALSE; // no commands
}
}
if( m_Command.bPause ) // needs to perform pause
{
m_Command.bPause = FALSE; // pause only ones
dwPause = m_Command.pStage[ m_Command.nStage ].nPause;
if( m_Command.bSuccess )
{
m_Command.nStage++;
}
else
{
// prepare what to do after pause
if( m_Command.pStage[ m_Command.nStage ].nRetry >= m_Command.pStage[ m_Command.nStage ].nCount )
{
m_Command.nStage++; // to next stage because of number retries expaired
if( m_Command.nStage == m_Command.nCount )
m_Command.nStage = 0; // repeat sequence
m_Command.pStage[m_Command.nStage].nRetry = 0;
}
else
{
m_Command.pStage[ m_Command.nStage ].nRetry++;
}
}
return bPause = TRUE; // nothing to write and read because of PAUSE
}
bPause = FALSE;
switch( m_Command.pStage[ m_Command.nStage ].nStage )
{
case STAGE_GET_HARDWARE_ID:
// always 12 ( with response )
pBuffer[ 0 ] = 12;
// command get hardware id
pBuffer[ 1 ] = 1 + 128;
// PC's address
pBuffer[ 2 ] = 0;
pBuffer[ 3 ] = 0;
// Device's address
pBuffer[ 4 ] = 0;
pBuffer[ 5 ] = 0;
// Parameters
pBuffer[ 6 ] = 0;
pBuffer[ 7 ] = 0;
pBuffer[ 8 ] = 0;
pBuffer[ 9 ] = 0;
// Always
pBuffer[ 11 ] = 243;
// Sum
pBuffer[ 10 ] = (BYTE)-((INT)pBuffer[ 0 ] +
(INT)pBuffer[ 1 ] +
(INT)pBuffer[ 2 ] +
(INT)pBuffer[ 3 ] +
(INT)pBuffer[ 4 ] +
(INT)pBuffer[ 5 ] +
(INT)pBuffer[ 6 ] +
(INT)pBuffer[ 7 ] +
(INT)pBuffer[ 8 ] +
(INT)pBuffer[ 9 ] +
(INT)pBuffer[ 11 ] );
break;
case STAGE_SET_LOGICAL_ID:
// always 12 ( with response )
pBuffer[ 0 ] = 12;
// command set logical id
pBuffer[ 1 ] = 0;// + 128;
// PC's address
pBuffer[ 2 ] = 0;
pBuffer[ 3 ] = 0;
// Device's address
pBuffer[ 4 ] = m_pLogicalID[0];
pBuffer[ 5 ] = m_pLogicalID[1];
// Parameters
pBuffer[ 6 ] = m_pHardwareID[0];
pBuffer[ 7 ] = m_pHardwareID[1];
pBuffer[ 8 ] = m_pHardwareID[2];
pBuffer[ 9 ] = m_pHardwareID[3];
// Always
pBuffer[ 11 ] = 243;
// Sum
pBuffer[ 10 ] = (BYTE)-((INT)pBuffer[ 0 ] +
(INT)pBuffer[ 1 ] +
(INT)pBuffer[ 2 ] +
(INT)pBuffer[ 3 ] +
(INT)pBuffer[ 4 ] +
(INT)pBuffer[ 5 ] +
(INT)pBuffer[ 6 ] +
(INT)pBuffer[ 7 ] +
(INT)pBuffer[ 8 ] +
(INT)pBuffer[ 9 ] +
(INT)pBuffer[ 11 ] );
break;
case STAGE_SET_CONNECT:
// always 12 ( with response )
pBuffer[ 0 ] = 12;
// command set video outputs
pBuffer[ 1 ] = 20;// + 128;
// PC's address
pBuffer[ 2 ] = 0;
pBuffer[ 3 ] = 0;
// Device's address
pBuffer[ 4 ] = m_pLogicalID[0];
pBuffer[ 5 ] = m_pLogicalID[1];
// Parameters
pBuffer[ 6 ] = ( m_DataConnect.pOutput[0] == VI_UNDEFINED ) ? 63 : m_DataConnect.pOutput[0];
pBuffer[ 7 ] = ( m_DataConnect.pOutput[1] == VI_UNDEFINED ) ? 63 : m_DataConnect.pOutput[1];
pBuffer[ 8 ] = ( m_DataConnect.pOutput[2] == VI_UNDEFINED ) ? 63 : m_DataConnect.pOutput[2];
pBuffer[ 9 ] = ( m_DataConnect.pOutput[3] == VI_UNDEFINED ) ? 63 : m_DataConnect.pOutput[3];
// Always
pBuffer[ 11 ] = 243;
// Sum
pBuffer[ 10 ] = (BYTE)-((INT)pBuffer[ 0 ] +
(INT)pBuffer[ 1 ] +
(INT)pBuffer[ 2 ] +
(INT)pBuffer[ 3 ] +
(INT)pBuffer[ 4 ] +
(INT)pBuffer[ 5 ] +
(INT)pBuffer[ 6 ] +
(INT)pBuffer[ 7 ] +
(INT)pBuffer[ 8 ] +
(INT)pBuffer[ 9 ] +
(INT)pBuffer[ 11 ] );
break;
case STAGE_GET_CONNECT:
// always 12 ( with response )
pBuffer[ 0 ] = 12;
// command set video outputs
pBuffer[ 1 ] = 21 + 128;
// PC's address
pBuffer[ 2 ] = 0;
pBuffer[ 3 ] = 0;
// Device's address
pBuffer[ 4 ] = m_pLogicalID[0];
pBuffer[ 5 ] = m_pLogicalID[1];
// Parameters
pBuffer[ 6 ] = 0;
pBuffer[ 7 ] = 0;
pBuffer[ 8 ] = 0;
pBuffer[ 9 ] = 0;
// Always
pBuffer[ 11 ] = 243;
// Sum
pBuffer[ 10 ] = (BYTE)-((INT)pBuffer[ 0 ] +
(INT)pBuffer[ 1 ] +
(INT)pBuffer[ 2 ] +
(INT)pBuffer[ 3 ] +
(INT)pBuffer[ 4 ] +
(INT)pBuffer[ 5 ] +
(INT)pBuffer[ 6 ] +
(INT)pBuffer[ 7 ] +
(INT)pBuffer[ 8 ] +
(INT)pBuffer[ 9 ] +
(INT)pBuffer[ 11 ] );
break;
}
return TRUE;
}
BOOL CDeviceVMs::SetAnswer( LPBYTE pBuffer, DWORD nCount )
{
switch( m_Command.pStage[ m_Command.nStage ].nStage )
{
case STAGE_GET_HARDWARE_ID:
case STAGE_GET_CONNECT:
m_Command.bSuccess = FALSE;
// verify response
if( nCount >= CSocketVM::MSG_LENGTH )
{
switch( pBuffer[1] & 0x7F )
{
case 1:
m_pHardwareID[0] = pBuffer[6];
m_pHardwareID[1] = pBuffer[7];
m_pHardwareID[2] = pBuffer[8];
m_pHardwareID[3] = pBuffer[9];
m_Command.bSuccess = TRUE;
break;
case 21:
m_DataConnect.pOutput[0] = ( pBuffer[6] > GetInputCount() ) ? VI_UNDEFINED : pBuffer[6];
m_DataConnect.pOutput[1] = ( pBuffer[7] > GetInputCount() ) ? VI_UNDEFINED : pBuffer[7];
m_DataConnect.pOutput[2] = ( pBuffer[8] > GetInputCount() ) ? VI_UNDEFINED : pBuffer[8];
m_DataConnect.pOutput[3] = ( pBuffer[9] > GetInputCount() ) ? VI_UNDEFINED : pBuffer[9];
CDeviceVM::SetConnect( m_DataConnect.pOutput[0], 0 );
CDeviceVM::SetConnect( m_DataConnect.pOutput[1], 1 );
CDeviceVM::SetConnect( m_DataConnect.pOutput[2], 2 );
CDeviceVM::SetConnect( m_DataConnect.pOutput[3], 3 );
m_Command.bSuccess = TRUE;
break;
}
// command succeeded
if( m_Command.bSuccess )
{
if( m_Command.pStage[ m_Command.nStage ].nPause )
{
m_Command.bPause = TRUE;
}
else
{
m_Command.bPause = FALSE;
m_Command.nStage++;
m_Command.pStage[ m_Command.nStage ].nRetry = 0;
}
break; // command succeeded
}
}
// command failed
m_Command.bSuccess = FALSE;
// if after command needs pause
if( m_Command.pStage[ m_Command.nStage ].nPause )
{
m_Command.bPause = TRUE;
}
else
{
m_Command.bPause = FALSE;
// it needs additional retry
m_Command.pStage[ m_Command.nStage ].nRetry++;
if( m_Command.pStage[ m_Command.nStage ].nRetry < m_Command.pStage[ m_Command.nStage ].nCount )
{
// repeat command
TRACE2( "(%02X%02X)Command repeat\n", m_pLogicalID[0], m_pLogicalID[1] );
}
else
{
// number retries expaired
m_Command.nStage++;
if( m_Command.nCount == m_Command.nStage )
{
m_Command.nStage = 0; // repeat sequence from the begin
TRACE2( "(%02X%02X)Sequence repeat\n", m_pLogicalID[0], m_pLogicalID[1] );
}
m_Command.pStage[ m_Command.nStage ].nRetry = 0;
}
}
break;
case STAGE_SET_LOGICAL_ID:
case STAGE_SET_CONNECT:
m_Command.bSuccess = TRUE; // command succeeded
if( m_Command.pStage[ m_Command.nStage ].nPause )
{
m_Command.bPause = TRUE;
}
else
{
m_Command.bPause = FALSE;
m_Command.pStage[ m_Command.nStage ].nRetry++;
if( m_Command.pStage[ m_Command.nStage ].nRetry < m_Command.pStage[ m_Command.nStage ].nCount )
{
// repeat command
}
else
{
// number retries expaired
m_Command.nStage++;
m_Command.pStage[ m_Command.nStage ].nRetry = 0;
}
}
break;
}
return m_Command.bSuccess;
}