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.
388 lines
11 KiB
388 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: DPelco.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of DPelco.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
|
|
#ifdef __BORLANDC__
|
|
#include "..\LgLib\Device\DPelco.h"
|
|
#else
|
|
#include "DPelco.h"
|
|
#endif
|
|
|
|
#define POSITION_QUALYTY 2
|
|
#define MAX_NO_REPLY_COUNT 20
|
|
#define MAX_NOT_IN_POSITION_COUNT 1
|
|
#define DELAY_BEFORE_GET_POSITION 500
|
|
|
|
CLgDevicePTZPelco::CLgDevicePTZPelco()
|
|
{
|
|
m_nMode = CMD_INIT;
|
|
m_nStage = STAGE_INIT_TURN_ON;
|
|
m_nNoReplyCount = 0;
|
|
m_nNotInPositionCount = 0;
|
|
memset( &m_PtzState, 0, sizeof(m_PtzState) );
|
|
m_bPause = FALSE;
|
|
}
|
|
|
|
CLgDevicePTZPelco::~CLgDevicePTZPelco()
|
|
{
|
|
|
|
}
|
|
|
|
BOOL CLgDevicePTZPelco::CmdInit()
|
|
{
|
|
// check availibility
|
|
return DoCmd( CMD_INIT );
|
|
}
|
|
|
|
void CLgDevicePTZPelco::OnPerformCmd()
|
|
{
|
|
// ready for new message
|
|
ReleaseCmd();
|
|
// ready to process new message
|
|
m_nMode = CMD_NONE;
|
|
}
|
|
|
|
BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout )
|
|
{
|
|
BYTE pSendBuffer[ CProtocolPTZPelco::MAX_COMMAND_SIZE ];
|
|
BYTE nSendCount = 0;
|
|
|
|
// cast to protocol
|
|
CProtocolPTZPelco *pProtocolPTZ = (CProtocolPTZPelco*)pProtocol;
|
|
|
|
// set working address
|
|
pProtocolPTZ->SetAddress( GetAddress() );
|
|
|
|
if( m_bPause )
|
|
{
|
|
m_bPause = FALSE;
|
|
*pTimeout = 100L;
|
|
return FALSE;
|
|
}
|
|
|
|
// check input command
|
|
if( m_nMode == CMD_NONE )
|
|
{
|
|
switch( GetCmd() )
|
|
{
|
|
case CMD_INIT:
|
|
m_nMode = CMD_INIT;
|
|
m_nStage = STAGE_INIT_TURN_ON;
|
|
break;
|
|
case CMD_GET_PTZ:
|
|
m_nMode = CMD_GET_PTZ;
|
|
m_nStage = STAGE_GET_PAN_POSITION;
|
|
break;
|
|
case CMD_SET_PTZ:
|
|
// validate positions
|
|
PTZ_PELCO::ValidatePan( m_Position.dPan );
|
|
PTZ_PELCO::ValidateTilt( m_Position.dTilt );
|
|
PTZ_PELCO::ValidateZoom( m_Position.dZoom );
|
|
m_nMode = CMD_SET_PTZ;
|
|
m_nStage = STAGE_SET_PAN_POSITION;
|
|
m_nNotInPositionCount = 0;
|
|
break;
|
|
case CMD_MOVE_TILT:
|
|
m_nMode = CMD_MOVE_TILT;
|
|
break;
|
|
case CMD_MOVE_PAN:
|
|
m_nMode = CMD_MOVE_PAN;
|
|
break;
|
|
case CMD_MOVE_ZOOM:
|
|
m_nMode = CMD_MOVE_ZOOM;
|
|
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;
|
|
}
|
|
}
|
|
|
|
// fill packet depend on command
|
|
switch( m_nMode )
|
|
{
|
|
case CMD_INIT:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_INIT_TURN_ON:
|
|
nSendCount += PTZ_PELCO::BuildMove( pSendBuffer, GetAddress(), m_PtzState );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_INIT_AUTO_FOCUS:
|
|
nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), 1 );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_INIT_AUTO_IRIS:
|
|
nSendCount += PTZ_PELCO::BuildAutoIris( pSendBuffer, GetAddress(), 1 );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_INIT_AUTO_GAIN_CONTROL:
|
|
nSendCount += PTZ_PELCO::BuildAutoGainControl( pSendBuffer, GetAddress(), 1 );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_INIT_BACKLIGHT_COMPENSATION:
|
|
nSendCount += PTZ_PELCO::BuildBacklightCompensation( pSendBuffer, GetAddress(), 1 );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_INIT_WHITE_BALANCE:
|
|
nSendCount += PTZ_PELCO::BuildWhiteBalance( pSendBuffer, GetAddress(), 1 );
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
}
|
|
break;
|
|
case CMD_SET_PTZ:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_SET_PAN_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildSetPan( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Pan( m_Position.dPan ) );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_SET_TILT_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildSetTilt( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Tilt( m_Position.dTilt ) );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_SET_ZOOM_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildSetZoom( pSendBuffer, GetAddress(), PTZ_PELCO::Percent2Zoom( m_Position.dZoom ) );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
case STAGE_GET_PAN_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
case STAGE_GET_TILT_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
case STAGE_GET_ZOOM_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case CMD_GET_PTZ:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_GET_PAN_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
case STAGE_GET_TILT_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
case STAGE_GET_ZOOM_POSITION:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 7 );
|
|
break;
|
|
}
|
|
break;
|
|
|
|
/* case CMD_INIT:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildMove( pSendBuffer, GetAddress(), m_PtzState );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
*/
|
|
case CMD_MOVE_PAN:
|
|
// ask for pan position
|
|
nSendCount += PTZ_PELCO::BuildPan( pSendBuffer, GetAddress(), m_MovePan.dSpeed > 0 ? ( m_MovePan.bRight ? PTZ_PELCO::PAN_RIGHT : PTZ_PELCO::PAN_LEFT ) : PTZ_PELCO::PAN_STOP, PTZ_PELCO::Percent2PanSpeed( m_MovePan.dSpeed ), m_PtzState );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
|
|
case CMD_MOVE_TILT:
|
|
nSendCount += PTZ_PELCO::BuildTilt( pSendBuffer, GetAddress(), m_MoveTilt.dSpeed > 0 ? ( m_MoveTilt.bUp ? PTZ_PELCO::TILT_UP : PTZ_PELCO::TILT_DOWN ) : PTZ_PELCO::TILT_STOP, PTZ_PELCO::Percent2TiltSpeed( m_MoveTilt.dSpeed ), m_PtzState );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
|
|
case CMD_MOVE_ZOOM:
|
|
nSendCount += PTZ_PELCO::BuildZoom( pSendBuffer, GetAddress(), m_MoveZoom.dSpeed > 0 ? ( m_MoveZoom.bTele ? PTZ_PELCO::ZOOM_TELE : PTZ_PELCO::ZOOM_WIDE ) : PTZ_PELCO::ZOOM_STOP, m_PtzState );
|
|
// set reply size
|
|
pProtocolPTZ->SetResponseSize( 4 );
|
|
break;
|
|
|
|
case CMD_NONE:
|
|
default:
|
|
*pTimeout = 10L;
|
|
return FALSE;
|
|
}
|
|
|
|
// write packet
|
|
if( !pProtocolPTZ->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) )
|
|
return FALSE;
|
|
|
|
*pTimeout = 250L; // TX + RX
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
|
|
{
|
|
CProtocolPTZPelco *pProtocolPTZ = (CProtocolPTZPelco*)pProtocol;
|
|
|
|
// read reply
|
|
PBYTE pReadBuffer = NULL;
|
|
INT nReadCount = 0;
|
|
if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
|
|
{
|
|
if( !bLastChance )
|
|
return FALSE;
|
|
|
|
m_nNoReplyCount++;
|
|
if( m_nNoReplyCount > MAX_NO_REPLY_COUNT )
|
|
{
|
|
// perform goto idle state
|
|
m_nMode = CMD_INIT;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
// reset retry counter
|
|
m_nNoReplyCount = 0;
|
|
|
|
// switch mode
|
|
switch( m_nMode )
|
|
{
|
|
case CMD_INIT:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_INIT_TURN_ON: m_nStage = STAGE_INIT_AUTO_FOCUS; break;
|
|
case STAGE_INIT_AUTO_FOCUS: m_nStage = STAGE_INIT_AUTO_IRIS; break;
|
|
case STAGE_INIT_AUTO_IRIS: m_nStage = STAGE_INIT_AUTO_GAIN_CONTROL; break;
|
|
case STAGE_INIT_AUTO_GAIN_CONTROL: m_nStage = STAGE_INIT_BACKLIGHT_COMPENSATION; break;
|
|
case STAGE_INIT_BACKLIGHT_COMPENSATION: m_nStage = STAGE_INIT_WHITE_BALANCE; break;
|
|
case STAGE_INIT_WHITE_BALANCE:
|
|
OnPerformCmd();
|
|
OnEvtInit();
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case CMD_GET_PTZ:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_GET_PAN_POSITION:
|
|
m_Position.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
m_nStage = STAGE_GET_TILT_POSITION;
|
|
break;
|
|
|
|
case STAGE_GET_TILT_POSITION:
|
|
m_Position.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
m_nStage = STAGE_GET_ZOOM_POSITION;
|
|
break;
|
|
|
|
case STAGE_GET_ZOOM_POSITION:
|
|
m_Position.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
// ready to process new message
|
|
OnPerformCmd();
|
|
//notify
|
|
OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case CMD_SET_PTZ:
|
|
switch( m_nStage )
|
|
{
|
|
case STAGE_SET_PAN_POSITION: m_nStage = STAGE_SET_TILT_POSITION; break;
|
|
case STAGE_SET_TILT_POSITION: m_nStage = STAGE_SET_ZOOM_POSITION; break;
|
|
case STAGE_SET_ZOOM_POSITION: /*m_nStage = STAGE_GET_PAN_POSITION; break;
|
|
case STAGE_GET_PAN_POSITION:
|
|
m_TempPosition.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
m_nStage = STAGE_GET_TILT_POSITION;
|
|
break;
|
|
|
|
case STAGE_GET_TILT_POSITION:
|
|
m_TempPosition.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
m_nStage = STAGE_GET_ZOOM_POSITION;
|
|
break;
|
|
|
|
case STAGE_GET_ZOOM_POSITION:
|
|
m_TempPosition.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) );
|
|
|
|
// calculate difference between desired position and current position
|
|
if( QualifyPosition( m_TempPosition.dPan - m_Position.dPan,
|
|
m_TempPosition.dTilt - m_Position.dTilt,
|
|
m_TempPosition.dZoom - m_Position.dZoom,
|
|
POSITION_QUALYTY ) ||
|
|
m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
|
|
{
|
|
// store position
|
|
m_Position.dPan = m_TempPosition.dPan;
|
|
m_Position.dTilt = m_TempPosition.dTilt;
|
|
m_Position.dZoom = m_TempPosition.dZoom;
|
|
*/ // ready to process new message
|
|
OnPerformCmd();
|
|
//notify
|
|
OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
|
|
/* }
|
|
else
|
|
{
|
|
// does not in posiiton
|
|
m_nStage = STAGE_SET_PAN_POSITION;
|
|
// increate fault counter
|
|
m_nNotInPositionCount++;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
*/ }
|
|
break;
|
|
|
|
case CMD_MOVE_PAN:
|
|
OnPerformCmd();
|
|
OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
|
|
break;
|
|
|
|
case CMD_MOVE_TILT:
|
|
OnPerformCmd();
|
|
OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
|
|
break;
|
|
|
|
case CMD_MOVE_ZOOM:
|
|
OnPerformCmd();
|
|
OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
|
|
break;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|