For DOS Protected Mode version of sensor core software
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.

585 lines
18 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: DPelco.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DPelco.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Device\DChina.h"
  17. static DWORD far* g_Timer = (DWORD far*) 0x46CL;
  18. #define TIMER() (((*g_Timer)*1000)/18)
  19. #else
  20. #include "DChina.h"
  21. #define TIMER() GetTickCount()
  22. #include "DPelco.h"
  23. #endif
  24. #define C2C_PAUSE180 5000
  25. BOOL CLgDevicePTZChina::LoadUserSettings( void ) {
  26. // read delay number
  27. return CLgDevicePTZBasePelco::LoadUserSettings("CHINA.INI" );
  28. }
  29. CLgDevicePTZChina::CLgDevicePTZChina()
  30. {
  31. m_nMode = CMD_INIT;
  32. m_nStage = STAGE_INIT_TURN_ON;
  33. m_nNoReplyCount = 0;
  34. m_nNotInPositionCount = 0;
  35. // Default Settings
  36. PELCO_WB_INIT = 0; // Auto Wite Ballance is ON(0)
  37. PELCO_AF_INIT = 0; // Autofocus = Auto ( Auto=0, Off=1, On = 2)
  38. PELCO_BLC_INIT = 0; // Back Light Compensation is ON(0)
  39. memset( &m_PtzState, 0, sizeof(m_PtzState) );
  40. PELCO_MAX_NO_REPLY_COUNT = 20; // Replaying Camera
  41. PELCO_DELAY_BEFORE_GET_POSITION = 500; // Replaying Camera
  42. PELCO_MAX_OPTICAL_ZOOM = 0x2C85;
  43. PELCO_PAN_REVERSE = 0; // No Reverse
  44. PELCO_NO_RESPONCE = TRUE;
  45. PELCO_NO_PAN = FALSE;
  46. PELCO_NO_TILT = TRUE;
  47. PELCO_NO_ZOOM = TRUE;
  48. PELCO_NO_FOCUS = TRUE;
  49. PELCO_C2C_PAUSE = C2C_PAUSE180;
  50. m_bPaused = FALSE;
  51. LoadUserSettings();
  52. m_dPausedValue = PELCO_C2C_PAUSE;
  53. s_Position.dPan = -1;
  54. s_Position.dTilt = -1;
  55. s_Position.dZoom = -1;
  56. m_dSpecialDelay = 0;
  57. }
  58. CLgDevicePTZChina::~CLgDevicePTZChina()
  59. {
  60. }
  61. BOOL CLgDevicePTZChina::CmdInit()
  62. {
  63. // check availibility
  64. return DoCmd( CMD_INIT );
  65. }
  66. void CLgDevicePTZChina::OnPerformCmd()
  67. {
  68. // ready for new message
  69. ReleaseCmd();
  70. // ready to process new message
  71. m_nMode = CMD_NONE;
  72. }
  73. BOOL CLgDevicePTZChina::WriteProc( void *pProtocol, PLONG pTimeout ){
  74. BYTE pSendBuffer[ CProtocolPTZChina::MAX_COMMAND_SIZE ];
  75. BYTE nSendCount = 0;
  76. DWORD dCTick= TIMER();
  77. //#ifndef __BORLANDC__
  78. if ( ( dCTick - m_dQDelay ) < PELCO_DELAY_BEFORE_GET_POSITION ){
  79. *pTimeout = 100L;
  80. return FALSE;
  81. };
  82. if( m_dSpecialDelay ){
  83. if ( ( dCTick - m_dQDelay ) < m_dSpecialDelay ){
  84. *pTimeout = 10L;
  85. return FALSE;
  86. };
  87. m_dSpecialDelay = 0;
  88. };
  89. //#endif
  90. // cast to protocol
  91. CProtocolPTZChina *pProtocolPTZ = (CProtocolPTZChina*)pProtocol;
  92. // set working address
  93. pProtocolPTZ->SetAddress( GetAddress() );
  94. // check input command
  95. if( m_nMode == CMD_NONE ){
  96. switch( GetCmd() ){
  97. case CMD_INIT:
  98. m_nMode = CMD_INIT;
  99. m_nStage = STAGE_INIT_TURN_ON;
  100. break;
  101. case CMD_GET_PTZ:
  102. if ( !PELCO_NO_RESPONCE ) {
  103. m_nMode = CMD_GET_PTZ;
  104. if( !PELCO_NO_PAN ) m_nStage = STAGE_GET_PAN_POSITION;
  105. else if( !PELCO_NO_TILT ) m_nStage = STAGE_GET_TILT_POSITION;
  106. else if( !PELCO_NO_ZOOM ) m_nStage = STAGE_GET_ZOOM_POSITION;
  107. else {
  108. OnPerformCmd();
  109. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  110. };
  111. } else {
  112. //command unsupported
  113. OnPerformCmd();
  114. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  115. }
  116. break;
  117. case CMD_SET_PTZ:
  118. // Delay of previous PTZ command
  119. if( PELCO_C2C_PAUSE > 0 ){
  120. if( m_bPaused ){
  121. if( ( dCTick - m_dPausedDelay ) < m_dPausedValue ){
  122. OnPerformCmd();
  123. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  124. break;
  125. };
  126. };
  127. m_dPausedDelay = dCTick;
  128. m_bPaused = TRUE;
  129. };
  130. // validate positions
  131. if( !PELCO_NO_ZOOM )PTZ_PELCO::ValidateZoom( m_Position.dZoom );
  132. if( !PELCO_NO_TILT )PTZ_PELCO::ValidateTilt( m_Position.dTilt );
  133. if( !PELCO_NO_PAN ) PTZ_PELCO::ValidatePan( m_Position.dPan );
  134. m_nMode = CMD_SET_PTZ;
  135. if( PELCO_C2C_PAUSE > 0 ){
  136. int dPan = abs( m_Position.dPan - t_Position.dPan );
  137. m_dPausedValue = 1 + ((int) ( ( ((long)PELCO_C2C_PAUSE) * ((long)dPan) ) / 180L ) );
  138. };
  139. t_Position = m_Position;
  140. if( !PELCO_NO_ZOOM ){
  141. m_nStage = STAGE_SET_ZOOM_POSITION;
  142. } else {
  143. if( ( !PELCO_NO_TILT ) && ( s_Position.dTilt != t_Position.dTilt ) ){
  144. m_nStage = STAGE_SET_TILT_POSITION;
  145. } else {
  146. if( !PELCO_NO_PAN ){
  147. m_nStage = STAGE_SET_PAN_POSITION;
  148. } else {
  149. OnPerformCmd();
  150. s_Position = t_Position;
  151. OnEvtGetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  152. };
  153. };
  154. };
  155. m_nNotInPositionCount = 0;
  156. break;
  157. case CMD_MOVE_TILT:
  158. m_nMode = CMD_MOVE_TILT;
  159. break;
  160. case CMD_MOVE_PAN:
  161. m_nMode = CMD_MOVE_PAN;
  162. break;
  163. case CMD_MOVE_ZOOM:
  164. m_nMode = CMD_MOVE_ZOOM;
  165. break;
  166. case CMD_NONE:
  167. // no new commands
  168. if( PELCO_C2C_PAUSE > 0 ){
  169. if( m_bPaused ){
  170. if( ( dCTick - m_dPausedDelay) > m_dPausedValue ){
  171. m_bPaused = FALSE;
  172. };
  173. };
  174. };
  175. break;
  176. case CMD_SET_IRIS:
  177. case CMD_SET_FOCUS:
  178. case CMD_WIPER:
  179. default:
  180. //command unsupported
  181. OnPerformCmd();
  182. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  183. break;
  184. }
  185. }
  186. // fill packet depend on command
  187. switch( m_nMode ){
  188. case CMD_INIT:
  189. switch( m_nStage ){
  190. case STAGE_INIT_TURN_ON:
  191. nSendCount += PTZ_PELCO::BuildMove( pSendBuffer, GetAddress(), m_PtzState );
  192. if ( !PELCO_NO_RESPONCE ) {
  193. pProtocolPTZ->SetResponseSize( 4 );
  194. } else {
  195. m_nStage = STAGE_INIT_AUTO_FOCUS;
  196. };
  197. break;
  198. case STAGE_INIT_AUTO_FOCUS:
  199. if( !PELCO_NO_FOCUS ){
  200. nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), PELCO_AF_INIT ); // 0 - On , 1 - Off
  201. } else {
  202. nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), 1 ); // 0 - On , 1 - Off
  203. };
  204. if( !PELCO_NO_RESPONCE ) {
  205. pProtocolPTZ->SetResponseSize( 4 );
  206. } else {
  207. m_nStage = STAGE_INIT_AUTO_IRIS;
  208. };
  209. break;
  210. case STAGE_INIT_AUTO_IRIS:
  211. nSendCount += PTZ_PELCO::BuildAutoIris( pSendBuffer, GetAddress(), 0 );
  212. if ( ! PELCO_NO_RESPONCE ) {
  213. pProtocolPTZ->SetResponseSize( 4 );
  214. } else {
  215. m_nStage = STAGE_INIT_AUTO_GAIN_CONTROL;
  216. };
  217. break;
  218. case STAGE_INIT_AUTO_GAIN_CONTROL:
  219. nSendCount += PTZ_PELCO::BuildAutoGainControl( pSendBuffer, GetAddress(), 0 );
  220. if ( ! PELCO_NO_RESPONCE ) {
  221. pProtocolPTZ->SetResponseSize( 4 );
  222. } else {
  223. m_nStage = STAGE_INIT_BACKLIGHT_COMPENSATION;
  224. };
  225. break;
  226. case STAGE_INIT_BACKLIGHT_COMPENSATION:
  227. nSendCount += PTZ_PELCO::BuildBacklightCompensation( pSendBuffer, GetAddress(), PELCO_BLC_INIT );
  228. if ( ! PELCO_NO_RESPONCE ) {
  229. pProtocolPTZ->SetResponseSize( 4 );
  230. } else {
  231. m_nStage = STAGE_INIT_WHITE_BALANCE;
  232. };
  233. break;
  234. case STAGE_INIT_WHITE_BALANCE:
  235. nSendCount += PTZ_PELCO::BuildWhiteBalance( pSendBuffer, GetAddress(), PELCO_WB_INIT ); // 0 - On, 1 - Off, 2 - Auto
  236. if ( ! PELCO_NO_RESPONCE ) {
  237. pProtocolPTZ->SetResponseSize( 4 );
  238. } else {
  239. OnPerformCmd();
  240. OnEvtInit();
  241. };
  242. break;
  243. }
  244. m_dQDelay = TIMER();
  245. break;
  246. case CMD_SET_PTZ:
  247. switch( m_nStage ){
  248. case STAGE_SET_ZOOM_POSITION:
  249. if( !PELCO_NO_ZOOM ){
  250. nSendCount += PTZ_PELCO::BuildSetZoom( pSendBuffer, GetAddress(), PTZ_PELCO::Percent2Zoom( t_Position.dZoom, PELCO_MAX_OPTICAL_ZOOM ) );
  251. if ( !PELCO_NO_RESPONCE ) {
  252. pProtocolPTZ->SetResponseSize( 7 );
  253. m_dQDelay = TIMER();
  254. };
  255. //m_nStage = STAGE_SET_TILT_POSITION;
  256. break;
  257. } else {
  258. // m_nStage = STAGE_SET_TILT_POSITION;
  259. };
  260. case STAGE_SET_TILT_POSITION:
  261. if( ( !PELCO_NO_TILT ) && ( s_Position.dTilt != t_Position.dTilt ) ){
  262. if( s_Position.dTilt == -1 ){
  263. m_dSpecialDelay = 2000;
  264. } else {
  265. int dTilt = abs( s_Position.dTilt - t_Position.dTilt );
  266. m_dSpecialDelay = 1 + ((int) ( ( (2000L) * ((long)dTilt) ) / 90L ) );
  267. };
  268. m_dQDelay = TIMER();
  269. nSendCount += PTZ_PELCO::BuildSetTilt( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Tilt( t_Position.dTilt ) );
  270. if( !PELCO_NO_RESPONCE ){
  271. pProtocolPTZ->SetResponseSize( 7 );
  272. //m_dQDelay = TIMER();
  273. };
  274. //m_nStage = STAGE_SET_PAN_POSITION;
  275. break;
  276. } else {
  277. //m_nStage = STAGE_SET_PAN_POSITION;
  278. };
  279. case STAGE_SET_PAN_POSITION:
  280. if( !PELCO_NO_PAN ){
  281. nSendCount += PTZ_PELCO::BuildSetPan( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Pan( t_Position.dPan ,PELCO_PAN_REVERSE) );
  282. if ( !PELCO_NO_RESPONCE ) {
  283. pProtocolPTZ->SetResponseSize( 7 );
  284. m_dQDelay = TIMER();
  285. //m_nStage = STAGE_GET_PAN_POSITION;
  286. } else {
  287. OnPerformCmd();
  288. s_Position = t_Position;
  289. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  290. };
  291. break;
  292. } else {
  293. if ( PELCO_NO_RESPONCE ) {
  294. OnPerformCmd();
  295. s_Position = t_Position;
  296. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  297. } else {
  298. //m_nStage = STAGE_GET_PAN_POSITION;
  299. };
  300. };
  301. break;
  302. case STAGE_GET_PAN_POSITION:
  303. if( !PELCO_NO_PAN ){
  304. nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
  305. pProtocolPTZ->SetResponseSize( 7 );
  306. } else {
  307. OnPerformCmd();
  308. s_Position = t_Position;
  309. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  310. };
  311. break;
  312. case STAGE_GET_TILT_POSITION:
  313. if( !PELCO_NO_TILT ){
  314. nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
  315. pProtocolPTZ->SetResponseSize( 7 );
  316. } else {
  317. OnPerformCmd();
  318. s_Position = t_Position;
  319. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  320. };
  321. break;
  322. case STAGE_GET_ZOOM_POSITION:
  323. if( !PELCO_NO_ZOOM ){
  324. nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
  325. pProtocolPTZ->SetResponseSize( 7 );
  326. } else {
  327. OnPerformCmd();
  328. s_Position = t_Position;
  329. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  330. };
  331. break;
  332. }
  333. break;
  334. case CMD_GET_PTZ:
  335. if( PELCO_NO_RESPONCE ){
  336. OnPerformCmd();
  337. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  338. } else {
  339. switch( m_nStage ){
  340. case STAGE_GET_PAN_POSITION:
  341. if( !PELCO_NO_PAN ){
  342. nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
  343. pProtocolPTZ->SetResponseSize( 7 );
  344. break;
  345. } else {
  346. OnPerformCmd();
  347. s_Position = m_Position;
  348. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  349. };
  350. case STAGE_GET_TILT_POSITION:
  351. if(!PELCO_NO_TILT ){
  352. nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
  353. pProtocolPTZ->SetResponseSize( 7 );
  354. break;
  355. } else {
  356. OnPerformCmd();
  357. s_Position = m_Position;
  358. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  359. };
  360. case STAGE_GET_ZOOM_POSITION:
  361. if(!PELCO_NO_ZOOM ){
  362. nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
  363. pProtocolPTZ->SetResponseSize( 7 );
  364. break;
  365. } else {
  366. OnPerformCmd();
  367. s_Position = m_Position;
  368. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  369. };
  370. };
  371. };
  372. break;
  373. case CMD_MOVE_TILT:
  374. s_Position.dTilt = -1;
  375. 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 );
  376. pProtocolPTZ->SetResponseSize( 4 );
  377. break;
  378. case CMD_MOVE_PAN:
  379. s_Position.dPan = -1;
  380. 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 );
  381. pProtocolPTZ->SetResponseSize( 4 );
  382. break;
  383. case CMD_MOVE_ZOOM:
  384. s_Position.dZoom = -1;
  385. 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 );
  386. pProtocolPTZ->SetResponseSize( 4 );
  387. break;
  388. case CMD_NONE:
  389. default:
  390. *pTimeout = 10L;
  391. return FALSE;
  392. };
  393. // write packet
  394. if( !pProtocolPTZ->Write( pSendBuffer, nSendCount, 10UL, TRUE ) ){
  395. *pTimeout = 10L; // TX Only
  396. return FALSE;
  397. } else {
  398. if ( !PELCO_NO_RESPONCE ) {
  399. *pTimeout = 70L;
  400. return TRUE;// TX + RX
  401. } else {
  402. *pTimeout = 70L; // TX Only
  403. return TRUE;
  404. };
  405. };
  406. };
  407. BOOL CLgDevicePTZChina::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
  408. {
  409. CProtocolPTZChina *pProtocolPTZ = (CProtocolPTZChina*)pProtocol;
  410. // read reply
  411. PBYTE pReadBuffer = NULL;
  412. INT nReadCount = 0;
  413. if( !PELCO_NO_RESPONCE ){
  414. //if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  415. if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount ){
  416. if( !bLastChance ) return FALSE;
  417. m_nNoReplyCount++;
  418. // if( m_nNoReplyCount > MAX_NO_REPLY_COUNT || ( m_nMode == CMD_GET_PTZ && m_nNoReplyCount > MAX_NOT_IN_POSITION_COUNT ) ) // v_1
  419. if( m_nNoReplyCount > PELCO_MAX_NO_REPLY_COUNT ) // v_2
  420. {
  421. // perform goto idle state
  422. // m_nMode = CMD_INIT; // Commented for BOSH/Pelco
  423. } else // else - Bosh Pelco
  424. return FALSE;
  425. };
  426. } else {
  427. #ifdef INCLUDE_WINDOWS_DEFINITIONS
  428. // DOS Mode
  429. //delay(lTimeout);
  430. #else
  431. Sleep(lTimeout);
  432. #endif
  433. nReadCount = 0;
  434. };
  435. // reset retry counter
  436. m_nNoReplyCount = 0;
  437. // switch mode
  438. switch( m_nMode ){
  439. case CMD_INIT:
  440. switch( m_nStage ){
  441. case STAGE_INIT_TURN_ON: m_nStage = STAGE_INIT_AUTO_FOCUS; break;
  442. case STAGE_INIT_AUTO_FOCUS: m_nStage = STAGE_INIT_AUTO_IRIS; break;
  443. case STAGE_INIT_AUTO_IRIS: m_nStage = STAGE_INIT_AUTO_GAIN_CONTROL; break;
  444. case STAGE_INIT_AUTO_GAIN_CONTROL: m_nStage = STAGE_INIT_BACKLIGHT_COMPENSATION; break;
  445. case STAGE_INIT_BACKLIGHT_COMPENSATION: m_nStage = STAGE_INIT_WHITE_BALANCE; break;
  446. case STAGE_INIT_WHITE_BALANCE: // m_nStage = STAGE_INIT_TURN_ON; break;
  447. OnPerformCmd();
  448. OnEvtInit();
  449. break;
  450. }
  451. break;
  452. case CMD_GET_PTZ:
  453. if( PELCO_NO_RESPONCE ){
  454. // ready to process new message
  455. OnPerformCmd();
  456. //notify
  457. OnEvtGetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  458. } else {
  459. switch( m_nStage ){
  460. case STAGE_GET_PAN_POSITION:
  461. m_Position.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ), PELCO_PAN_REVERSE );
  462. m_nStage = STAGE_GET_TILT_POSITION;
  463. break;
  464. case STAGE_GET_TILT_POSITION:
  465. m_Position.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  466. m_nStage = STAGE_GET_ZOOM_POSITION;
  467. break;
  468. case STAGE_GET_ZOOM_POSITION:
  469. m_Position.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ), PELCO_MAX_OPTICAL_ZOOM );
  470. OnPerformCmd();
  471. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  472. break;
  473. };
  474. };
  475. break;
  476. case CMD_SET_PTZ:
  477. if( PELCO_NO_RESPONCE ){
  478. switch( m_nStage ){
  479. case STAGE_SET_ZOOM_POSITION: m_nStage = STAGE_SET_TILT_POSITION; break;
  480. case STAGE_SET_TILT_POSITION: m_nStage = STAGE_SET_PAN_POSITION; break;
  481. case STAGE_SET_PAN_POSITION:
  482. OnPerformCmd();
  483. s_Position = t_Position;
  484. OnEvtSetPTZ( t_Position.dPan, t_Position.dTilt, t_Position.dZoom );
  485. break;
  486. };
  487. break;
  488. } else {
  489. switch( m_nStage ){
  490. case STAGE_SET_ZOOM_POSITION: m_nStage = STAGE_SET_TILT_POSITION; break;
  491. case STAGE_SET_TILT_POSITION: m_nStage = STAGE_SET_PAN_POSITION; break;
  492. case STAGE_SET_PAN_POSITION: m_nStage = STAGE_GET_PAN_POSITION; break;
  493. case STAGE_GET_PAN_POSITION:
  494. m_TempPosition.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ), PELCO_PAN_REVERSE );
  495. m_nStage = STAGE_GET_TILT_POSITION;
  496. break;
  497. case STAGE_GET_TILT_POSITION:
  498. m_TempPosition.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  499. m_nStage = STAGE_GET_ZOOM_POSITION;
  500. break;
  501. case STAGE_GET_ZOOM_POSITION:
  502. m_TempPosition.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) , PELCO_MAX_OPTICAL_ZOOM );
  503. // calculate difference between desired position and current position
  504. 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 ) ){
  505. // store position
  506. m_Position.dPan = m_TempPosition.dPan;
  507. m_Position.dTilt = m_TempPosition.dTilt;
  508. m_Position.dZoom = m_TempPosition.dZoom;
  509. // ready to process new message
  510. OnPerformCmd();
  511. s_Position = m_Position;
  512. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  513. } else {
  514. // does not in posiiton
  515. m_nStage = STAGE_SET_PAN_POSITION;
  516. // increate fault counter
  517. m_nNotInPositionCount++;
  518. return TRUE;
  519. };
  520. break;
  521. };
  522. };
  523. break;
  524. case CMD_MOVE_PAN:
  525. OnPerformCmd();
  526. OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
  527. break;
  528. case CMD_MOVE_TILT:
  529. OnPerformCmd();
  530. OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
  531. break;
  532. case CMD_MOVE_ZOOM:
  533. OnPerformCmd();
  534. OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
  535. break;
  536. };
  537. return TRUE;
  538. }