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.

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