For DOS Protected Mode version of sensor core software
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.

463 lines
12 KiB

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