#include "protocolPTZ_PELCO.h" #include "../../hardware/uart/serialPort.h" #define LOG( X ) m_pLog->Log( "Proto[PELCO]", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "Proto[PELCO]", this, _LOG_ERROR, X ) protocolPTZ_PELCO::protocolPTZ_PELCO(config_setting_t *Settings) :protocolPTZ(Settings), m_mInquiryMutex(){ m_pDataTransport = NULL; m_bReplyRequired = false; m_bDPlusEna = false; m_bCAM_ID = 2; m_bLastCommandIssued = CMD_PTZ_NOOP; }; protocolPTZ_PELCO::~protocolPTZ_PELCO(){ if (m_pDataTransport!=NULL)delete((serialPort*)m_pDataTransport); }; U8 protocolPTZ_PELCO::finishUpPackage(U8 *pBuffer){ U8 nChecksum = 0; pBuffer[SYNC_IDX] = 0xFF; pBuffer[ADR_IDX] = m_bCAM_ID; for (U8 i = ADR_IDX; i < CHKSUM_IDX; i++) nChecksum += pBuffer[i]; pBuffer[CHKSUM_IDX] = nChecksum; return 7; }; int protocolPTZ_PELCO::packetChecker(U8* Buffer, size_t BufferSize) { if( Buffer[0] == 0xFF ) { if( BufferSize > 1 ){ if( Buffer[1] == 0x00 || Buffer[1] == m_bCAM_ID ) { U8 tChSumm; // Check 4 Bytes Responce if( BufferSize > 3 ){ tChSumm = 0; for (U8 i = 1; i< 4; i++) tChSumm += Buffer[i]; if (Buffer[4] == tChSumm){ #ifdef DEBUG LOG("PacketChecker: 4 Byte Packet Is Recognized"); #endif return 0; }; }; // Check 7 Bytes Responce if( BufferSize > 6 ){ U8 tChSumm = 0; for( U8 i=1; i< 6; i++ ) tChSumm += Buffer[ i ]; if( Buffer[6] == tChSumm ){ #ifdef DEBUG LOG("PacketChecker: 7 Byte Packet Is Recognized"); #endif return 0; }; }; // Check 18 Bytes Responce if (BufferSize > 17){ U8 tChSumm = 0; for( U8 i = 1; i< 17; i++ ) tChSumm += Buffer[i]; if( Buffer[17] == tChSumm ){ #ifdef DEBUG LOG("PacketChecker: 18 Byte Packet Is Recognized"); #endif return 0; }; }; } else return -1; }; return 1; }; #ifdef DEBUG LOG("PacketChecker: Packet Not Recognized"); #endif return -1; }; protocolPackage *protocolPTZ_PELCO::packetExtractor(unsigned char *Buffer, size_t BufferSize){ protocolPackage *RetVal; if( BufferSize < 4 ) return NULL; RetVal = (protocolPackage*)malloc(sizeof(protocolPackage)); if( RetVal != NULL ){ RetVal->m_pData = (U8*) malloc(18); if (RetVal->m_pData != NULL){ U8 tChSumm = 0; U8 i; RetVal->m_pData[tChSumm] = Buffer[tChSumm]; for (i = 1; i < 18; i++){ tChSumm += RetVal->m_pData[i] = Buffer[i]; if ((i == 17 || i == 6 || i == 3) && (Buffer[i] == tChSumm)){ RetVal->m_nSize = i + 1; return RetVal; }; }; free(RetVal->m_pData); }; free(RetVal); }; return NULL; }; bool protocolPTZ_PELCO::packetHandler(protocolPackage *Package){ U8 Com_Mode; U8 Cat_Code; LOG( "PacketHandler: Packet Is Handling"); Com_Mode = Package->m_pData[1]; Cat_Code = Package->m_pData[2]; m_bLastCommandIssued = CMD_PTZ_NOOP; free( Package->m_pData ); free( Package ); return true; }; bool protocolPTZ_PELCO::create( void ){ config_setting_t *pSetting; int tPort, tSpeed; if (!protocolPTZ::create()) return false; m_bStatus = 0; pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_COM_PORT); if (pSetting == NULL) return false; tPort = config_setting_get_int(pSetting); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_COM_SPEED); if (pSetting == NULL) return false; tSpeed = config_setting_get_int(pSetting); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_CAM_ID); if (pSetting == NULL) return false; m_bCAM_ID = (U8)config_setting_get_int(pSetting); pSetting = config_setting_get_member(m_pSettings, PELCO_D_PLUS_ENABLE); if (pSetting != NULL) { m_bDPlusEna = (bool)config_setting_get_bool(pSetting); }; char tBuf[16]; #ifdef __linux__ sprintf( tBuf,"/dev/ttyS%d",tPort); #else sprintf(tBuf, "com%d", tPort); #endif m_pDataTransport = new serialPort(tBuf,tSpeed); if (m_pDataTransport != NULL){ if (m_pDataTransport->addRecipient(this)){ setConnectionStatus(CS_CONNECTED); m_bStatus = 1; return true; }; }; return false; }; bool protocolPTZ_PELCO::initCamera(void){ return setPowerSaveMode( false ); }; bool protocolPTZ_PELCO::setPowerSaveMode( bool nMode ){ U8 Buffer[8]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; m_bPowerSave_Mode = !nMode; m_bLastCommandIssued =( m_bPowerSave_Mode )?CMD_PTZ_POWER_ON:CMD_PTZ_POWER_OFF; memset(Buffer, 0, 8); Buffer[DCMD1_IDX] = (m_bPowerSave_Mode) ? 0x88 : 0x08; LOG("Sending POWER ON/OFF"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::getPowerSaveMode( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x09; Buffer[2]=0x04; Buffer[3]=0x00; Buffer[4]=0xFF; m_mInquiryMutex.lock(); LOG("Ask POWER mode"); m_pDataTransport->sendData( Buffer, 5 ); m_mInquiryMutex.lock(); m_mInquiryMutex.unlock(); LOG("Ask POWER mode -> Done"); return m_bPowerSave_Mode; }; bool protocolPTZ_PELCO::startZoomWide( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x04; Buffer[3]=0x07; Buffer[4]=0x03; Buffer[5]=0xFF; LOG("Start Zoom Wide(standard)"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; bool protocolPTZ_PELCO::startZoomTele( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x04; Buffer[3]=0x07; Buffer[4]=0x02; Buffer[5]=0xFF; LOG("Start Zoom Tele(standard)"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; bool protocolPTZ_PELCO::startZoomWide( double nSpeed ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x04; Buffer[3]=0x07; U8 Param; Param = ((U8)( 2 + nSpeed * 5 )) & 0x07; Buffer[4]=0x30 | Param; Buffer[5]=0xFF; LOG("Start Zoom Wide(parameter)"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; bool protocolPTZ_PELCO::startZoomTele( double nSpeed ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x04; Buffer[3]=0x07; U8 Param; Param = ((U8)( 2 + nSpeed * 5 )) & 0x07; Buffer[4]=0x20 | Param; Buffer[5]=0xFF; LOG("Start Zoom Tele(parameter)"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; bool protocolPTZ_PELCO::stopZoom( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x04; Buffer[3]=0x07; Buffer[4]=0x00; Buffer[5]=0xFF; LOG("Stop Zoom"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; /*double protocolPTZ_PELCO::getZoom( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x09; Buffer[2]=0x04; Buffer[3]=0x47; Buffer[4]=0xFF; m_mInquiryMutex.lock(); LOG("Ask Zoom"); m_pDataTransport->sendData( Buffer, 5 ); m_mInquiryMutex.lock(); m_mInquiryMutex.unlock(); LOG("Ask Zoom -> Done"); return m_dZoom; }; */ bool protocolPTZ_PELCO::resetPTZDrive( void ){ U8 Buffer[16]; if (m_bStatus == 0) return false; memset(Buffer, 0, 8); Buffer[DCMD1_IDX] = 0x00; Buffer[DCMD2_IDX] = 0x0F; Buffer[DATA1_IDX] = 0x00; Buffer[DATA2_IDX] = 0x00; LOG("Reset PTZ"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::goHomePosition( void ){ U8 Buffer[8]; if (m_bStatus == 0) return false; memset(Buffer, 0, 8); Buffer[DCMD2_IDX] = 0x07; Buffer[DATA2_IDX] = 0x22; LOG("Go Home"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::setPanPos( double dPan ){ U8 Buffer[16]; U16 SPan; SPan = ((U16)( 36000.0 + 18000.0 * dPan )) % 36000; Buffer[DCMD2_IDX] = 0x4B; Buffer[DATA1_IDX] = ( SPan >> 8 ) & 0xFF; Buffer[DATA2_IDX] = SPan & 0xFF; #ifdef DEBUG char tBuffer[64]; sprintf(tBuffer, "setPanPos %.3f", dPan ); LOG(tBuffer); #endif return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::setTiltPos( double dTilt ){ U8 Buffer[16]; U16 STlt; STlt = ((U16)(18000.0 + 18000.0 * ((dTilt + 1.f) / 2.f))) % 18000; Buffer[DCMD2_IDX] = 0x4D; Buffer[DATA1_IDX] = ( STlt >> 8 ) & 0xFF; Buffer[DATA2_IDX] = STlt & 0xFF; #ifdef DEBUG char tBuffer[64]; sprintf(tBuffer, "setTiltPos %.3f -> %d", dTilt, STlt); LOG(tBuffer); #endif return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::setZoom( double dZoom ){ U8 Buffer[16]; U16 SZum; SZum = (U16)(65535 * dZoom ); Buffer[DCMD2_IDX] = 0x4F; Buffer[DATA1_IDX] = ( SZum >> 8 ) & 0xFF; Buffer[DATA2_IDX] = SZum & 0xFF; #ifdef DEBUG char tBuffer[64]; sprintf(tBuffer, "setZoomPos %.3f", dZoom); LOG(tBuffer); #endif return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::move(uint32_t MoveMatrix){ U8 Buffer[16]; U8 Param1, Param2; if (m_bStatus == 0) return false; // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Param1 = (U8)(m_dPanSpeed * 64.0) & 0xFF; Param2 = (U8)(m_dTiltSpeed * 64.0) & 0xFF; memset(Buffer, 0, 16); if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) != 0){ if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){ Buffer[DCMD2_IDX] |= 0x04; } else { Buffer[DCMD2_IDX] |= 0x02; }; }; if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) != 0){ if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){ Buffer[DCMD2_IDX] |= 0x08; } else { Buffer[DCMD2_IDX] |= 0x10; }; }; if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0){ if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0){ Buffer[DCMD2_IDX] |= 0x40; } else { Buffer[DCMD2_IDX] |= 0x20; }; }; Buffer[DATA1_IDX] = Param1; // Max = 3f Turbo Pan = FF Buffer[DATA2_IDX] = Param2; LOG("Make move"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::goAbsolute(double dPan, double dTilt, double dZoom) { U8 Buffer[16]; U8 Param1, Param2; if (m_bStatus == 0) return false; // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; if (m_bDPlusEna) { Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1] = 0x01; Buffer[2] = 0x06; Buffer[3] = 0x02; Buffer[4] = 0x18;//Max Pan Speed Buffer[5] = 0x14;//Max Tilt Speed uint16_t pPos, tPos; //-880 .. 880 pPos = (uint16_t)(880.0 * dPan); Buffer[6] = 0x0F & (pPos >> 12); Buffer[7] = 0x0F & (pPos >> 8); Buffer[8] = 0x0F & (pPos >> 4); Buffer[9] = 0x0F & pPos; //-300 .. 300 tPos = (uint16_t)(300.0 * dTilt); Buffer[10] = 0x0F & (tPos >> 12); Buffer[11] = 0x0F & (tPos >> 8); Buffer[12] = 0x0F & (tPos >> 4); Buffer[13] = 0x0F & tPos; Buffer[14] = 0xFF; LOG("ABS move"); return m_pDataTransport->sendData(Buffer, 15); } else { LOG("ABS move"); return true; }; }; bool protocolPTZ_PELCO::setAutoFocus(bool nMode){ m_bAutoFocus_Mode = nMode; U8 Buffer[8]; if (m_bStatus == 0) return false; memset(Buffer, 0, 8); Buffer[DCMD2_IDX] = 0x2B; Buffer[DATA2_IDX] = (m_bAutoFocus_Mode) ? 0x01 : 0x02; LOG("setAutoFocus"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::setOSD( bool nMode ){ U8 Buffer[16]; if (m_bStatus == 0) return false; //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; Buffer[0] = 0x80 + m_bCAM_ID; Buffer[1]=0x01; Buffer[2]=0x06; Buffer[3]=0x06; Buffer[4]=(nMode)?0x02:0x03; Buffer[5]=0xFF; LOG("Set On Screen Display mode"); m_pDataTransport->sendData( Buffer, 6 ); return true; }; /* bool protocolPTZ_PELCO::setPSpeed(double Speed) { m_dPanSpeed = Speed; LOG("setPanSpeed"); return move(0); }; bool protocolPTZ_PELCO::setTSpeed(double Speed) { m_dTiltSpeed = Speed; LOG("setTiltSpeed"); return move(0); }; bool protocolPTZ_PELCO::setPTZSpeed(double pSpeed, double tSpeed, double zSpeed) { m_dPanSpeed = pSpeed; m_dTiltSpeed = tSpeed; m_dZoomSpeed = zSpeed; LOG("setPTZSpeed"); return move(0); }; bool protocolPTZ_PELCO::setZSpeed(double Speed) { m_dZoomSpeed = Speed; U8 Buffer[8]; if (m_bStatus == 0) return false; memset(Buffer, 0, 8); Buffer[DCMD2_IDX] = 0x25; U8 sValue = (U8)floor(m_dZoomSpeed * 3.f); Buffer[DATA2_IDX] = sValue; LOG("setZoomSpeed"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; bool protocolPTZ_PELCO::setFSpeed(double Speed) { m_dZoomSpeed = Speed; U8 Buffer[8]; if (m_bStatus == 0) return false; memset(Buffer, 0, 8); Buffer[DCMD2_IDX] = 0x27; U8 sValue = (U8)floor( m_dZoomSpeed * 3.f ); Buffer[DATA2_IDX] = sValue; LOG("setFocusSpeed"); return m_pDataTransport->sendData(Buffer, finishUpPackage(Buffer)); }; */ #undef LOG #undef LOG_ERROR