//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: DNesher.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of DNesher.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Device\DNesher.h" #include "..\LgLib\Comm\lgsm.h" #else #include "DNesher.h" #include "..\Comm\lgsm.h" #endif #define TIMEOUT_BETWEEN_COMMANDS 500 CLgDevicePTZNesher::CLgDevicePTZNesher() { m_nMode = CMD_GET_PTZ; m_RealPosition.dPan = 0; m_nTimeoutCommand = 100; m_nTimeoutAfterCommand = 500; m_nTimeoutEnterSwitch = 5000; m_nTimeoutInterSwitch = 5000; m_nSwitchMode = -1; m_bPause = FALSE; m_nSwitchEnterRemain = m_nTimeoutEnterSwitch; m_nSwitchInterRemain = m_nTimeoutInterSwitch; } CLgDevicePTZNesher::~CLgDevicePTZNesher() { } void CLgDevicePTZNesher::OnPerformCmd() { // ready for new message ReleaseCmd(); // ready to process new message m_nMode = CMD_NONE; } BOOL CLgDevicePTZNesher::WriteProc( void *pProtocol, PLONG pTimeout ) { // cast to protocol CProtocolPTZNesher *pProtocolPTZ = (CProtocolPTZNesher*)pProtocol; CConnection *pConnection = pProtocolPTZ->GetConnection(); // after command timeout if( m_bPause ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER PAUSE"), _T("") ); *pTimeout = m_nTimeoutAfterCommand; m_bPause = FALSE; // do not read, just wait for timeout return FALSE; } // check input command if( m_nMode == CMD_NONE ) { switch( GetCmd() ) { case CMD_GET_PTZ: m_nMode = CMD_GET_PTZ; break; case CMD_SET_PTZ: // validate positions /* PTZ_NESHER::ValidatePan( m_Position.dPan ); PTZ_NESHER::ValidateTilt( m_Position.dTilt ); PTZ_NESHER::ValidateZoom( m_Position.dZoom ); */ m_nMode = CMD_SET_PTZ; break; case CMD_MOVE_PAN: m_nMode = CMD_MOVE_PAN; break; case CMD_NONE: // no new commands break; case CMD_SET_IRIS: case CMD_SET_FOCUS: case CMD_WIPER: default: //command unsupported OnPerformCmd(); OnEvtCommandError( m_nMode, _T("UNSUPPORTED") ); break; } } INT nRtsDtr; // fill packet depend on command switch( m_nMode ) { case CMD_SET_PTZ: // validate postion if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 ) { nRtsDtr = 0x00; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 ) { nRtsDtr = 0x01; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 ) { nRtsDtr = 0x02; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else { nRtsDtr = 0x03; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 0 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } pConnection->SetRTS( nRtsDtr & 0x01 ); pConnection->SetDTR( nRtsDtr & 0x02 ); LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER SET PTZ"), _T("RTS:%d DTR:%d POS:%d"), nRtsDtr & 0x01, (nRtsDtr & 0x02) == 0x02, m_RealPosition.dPan ); break; case CMD_GET_PTZ: LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER GET PTZ"), _T("") ); break; case CMD_MOVE_PAN: // update position if( m_MovePan.dSpeed ) { if( m_MovePan.bRight ) m_RealPosition.dPan -= LGS_MIRROR_ANGLE_OF_VIEW / 4; else m_RealPosition.dPan += LGS_MIRROR_ANGLE_OF_VIEW / 4; // validate if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 ) { nRtsDtr = 0x00; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 ) { nRtsDtr = 0x01; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 ) { nRtsDtr = 0x02; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else { nRtsDtr = 0x03; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 0 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } pConnection->SetRTS( nRtsDtr & 0x01 ); pConnection->SetDTR( nRtsDtr & 0x02 ); LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER PAN MOVE"), _T("RTS:%d DTR:%d POS:%d"), nRtsDtr & 0x01, (nRtsDtr & 0x02) == 0x02, m_RealPosition.dPan ); } else LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER PAN STOP"), _T("") ); break; case CMD_NONE: default: // start new mode if( m_nSwitchEnterRemain <= 0 ) { if( m_nSwitchInterRemain <= 0 ) { // switch according to mode switch( m_nSwitchMode ) { case 0: // always move right if( m_nSwitchInterDir ) m_RealPosition.dPan += LGS_MIRROR_ANGLE_OF_VIEW / 4; else m_RealPosition.dPan -= LGS_MIRROR_ANGLE_OF_VIEW / 4; // validate if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 ) { nRtsDtr = 0x00; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; m_nSwitchInterDir = !m_nSwitchInterDir; } else if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 ) { nRtsDtr = 0x01; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_RealPosition.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 ) { nRtsDtr = 0x02; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else { nRtsDtr = 0x03; m_RealPosition.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 0 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; m_nSwitchInterDir = !m_nSwitchInterDir; } LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER PAN AUTO"), _T("RTS:%d DTR:%d POS:%d"), nRtsDtr & 0x01, (nRtsDtr & 0x02) == 0x02, m_RealPosition.dPan ); break; default: // set camera in default position m_Position.dPan = m_nSwitchMode * LGS_MIRROR_ANGLE_OF_VIEW / 4 - LGS_MIRROR_ANGLE_OF_VIEW / 8; // validate postion if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 ) { nRtsDtr = 0x00; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 ) { nRtsDtr = 0x01; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else if ( m_Position.dPan > LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 ) { nRtsDtr = 0x02; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } else { nRtsDtr = 0x03; m_RealPosition.dPan = m_Position.dPan = LGS_MIRROR_ANGLE_OF_VIEW * 0 / 4 + LGS_MIRROR_ANGLE_OF_VIEW / 8; } break; } pConnection->SetRTS( nRtsDtr & 0x01 ); pConnection->SetDTR( nRtsDtr & 0x02 ); // reintialize inter remaining time m_nSwitchInterRemain = m_nTimeoutInterSwitch; } else // decrease time remain between consecutive switches m_nSwitchInterRemain = m_nSwitchInterRemain - 50L; } else // decrease time remaining to enter to switch mode m_nSwitchEnterRemain = m_nSwitchEnterRemain - 50L; *pTimeout = 50L; // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER NO COMMAND"), _T("") ); return FALSE; } // till next switch remain ... m_nSwitchEnterRemain = m_nTimeoutEnterSwitch; m_nSwitchInterRemain = m_nTimeoutInterSwitch; // after the time confirmation will be sent *pTimeout = m_nTimeoutCommand; // do perform read return TRUE; } BOOL CLgDevicePTZNesher::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance ) { // only at the end of timeout if( !bLastChance ) return FALSE; switch( m_nMode ) { case CMD_SET_PTZ: OnPerformCmd(); //notify OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER SET PTZ"), _T("CONFIRMATION") ); break; case CMD_GET_PTZ: // ready to process new message OnPerformCmd(); //notify OnEvtGetPTZ( m_Position.dPan = m_RealPosition.dPan, m_Position.dTilt = m_RealPosition.dTilt, m_Position.dZoom = m_RealPosition.dZoom ); LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER GET PTZ"), _T("CONFIRMATION") ); break; case CMD_MOVE_PAN: OnPerformCmd(); OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("NESHER PAN"), _T("CONFIRMATION") ); break; } // after/inter command timeout m_bPause = TRUE; return TRUE; }