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.

605 lines
16 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: DevicePTZm.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DevicePTZm.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "DevicePTZm.h"
  16. #include "..\..\LgLib\Comm\Connect.h"
  17. #include "SensorArray.h"
  18. #include "..\..\LgLib\Version.h"
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[]=__FILE__;
  22. #define new DEBUG_NEW
  23. #endif
  24. /*
  25. const CDevicePTZs::COMMAND CDevicePTZs::CMD_AUTO_INIT = { 3, 0, FALSE, FALSE, { { STAGE_GET_ADDRESS, 100, 3, 0 }, { STAGE_SET_ADDRESS, 300, 0, 0 }, { STAGE_GET_PTZ, 100, 3, 0 } } };
  26. const CDevicePTZs::COMMAND CDevicePTZs::CMD_INIT = { 2, 0, FALSE, FALSE, { { STAGE_SET_ADDRESS, 500, 0, 0 }, { STAGE_GET_PTZ, 100, 5, 0 } } };
  27. //const CDevicePTZs::COMMAND CDevicePTZs::CMD_SET_PTZ = { 3, 0, FALSE, FALSE, { { STAGE_SET_PRESET, 0, 0, 0 }, { STAGE_GOTO_PRESET, 50, 0, 0 }, { STAGE_GET_PTZ, 100, 20, 0 } } };
  28. const CDevicePTZs::COMMAND CDevicePTZs::CMD_SET_PTZ = { 3, 0, FALSE, FALSE, { { STAGE_SET_PRESET, 50, 0, 0 }, { STAGE_GOTO_PRESET, 100, 0, 0 }, { STAGE_GET_PTZ, 300, 20, 0 } } };
  29. const CDevicePTZs::COMMAND CDevicePTZs::CMD_GET_PTZ = { 1, 0, FALSE, FALSE, { { STAGE_GET_PTZ, 50, 20, 0 } } };
  30. const CDevicePTZs::COMMAND CDevicePTZs::CMD_PAN = { 1, 0, FALSE, FALSE, { { STAGE_PAN, 0, 1, 0 } } };
  31. const CDevicePTZs::COMMAND CDevicePTZs::CMD_TILT = { 1, 0, FALSE, FALSE, { { STAGE_TILT, 0, 1, 0 } } };
  32. const CDevicePTZs::COMMAND CDevicePTZs::CMD_ZOOM = { 1, 0, FALSE, FALSE, { { STAGE_ZOOM, 0, 1, 0 } } };
  33. */
  34. const CDevicePTZm::COMMAND CDevicePTZm::CMD_AUTO_INIT = { 3, 10, { { SUBCMD_GET_ADDRESS, 100, 3 }, { SUBCMD_SET_ADDRESS, 500, 0 }, { SUBCMD_GET_PTZ, 100, 3 } } };
  35. const CDevicePTZm::COMMAND CDevicePTZm::CMD_INIT = { 2, 10, { { SUBCMD_SET_ADDRESS, 500, 0 }, { SUBCMD_GET_PTZ, 100, 5 } } };
  36. const CDevicePTZm::COMMAND CDevicePTZm::CMD_SET_PTZ = { 3, 0, { { SUBCMD_SET_PRESET, 0, 0 }, { SUBCMD_GOTO_PRESET, 500, 0 }, { SUBCMD_GET_PTZ, 100, 20 } } };
  37. const CDevicePTZm::COMMAND CDevicePTZm::CMD_GET_PTZ = { 1, 0, { { SUBCMD_GET_PTZ, 100, 20 } } };
  38. const CDevicePTZm::COMMAND CDevicePTZm::CMD_PAN = { 1, 0, { { SUBCMD_PAN, 0, 0 } } };
  39. const CDevicePTZm::COMMAND CDevicePTZm::CMD_TILT = { 1, 0, { { SUBCMD_TILT, 0, 0 } } };
  40. const CDevicePTZm::COMMAND CDevicePTZm::CMD_ZOOM = { 1, 0, { { SUBCMD_ZOOM, 0, 0 } } };
  41. const CDevicePTZm::COMMAND CDevicePTZm::CMD_RESET = { 2, 0, { { SUBCMD_REBOOT, 500, 0 }, { SUBCMD_GET_PTZ, 500, 100 } } };
  42. const CDevicePTZm::COMMAND CDevicePTZm::CMD_IRIS = { 2, 0, { { SUBCMD_IRIS, 0, 0 }, { SUBCMD_IRIS, 0, 0 } } };
  43. CDevicePTZm::CDevicePTZm()
  44. {
  45. ::ZeroMemory( m_pHardwareID, sizeof( m_pHardwareID ) );
  46. ::ZeroMemory( &m_Command, sizeof( m_Command ) );
  47. ::ZeroMemory( m_pCommand, sizeof( m_pCommand ) );
  48. m_nAddress = 0;
  49. m_bAutoInit = FALSE;
  50. m_nCommandStage = 0;
  51. m_nCommandRetry = 0;
  52. m_nCommandStage = 0;
  53. m_nStageRetry = 0;
  54. m_bStageResult = FALSE;
  55. m_bStagePause = FALSE;
  56. m_bCommandResult = TRUE;
  57. m_bSetPreset = FALSE;
  58. m_nPresetCount = 0;
  59. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CDevicePTZm::CDevicePTZm") );
  60. }
  61. CDevicePTZm::~CDevicePTZm()
  62. {
  63. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CDevicePTZm::~CDevicePTZm") );
  64. }
  65. DWORD CDevicePTZm::BuildCheckSum(LPBYTE pBuffer)
  66. {
  67. BYTE size = pBuffer[1] + 2;
  68. pBuffer[size] = 0;
  69. for (int i = 0; i < size; i++ )
  70. pBuffer[size] += pBuffer[i];
  71. return (DWORD)(size + 1);
  72. }
  73. BOOL CDevicePTZm::TestCheckSum(LPBYTE pBuffer, DWORD dwCount)
  74. {
  75. if( (DWORD)(pBuffer[1] + 3) == dwCount )
  76. {
  77. BYTE cs = 0;
  78. for( DWORD i = 0; i < dwCount-1; i++ )
  79. cs += pBuffer[ i ];
  80. return (BOOL)( cs == pBuffer[ dwCount-1 ] );
  81. }
  82. return FALSE;
  83. }
  84. DWORD CDevicePTZm::CompleteIO( CConnection *pConnection )
  85. {
  86. // check for demo mode
  87. BOOL bDemo = FALSE;
  88. CSocket *pSocket = CDevice::GetSocket();
  89. if( pSocket )
  90. {
  91. CSensorArray *pSensorArray = pSocket->GetSensorArray();
  92. if( pSensorArray )
  93. bDemo = pSensorArray->IsDemo();
  94. }
  95. // if demo mode to do emulation
  96. if( bDemo )
  97. {
  98. // "process" commands
  99. PTZEVENT event;
  100. while( m_Queue.Pop( event ) )
  101. {
  102. switch( event.id )
  103. {
  104. case PTZC_INIT:
  105. case PTZC_GET_PTZ:
  106. CDevicePTZ::SetPTZ( 0, 0, 0 );
  107. break;
  108. case PTZC_SET_PTZ:
  109. CDevicePTZ::SetPTZ( event.ptz.pan, event.ptz.tilt, event.ptz.zoom );
  110. break;
  111. case PTZC_RESET:
  112. case PTZC_PAN:
  113. case PTZC_TILT:
  114. case PTZC_ZOOM:
  115. case PTZC_IRIS:
  116. break;
  117. }
  118. }
  119. // to prevent processor overheating
  120. Sleep( 10 );
  121. return 0;
  122. }
  123. // to do normal processing
  124. // if command performed
  125. if( m_bCommandResult )
  126. {
  127. m_bSetPreset = FALSE;
  128. PTZEVENT event;
  129. while( m_Queue.Pop( event ) )
  130. {
  131. m_pCommand[ event.id ] = TRUE;
  132. switch( event.id )
  133. {
  134. case PTZC_INIT:
  135. case PTZC_GET_PTZ:
  136. break;
  137. case PTZC_RESET:
  138. // if number of performed presets less 256 then skip reset command
  139. if( m_nPresetCount < 7 )
  140. m_pCommand[ event.id ] = FALSE;
  141. else
  142. m_nPresetCount = 0;
  143. break;
  144. case PTZC_SET_PTZ:
  145. m_DataPtz.pan = ConvertDegreeToPan( event.ptz.pan );
  146. m_DataPtz.tilt = ConvertDegreeToTilt( event.ptz.tilt );
  147. m_DataPtz.zoom = ConvertPercentToZoom( event.ptz.zoom );
  148. break;
  149. case PTZC_PAN:
  150. m_DataPan.nDir = event.move.nDir;
  151. m_DataPan.speed = event.move.speed;
  152. break;
  153. case PTZC_TILT:
  154. m_DataTilt.nDir = event.move.nDir;
  155. m_DataTilt.speed = event.move.speed;
  156. break;
  157. case PTZC_ZOOM:
  158. m_DataZoom.nDir = event.move.nDir;
  159. m_DataZoom.speed = event.move.speed;
  160. break;
  161. case PTZC_IRIS:
  162. m_DataIris.value = ConvertValueToIris( event.iris.value );
  163. break;
  164. }
  165. }
  166. if( m_pCommand[ PTZC_INIT ] )
  167. {
  168. m_pCommand[ PTZC_INIT ] = FALSE;
  169. if( m_bAutoInit ) m_Command = CMD_AUTO_INIT;
  170. else m_Command = CMD_INIT;
  171. }
  172. else
  173. if( m_pCommand[ PTZC_RESET ] )
  174. {
  175. m_pCommand[ PTZC_RESET ] = FALSE;
  176. m_Command = CMD_RESET;
  177. }
  178. else
  179. if( m_pCommand[ PTZC_SET_PTZ ] )
  180. {
  181. m_pCommand[ PTZC_SET_PTZ ] = FALSE;
  182. m_Command = CMD_SET_PTZ;
  183. m_bSetPreset = TRUE;
  184. // calculate count of performed presets
  185. m_nPresetCount ++;
  186. }
  187. else
  188. if( m_pCommand[ PTZC_GET_PTZ ] )
  189. {
  190. m_pCommand[ PTZC_GET_PTZ ] = FALSE;
  191. m_Command = CMD_GET_PTZ;
  192. }
  193. else
  194. if( m_pCommand[ PTZC_PAN ] )
  195. {
  196. m_pCommand[ PTZC_PAN ] = FALSE;
  197. m_Command = CMD_PAN;
  198. }
  199. else
  200. if( m_pCommand[ PTZC_TILT ] )
  201. {
  202. m_pCommand[ PTZC_TILT ] = FALSE;
  203. m_Command = CMD_TILT;
  204. }
  205. else
  206. if( m_pCommand[ PTZC_ZOOM ] )
  207. {
  208. m_pCommand[ PTZC_ZOOM ] = FALSE;
  209. m_Command = CMD_ZOOM;
  210. }
  211. else
  212. if( m_pCommand[ PTZC_IRIS ] )
  213. {
  214. m_pCommand[ PTZC_IRIS ] = FALSE;
  215. m_Command = CMD_IRIS;
  216. }
  217. else
  218. {
  219. return INFINITE; // no command
  220. }
  221. m_nCommandRetry = 0;
  222. m_nCommandStage = 0;
  223. m_bCommandResult = FALSE;
  224. m_nStageRetry = 0;
  225. m_bStageResult = FALSE;
  226. m_bStagePause = FALSE;
  227. }
  228. // check if nesseary to pause
  229. if( m_bStagePause )
  230. {
  231. // perform pause
  232. ::Sleep( m_Command.pStage[ m_nCommandStage ].dwPause );
  233. // next stage begin from command
  234. m_bStagePause = FALSE;
  235. // if sequence performed
  236. if( m_Command.nCount == m_nCommandStage + 1 )
  237. {
  238. // if last stage was successeful
  239. if( m_bStageResult )
  240. {
  241. m_bCommandResult = TRUE;
  242. return INFINITE;
  243. }
  244. // if nessesary to repeat stage sequence
  245. if( m_Command.pStage[m_nCommandStage].nRetry > m_nStageRetry )
  246. m_nStageRetry++;
  247. else
  248. // if nessesary to repeat command sequence
  249. if( m_Command.nRetry > m_nCommandRetry )
  250. {
  251. m_nCommandRetry++;
  252. m_nCommandStage = 0;
  253. m_nStageRetry = 0;
  254. m_bStageResult = FALSE;
  255. }
  256. else
  257. {
  258. m_bCommandResult = TRUE;
  259. return INFINITE;
  260. }
  261. }
  262. else
  263. {
  264. // if last stage was successeful
  265. if( m_bStageResult )
  266. {
  267. // go to next stage
  268. m_nCommandStage++;
  269. m_nStageRetry = 0;
  270. }
  271. else
  272. {
  273. // if nessesary to repeat stage sequence
  274. if( m_Command.pStage[m_nCommandStage].nRetry > m_nStageRetry )
  275. {
  276. m_nStageRetry++;
  277. m_bStageResult = FALSE;
  278. }
  279. else
  280. {
  281. // go to next stage
  282. m_nCommandStage++;
  283. m_nStageRetry = 0;
  284. }
  285. }
  286. }
  287. }
  288. BYTE pBuffer[100];
  289. DWORD dwWriteCount = 0UL;
  290. DWORD dwReadCount = 0UL;
  291. // perform sub command
  292. switch( m_Command.pStage[ m_nCommandStage ].nSubCmd )
  293. {
  294. case SUBCMD_SET_ADDRESS:
  295. pBuffer[ 0] = 0; // address
  296. pBuffer[ 1] = 9; // length
  297. pBuffer[ 2] = 16; // type
  298. pBuffer[ 3] = 4; // command
  299. pBuffer[ 4] = m_pHardwareID[0];
  300. pBuffer[ 5] = m_pHardwareID[1];
  301. pBuffer[ 6] = m_pHardwareID[2];
  302. pBuffer[ 7] = m_pHardwareID[3];
  303. pBuffer[ 8] = m_pHardwareID[4];
  304. pBuffer[ 9] = m_nAddress; // new dome address
  305. pBuffer[10] = 4; // dome type
  306. dwWriteCount = BuildCheckSum( pBuffer );
  307. dwReadCount = 0;
  308. break;
  309. case SUBCMD_GET_ADDRESS:
  310. pBuffer[ 0] = 0; // address
  311. pBuffer[ 1] = 2; // length
  312. pBuffer[ 2] = 16; // type
  313. pBuffer[ 3] = 8; // command
  314. dwWriteCount = BuildCheckSum( pBuffer );
  315. dwReadCount = 13;
  316. break;
  317. case SUBCMD_REBOOT:
  318. pBuffer[ 0] = 0; // address
  319. pBuffer[ 1] = 2; // length
  320. pBuffer[ 2] = 16; // type
  321. pBuffer[ 3] = 16; // command
  322. dwWriteCount = BuildCheckSum( pBuffer );
  323. dwReadCount = 0;
  324. break;
  325. case SUBCMD_SET_PRESET:
  326. pBuffer[ 0] = m_nAddress; // address
  327. pBuffer[ 1] = 11; // length
  328. pBuffer[ 2] = 4; // type
  329. pBuffer[ 3] = 4; // command
  330. pBuffer[ 4] = 1; // preset number
  331. pBuffer[ 5] = m_DataPtz.pan; // pan pos
  332. pBuffer[ 6] = m_DataPtz.tilt; // tilt pos
  333. pBuffer[ 7] = 255; // pan speed
  334. pBuffer[ 8] = 255; // tilt speed
  335. pBuffer[ 9] = 0; // wait time L
  336. pBuffer[10] = 0; // wait time H
  337. pBuffer[11] = LOBYTE( m_DataPtz.zoom ); // zoom pos L
  338. pBuffer[12] = HIBYTE( m_DataPtz.zoom ); // zoom pos H
  339. dwWriteCount = BuildCheckSum( pBuffer );
  340. dwReadCount = 0;
  341. break;
  342. case SUBCMD_GOTO_PRESET:
  343. pBuffer[0] = m_nAddress; // address
  344. pBuffer[1] = 3; // length
  345. pBuffer[2] = 2; // type
  346. pBuffer[3] = 16; // command
  347. pBuffer[4] = 1; // preset number
  348. dwWriteCount = BuildCheckSum( pBuffer );
  349. dwReadCount = 0;
  350. break;
  351. case SUBCMD_GET_PTZ:
  352. pBuffer[0] = m_nAddress; // address
  353. pBuffer[1] = 2; // length
  354. pBuffer[2] = 4; // type
  355. pBuffer[3] = 8; // command
  356. dwWriteCount = BuildCheckSum( pBuffer );
  357. dwReadCount = 9;
  358. break;
  359. case SUBCMD_PAN:
  360. pBuffer[0] = m_nAddress; // address
  361. pBuffer[1] = 3; // length
  362. pBuffer[2] = 2; // type
  363. switch( m_DataPan.nDir )
  364. {
  365. case PAN_LEFT:
  366. pBuffer[3] = 129; // command
  367. pBuffer[4] = ConvertPercentToSpeed( m_DataPan.speed ); // speed
  368. break;
  369. case PAN_RIGHT:
  370. pBuffer[3] = 1; // command
  371. pBuffer[4] = ConvertPercentToSpeed( m_DataPan.speed ); // speed
  372. break;
  373. case PAN_STOP: pBuffer[3] = 1;
  374. pBuffer[3] = 1; // command
  375. pBuffer[4] = 0; // speed
  376. break;
  377. }
  378. dwWriteCount = BuildCheckSum( pBuffer );
  379. dwReadCount = 0;
  380. break;
  381. case SUBCMD_TILT:
  382. pBuffer[0] = m_nAddress; // address
  383. pBuffer[1] = 3; // length
  384. pBuffer[2] = 2; // type
  385. switch( m_DataTilt.nDir )
  386. {
  387. case TILT_UP:
  388. pBuffer[3] = 2; // command
  389. pBuffer[4] = ConvertPercentToSpeed( m_DataTilt.speed ); // speed
  390. break;
  391. case TILT_DOWN:
  392. pBuffer[3] = 130; // command
  393. pBuffer[4] = ConvertPercentToSpeed( m_DataTilt.speed ); // speed
  394. break;
  395. case TILT_STOP:
  396. pBuffer[3] = 2; // command
  397. pBuffer[4] = 0; // speed
  398. break;
  399. }
  400. dwWriteCount = BuildCheckSum( pBuffer );
  401. dwReadCount = 0;
  402. break;
  403. case SUBCMD_ZOOM:
  404. pBuffer[ 0] = m_nAddress; // address
  405. pBuffer[ 1] = 9; // length
  406. pBuffer[ 2] = 1;
  407. pBuffer[ 3] = ':';
  408. pBuffer[ 4] = 'W';
  409. pBuffer[ 5] = 'F';
  410. pBuffer[ 6] = 'C';
  411. pBuffer[ 7] = 'B';
  412. pBuffer[ 8] = 'B';
  413. switch( m_DataZoom.nDir )
  414. {
  415. case ZOOM_TELE:
  416. pBuffer[ 9] = '9';
  417. pBuffer[10] = '9';
  418. break;
  419. case ZOOM_WIDE:
  420. pBuffer[ 9] = '9';
  421. pBuffer[10] = 'B';
  422. break;
  423. case ZOOM_STOP:
  424. pBuffer[ 9] = 'F';
  425. pBuffer[10] = 'E';
  426. break;
  427. }
  428. dwWriteCount = BuildCheckSum( pBuffer );
  429. dwReadCount = 0;
  430. break;
  431. case SUBCMD_IRIS:
  432. static BYTE hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  433. pBuffer[ 0] = m_nAddress; // address
  434. pBuffer[ 1] = 9; // length
  435. pBuffer[ 2] = 1;
  436. pBuffer[ 3] = ':';
  437. pBuffer[ 4] = 'W';
  438. pBuffer[ 5] = 'F';
  439. pBuffer[ 6] = 'D';
  440. pBuffer[ 7] = '9';
  441. pBuffer[ 8] = 'E';
  442. pBuffer[ 9] = hex[(m_DataIris.value & 0xF0) >> 4];
  443. pBuffer[10] = hex[ m_DataIris.value & 0x0F ];
  444. dwWriteCount = BuildCheckSum( pBuffer );
  445. dwReadCount = 0;
  446. break;
  447. }
  448. // write command
  449. BOOL bResult = pConnection->Write( pBuffer, dwWriteCount, 2000UL );
  450. // read reply
  451. if( bResult && dwReadCount )
  452. {
  453. // actual reading
  454. DWORD dwRead = pConnection->Read( pBuffer, dwReadCount, dwReadCount * 10UL );
  455. // test reply
  456. bResult = (dwRead == dwReadCount) ? TestCheckSum( pBuffer, dwReadCount ) : FALSE;
  457. }
  458. // apply answer
  459. if( bResult )
  460. {
  461. // assume that reply is incorrect
  462. bResult = FALSE;
  463. switch( m_Command.pStage[ m_nCommandStage ].nSubCmd )
  464. {
  465. case SUBCMD_GET_PTZ:
  466. if( pBuffer[0] == m_nAddress && pBuffer[1] == 6 && pBuffer[2] == 4 && pBuffer[3] == 9 )
  467. {
  468. if( m_bSetPreset )
  469. {
  470. BYTE pan = pBuffer[4];
  471. BYTE tilt = pBuffer[5];
  472. if( abs( pan - m_DataPtz.pan ) > 5 ||
  473. abs( tilt - m_DataPtz.tilt ) > 5 )
  474. break;
  475. }
  476. // notify itself about position
  477. CDevicePTZ::SetPTZ( ConvertPanToDegree( pBuffer[4] ),
  478. ConvertTiltToDegree( pBuffer[5] ),
  479. ConvertZoomToPercent( MAKEWORD( pBuffer[6], pBuffer[7] ) ) );
  480. bResult = TRUE;
  481. }
  482. break;
  483. case SUBCMD_GET_ADDRESS:
  484. if( pBuffer[1] == 10 && pBuffer[2] == 16 && pBuffer[3] == 1 )
  485. {
  486. // store hardware id
  487. m_pHardwareID[0] = pBuffer[4];
  488. m_pHardwareID[1] = pBuffer[5];
  489. m_pHardwareID[2] = pBuffer[6];
  490. m_pHardwareID[3] = pBuffer[7];
  491. m_pHardwareID[4] = pBuffer[8];
  492. bResult = TRUE;
  493. }
  494. break;
  495. case SUBCMD_SET_ADDRESS:
  496. case SUBCMD_SET_PRESET:
  497. case SUBCMD_GOTO_PRESET:
  498. case SUBCMD_PAN:
  499. case SUBCMD_TILT:
  500. case SUBCMD_ZOOM:
  501. case SUBCMD_REBOOT:
  502. case SUBCMD_IRIS:
  503. bResult = TRUE;
  504. break;
  505. }
  506. }
  507. // remember result of last stage
  508. m_bStageResult = bResult;
  509. // perform pause
  510. m_bStagePause = TRUE;
  511. return 0UL;
  512. }
  513. double CDevicePTZm::ConvertPanToDegree( BYTE Pan )
  514. {
  515. return 360.0 - ((double)Pan) * 360.0 / 256.0;
  516. }
  517. BYTE CDevicePTZm::ConvertDegreeToPan( double Angle )
  518. {
  519. return (BYTE)( ( (360.0 - Angle) * 256.0 ) / 360.0 );
  520. }
  521. double CDevicePTZm::ConvertTiltToDegree( BYTE Tilt )
  522. {
  523. return 90.0 - ((double)Tilt) * 360.0 / 256.0;
  524. }
  525. BYTE CDevicePTZm::ConvertDegreeToTilt( double Angle )
  526. {
  527. return (BYTE)( ( ( 90.0 - Angle ) * 256.0 ) / 360.0 );
  528. }
  529. WORD CDevicePTZm::ConvertPercentToZoom( double percent )
  530. {
  531. const int zr = 0x2178;
  532. int zv = (int)( ((double)zr) * ( 1.0 - percent ) );
  533. if( zv < 0 ) zv = 0;
  534. if( zv > zr ) zv = zr;
  535. return (WORD)(((DWORD)zv) - 0x108C);
  536. }
  537. double CDevicePTZm::ConvertZoomToPercent( WORD zoom )
  538. {
  539. const int zr = 0x2170;
  540. if( HIBYTE(zoom) == 0xFF ) return -1.0;
  541. return 1.0 - ((double)((WORD)(zoom + (WORD)0x10BC))) / ((double)zr);
  542. }
  543. BYTE CDevicePTZm::ConvertPercentToSpeed( double percent )
  544. {
  545. return (BYTE)( percent * 255.0 );
  546. }
  547. BYTE CDevicePTZm::ConvertValueToIris( double value )
  548. {
  549. return (BYTE)( value * 255.0 );
  550. }
  551. double CDevicePTZm::ConvertSpeedToPercent( BYTE speed )
  552. {
  553. return ((double)speed) / 256.0;
  554. }