diff --git a/LgLib/Comm/PTZPelco.h b/LgLib/Comm/PTZPelco.h index a9a81f3..9410d7e 100644 --- a/LgLib/Comm/PTZPelco.h +++ b/LgLib/Comm/PTZPelco.h @@ -123,12 +123,11 @@ typedef struct PTZ_PELCO static INT BuildSetZoom( LPBYTE pBuffer, BYTE bAddress, WORD wZoom ) { pBuffer[ 0] = 0xFF; // data byte 1 - pBuffer[ 1] = bAddress; // data byte 2 + pBuffer[ 1] = bAddress; // data byte 2 pBuffer[ 2] = 0x00; // data byte 3 pBuffer[ 3] = 0x4F; // data byte 4 - pBuffer[ 4] = ( wZoom >> 8 ) & 0xFF; // data byte 5 - pBuffer[ 5] = ( wZoom >> 0 ) & 0xFF; // data byte 6 - printf(" Camera SetZoom Package Sent: %d\n", wZoom ); + pBuffer[ 4] = ( wZoom >> 8 ) & 0xFF; // data byte 5 + pBuffer[ 5] = ( wZoom >> 0 ) & 0xFF; // data byte 6 return CheckSum( pBuffer ); // data byte 9 } @@ -181,7 +180,6 @@ typedef struct PTZ_PELCO { PtzState.eZoomDir = eZoomDir; return BuildMove( pBuffer, bAddress, PtzState ); - printf("Camera BuildZoom !\n"); } static INT BuildMove( LPBYTE pBuffer, BYTE bAddress, PTZ_STATE& PtzState ) @@ -289,21 +287,21 @@ typedef struct PTZ_PELCO public: // pan static CLgDevicePTZ::PTZ_DEGREE Pan2Degree( WORD wPan ) - { + { return 360 - wPan * 360L / 35999L; - } + } static WORD Degree2Pan( CLgDevicePTZ::PTZ_DEGREE Angle ) - { + { return (WORD)( (360 - Angle) * 35999L / 360L ); } // tilt static CLgDevicePTZ::PTZ_DEGREE Tilt2Degree( WORD wTilt ) - { + { return wTilt * 360L / 35999L; - } + } static WORD Degree2Tilt( CLgDevicePTZ::PTZ_DEGREE dAngle ) - { + { return (WORD)( dAngle * 35999L / 360L ); } @@ -320,13 +318,15 @@ public: #define ZOOM_LIMIT_MIN 0x0164L // zoom - static WORD Percent2Zoom( CLgDevicePTZ::PTZ_PERCENT dPercent ) + static WORD Percent2Zoom( CLgDevicePTZ::PTZ_PERCENT dPercent , int MaxZoom ) { - float ZMIN = 0x0164L; - float ZMAX = 0x2C85L; - float ZLIM = ZMAX - ZMIN; - long AA = (long)((float)dPercent * ZLIM / 100.00 + ZMIN ); - return (WORD) AA; + int ZMIN = 0; + int ZMAX = MaxZoom; + int ZLIM = ZMAX - ZMIN; + WORD PERC = ZLIM / 100; + WORD AA = (dPercent * PERC) & 0xFFFF; + return AA; + //return 0x0010; } static CLgDevicePTZ::PTZ_PERCENT Zoom2Percent( WORD wZoom ) { diff --git a/LgLib/Device/DPelco.cpp b/LgLib/Device/DPelco.cpp index d72b6aa..5176e89 100644 --- a/LgLib/Device/DPelco.cpp +++ b/LgLib/Device/DPelco.cpp @@ -3,8 +3,8 @@ // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 -// PROJECT: -// SUBSYSTEM: +// PROJECT: +// SUBSYSTEM: // FILE: DPelco.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW @@ -24,6 +24,66 @@ #define MAX_NOT_IN_POSITION_COUNT 1 #define DELAY_BEFORE_GET_POSITION 500 + + +typedef enum PELCO_SETTING_TYPE +{ + P_ST_NONE = -1, + P_ST_MAX_NO_REPLY_COUNT = 0, + P_ST_DELAY_BEFORE_GET_POSITION, + P_ST_MAX_OPTICAL_ZOOM +}PELCO_SETTING_TYPE; + +LPCTSTR m_pPelco_Setting[] = +{ + { "MAX_NO_REPLY_COUNT" }, + { "DELAY_BEFORE_GET_POSITION" }, + ( "MAX_OPTICAL_ZOOM" ) +}; + +BOOL CLgDevicePTZPelco::LoadUserSettings( void ) { + // read delay number + FILE *pFile = fopen( "PELCO.INI", "rt" ); + if( !pFile ) return FALSE; + + CHAR pLine[MAX_PATH]; + while( fgets( pLine, MAX_PATH, pFile ) ) + { + PELCO_SETTING_TYPE eType = P_ST_NONE; + PCHAR pTemp = NULL; + for( INT i = 0; i < 3; i++ ) + { + pTemp = strstr( pLine, m_pPelco_Setting[i] ); + if( pTemp ) + { + eType = (PELCO_SETTING_TYPE)i; + pTemp = strstr( pTemp, "=" ); + if( pTemp ) + pTemp = strtok( pTemp + 1, " \t\n\r" ); + break; + } + } + + if( eType == P_ST_NONE || !pTemp ) + continue; + + switch( eType ) + { + case P_ST_MAX_NO_REPLY_COUNT: { + PELCO_MAX_NO_REPLY_COUNT = atoi( pTemp ); break; + }; + case P_ST_MAX_OPTICAL_ZOOM: { + PELCO_MAX_OPTICAL_ZOOM = atoi( pTemp ); break; + }; + } + } + + fclose( pFile ); + return TRUE; +} + + + CLgDevicePTZPelco::CLgDevicePTZPelco() { m_nMode = CMD_INIT; @@ -32,6 +92,10 @@ CLgDevicePTZPelco::CLgDevicePTZPelco() m_nNotInPositionCount = 0; memset( &m_PtzState, 0, sizeof(m_PtzState) ); m_bPause = FALSE; + PELCO_MAX_NO_REPLY_COUNT = 20; // Default for Replaying Camera + PELCO_DELAY_BEFORE_GET_POSITION = 500; // Default for Replaying Camera + PELCO_MAX_OPTICAL_ZOOM = 0x2C85; + LoadUserSettings(); } CLgDevicePTZPelco::~CLgDevicePTZPelco() @@ -50,7 +114,7 @@ void CLgDevicePTZPelco::OnPerformCmd() // ready for new message ReleaseCmd(); // ready to process new message - m_nMode = CMD_NONE; + m_nMode = CMD_NONE; } BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout ) @@ -128,23 +192,23 @@ BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout ) pProtocolPTZ->SetResponseSize( 4 ); break; case STAGE_INIT_AUTO_FOCUS: - nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), 1 ); + nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), 0 ); // 0 - On , 1 - Off pProtocolPTZ->SetResponseSize( 4 ); break; case STAGE_INIT_AUTO_IRIS: - nSendCount += PTZ_PELCO::BuildAutoIris( pSendBuffer, GetAddress(), 1 ); + nSendCount += PTZ_PELCO::BuildAutoIris( pSendBuffer, GetAddress(), 0 ); pProtocolPTZ->SetResponseSize( 4 ); break; case STAGE_INIT_AUTO_GAIN_CONTROL: - nSendCount += PTZ_PELCO::BuildAutoGainControl( pSendBuffer, GetAddress(), 1 ); + nSendCount += PTZ_PELCO::BuildAutoGainControl( pSendBuffer, GetAddress(), 0 ); pProtocolPTZ->SetResponseSize( 4 ); break; case STAGE_INIT_BACKLIGHT_COMPENSATION: - nSendCount += PTZ_PELCO::BuildBacklightCompensation( pSendBuffer, GetAddress(), 1 ); + nSendCount += PTZ_PELCO::BuildBacklightCompensation( pSendBuffer, GetAddress(), 0 ); pProtocolPTZ->SetResponseSize( 4 ); break; case STAGE_INIT_WHITE_BALANCE: - nSendCount += PTZ_PELCO::BuildWhiteBalance( pSendBuffer, GetAddress(), 1 ); + nSendCount += PTZ_PELCO::BuildWhiteBalance( pSendBuffer, GetAddress(), 2 ); // 0 - On, 1 - Off, 2 - Auto pProtocolPTZ->SetResponseSize( 4 ); break; } @@ -154,7 +218,7 @@ BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout ) { case STAGE_SET_PAN_POSITION: // ask for pan position - nSendCount += PTZ_PELCO::BuildSetPan( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Pan( m_Position.dPan ) ); + nSendCount += PTZ_PELCO::BuildSetPan( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Pan( m_Position.dPan ) ); // set reply size pProtocolPTZ->SetResponseSize( 4 ); break; @@ -166,7 +230,7 @@ BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout ) break; case STAGE_SET_ZOOM_POSITION: // ask for pan position - nSendCount += PTZ_PELCO::BuildSetZoom( pSendBuffer, GetAddress(), PTZ_PELCO::Percent2Zoom( m_Position.dZoom ) ); + nSendCount += PTZ_PELCO::BuildSetZoom( pSendBuffer, GetAddress(), PTZ_PELCO::Percent2Zoom( m_Position.dZoom, PELCO_MAX_OPTICAL_ZOOM ) ); // set reply size pProtocolPTZ->SetResponseSize( 4 ); break; @@ -196,7 +260,7 @@ BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout ) { case STAGE_GET_PAN_POSITION: // ask for pan position - nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() ); + nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() ); // set reply size pProtocolPTZ->SetResponseSize( 7 ); break; @@ -261,17 +325,20 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan // read reply PBYTE pReadBuffer = NULL; - INT nReadCount = 0; + INT nReadCount = 0; + + //if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount ) if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount ) { if( !bLastChance ) return FALSE; m_nNoReplyCount++; - if( m_nNoReplyCount > MAX_NO_REPLY_COUNT ) + // if( m_nNoReplyCount > MAX_NO_REPLY_COUNT || ( m_nMode == CMD_GET_PTZ && m_nNoReplyCount > MAX_NOT_IN_POSITION_COUNT ) ) // v_1 + if( m_nNoReplyCount > PELCO_MAX_NO_REPLY_COUNT ) // v_2 { // perform goto idle state - // m_nMode = CMD_INIT; // Comented for BOSH/Pelco + // m_nMode = CMD_INIT; // Commented for BOSH/Pelco } else // else - Bosh Pelco return FALSE; @@ -293,7 +360,7 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan case STAGE_INIT_BACKLIGHT_COMPENSATION: m_nStage = STAGE_INIT_WHITE_BALANCE; break; case STAGE_INIT_WHITE_BALANCE: OnPerformCmd(); - OnEvtInit(); + OnEvtInit(); break; } break; @@ -301,17 +368,17 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan case CMD_GET_PTZ: switch( m_nStage ) { - case STAGE_GET_PAN_POSITION: + 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: + 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: + case STAGE_GET_ZOOM_POSITION: m_Position.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) ); // ready to process new message OnPerformCmd(); @@ -327,17 +394,17 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan 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: + 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: + 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: + 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 @@ -352,7 +419,7 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan m_Position.dTilt = m_TempPosition.dTilt; m_Position.dZoom = m_TempPosition.dZoom; */ // ready to process new message - OnPerformCmd(); + OnPerformCmd(); //notify OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); /* } @@ -370,17 +437,17 @@ BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChan case CMD_MOVE_PAN: OnPerformCmd(); - OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); + OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break; case CMD_MOVE_TILT: OnPerformCmd(); - OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); + OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break; case CMD_MOVE_ZOOM: OnPerformCmd(); - OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); + OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break; } diff --git a/LgLib/Device/DPelco.h b/LgLib/Device/DPelco.h index 09b1bff..292514c 100644 --- a/LgLib/Device/DPelco.h +++ b/LgLib/Device/DPelco.h @@ -62,6 +62,10 @@ private: STAGE_SET_IRIS, }; + BOOL LoadUserSettings( void ); + int PELCO_MAX_NO_REPLY_COUNT; + int PELCO_MAX_OPTICAL_ZOOM; + int PELCO_DELAY_BEFORE_GET_POSITION; public: enum diff --git a/lgs/Comm485.cpp b/lgs/Comm485.cpp index 7abe1af..ea515f2 100644 --- a/lgs/Comm485.cpp +++ b/lgs/Comm485.cpp @@ -402,10 +402,18 @@ DWORD CComm485::Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ) { // unused parameter dwTimeout; - - // write bytes to transmit stream - INT nWritten = m_OutputStream.Put( pBuffer, dwSize ); - + INT nWritten = 0; + if ( dwSize > 0 ) { + // write bytes to transmit stream + // Sending 0xFF after Data + pBuffer[dwSize]=0xFF; + dwSize++; + // Remove previews FF from queue + m_OutputStream.RemoveLast(); + // write bytes to transmit stream + nWritten = m_OutputStream.Put( pBuffer, dwSize ); + nWritten -- ; // decrease because +FF + }; #ifdef DEBUG_SHOW_COMMUNICATION if( m_bShowCommunication ) { diff --git a/lgs/Comm485.h b/lgs/Comm485.h index f3ece56..c31d31c 100644 --- a/lgs/Comm485.h +++ b/lgs/Comm485.h @@ -185,7 +185,7 @@ private: CFastStream m_OutputStream; LONG m_nBaudRate; INT m_nBitCount; - PARITY m_eParity; + PARITY m_eParity; private: static const BYTE c_acPort[]; diff --git a/lgs/LGS.IDE b/lgs/LGS.IDE index f4245e7..4fdbd32 100644 Binary files a/lgs/LGS.IDE and b/lgs/LGS.IDE differ diff --git a/lgs/Stdafx.h b/lgs/Stdafx.h index a529adc..7ce85f3 100644 --- a/lgs/Stdafx.h +++ b/lgs/Stdafx.h @@ -20,7 +20,7 @@ #endif #define BOARD_A2D_FOUR_SAMPLES -//#define LGS_MIRROR 4 +#define LGS_MIRROR 4 //#define DEBUG_ENABLE_OLD_BOARD #define DEBUG_ENABLE_SEND_BOARDDATA diff --git a/lgs/Stream.h b/lgs/Stream.h index 898d899..03f0a51 100644 --- a/lgs/Stream.h +++ b/lgs/Stream.h @@ -197,6 +197,20 @@ INT CStream::Get( TYPE *pByte, INT nCount ) return i; } +template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW > +INT CStream::RemoveLast( INT nCount ) +{ + for( INT i = 0; (i < nCount) && m_nCount; i++ ) + { + + m_nHead = ( m_nHead - 1 ) % MAX_CAPACITY; + m_nCount --; + } + + return i; +} + + template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW > void CStream::Clear() { @@ -230,9 +244,11 @@ public: public: virtual BOOL Put( TYPE byte ); + BOOL RemoveLast( void ); BOOL Get( TYPE *pByte ); virtual INT Put( TYPE *pByte, INT nCount ); + INT RemoveLast( INT nCount ); INT Get( TYPE *pByte, INT nCount ); INT GetCount() { return m_nCount; } @@ -314,6 +330,18 @@ BOOL CFastStream::Get( TYPE *pByte ) return TRUE; } +template< class TYPE, const INT MAX_CAPACITY > +BOOL CFastStream::RemoveLast( void ) +{ + const INT cMaxCapacity = (1< 0 ){ + m_nHead = ( m_nHead - 1 ) & cCapacityMask; + m_nCount --; + } + return TRUE; +} template< class TYPE, const INT MAX_CAPACITY > INT CFastStream::Put( TYPE *pByte, INT nCount ) @@ -347,6 +375,22 @@ INT CFastStream::Get( TYPE *pByte, INT nCount ) return i; } +template< class TYPE, const INT MAX_CAPACITY > +INT CFastStream::RemoveLast( INT nCount ) +{ + const INT cMaxCapacity = (1< void CFastStream::Clear() { diff --git a/lgs/kernel_.cpp b/lgs/kernel_.cpp index 37006a8..4817a4f 100644 --- a/lgs/kernel_.cpp +++ b/lgs/kernel_.cpp @@ -268,6 +268,7 @@ BOOL CKernel::Create( CBoard *pBoard, CComm485 *pCommPort, CShadow *pShadow, CCo // printf( "calibration 2 created...\n" ); m_pBoard->Reset(); +/* // prepare software version #ifdef LGS_MIRROR s_nSoftwareVersion = sprintf( s_pSoftwareVersion, "%s %dM,%s,%d", CLgVersion::GetBuildTimeStr(), LGS_MIRROR, @@ -346,7 +347,7 @@ BOOL CKernel::Create( CBoard *pBoard, CComm485 *pCommPort, CShadow *pShadow, CCo "A" #endif , m_pBoard->GetBSN()); - + */ #ifdef WATCHDOG_ENABLED // start watchdog diff --git a/lgs/lgs.DSW b/lgs/lgs.DSW index 8b45cd5..6b9baef 100644 Binary files a/lgs/lgs.DSW and b/lgs/lgs.DSW differ