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.

460 lines
12 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: DMicPTZ.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DMicPTZ.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "..\LgLib\Version.h"
  17. #include "..\LgLib\Device\DMicPTZ.h"
  18. #else
  19. #include "..\Version.h"
  20. #include "DMicPTZ.h"
  21. #endif
  22. #define POSITION_QUALYTY 2
  23. #define MAX_NO_REPLY_COUNT 20
  24. #define MAX_NOT_IN_POSITION_COUNT 10
  25. #define IDLE_DELAY 10L
  26. #define COMMAND_DELAY 50L
  27. #define WRITE_READ_DELAY 250L
  28. CLgDevicePTZMic1300::CLgDevicePTZMic1300()
  29. {
  30. // GetAddress() = 0;
  31. memset( &m_Status, 0, sizeof( m_Status ) );
  32. m_nMode = CMD_NONE;
  33. m_nStage = STAGE_GET_CONFIG_LOW_SPEED;
  34. m_nNoReplyCount = 0;
  35. m_nNotInPositionCount = 0;
  36. m_bCommandPreDelay = TRUE;
  37. }
  38. CLgDevicePTZMic1300::~CLgDevicePTZMic1300()
  39. {
  40. }
  41. BOOL CLgDevicePTZMic1300::CmdSetAddress( BYTE bAddress )
  42. {
  43. // check availibility
  44. if( !DoCmd( CMD_SET_ADDRESS ) )
  45. return FALSE;
  46. // store command data
  47. m_bNewAddress = bAddress;
  48. return TRUE;
  49. }
  50. BOOL CLgDevicePTZMic1300::CmdInit()
  51. {
  52. // check availibility
  53. return DoCmd( CMD_INIT );
  54. }
  55. void CLgDevicePTZMic1300::OnPerformCmd()
  56. {
  57. // ready for new message
  58. ReleaseCmd();
  59. // ready to process new message
  60. m_nMode = CMD_NONE;
  61. }
  62. BOOL CLgDevicePTZMic1300::WriteProc( void *pProtocol, PLONG pTimeout )
  63. {
  64. BYTE nSendCount = 0;
  65. // cast to protocol
  66. CProtocolMic1300PTZ *pProtocolPTZ = (CProtocolMic1300PTZ*)pProtocol;
  67. // set working address
  68. pProtocolPTZ->SetAddress( GetAddress() );
  69. // check input command
  70. if( m_nMode == CMD_NONE )
  71. {
  72. switch( GetCmd() )
  73. {
  74. case CMD_INIT:
  75. m_nMode = CMD_INIT;
  76. m_nStage = STAGE_GET_CONFIG_LOW_SPEED;
  77. break;
  78. case CMD_GET_PTZ:
  79. m_nMode = CMD_GET_PTZ;
  80. break;
  81. case CMD_SET_PTZ:
  82. m_nMode = CMD_SET_PTZ;
  83. m_nStage = STAGE_GOTO_POSITION;
  84. m_nNotInPositionCount = 0;
  85. break;
  86. case CMD_MOVE_TILT:
  87. m_nMode = CMD_MOVE_TILT;
  88. break;
  89. case CMD_MOVE_PAN:
  90. m_nMode = CMD_MOVE_PAN;
  91. break;
  92. case CMD_MOVE_ZOOM:
  93. m_nMode = CMD_MOVE_ZOOM;
  94. break;
  95. case CMD_SET_IRIS:
  96. m_nMode = CMD_SET_IRIS;
  97. m_nStage = STAGE_SET_MANUAL_CONTROL;
  98. break;
  99. case CMD_WIPER:
  100. m_nMode = CMD_WIPER;
  101. break;
  102. case CMD_SET_ADDRESS:
  103. m_nMode = CMD_SET_ADDRESS;
  104. m_nStage = STAGE_SET_ADDRESS1;
  105. break;
  106. case CMD_NONE:
  107. // no new commands
  108. break;
  109. case CMD_SET_FOCUS:
  110. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgDevicePTZMic1300::WriteProc"), _T("CMD_SET_FOCUS is not supported yet") );
  111. default:
  112. //command unsupported
  113. OnPerformCmd();
  114. OnEvtCommandError( m_nMode, _T("UNSUPPORTED") );
  115. break;
  116. }
  117. // set initial pause
  118. m_bCommandPreDelay = TRUE;
  119. }
  120. // fill packet depend on command
  121. switch( m_nMode )
  122. {
  123. case CMD_INIT:
  124. // set initial pause
  125. if( m_bCommandPreDelay )
  126. {
  127. m_bCommandPreDelay = FALSE;
  128. *pTimeout = COMMAND_DELAY;
  129. return FALSE;
  130. }
  131. switch( m_nStage )
  132. {
  133. case STAGE_GET_CONFIG_LOW_SPEED:
  134. // change baudrate to low speed
  135. SetLowBaudrate( TRUE );
  136. nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
  137. break;
  138. case STAGE_SET_CONFIG_LOW_SPEED:
  139. // change baudrate to normal speed
  140. SetLowBaudrate( TRUE );
  141. nSendCount += PTZ_MIC_1_300::BuildSetConfig( m_pSendBuffer, GetAddress(), m_bConfig );
  142. break;
  143. case STAGE_GET_CONFIG_NORMAL_SPEED:
  144. // change baudrate to low speed
  145. SetLowBaudrate( FALSE );
  146. nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
  147. break;
  148. case STAGE_SET_DEFAULT_SETTINGS:
  149. // change baudrate to normal speed
  150. SetLowBaudrate( FALSE );
  151. nSendCount += PTZ_MIC_1_300::BuildSetDefaultSettings( m_pSendBuffer, GetAddress(), &m_Status );
  152. break;
  153. }
  154. break;
  155. case CMD_SET_ADDRESS:
  156. // set initial pause
  157. if( m_bCommandPreDelay )
  158. {
  159. m_bCommandPreDelay = FALSE;
  160. *pTimeout = COMMAND_DELAY;
  161. return FALSE;
  162. }
  163. switch( m_nStage )
  164. {
  165. case STAGE_SET_ADDRESS1:
  166. // send adddress first time
  167. nSendCount += PTZ_MIC_1_300::BuildSetAddress( m_pSendBuffer, GetAddress(), m_bNewAddress );
  168. break;
  169. case STAGE_SET_ADDRESS2:
  170. // send address second time
  171. nSendCount += PTZ_MIC_1_300::BuildSetAddress( m_pSendBuffer, GetAddress(), m_bNewAddress );
  172. SetAddress(m_bNewAddress);
  173. pProtocolPTZ->SetAddress( m_bNewAddress );
  174. break;
  175. case STAGE_GET_POSITION:
  176. // check address settings
  177. nSendCount += PTZ_MIC_1_300::BuildGetConfig( m_pSendBuffer, GetAddress() );
  178. break;
  179. }
  180. break;
  181. case CMD_GET_PTZ:
  182. nSendCount += PTZ_MIC_1_300::BuildGetPosition( m_pSendBuffer, GetAddress() );
  183. break;
  184. case CMD_SET_PTZ:
  185. switch( m_nStage )
  186. {
  187. case STAGE_GOTO_POSITION:
  188. nSendCount += PTZ_MIC_1_300::BuildGotoPosition( m_pSendBuffer, GetAddress(), PTZ_MIC_1_300::Degree2Pan( m_Position.dPan ), PTZ_MIC_1_300::Degree2Tilt( m_Position.dTilt ), PTZ_MIC_1_300::Percent2Zoom( m_Position.dZoom ) );
  189. break;
  190. case STAGE_GET_POSITION:
  191. nSendCount += PTZ_MIC_1_300::BuildGetPosition( m_pSendBuffer, GetAddress() );
  192. break;
  193. }
  194. break;
  195. case CMD_MOVE_PAN:
  196. nSendCount += PTZ_MIC_1_300::BuildPan( m_pSendBuffer, GetAddress(), m_MovePan.bRight, PTZ_MIC_1_300::Percent2Speed(m_MovePan.dSpeed), &m_Status );
  197. break;
  198. case CMD_MOVE_TILT:
  199. nSendCount += PTZ_MIC_1_300::BuildTilt( m_pSendBuffer, GetAddress(), m_MoveTilt.bUp, PTZ_MIC_1_300::Percent2Speed(m_MoveTilt.dSpeed), &m_Status );
  200. break;
  201. case CMD_MOVE_ZOOM:
  202. nSendCount += PTZ_MIC_1_300::BuildZoom( m_pSendBuffer, GetAddress(), m_MoveZoom.bTele, PTZ_MIC_1_300::Percent2Speed(m_MoveZoom.dSpeed), &m_Status );
  203. break;
  204. case CMD_SET_IRIS:
  205. // set initial pause
  206. if( m_bCommandPreDelay )
  207. {
  208. m_bCommandPreDelay = FALSE;
  209. *pTimeout = COMMAND_DELAY;
  210. return FALSE;
  211. }
  212. switch( m_nStage )
  213. {
  214. case STAGE_SET_MANUAL_CONTROL:
  215. m_Status.bLampsOn = m_Iris > 70.0;
  216. nSendCount += PTZ_MIC_1_300::BuildSetManualControl( m_pSendBuffer, GetAddress(), &m_Status );
  217. break;
  218. case STAGE_SET_SLOW_SHUTTER:
  219. nSendCount += PTZ_MIC_1_300::BuildSetSlowShutter( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
  220. break;
  221. case STAGE_SET_BACK_LIGHT:
  222. nSendCount += PTZ_MIC_1_300::BuildSetBackLight( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
  223. break;
  224. case STAGE_SET_LOW_LUX:
  225. nSendCount += PTZ_MIC_1_300::BuildSetLowLux( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
  226. break;
  227. case STAGE_SET_IR_LED:
  228. nSendCount += PTZ_MIC_1_300::BuildSetIRled( m_pSendBuffer, GetAddress(), m_Status.bLampsOn );
  229. break;
  230. }
  231. break;
  232. case CMD_WIPER:
  233. // update wiper state
  234. m_Status.bWiperOn = m_Wiper;
  235. nSendCount += PTZ_MIC_1_300::BuildSetManualControl( m_pSendBuffer, GetAddress(), &m_Status );
  236. break;
  237. case CMD_NONE:
  238. default:
  239. *pTimeout = IDLE_DELAY;
  240. return FALSE;
  241. }
  242. // write packet
  243. if( !pProtocolPTZ->Write( m_pSendBuffer, nSendCount, 1000UL, TRUE ) )
  244. return FALSE;
  245. *pTimeout = WRITE_READ_DELAY; // TX + RX
  246. return TRUE;
  247. }
  248. BOOL CLgDevicePTZMic1300::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance )
  249. {
  250. // cast to protocol
  251. CProtocolMic1300PTZ *pProtocolPTZ = (CProtocolMic1300PTZ*)pProtocol;
  252. // read reply
  253. PBYTE pReadBuffer = NULL;
  254. INT nReadCount = 0;
  255. if( !pProtocolPTZ->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount )
  256. {
  257. if( !bLastChance )
  258. return FALSE;
  259. if( m_nMode == CMD_INIT )
  260. {
  261. switch( m_nStage )
  262. {
  263. case STAGE_GET_CONFIG_LOW_SPEED: m_nStage = STAGE_GET_CONFIG_NORMAL_SPEED; break;
  264. case STAGE_SET_CONFIG_LOW_SPEED: m_nStage = STAGE_GET_CONFIG_NORMAL_SPEED; break;
  265. case STAGE_GET_CONFIG_NORMAL_SPEED: m_nStage = STAGE_GET_CONFIG_LOW_SPEED; break;
  266. case STAGE_SET_DEFAULT_SETTINGS: m_nStage = STAGE_GET_CONFIG_LOW_SPEED; break;
  267. }
  268. }
  269. else
  270. {
  271. m_nNoReplyCount++;
  272. if( m_nNoReplyCount > MAX_NO_REPLY_COUNT )
  273. {
  274. // perform reinitialization
  275. m_nMode = CMD_INIT;
  276. m_nStage = STAGE_GET_CONFIG_LOW_SPEED;
  277. }
  278. }
  279. return FALSE;
  280. }
  281. // reset retry counter
  282. m_nNoReplyCount = 0;
  283. // check control flag
  284. if( pReadBuffer[5] != 'E' &&
  285. pReadBuffer[5] != 'C' )
  286. return TRUE;
  287. // check response result
  288. switch( pReadBuffer[6] )
  289. {
  290. // normal response
  291. case 'G': case 'H': case 'N': case 'O': case 'a': case 'q': case 'w': case 'r': case 's': case '0': break;
  292. // error response
  293. default:
  294. return TRUE;
  295. }
  296. // again pause before new command
  297. m_bCommandPreDelay = TRUE;
  298. PTZ_DEGREE pan, tilt/*, dPan, dTilt*/;
  299. PTZ_PERCENT zoom/*, dZoom*/;
  300. // switch mode
  301. switch( m_nMode )
  302. {
  303. case CMD_INIT:
  304. switch( m_nStage )
  305. {
  306. case STAGE_GET_CONFIG_LOW_SPEED:
  307. // toggle low speed comms
  308. m_bConfig = 0x80;
  309. m_nStage = STAGE_SET_CONFIG_LOW_SPEED;
  310. return TRUE;
  311. case STAGE_SET_CONFIG_LOW_SPEED:
  312. case STAGE_GET_CONFIG_NORMAL_SPEED:
  313. m_nStage = STAGE_SET_DEFAULT_SETTINGS;
  314. return TRUE;
  315. case STAGE_SET_DEFAULT_SETTINGS:
  316. // ready to process new message
  317. OnPerformCmd();
  318. //notify
  319. OnEvtInit();
  320. break;
  321. }
  322. break;
  323. case CMD_SET_ADDRESS:
  324. switch( m_nStage )
  325. {
  326. case STAGE_SET_ADDRESS1:
  327. m_nStage = STAGE_SET_ADDRESS2;
  328. return TRUE;
  329. case STAGE_SET_ADDRESS2:
  330. m_nStage = STAGE_GET_POSITION;
  331. return TRUE;
  332. case STAGE_GET_POSITION:
  333. // switch address
  334. SetAddress( m_bNewAddress );
  335. // ready to process new message
  336. OnPerformCmd();
  337. //notify
  338. OnEvtSetAddress( GetAddress() );
  339. break;
  340. }
  341. break;
  342. case CMD_GET_PTZ:
  343. // store position
  344. m_Position.dPan = PTZ_MIC_1_300::Pan2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 13, 3 ) );
  345. m_Position.dTilt = PTZ_MIC_1_300::Tilt2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 10, 3 ) );
  346. m_Position.dZoom = PTZ_MIC_1_300::Zoom2Percent( PTZ_MIC_1_300::ToValue( pReadBuffer + 16, 3 ) );
  347. // ready to process new message
  348. OnPerformCmd();
  349. //notify
  350. OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  351. break;
  352. case CMD_SET_PTZ:
  353. switch( m_nStage )
  354. {
  355. case STAGE_GOTO_POSITION:
  356. // next step is check position
  357. m_nStage = STAGE_GET_POSITION;
  358. return TRUE;
  359. case STAGE_GET_POSITION:
  360. // calculate difference between desired position and current position
  361. pan = PTZ_MIC_1_300::Pan2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 13, 3 ) );
  362. tilt = PTZ_MIC_1_300::Tilt2Degree( PTZ_MIC_1_300::ToValue( pReadBuffer + 10, 3 ) );
  363. zoom = PTZ_MIC_1_300::Zoom2Percent( PTZ_MIC_1_300::ToValue( pReadBuffer + 16, 3 ) );
  364. if( QualifyPosition( pan - m_Position.dPan,
  365. tilt - m_Position.dTilt,
  366. zoom - m_Position.dZoom,
  367. POSITION_QUALYTY ) ||
  368. m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT )
  369. {
  370. m_Position.dPan = pan;
  371. m_Position.dTilt = tilt;
  372. m_Position.dZoom = zoom;
  373. // ready to process new message
  374. OnPerformCmd();
  375. //notify
  376. OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
  377. }
  378. else
  379. {
  380. // does not in posiiton
  381. m_nStage = STAGE_GOTO_POSITION;
  382. // increate fault counter
  383. m_nNotInPositionCount++;
  384. return TRUE;
  385. }
  386. break;
  387. }
  388. break;
  389. case CMD_MOVE_PAN:
  390. // ready to process new message
  391. OnPerformCmd();
  392. //notify
  393. OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
  394. break;
  395. case CMD_MOVE_TILT:
  396. // ready to process new message
  397. OnPerformCmd();
  398. //notify
  399. OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
  400. break;
  401. case CMD_MOVE_ZOOM:
  402. // ready to process new message
  403. OnPerformCmd();
  404. //notify
  405. OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
  406. break;
  407. case CMD_SET_IRIS:
  408. switch( m_nStage )
  409. {
  410. case STAGE_SET_MANUAL_CONTROL: m_nStage = STAGE_SET_SLOW_SHUTTER; return TRUE;
  411. case STAGE_SET_SLOW_SHUTTER: m_nStage = STAGE_SET_BACK_LIGHT; return TRUE;
  412. case STAGE_SET_BACK_LIGHT: m_nStage = STAGE_SET_LOW_LUX; return TRUE;
  413. case STAGE_SET_LOW_LUX: m_nStage = STAGE_SET_IR_LED; return TRUE;
  414. case STAGE_SET_IR_LED: break;
  415. }
  416. // ready to process new message
  417. OnPerformCmd();
  418. //notify
  419. OnEvtSetIris( m_Iris );
  420. break;
  421. case CMD_WIPER:
  422. // ready to process new message
  423. OnPerformCmd();
  424. //notify
  425. OnEvtWiper( m_Status.bWiperOn );
  426. break;
  427. }
  428. return TRUE;
  429. }