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.

298 lines
9.2 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: DPG3AD.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DPG3AD.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Device\DPG3AD.h"
  17. #else
  18. #include "DPG3AD.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. CLgDevicePTZPG3AD::CLgDevicePTZPG3AD()
  25. {
  26. m_nMode = CMD_NONE;
  27. m_nStage = STAGE_GET_POSITION;
  28. m_nNoReplyCount = 0;
  29. m_nNotInPositionCount = 0;
  30. memset( &m_PtzState, 0, sizeof(m_PtzState) );
  31. m_pPan2Degree = PTZ_PHILIPS_G3_AUTODOME::Pan2DegreeNew;
  32. m_pDegree2Pan = PTZ_PHILIPS_G3_AUTODOME::Degree2PanNew;
  33. }
  34. CLgDevicePTZPG3AD::~CLgDevicePTZPG3AD()
  35. {
  36. }
  37. BOOL CLgDevicePTZPG3AD::SetOldType()
  38. {
  39. m_pPan2Degree = PTZ_PHILIPS_G3_AUTODOME::Pan2DegreeOld;
  40. m_pDegree2Pan = PTZ_PHILIPS_G3_AUTODOME::Degree2PanOld;
  41. return TRUE;
  42. }
  43. void CLgDevicePTZPG3AD::OnPerformCmd()
  44. {
  45. // ready for new message
  46. ReleaseCmd();
  47. // ready to process new message
  48. m_nMode = CMD_NONE;
  49. }
  50. BOOL CLgDevicePTZPG3AD::WriteProc( void *pProtocol, PLONG pTimeout )
  51. {
  52. BYTE pSendBuffer[ CProtocolPTZPG3AD::MAX_COMMAND_SIZE ];
  53. BYTE nSendCount = 0;
  54. // cast to protocol
  55. CProtocolPTZPG3AD *pProtocolPTZ = (CProtocolPTZPG3AD*)pProtocol;
  56. // set working address
  57. pProtocolPTZ->SetAddress( GetAddress() );
  58. // check input command
  59. if( m_nMode == CMD_NONE )
  60. {
  61. switch( GetCmd() )
  62. {
  63. case CMD_GET_PTZ:
  64. m_nMode = CMD_GET_PTZ;
  65. break;
  66. case CMD_SET_PTZ:
  67. // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDevicePTZPG3AD::WriteProc"), _T("CMD_SET_PTZ %d,%d,%d"), m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  68. // validate positions
  69. PTZ_PHILIPS_G3_AUTODOME::ValidatePan( m_Position.dPan );
  70. PTZ_PHILIPS_G3_AUTODOME::ValidateTilt( m_Position.dTilt );
  71. PTZ_PHILIPS_G3_AUTODOME::ValidateZoom( m_Position.dZoom );
  72. m_nMode = CMD_SET_PTZ;
  73. m_nStage = STAGE_GOTO_POSITION;
  74. m_nNotInPositionCount = 0;
  75. break;
  76. case CMD_MOVE_TILT:
  77. m_nMode = CMD_MOVE_TILT;
  78. break;
  79. case CMD_MOVE_PAN:
  80. m_nMode = CMD_MOVE_PAN;
  81. break;
  82. case CMD_MOVE_ZOOM:
  83. m_nMode = CMD_MOVE_ZOOM;
  84. break;
  85. case CMD_SET_IRIS:
  86. m_nStage = STAGE_GET_POSITION;
  87. m_nMode = CMD_SET_IRIS;
  88. break;
  89. case CMD_NONE:
  90. // no new commands
  91. break;
  92. case CMD_SET_FOCUS:
  93. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgDevicePTZPG3AD::WriteProc"), _T("CMD_FOCUS is not supported yet") );
  94. case CMD_WIPER:
  95. default:
  96. //command unsupported
  97. OnPerformCmd();
  98. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  99. break;
  100. }
  101. }
  102. BOOL bReadReply = FALSE;
  103. // fill packet depend on command
  104. switch( m_nMode )
  105. {
  106. case CMD_SET_PTZ:
  107. switch( m_nStage )
  108. {
  109. case STAGE_GOTO_POSITION:
  110. // ask for pan position
  111. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGotoPosition( pSendBuffer, GetAddress(),
  112. m_pDegree2Pan( m_Position.dPan ),
  113. PTZ_PHILIPS_G3_AUTODOME::Degree2Tilt( m_Position.dTilt ),
  114. PTZ_PHILIPS_G3_AUTODOME::Percent2Zoom( m_Position.dZoom ) );
  115. break;
  116. case STAGE_GET_POSITION:
  117. // ask for pan position
  118. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() );
  119. // short response
  120. bReadReply = TRUE;
  121. break;
  122. }
  123. break;
  124. case CMD_GET_PTZ:
  125. // ask for pan position
  126. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() );
  127. // short response
  128. bReadReply = TRUE;
  129. break;
  130. case CMD_SET_IRIS:
  131. switch( m_nStage )
  132. {
  133. case STAGE_GET_POSITION:
  134. // ask for zoom position
  135. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() );
  136. // short response
  137. bReadReply = TRUE;
  138. break;
  139. case STAGE_SET_IRIS:
  140. // set iris command
  141. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildIris( pSendBuffer, GetAddress(), PTZ_PHILIPS_G3_AUTODOME::Percent2Iris( m_Iris ) );
  142. break;
  143. }
  144. break;
  145. case CMD_MOVE_PAN:
  146. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildPan( pSendBuffer, GetAddress(), m_MovePan.dSpeed > 0 ? ( m_MovePan.bRight ? PTZ_PHILIPS_G3_AUTODOME::PAN_RIGHT : PTZ_PHILIPS_G3_AUTODOME::PAN_LEFT ) : PTZ_PHILIPS_G3_AUTODOME::PAN_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2PanSpeed( m_MovePan.dSpeed ), m_PtzState );
  147. break;
  148. case CMD_MOVE_TILT:
  149. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildTilt( pSendBuffer, GetAddress(), m_MoveTilt.dSpeed > 0 ? ( m_MoveTilt.bUp ? PTZ_PHILIPS_G3_AUTODOME::TILT_UP : PTZ_PHILIPS_G3_AUTODOME::TILT_DOWN ) : PTZ_PHILIPS_G3_AUTODOME::TILT_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2TiltSpeed( m_MoveTilt.dSpeed ), m_PtzState );
  150. break;
  151. case CMD_MOVE_ZOOM:
  152. nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildZoom( pSendBuffer, GetAddress(), m_MoveZoom.dSpeed > 0 ? ( m_MoveZoom.bTele ? PTZ_PHILIPS_G3_AUTODOME::ZOOM_TELE : PTZ_PHILIPS_G3_AUTODOME::ZOOM_WIDE ) : PTZ_PHILIPS_G3_AUTODOME::ZOOM_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2ZoomSpeed( m_MoveZoom.dSpeed ), m_PtzState );
  153. break;
  154. case CMD_NONE:
  155. default:
  156. *pTimeout = 10L;
  157. return FALSE;
  158. }
  159. // write packet
  160. if( !pProtocolPTZ->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) )
  161. return FALSE;
  162. // if command without reply
  163. if( !bReadReply )
  164. {
  165. INT nMode;
  166. switch( m_nMode )
  167. {
  168. case CMD_SET_PTZ:
  169. m_nStage = STAGE_GET_POSITION;
  170. *pTimeout = DELAY_BEFORE_GET_POSITION;
  171. return FALSE;
  172. case CMD_MOVE_PAN:
  173. case CMD_MOVE_TILT:
  174. case CMD_MOVE_ZOOM:
  175. case CMD_SET_IRIS:
  176. nMode = m_nMode;
  177. OnPerformCmd();
  178. switch( nMode )
  179. {
  180. case CMD_MOVE_PAN: OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break;
  181. case CMD_MOVE_TILT: OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break;
  182. case CMD_MOVE_ZOOM: OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break;
  183. case CMD_SET_IRIS: OnEvtSetIris( m_Iris ); break;
  184. }
  185. return FALSE;
  186. }
  187. return FALSE;
  188. }
  189. *pTimeout = 2000;250L; // TX + RX
  190. return TRUE;
  191. }
  192. BOOL CLgDevicePTZPG3AD::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
  193. {
  194. CProtocolPTZPG3AD *pProtocolPTZ = (CProtocolPTZPG3AD*)pProtocol;
  195. // read reply
  196. PBYTE pReadBuffer = NULL;
  197. INT nReadCount = 0;
  198. if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  199. {
  200. if( !bLastChance )
  201. return FALSE;
  202. m_nNoReplyCount++;
  203. if( m_nNoReplyCount > MAX_NO_REPLY_COUNT )
  204. {
  205. // perform goto idle state
  206. m_nMode = CMD_NONE;
  207. }
  208. return FALSE;
  209. }
  210. // reset retry counter
  211. m_nNoReplyCount = 0;
  212. // switch mode
  213. switch( m_nMode )
  214. {
  215. case CMD_GET_PTZ:
  216. // store position
  217. m_Position.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) );
  218. m_Position.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) );
  219. m_Position.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) );
  220. // ready to process new message
  221. OnPerformCmd();
  222. //notify
  223. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  224. break;
  225. case CMD_SET_PTZ:
  226. m_TempPosition.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) );
  227. m_TempPosition.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) );
  228. m_TempPosition.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) );
  229. // calculate difference between desired position and current position
  230. if( QualifyPosition( m_TempPosition.dPan - m_Position.dPan,
  231. m_TempPosition.dTilt - m_Position.dTilt,
  232. m_TempPosition.dZoom - m_Position.dZoom,
  233. POSITION_QUALYTY ) ||
  234. m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
  235. {
  236. // store position
  237. m_Position.dPan = m_TempPosition.dPan;
  238. m_Position.dTilt = m_TempPosition.dTilt;
  239. m_Position.dZoom = m_TempPosition.dZoom;
  240. // ready to process new message
  241. OnPerformCmd();
  242. //notify
  243. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  244. }
  245. else
  246. {
  247. // does not in posiiton
  248. m_nStage = STAGE_GOTO_POSITION;
  249. // increate fault counter
  250. m_nNotInPositionCount++;
  251. return TRUE;
  252. }
  253. break;
  254. case CMD_SET_IRIS:
  255. // store position
  256. m_Position.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) );
  257. m_Position.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) );
  258. m_Position.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) );
  259. // position have got so can perform set iris
  260. m_nStage = STAGE_SET_IRIS;
  261. return TRUE;
  262. }
  263. return TRUE;
  264. }