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.

455 lines
13 KiB

4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
4 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\DPelco.h"
  17. #else
  18. #include "DPelco.h"
  19. #endif
  20. #define POSITION_QUALYTY 2
  21. #define MAX_NO_REPLY_COUNT 20
  22. #define MAX_NOT_IN_POSITION_COUNT 1
  23. #define DELAY_BEFORE_GET_POSITION 500
  24. typedef enum PELCO_SETTING_TYPE
  25. {
  26. P_ST_NONE = -1,
  27. P_ST_MAX_NO_REPLY_COUNT = 0,
  28. P_ST_DELAY_BEFORE_GET_POSITION,
  29. P_ST_MAX_OPTICAL_ZOOM
  30. }PELCO_SETTING_TYPE;
  31. LPCTSTR m_pPelco_Setting[] =
  32. {
  33. { "MAX_NO_REPLY_COUNT" },
  34. { "DELAY_BEFORE_GET_POSITION" },
  35. ( "MAX_OPTICAL_ZOOM" )
  36. };
  37. BOOL CLgDevicePTZPelco::LoadUserSettings( void ) {
  38. // read delay number
  39. FILE *pFile = fopen( "PELCO.INI", "rt" );
  40. if( !pFile ) return FALSE;
  41. CHAR pLine[MAX_PATH];
  42. while( fgets( pLine, MAX_PATH, pFile ) )
  43. {
  44. PELCO_SETTING_TYPE eType = P_ST_NONE;
  45. PCHAR pTemp = NULL;
  46. for( INT i = 0; i < 3; i++ )
  47. {
  48. pTemp = strstr( pLine, m_pPelco_Setting[i] );
  49. if( pTemp )
  50. {
  51. eType = (PELCO_SETTING_TYPE)i;
  52. pTemp = strstr( pTemp, "=" );
  53. if( pTemp )
  54. pTemp = strtok( pTemp + 1, " \t\n\r" );
  55. break;
  56. }
  57. }
  58. if( eType == P_ST_NONE || !pTemp )
  59. continue;
  60. switch( eType )
  61. {
  62. case P_ST_MAX_NO_REPLY_COUNT: {
  63. PELCO_MAX_NO_REPLY_COUNT = atoi( pTemp ); break;
  64. };
  65. case P_ST_MAX_OPTICAL_ZOOM: {
  66. PELCO_MAX_OPTICAL_ZOOM = atoi( pTemp ); break;
  67. };
  68. }
  69. }
  70. fclose( pFile );
  71. return TRUE;
  72. }
  73. CLgDevicePTZPelco::CLgDevicePTZPelco()
  74. {
  75. m_nMode = CMD_INIT;
  76. m_nStage = STAGE_INIT_TURN_ON;
  77. m_nNoReplyCount = 0;
  78. m_nNotInPositionCount = 0;
  79. memset( &m_PtzState, 0, sizeof(m_PtzState) );
  80. m_bPause = FALSE;
  81. PELCO_MAX_NO_REPLY_COUNT = 20; // Default for Replaying Camera
  82. PELCO_DELAY_BEFORE_GET_POSITION = 500; // Default for Replaying Camera
  83. PELCO_MAX_OPTICAL_ZOOM = 0x2C85;
  84. LoadUserSettings();
  85. }
  86. CLgDevicePTZPelco::~CLgDevicePTZPelco()
  87. {
  88. }
  89. BOOL CLgDevicePTZPelco::CmdInit()
  90. {
  91. // check availibility
  92. return DoCmd( CMD_INIT );
  93. }
  94. void CLgDevicePTZPelco::OnPerformCmd()
  95. {
  96. // ready for new message
  97. ReleaseCmd();
  98. // ready to process new message
  99. m_nMode = CMD_NONE;
  100. }
  101. BOOL CLgDevicePTZPelco::WriteProc( void *pProtocol, PLONG pTimeout )
  102. {
  103. BYTE pSendBuffer[ CProtocolPTZPelco::MAX_COMMAND_SIZE ];
  104. BYTE nSendCount = 0;
  105. // cast to protocol
  106. CProtocolPTZPelco *pProtocolPTZ = (CProtocolPTZPelco*)pProtocol;
  107. // set working address
  108. pProtocolPTZ->SetAddress( GetAddress() );
  109. if( m_bPause )
  110. {
  111. m_bPause = FALSE;
  112. *pTimeout = 100L;
  113. return FALSE;
  114. }
  115. // check input command
  116. if( m_nMode == CMD_NONE )
  117. {
  118. switch( GetCmd() )
  119. {
  120. case CMD_INIT:
  121. m_nMode = CMD_INIT;
  122. m_nStage = STAGE_INIT_TURN_ON;
  123. break;
  124. case CMD_GET_PTZ:
  125. m_nMode = CMD_GET_PTZ;
  126. m_nStage = STAGE_GET_PAN_POSITION;
  127. break;
  128. case CMD_SET_PTZ:
  129. // validate positions
  130. PTZ_PELCO::ValidatePan( m_Position.dPan );
  131. PTZ_PELCO::ValidateTilt( m_Position.dTilt );
  132. PTZ_PELCO::ValidateZoom( m_Position.dZoom );
  133. m_nMode = CMD_SET_PTZ;
  134. m_nStage = STAGE_SET_PAN_POSITION;
  135. m_nNotInPositionCount = 0;
  136. break;
  137. case CMD_MOVE_TILT:
  138. m_nMode = CMD_MOVE_TILT;
  139. break;
  140. case CMD_MOVE_PAN:
  141. m_nMode = CMD_MOVE_PAN;
  142. break;
  143. case CMD_MOVE_ZOOM:
  144. m_nMode = CMD_MOVE_ZOOM;
  145. break;
  146. case CMD_NONE:
  147. // no new commands
  148. break;
  149. case CMD_SET_IRIS:
  150. case CMD_SET_FOCUS:
  151. case CMD_WIPER:
  152. default:
  153. //command unsupported
  154. OnPerformCmd();
  155. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  156. break;
  157. }
  158. }
  159. // fill packet depend on command
  160. switch( m_nMode )
  161. {
  162. case CMD_INIT:
  163. switch( m_nStage )
  164. {
  165. case STAGE_INIT_TURN_ON:
  166. nSendCount += PTZ_PELCO::BuildMove( pSendBuffer, GetAddress(), m_PtzState );
  167. pProtocolPTZ->SetResponseSize( 4 );
  168. break;
  169. case STAGE_INIT_AUTO_FOCUS:
  170. nSendCount += PTZ_PELCO::BuildAutoFocus( pSendBuffer, GetAddress(), 0 ); // 0 - On , 1 - Off
  171. pProtocolPTZ->SetResponseSize( 4 );
  172. break;
  173. case STAGE_INIT_AUTO_IRIS:
  174. nSendCount += PTZ_PELCO::BuildAutoIris( pSendBuffer, GetAddress(), 0 );
  175. pProtocolPTZ->SetResponseSize( 4 );
  176. break;
  177. case STAGE_INIT_AUTO_GAIN_CONTROL:
  178. nSendCount += PTZ_PELCO::BuildAutoGainControl( pSendBuffer, GetAddress(), 0 );
  179. pProtocolPTZ->SetResponseSize( 4 );
  180. break;
  181. case STAGE_INIT_BACKLIGHT_COMPENSATION:
  182. nSendCount += PTZ_PELCO::BuildBacklightCompensation( pSendBuffer, GetAddress(), 0 );
  183. pProtocolPTZ->SetResponseSize( 4 );
  184. break;
  185. case STAGE_INIT_WHITE_BALANCE:
  186. nSendCount += PTZ_PELCO::BuildWhiteBalance( pSendBuffer, GetAddress(), 2 ); // 0 - On, 1 - Off, 2 - Auto
  187. pProtocolPTZ->SetResponseSize( 4 );
  188. break;
  189. }
  190. break;
  191. case CMD_SET_PTZ:
  192. switch( m_nStage )
  193. {
  194. case STAGE_SET_PAN_POSITION:
  195. // ask for pan position
  196. nSendCount += PTZ_PELCO::BuildSetPan( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Pan( m_Position.dPan ) );
  197. // set reply size
  198. pProtocolPTZ->SetResponseSize( 4 );
  199. break;
  200. case STAGE_SET_TILT_POSITION:
  201. // ask for pan position
  202. nSendCount += PTZ_PELCO::BuildSetTilt( pSendBuffer, GetAddress(), PTZ_PELCO::Degree2Tilt( m_Position.dTilt ) );
  203. // set reply size
  204. pProtocolPTZ->SetResponseSize( 4 );
  205. break;
  206. case STAGE_SET_ZOOM_POSITION:
  207. // ask for pan position
  208. nSendCount += PTZ_PELCO::BuildSetZoom( pSendBuffer, GetAddress(), PTZ_PELCO::Percent2Zoom( m_Position.dZoom, PELCO_MAX_OPTICAL_ZOOM ) );
  209. // set reply size
  210. pProtocolPTZ->SetResponseSize( 4 );
  211. break;
  212. case STAGE_GET_PAN_POSITION:
  213. // ask for pan position
  214. nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
  215. // set reply size
  216. pProtocolPTZ->SetResponseSize( 7 );
  217. break;
  218. case STAGE_GET_TILT_POSITION:
  219. // ask for pan position
  220. nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
  221. // set reply size
  222. pProtocolPTZ->SetResponseSize( 7 );
  223. break;
  224. case STAGE_GET_ZOOM_POSITION:
  225. // ask for pan position
  226. nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
  227. // set reply size
  228. pProtocolPTZ->SetResponseSize( 7 );
  229. break;
  230. }
  231. break;
  232. case CMD_GET_PTZ:
  233. switch( m_nStage )
  234. {
  235. case STAGE_GET_PAN_POSITION:
  236. // ask for pan position
  237. nSendCount += PTZ_PELCO::BuildGetPan( pSendBuffer, GetAddress() );
  238. // set reply size
  239. pProtocolPTZ->SetResponseSize( 7 );
  240. break;
  241. case STAGE_GET_TILT_POSITION:
  242. // ask for pan position
  243. nSendCount += PTZ_PELCO::BuildGetTilt( pSendBuffer, GetAddress() );
  244. // set reply size
  245. pProtocolPTZ->SetResponseSize( 7 );
  246. break;
  247. case STAGE_GET_ZOOM_POSITION:
  248. // ask for pan position
  249. nSendCount += PTZ_PELCO::BuildGetZoom( pSendBuffer, GetAddress() );
  250. // set reply size
  251. pProtocolPTZ->SetResponseSize( 7 );
  252. break;
  253. }
  254. break;
  255. /* case CMD_INIT:
  256. // ask for pan position
  257. nSendCount += PTZ_PELCO::BuildMove( pSendBuffer, GetAddress(), m_PtzState );
  258. // set reply size
  259. pProtocolPTZ->SetResponseSize( 4 );
  260. break;
  261. */
  262. case CMD_MOVE_PAN:
  263. // ask for pan position
  264. 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 );
  265. // set reply size
  266. pProtocolPTZ->SetResponseSize( 4 );
  267. break;
  268. case CMD_MOVE_TILT:
  269. 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 );
  270. // set reply size
  271. pProtocolPTZ->SetResponseSize( 4 );
  272. break;
  273. case CMD_MOVE_ZOOM:
  274. 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 );
  275. // set reply size
  276. pProtocolPTZ->SetResponseSize( 4 );
  277. break;
  278. case CMD_NONE:
  279. default:
  280. *pTimeout = 10L;
  281. return FALSE;
  282. }
  283. // write packet
  284. if( !pProtocolPTZ->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) )
  285. return FALSE;
  286. *pTimeout = 250L; // TX + RX
  287. return TRUE;
  288. }
  289. BOOL CLgDevicePTZPelco::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
  290. {
  291. CProtocolPTZPelco *pProtocolPTZ = (CProtocolPTZPelco*)pProtocol;
  292. // read reply
  293. PBYTE pReadBuffer = NULL;
  294. INT nReadCount = 0;
  295. //if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  296. if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  297. {
  298. if( !bLastChance )
  299. return FALSE;
  300. m_nNoReplyCount++;
  301. // if( m_nNoReplyCount > MAX_NO_REPLY_COUNT || ( m_nMode == CMD_GET_PTZ && m_nNoReplyCount > MAX_NOT_IN_POSITION_COUNT ) ) // v_1
  302. if( m_nNoReplyCount > PELCO_MAX_NO_REPLY_COUNT ) // v_2
  303. {
  304. // perform goto idle state
  305. // m_nMode = CMD_INIT; // Commented for BOSH/Pelco
  306. } else // else - Bosh Pelco
  307. return FALSE;
  308. }
  309. // reset retry counter
  310. m_nNoReplyCount = 0;
  311. // switch mode
  312. switch( m_nMode )
  313. {
  314. case CMD_INIT:
  315. switch( m_nStage )
  316. {
  317. case STAGE_INIT_TURN_ON: m_nStage = STAGE_INIT_AUTO_FOCUS; break;
  318. case STAGE_INIT_AUTO_FOCUS: m_nStage = STAGE_INIT_AUTO_IRIS; break;
  319. case STAGE_INIT_AUTO_IRIS: m_nStage = STAGE_INIT_AUTO_GAIN_CONTROL; break;
  320. case STAGE_INIT_AUTO_GAIN_CONTROL: m_nStage = STAGE_INIT_BACKLIGHT_COMPENSATION; break;
  321. case STAGE_INIT_BACKLIGHT_COMPENSATION: m_nStage = STAGE_INIT_WHITE_BALANCE; break;
  322. case STAGE_INIT_WHITE_BALANCE:
  323. OnPerformCmd();
  324. OnEvtInit();
  325. break;
  326. }
  327. break;
  328. case CMD_GET_PTZ:
  329. switch( m_nStage )
  330. {
  331. case STAGE_GET_PAN_POSITION:
  332. m_Position.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ) );
  333. m_nStage = STAGE_GET_TILT_POSITION;
  334. break;
  335. case STAGE_GET_TILT_POSITION:
  336. m_Position.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  337. m_nStage = STAGE_GET_ZOOM_POSITION;
  338. break;
  339. case STAGE_GET_ZOOM_POSITION:
  340. m_Position.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) );
  341. // ready to process new message
  342. OnPerformCmd();
  343. //notify
  344. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  345. break;
  346. }
  347. break;
  348. case CMD_SET_PTZ:
  349. switch( m_nStage )
  350. {
  351. case STAGE_SET_PAN_POSITION: m_nStage = STAGE_SET_TILT_POSITION; break;
  352. case STAGE_SET_TILT_POSITION: m_nStage = STAGE_SET_ZOOM_POSITION; break;
  353. case STAGE_SET_ZOOM_POSITION: /*m_nStage = STAGE_GET_PAN_POSITION; break;
  354. case STAGE_GET_PAN_POSITION:
  355. m_TempPosition.dPan = PTZ_PELCO::Pan2Degree( PTZ_PELCO::Pan2Value( pReadBuffer[4], pReadBuffer[5] ) );
  356. m_nStage = STAGE_GET_TILT_POSITION;
  357. break;
  358. case STAGE_GET_TILT_POSITION:
  359. m_TempPosition.dTilt = PTZ_PELCO::Tilt2Degree( PTZ_PELCO::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) );
  360. m_nStage = STAGE_GET_ZOOM_POSITION;
  361. break;
  362. case STAGE_GET_ZOOM_POSITION:
  363. m_TempPosition.dZoom = PTZ_PELCO::Zoom2Percent( PTZ_PELCO::Zoom2Value( pReadBuffer[4], pReadBuffer[5] ) );
  364. // calculate difference between desired position and current position
  365. if( QualifyPosition( m_TempPosition.dPan - m_Position.dPan,
  366. m_TempPosition.dTilt - m_Position.dTilt,
  367. m_TempPosition.dZoom - m_Position.dZoom,
  368. POSITION_QUALYTY ) ||
  369. m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
  370. {
  371. // store position
  372. m_Position.dPan = m_TempPosition.dPan;
  373. m_Position.dTilt = m_TempPosition.dTilt;
  374. m_Position.dZoom = m_TempPosition.dZoom;
  375. */ // ready to process new message
  376. OnPerformCmd();
  377. //notify
  378. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  379. /* }
  380. else
  381. {
  382. // does not in posiiton
  383. m_nStage = STAGE_SET_PAN_POSITION;
  384. // increate fault counter
  385. m_nNotInPositionCount++;
  386. return TRUE;
  387. }
  388. break;
  389. */ }
  390. break;
  391. case CMD_MOVE_PAN:
  392. OnPerformCmd();
  393. OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
  394. break;
  395. case CMD_MOVE_TILT:
  396. OnPerformCmd();
  397. OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
  398. break;
  399. case CMD_MOVE_ZOOM:
  400. OnPerformCmd();
  401. OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
  402. break;
  403. }
  404. return TRUE;
  405. }