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.

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