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.

388 lines
11 KiB

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