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.
 
 
 
 
 

460 lines
12 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DMicPTZ.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DMicPTZ.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#ifdef __BORLANDC__
#include "..\LgLib\Version.h"
#include "..\LgLib\Device\DMicPTZ.h"
#else
#include "..\Version.h"
#include "DMicPTZ.h"
#endif
#define POSITION_QUALYTY 2
#define MAX_NO_REPLY_COUNT 20
#define MAX_NOT_IN_POSITION_COUNT 10
#define IDLE_DELAY 10L
#define COMMAND_DELAY 50L
#define WRITE_READ_DELAY 250L
CLgDevicePTZMic1300::CLgDevicePTZMic1300()
{
// GetAddress() = 0;
memset( &m_Status, 0, sizeof( m_Status ) );
m_nMode = CMD_NONE;
m_nStage = STAGE_GET_CONFIG_LOW_SPEED;
m_nNoReplyCount = 0;
m_nNotInPositionCount = 0;
m_bCommandPreDelay = TRUE;
}
CLgDevicePTZMic1300::~CLgDevicePTZMic1300()
{
}
BOOL CLgDevicePTZMic1300::CmdSetAddress( BYTE bAddress )
{
// check availibility
if( !DoCmd( CMD_SET_ADDRESS ) )
return FALSE;
// store command data
m_bNewAddress = bAddress;
return TRUE;
}
BOOL CLgDevicePTZMic1300::CmdInit()
{
// check availibility
return DoCmd( CMD_INIT );
}
void CLgDevicePTZMic1300::OnPerformCmd()
{
// ready for new message
ReleaseCmd();
// ready to process new message
m_nMode = CMD_NONE;
}
BOOL CLgDevicePTZMic1300::WriteProc( void *pProtocol, PLONG pTimeout )
{
BYTE nSendCount = 0;
// cast to protocol
CProtocolMic1300PTZ *pProtocolPTZ = (CProtocolMic1300PTZ*)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_GET_CONFIG_LOW_SPEED;
break;
case CMD_GET_PTZ:
m_nMode = CMD_GET_PTZ;
break;
case CMD_SET_PTZ:
m_nMode = CMD_SET_PTZ;
m_nStage = STAGE_GOTO_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_SET_IRIS:
m_nMode = CMD_SET_IRIS;
m_nStage = STAGE_SET_MANUAL_CONTROL;
break;
case CMD_WIPER:
m_nMode = CMD_WIPER;
break;
case CMD_SET_ADDRESS:
m_nMode = CMD_SET_ADDRESS;
m_nStage = STAGE_SET_ADDRESS1;
break;
case CMD_NONE:
// no new commands
break;
case CMD_SET_FOCUS:
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgDevicePTZMic1300::WriteProc"), _T("CMD_SET_FOCUS is not supported yet") );
default:
//command unsupported
OnPerformCmd();
OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
break;
}
// set initial pause
m_bCommandPreDelay = TRUE;
}
// fill packet depend on command
switch( m_nMode )
{
case CMD_INIT:
// set initial pause
if( m_bCommandPreDelay )
{
m_bCommandPreDelay = FALSE;
*pTimeout = COMMAND_DELAY;
return FALSE;
}
switch( m_nStage )
{
case STAGE_GET_CONFIG_LOW_SPEED:
// change baudrate to low speed
SetLowBaudrate( TRUE );
nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
break;
case STAGE_SET_CONFIG_LOW_SPEED:
// change baudrate to normal speed
SetLowBaudrate( TRUE );
nSendCount += PTZ_MIC_1_300::BuildSetConfig( m_pSendBuffer, GetAddress(), m_bConfig );
break;
case STAGE_GET_CONFIG_NORMAL_SPEED:
// change baudrate to low speed
SetLowBaudrate( FALSE );
nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
break;
case STAGE_SET_DEFAULT_SETTINGS:
// change baudrate to normal speed
SetLowBaudrate( FALSE );
nSendCount += PTZ_MIC_1_300::BuildSetDefaultSettings( m_pSendBuffer, GetAddress(), &m_Status );
break;
}
break;
case CMD_SET_ADDRESS:
// set initial pause
if( m_bCommandPreDelay )
{
m_bCommandPreDelay = FALSE;
*pTimeout = COMMAND_DELAY;
return FALSE;
}
switch( m_nStage )
{
case STAGE_SET_ADDRESS1:
// send adddress first time
nSendCount += PTZ_MIC_1_300::BuildSetAddress( m_pSendBuffer, GetAddress(), m_bNewAddress );
break;
case STAGE_SET_ADDRESS2:
// send address second time
nSendCount += PTZ_MIC_1_300::BuildSetAddress( m_pSendBuffer, GetAddress(), m_bNewAddress );
SetAddress(m_bNewAddress);
pProtocolPTZ->SetAddress( m_bNewAddress );
break;
case STAGE_GET_POSITION:
// check address settings
nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
break;
}
break;
case CMD_GET_PTZ:
nSendCount += PTZ_MIC_1_300::BuildGetPosition( m_pSendBuffer, GetAddress() );
break;
case CMD_SET_PTZ:
switch( m_nStage )
{
case STAGE_GOTO_POSITION:
nSendCount += PTZ_MIC_1_300::BuildGotoPosition( m_pSendBuffer, GetAddress(), PTZ_MIC_1_300::Degree2Pan( m_Position.dPan ), PTZ_MIC_1_300::Degree2Tilt( m_Position.dTilt ), PTZ_MIC_1_300::Percent2Zoom( m_Position.dZoom ) );
break;
case STAGE_GET_POSITION:
nSendCount += PTZ_MIC_1_300::BuildGetPosition( m_pSendBuffer, GetAddress() );
break;
}
break;
case CMD_MOVE_PAN:
nSendCount += PTZ_MIC_1_300::BuildPan( m_pSendBuffer, GetAddress(), m_MovePan.bRight, PTZ_MIC_1_300::Percent2Speed(m_MovePan.dSpeed), &m_Status );
break;
case CMD_MOVE_TILT:
nSendCount += PTZ_MIC_1_300::BuildTilt( m_pSendBuffer, GetAddress(), m_MoveTilt.bUp, PTZ_MIC_1_300::Percent2Speed(m_MoveTilt.dSpeed), &m_Status );
break;
case CMD_MOVE_ZOOM:
nSendCount += PTZ_MIC_1_300::BuildZoom( m_pSendBuffer, GetAddress(), m_MoveZoom.bTele, PTZ_MIC_1_300::Percent2Speed(m_MoveZoom.dSpeed), &m_Status );
break;
case CMD_SET_IRIS:
// set initial pause
if( m_bCommandPreDelay )
{
m_bCommandPreDelay = FALSE;
*pTimeout = COMMAND_DELAY;
return FALSE;
}
switch( m_nStage )
{
case STAGE_SET_MANUAL_CONTROL:
m_Status.bLampsOn = m_Iris > 70.0;
nSendCount += PTZ_MIC_1_300::BuildSetManualControl( m_pSendBuffer, GetAddress(), &m_Status );
break;
case STAGE_SET_SLOW_SHUTTER:
nSendCount += PTZ_MIC_1_300::BuildSetSlowShutter( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
break;
case STAGE_SET_BACK_LIGHT:
nSendCount += PTZ_MIC_1_300::BuildSetBackLight( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
break;
case STAGE_SET_LOW_LUX:
nSendCount += PTZ_MIC_1_300::BuildSetLowLux( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
break;
case STAGE_SET_IR_LED:
nSendCount += PTZ_MIC_1_300::BuildSetIRled( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
break;
}
break;
case CMD_WIPER:
// update wiper state
m_Status.bWiperOn = m_Wiper;
nSendCount += PTZ_MIC_1_300::BuildSetManualControl( m_pSendBuffer, GetAddress(), &m_Status );
break;
case CMD_NONE:
default:
*pTimeout = IDLE_DELAY;
return FALSE;
}
// write packet
if( !pProtocolPTZ->Write( m_pSendBuffer, nSendCount, 1000UL, TRUE ) )
return FALSE;
*pTimeout = WRITE_READ_DELAY; // TX + RX
return TRUE;
}
BOOL CLgDevicePTZMic1300::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
{
// cast to protocol
CProtocolMic1300PTZ *pProtocolPTZ = (CProtocolMic1300PTZ*)pProtocol;
// read reply
PBYTE pReadBuffer = NULL;
INT nReadCount = 0;
if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
{
if( !bLastChance )
return FALSE;
if( m_nMode == CMD_INIT )
{
switch( m_nStage )
{
case STAGE_GET_CONFIG_LOW_SPEED: m_nStage = STAGE_GET_CONFIG_NORMAL_SPEED; break;
case STAGE_SET_CONFIG_LOW_SPEED: m_nStage = STAGE_GET_CONFIG_NORMAL_SPEED; break;
case STAGE_GET_CONFIG_NORMAL_SPEED: m_nStage = STAGE_GET_CONFIG_LOW_SPEED; break;
case STAGE_SET_DEFAULT_SETTINGS: m_nStage = STAGE_GET_CONFIG_LOW_SPEED; break;
}
}
else
{
m_nNoReplyCount++;
if( m_nNoReplyCount > MAX_NO_REPLY_COUNT )
{
// perform reinitialization
m_nMode = CMD_INIT;
m_nStage = STAGE_GET_CONFIG_LOW_SPEED;
}
}
return FALSE;
}
// reset retry counter
m_nNoReplyCount = 0;
// check control flag
if( pReadBuffer[5] != 'E' &&
pReadBuffer[5] != 'C' )
return TRUE;
// check response result
switch( pReadBuffer[6] )
{
// normal response
case 'G': case 'H': case 'N': case 'O': case 'a': case 'q': case 'w': case 'r': case 's': case '0': break;
// error response
default:
return TRUE;
}
// again pause before new command
m_bCommandPreDelay = TRUE;
PTZ_DEGREE pan, tilt/*, dPan, dTilt*/;
PTZ_PERCENT zoom/*, dZoom*/;
// switch mode
switch( m_nMode )
{
case CMD_INIT:
switch( m_nStage )
{
case STAGE_GET_CONFIG_LOW_SPEED:
// toggle low speed comms
m_bConfig = 0x80;
m_nStage = STAGE_SET_CONFIG_LOW_SPEED;
return TRUE;
case STAGE_SET_CONFIG_LOW_SPEED:
case STAGE_GET_CONFIG_NORMAL_SPEED:
m_nStage = STAGE_SET_DEFAULT_SETTINGS;
return TRUE;
case STAGE_SET_DEFAULT_SETTINGS:
// ready to process new message
OnPerformCmd();
//notify
OnEvtInit();
break;
}
break;
case CMD_SET_ADDRESS:
switch( m_nStage )
{
case STAGE_SET_ADDRESS1:
m_nStage = STAGE_SET_ADDRESS2;
return TRUE;
case STAGE_SET_ADDRESS2:
m_nStage = STAGE_GET_POSITION;
return TRUE;
case STAGE_GET_POSITION:
// switch address
SetAddress( m_bNewAddress );
// ready to process new message
OnPerformCmd();
//notify
OnEvtSetAddress( GetAddress() );
break;
}
break;
case CMD_GET_PTZ:
// store position
m_Position.dPan = PTZ_MIC_1_300::Pan2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 13, 3 ) );
m_Position.dTilt = PTZ_MIC_1_300::Tilt2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 10, 3 ) );
m_Position.dZoom = PTZ_MIC_1_300::Zoom2Percent( PTZ_MIC_1_300::ToValue( pReadBuffer + 16, 3 ) );
// ready to process new message
OnPerformCmd();
//notify
OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
break;
case CMD_SET_PTZ:
switch( m_nStage )
{
case STAGE_GOTO_POSITION:
// next step is check position
m_nStage = STAGE_GET_POSITION;
return TRUE;
case STAGE_GET_POSITION:
// calculate difference between desired position and current position
pan = PTZ_MIC_1_300::Pan2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 13, 3 ) );
tilt = PTZ_MIC_1_300::Tilt2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 10, 3 ) );
zoom = PTZ_MIC_1_300::Zoom2Percent( PTZ_MIC_1_300::ToValue( pReadBuffer + 16, 3 ) );
if( QualifyPosition( pan - m_Position.dPan,
tilt - m_Position.dTilt,
zoom - m_Position.dZoom,
POSITION_QUALYTY ) ||
m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
{
m_Position.dPan = pan;
m_Position.dTilt = tilt;
m_Position.dZoom = zoom;
// 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_GOTO_POSITION;
// 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_SET_IRIS:
switch( m_nStage )
{
case STAGE_SET_MANUAL_CONTROL: m_nStage = STAGE_SET_SLOW_SHUTTER; return TRUE;
case STAGE_SET_SLOW_SHUTTER: m_nStage = STAGE_SET_BACK_LIGHT; return TRUE;
case STAGE_SET_BACK_LIGHT: m_nStage = STAGE_SET_LOW_LUX; return TRUE;
case STAGE_SET_LOW_LUX: m_nStage = STAGE_SET_IR_LED; return TRUE;
case STAGE_SET_IR_LED: break;
}
// ready to process new message
OnPerformCmd();
//notify
OnEvtSetIris( m_Iris );
break;
case CMD_WIPER:
// ready to process new message
OnPerformCmd();
//notify
OnEvtWiper( m_Status.bWiperOn );
break;
}
return TRUE;
}