|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DTKS576.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DTKS576.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#ifdef __BORLANDC__
#include "..\LgLib\Device\DTKS576.h"
#else
#include "DTKS576.h"
#endif
#define POSITION_QUALYTY 2
#define MAX_NO_REPLY_COUNT 20
#define MAX_NOT_IN_POSITION_COUNT 1
#define DELAY_BETWEEN_COMMANDS 150
#define ONLY_WAIT_AFTER_MOVEMENT 250
CLgDevicePTZTKS576::CLgDevicePTZTKS576(){ m_nMode = CMD_NONE; m_nStage = STAGE_GET_PAN; m_nNoReplyCount = 0; m_nNotInPositionCount = 0; m_bReadOnlyHeader = TRUE;}
CLgDevicePTZTKS576::~CLgDevicePTZTKS576(){
}
void CLgDevicePTZTKS576::OnPerformCmd(){ // ready for new message
ReleaseCmd(); // ready to process new message
m_nMode = CMD_NONE; }
BOOL CLgDevicePTZTKS576::CmdInit(){ // check availibility
return DoCmd( CMD_INIT );}
BOOL CLgDevicePTZTKS576::WriteProc( void *pProtocol, PLONG pTimeout ){ BYTE pSendBuffer[ CProtocolPTZTKS576::MAX_COMMAND_SIZE ]; BYTE nSendCount = 0;
// cast to protocol
CProtocolPTZTKS576 *pProtocolPTZ = (CProtocolPTZTKS576*)pProtocol;
// set working address
pProtocolPTZ->SetAddress( GetAddress() );
// check input command
if( m_nMode == CMD_NONE ) { switch( GetCmd() ) { case CMD_INIT: m_nMode = CMD_INIT; m_nStage = STAGE_SET_AUTO_FOCUS; break; case CMD_GET_PTZ: m_nMode = CMD_GET_PTZ; m_nStage = STAGE_GET_PAN; break; case CMD_SET_PTZ: m_nMode = CMD_SET_PTZ; m_nStage = STAGE_SET_PAN; m_nNotInPositionCount = 0; break; case CMD_MOVE_TILT: m_nMode = CMD_MOVE_TILT; m_nStage = STAGE_TILT; break; case CMD_MOVE_PAN: m_nMode = CMD_MOVE_PAN; m_nStage = STAGE_PAN; break; case CMD_MOVE_ZOOM: m_nMode = CMD_MOVE_ZOOM; m_nStage = STAGE_ZOOM; break; case CMD_MOVE_FOCUS: m_nMode = CMD_MOVE_FOCUS; m_nStage = STAGE_FOCUS; break; case CMD_SET_FOCUS: m_nMode = CMD_SET_FOCUS; m_nStage = STAGE_FOCUS; break; case CMD_GET_FOCUS: m_nMode = CMD_GET_FOCUS; m_nStage = STAGE_FOCUS; break; case CMD_NONE: // no new commands
break; case CMD_WIPER: case CMD_SET_IRIS: default: //command unsupported
OnPerformCmd(); OnEvtCommandError( m_nMode, _T("UNSUPPORTED") ); break; } }
#ifdef ONLY_WAIT_AFTER_MOVEMENT
BOOL bOnlyWait = FALSE;#endif
// fill packet depend on command
switch( m_nMode ) { case CMD_SET_PTZ: switch( m_nStage ) { case STAGE_SET_PAN: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildSetPanPosition( pSendBuffer, GetAddress(), PTZ_JVC_TK_S576::Degree2Pan( m_Position.dPan ) ); // short response
m_bReadOnlyHeader = TRUE; break; case STAGE_SET_TILT: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildSetTiltPosition( pSendBuffer, GetAddress(), PTZ_JVC_TK_S576::Degree2Tilt( m_Position.dTilt ) ); // short response
m_bReadOnlyHeader = TRUE; break; case STAGE_SET_ZOOM: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildSetZoomPosition( pSendBuffer, GetAddress(), PTZ_JVC_TK_S576::Percent2Zoom( m_Position.dZoom ) ); // short response
m_bReadOnlyHeader = TRUE; break; case STAGE_SET_PAN_DELAY: m_nStage = STAGE_SET_TILT; *pTimeout = DELAY_BETWEEN_COMMANDS; return FALSE; case STAGE_SET_TILT_DELAY: m_nStage = STAGE_SET_ZOOM; *pTimeout = DELAY_BETWEEN_COMMANDS; return FALSE; case STAGE_SET_ZOOM_DELAY: m_nStage = STAGE_GET_PAN; *pTimeout = DELAY_BETWEEN_COMMANDS;// * 6;
return FALSE; case STAGE_GET_PAN: // ready to process new message
OnPerformCmd(); //notify
OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); // default delay
*pTimeout = 10; return FALSE; } break;
case CMD_INIT: switch( m_nStage ) { case STAGE_SET_AUTO_FOCUS: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildSetAutoFocus( pSendBuffer, GetAddress() ); // long response
m_bReadOnlyHeader = TRUE; break; } // next steps like in "get position"
case CMD_GET_PTZ: switch( m_nStage ) { case STAGE_GET_PAN: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildGetPanPosition( pSendBuffer, GetAddress() ); // long response
m_bReadOnlyHeader = FALSE; break; case STAGE_GET_TILT: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildGetTiltPosition( pSendBuffer, GetAddress() ); // long response
m_bReadOnlyHeader = FALSE; break; case STAGE_GET_ZOOM: // ask for pan position
nSendCount += PTZ_JVC_TK_S576::BuildGetZoomPosition( pSendBuffer, GetAddress() ); // long response
m_bReadOnlyHeader = FALSE; break; case STAGE_GET_PAN_DELAY: m_nStage = STAGE_GET_TILT; *pTimeout = DELAY_BETWEEN_COMMANDS; return FALSE; case STAGE_GET_TILT_DELAY: m_nStage = STAGE_GET_ZOOM; *pTimeout = DELAY_BETWEEN_COMMANDS; return FALSE; } break;
case CMD_MOVE_PAN:#ifdef ONLY_WAIT_AFTER_MOVEMENT
switch( m_nStage ) { case STAGE_PAN: // write command
nSendCount += PTZ_JVC_TK_S576::BuildPan( pSendBuffer, GetAddress(), m_MovePan.dSpeed > 0 ? ( m_MovePan.bRight ? PTZ_JVC_TK_S576::PAN_RIGHT : PTZ_JVC_TK_S576::PAN_LEFT ) : PTZ_JVC_TK_S576::PAN_STOP, PTZ_JVC_TK_S576::Percent2PanSpeed( m_MovePan.dSpeed ) ); // set next stage
m_nStage = STAGE_PAN_DELAY; // don't read reply
bOnlyWait = TRUE; break; case STAGE_PAN_DELAY: // ready to process new message
OnPerformCmd(); //notify
OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); // no timeout
*pTimeout = 0; return FALSE; }#else
// write command
nSendCount += PTZ_JVC_TK_S576::BuildPan( pSendBuffer, GetAddress(), m_MovePan.dSpeed > 0 ? ( m_MovePan.bRight ? PTZ_JVC_TK_S576::PAN_RIGHT : PTZ_JVC_TK_S576::PAN_LEFT ) : PTZ_JVC_TK_S576::PAN_STOP, PTZ_JVC_TK_S576::Percent2PanSpeed( m_MovePan.dSpeed ) ); // short response
m_bReadOnlyHeader = TRUE;#endif
break; case CMD_MOVE_TILT: #ifdef ONLY_WAIT_AFTER_MOVEMENT
switch( m_nStage ) { case STAGE_TILT: // write command
nSendCount += PTZ_JVC_TK_S576::BuildTilt( pSendBuffer, GetAddress(), m_MoveTilt.dSpeed > 0 ? ( m_MoveTilt.bUp ? PTZ_JVC_TK_S576::TILT_UP : PTZ_JVC_TK_S576::TILT_DOWN ) : PTZ_JVC_TK_S576::TILT_STOP, PTZ_JVC_TK_S576::Percent2TiltSpeed( m_MoveTilt.dSpeed ) ); // set next stage
m_nStage = STAGE_TILT_DELAY; // don't read reply
bOnlyWait = TRUE; break; case STAGE_TILT_DELAY: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); // no timeout
*pTimeout = 0; return FALSE; }#else
// write command
nSendCount += PTZ_JVC_TK_S576::BuildTilt( pSendBuffer, GetAddress(), m_MoveTilt.dSpeed > 0 ? ( m_MoveTilt.bUp ? PTZ_JVC_TK_S576::TILT_UP : PTZ_JVC_TK_S576::TILT_DOWN ) : PTZ_JVC_TK_S576::TILT_STOP, PTZ_JVC_TK_S576::Percent2TiltSpeed( m_MoveTilt.dSpeed ) ); // short response
m_bReadOnlyHeader = TRUE;#endif
break; case CMD_MOVE_ZOOM:#ifdef ONLY_WAIT_AFTER_MOVEMENT
switch( m_nStage ) { case STAGE_ZOOM: // write command
nSendCount += PTZ_JVC_TK_S576::BuildZoom( pSendBuffer, GetAddress(), m_MoveZoom.dSpeed > 0 ? ( m_MoveZoom.bTele ? PTZ_JVC_TK_S576::ZOOM_TELE : PTZ_JVC_TK_S576::ZOOM_WIDE ) : PTZ_JVC_TK_S576::ZOOM_STOP, PTZ_JVC_TK_S576::Percent2ZoomSpeed( m_MoveZoom.dSpeed ) ); // set next stage
m_nStage = STAGE_ZOOM_DELAY; // don't read reply
bOnlyWait = TRUE; break; case STAGE_ZOOM_DELAY: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); // no timeout
*pTimeout = 0; return FALSE; }#else
// write command
nSendCount += PTZ_JVC_TK_S576::BuildZoom( pSendBuffer, GetAddress(), m_MoveZoom.dSpeed > 0 ? ( m_MoveZoom.bTele ? PTZ_JVC_TK_S576::ZOOM_TELE : PTZ_JVC_TK_S576::ZOOM_WIDE ) : PTZ_JVC_TK_S576::ZOOM_STOP, PTZ_JVC_TK_S576::Percent2ZoomSpeed( m_MoveZoom.dSpeed ) ); // short response
m_bReadOnlyHeader = TRUE;#endif
break; case CMD_MOVE_FOCUS:#ifdef ONLY_WAIT_AFTER_MOVEMENT
switch( m_nStage ) { case STAGE_FOCUS: // write command
nSendCount += PTZ_JVC_TK_S576::BuildMoveFocus( pSendBuffer, GetAddress(), m_MoveFocus.dSpeed > 0 ? ( m_MoveFocus.bFar ? PTZ_JVC_TK_S576::FOCUS_FAR : PTZ_JVC_TK_S576::FOCUS_NEAR ) : PTZ_JVC_TK_S576::FOCUS_STOP, PTZ_JVC_TK_S576::Percent2FocusSpeed( m_MoveFocus.dSpeed ) ); // set next stage
m_nStage = STAGE_FOCUS_DELAY; // don't read reply
bOnlyWait = TRUE; break; case STAGE_FOCUS_DELAY: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveFocus( m_MoveFocus.bFar, m_MoveFocus.dSpeed ); // no timeout
*pTimeout = 0; return FALSE; }#else
// write command
nSendCount += PTZ_JVC_TK_S576::BuildMoveFocus( pSendBuffer, GetAddress(), m_MoveFocus.dSpeed > 0 ? ( m_MoveFocus.bFar ? PTZ_JVC_TK_S576::FOCUS_FAR : PTZ_JVC_TK_S576::FOCUS_NEAR ) : PTZ_JVC_TK_S576::FOCUS_STOP, PTZ_JVC_TK_S576::Percent2FocusSpeed( m_MoveFocus.dSpeed ) ); // short response
m_bReadOnlyHeader = TRUE;#endif
break; case CMD_SET_FOCUS:#ifdef ONLY_WAIT_AFTER_MOVEMENT
switch( m_nStage ) { case STAGE_FOCUS: // write data
nSendCount += PTZ_JVC_TK_S576::BuildSetFocusPosition( pSendBuffer, GetAddress(), PTZ_JVC_TK_S576::Percent2Focus( m_Focus ) ); // set next stage
m_nStage = STAGE_FOCUS_DELAY; // don't read reply
bOnlyWait = TRUE; break; case STAGE_FOCUS_DELAY: // ready to process new message
OnPerformCmd(); //notify
OnEvtSetFocus( m_Focus ); // no timeout
*pTimeout = 0; return FALSE; }#else
// write data
nSendCount += PTZ_JVC_TK_S576::BuildSetFocusPosition( pSendBuffer, GetAddress(), PTZ_JVC_TK_S576::Percent2Focus( m_Focus ) ); // short response
m_bReadOnlyHeader = TRUE;#endif
break;
case CMD_GET_FOCUS: // write command
nSendCount += PTZ_JVC_TK_S576::BuildGetFocusPosition( pSendBuffer, GetAddress() ); // short response
m_bReadOnlyHeader = FALSE; break;
case CMD_NONE: default: *pTimeout = 10L; return FALSE; } // write packet
if( !pProtocolPTZ->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) ) return FALSE;
#ifdef ONLY_WAIT_AFTER_MOVEMENT
if( bOnlyWait ) // only wait
*pTimeout = ONLY_WAIT_AFTER_MOVEMENT; else#endif
*pTimeout = m_bReadOnlyHeader ? 250L : 1000L; // TX + RX
return TRUE;}
BOOL CLgDevicePTZTKS576::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance ){ CProtocolPTZTKS576 *pProtocolPTZ = (CProtocolPTZTKS576*) pProtocol;
// set protocol read mode
pProtocolPTZ->SetReadOnlyHeaderMode( m_bReadOnlyHeader );
// 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 reinitialization
m_nMode = CMD_INIT; m_nStage = STAGE_SET_AUTO_FOCUS; }
return FALSE; }
// reset retry counter
m_nNoReplyCount = 0;
// switch mode
switch( m_nMode ) { case CMD_INIT: switch( m_nStage ) { case STAGE_SET_AUTO_FOCUS: // next stage
m_nStage = STAGE_GET_PAN; return TRUE; } // next steps like in get position
case CMD_GET_PTZ: switch( m_nStage ) { case STAGE_GET_PAN: // store position
m_Position.dPan = PTZ_JVC_TK_S576::Pan2Degree( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // next stage
m_nStage = STAGE_GET_PAN_DELAY; return TRUE; case STAGE_GET_TILT: // store position
m_Position.dTilt = PTZ_JVC_TK_S576::Tilt2Degree( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // next stage
m_nStage = STAGE_GET_TILT_DELAY; return TRUE; case STAGE_GET_ZOOM: // store position
m_Position.dZoom = PTZ_JVC_TK_S576::Zoom2Percent( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // ready to process new message
OnPerformCmd(); //notify
if( m_nMode == CMD_INIT ) OnEvtInit(); else OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); break; } break; case CMD_SET_PTZ: switch( m_nStage ) { case STAGE_SET_PAN: m_nStage = STAGE_SET_PAN_DELAY; return TRUE; case STAGE_SET_TILT: m_nStage = STAGE_SET_TILT_DELAY; return TRUE; case STAGE_SET_ZOOM: m_nStage = STAGE_SET_ZOOM_DELAY; return TRUE;/*
case STAGE_GET_PAN: // store position
m_TempPosition.dPan = PTZ_JVC_TK_S576::Pan2Degree( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // next stage
m_nStage = STAGE_GET_PAN_DELAY; return TRUE; case STAGE_GET_TILT: // store position
m_TempPosition.dTilt = PTZ_JVC_TK_S576::Tilt2Degree( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // next stage
m_nStage = STAGE_GET_TILT_DELAY; return TRUE; case STAGE_GET_ZOOM: // store position
m_TempPosition.dZoom = PTZ_JVC_TK_S576::Zoom2Percent( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // calculate difference between desired position and current position
if( PTZ_JVC_TK_S576::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
OnEvtGotoPosition( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); } else { // does not in posiiton
m_nStage = STAGE_SET_PAN; // increate fault counter
m_nNotInPositionCount++; return TRUE; } break;*/ } break; case CMD_MOVE_PAN: // ready to process new message
OnPerformCmd(); //notify
OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break; case CMD_MOVE_TILT: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break; case CMD_MOVE_ZOOM: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break; case CMD_MOVE_FOCUS: // ready to process new message
OnPerformCmd(); //notify
OnEvtMoveFocus( m_MoveFocus.bFar, m_MoveFocus.dSpeed ); break; case CMD_SET_FOCUS: // ready to process new message
OnPerformCmd(); //notify
OnEvtSetFocus( m_Focus ); break; case CMD_GET_FOCUS: // store position
m_Focus = PTZ_JVC_TK_S576::Focus2Percent( PTZ_JVC_TK_S576::ToValue( pReadBuffer[1], pReadBuffer[4], pReadBuffer[5] ) ); // ready to process new message
OnPerformCmd(); //notify
OnEvtGetFocus( m_Focus ); break; }
return TRUE;}
|