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.

441 lines
13 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: Laserguard libraries
  7. // SUBSYSTEM: Communication support library
  8. // FILE: PTZPG3AD.h
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of CProtocolPTZPG3AD
  13. //----------------------------------------------------------------------------
  14. #ifndef __PTZ__PHILIPS__G3__AUTO__DOME__INCLUDED__
  15. #define __PTZ__PHILIPS__G3__AUTO__DOME__INCLUDED__
  16. #if _MSC_VER > 1000
  17. #pragma once
  18. #endif
  19. #ifdef __BORLANDC__
  20. #include "..\LgLib\Comm\Connect.h"
  21. #include "..\LgLib\Comm\Protocol.h"
  22. #include "..\LgLib\Device\LgDevPTZ.h"
  23. #else
  24. #include "Connect.h"
  25. #include "Protocol.h"
  26. #include "..\Device\LgDevPTZ.h"
  27. #endif
  28. class CProtocolPTZPG3AD :
  29. public CProtocol
  30. {
  31. public:
  32. CProtocolPTZPG3AD();
  33. virtual ~CProtocolPTZPG3AD();
  34. public:
  35. BOOL Create( CConnection *pConnection );
  36. void Destroy();
  37. void SetAddress( WORD bAddress );
  38. public:
  39. virtual BOOL Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost );
  40. virtual BOOL Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost );
  41. public:
  42. enum {
  43. HEADER_SIZE = 1U, // header + address(2) + length(2)
  44. HEADER_TAG = 0x80U,
  45. LENGTH_POS = 0U,
  46. ADDRESS_POS_0 = 1U,
  47. ADDRESS_POS_1 = 2U,
  48. MIN_COMMAND_SIZE = 5U,
  49. MAX_DATA_LENGTH = 15U, // maximun data size
  50. MAX_COMMAND_SIZE = (MAX_DATA_LENGTH + MIN_COMMAND_SIZE), // header + address(2) + length(2) + ctrlflag + ctrlcode + data(N) + checksumm
  51. };
  52. private:
  53. CConnection*m_pConnection;
  54. BYTE m_pBuffer[MAX_COMMAND_SIZE];
  55. PBYTE m_pBufferPtr;
  56. INT m_nCount;
  57. private:
  58. BYTE m_pAddress[2];
  59. private:
  60. BOOL m_bReadAnyAddress;
  61. };
  62. #ifdef __BCPLUSPLUS__
  63. #pragma warn -inl
  64. #endif
  65. typedef struct PTZ_PHILIPS_G3_AUTODOME
  66. {
  67. typedef enum { PAN_RIGHT = 0x0001, PAN_LEFT = 0x0003, PAN_STOP = 0 } E_PAN_DIR;
  68. typedef enum { TILT_UP = 0x000C, TILT_DOWN = 0x0004, TILT_STOP = 0 } E_TILT_DIR;
  69. typedef enum { ZOOM_TELE = 0x0010, ZOOM_WIDE = 0x0030, ZOOM_STOP = 0 } E_ZOOM_DIR;
  70. typedef enum { IRIS_OPEN = 0x00C0, IRIS_CLOSE = 0x0040, IRIS_STOP = 0 } E_IRIS_DIR;
  71. typedef enum { FOCUS_FAR = 0x0100, FOCUS_NEAR = 0x0300, FOCUS_STOP = 0 } E_FOCUS_DIR;
  72. typedef struct PTZ_STATE
  73. {
  74. BYTE bZoomSpeed;
  75. BYTE bTiltSpeed;
  76. BYTE bPanSpeed;
  77. E_PAN_DIR ePanState;
  78. E_TILT_DIR eTiltState;
  79. E_ZOOM_DIR eZoomState;
  80. E_IRIS_DIR eIrisState;
  81. E_FOCUS_DIR eFocusState;
  82. }PTZ_STATE, *PPTZ_STATE;
  83. static INT BuildGetPosition( LPBYTE pBuffer, WORD bAddress )
  84. {
  85. pBuffer[ 0] = 0x86; // length
  86. pBuffer[ 1] = To14bit( bAddress, 1 ); // address high byte
  87. pBuffer[ 2] = To14bit( bAddress, 0 ); // address low byte
  88. pBuffer[ 3] = 0x0C; // opcode
  89. pBuffer[ 4] = 0x01; // ping type
  90. // pBuffer[ 5] = 0x3F; // requested data type
  91. pBuffer[ 5] = 0x27; // requested data type
  92. return CheckSum( pBuffer, 7 );
  93. }
  94. static INT BuildGotoPosition( LPBYTE pBuffer, WORD bAddress, DWORD dwPan, DWORD dwTilt, BYTE bZoom )
  95. {
  96. pBuffer[ 0] = 0x90; // length
  97. pBuffer[ 1] = To14bit( bAddress, 1 ); // address high byte
  98. pBuffer[ 2] = To14bit( bAddress, 0 ); // address low byte
  99. pBuffer[ 3] = 10; // opcode
  100. pBuffer[ 4] = (BYTE)( dwPan >> 14 ) & 0x07;
  101. pBuffer[ 5] = (BYTE)( dwPan >> 7 ) & 0x7F;
  102. pBuffer[ 6] = (BYTE)( dwPan >> 0 ) & 0x7F;
  103. pBuffer[ 7] = (BYTE)( dwTilt >> 14 ) & 0x01;
  104. pBuffer[ 8] = (BYTE)( dwTilt >> 7 ) & 0x7F;
  105. pBuffer[ 9] = (BYTE)( dwTilt >> 0 ) & 0x7F;
  106. pBuffer[10] = 0x40; // ignore pan velocity & acceleration
  107. pBuffer[11] = 0x40; // ignore tilt velocity & acceleration
  108. pBuffer[12] = 0x10 | 0x01 | ( ( bZoom & 0x01 ) << 2 ); // autofocus + zoom 0 bit + ignore iris
  109. pBuffer[13] = (bZoom >> 1) & 0x7F; // zoom 1..7 bits
  110. pBuffer[14] = 0; // autofocus
  111. pBuffer[15] = 0x7F; // ignore iris
  112. return CheckSum( pBuffer, 17 );
  113. }
  114. static INT BuildPan( LPBYTE pBuffer, WORD bAddress, E_PAN_DIR ePanDir, BYTE bSpeed, PTZ_STATE &PtzState )
  115. {
  116. PtzState.bPanSpeed = bSpeed;
  117. PtzState.ePanState = ePanDir;
  118. return BuildMove( pBuffer, bAddress, PtzState );
  119. }
  120. static INT BuildTilt( LPBYTE pBuffer, WORD bAddress, E_TILT_DIR eTiltDir, BYTE bSpeed, PTZ_STATE &PtzState )
  121. {
  122. PtzState.bTiltSpeed = bSpeed;
  123. PtzState.eTiltState = eTiltDir;
  124. return BuildMove( pBuffer, bAddress, PtzState );
  125. }
  126. static INT BuildZoom( LPBYTE pBuffer, WORD bAddress, E_ZOOM_DIR eZoomDir, BYTE bSpeed, PTZ_STATE &PtzState )
  127. {
  128. PtzState.bZoomSpeed = bSpeed;
  129. PtzState.eZoomState = eZoomDir;
  130. return BuildMove( pBuffer, bAddress, PtzState );
  131. }
  132. static INT BuildMove( LPBYTE pBuffer, WORD bAddress, PTZ_STATE &PtzState )
  133. {
  134. pBuffer[0] = 0x87; // length
  135. pBuffer[1] = To14bit( bAddress, 1 ); // address high byte
  136. pBuffer[2] = To14bit( bAddress, 0 ); // address low byte
  137. pBuffer[3] = 5; // opcode
  138. pBuffer[4] = (PtzState.bZoomSpeed << 4) | PtzState.bTiltSpeed;
  139. BYTE bData2 = 0;
  140. BYTE bData3 = 0;
  141. switch( PtzState.eIrisState )
  142. {
  143. case IRIS_OPEN: bData2 = 0x04; break;
  144. case IRIS_CLOSE: bData2 = 0x02; break;
  145. case IRIS_STOP: break;
  146. }
  147. switch( PtzState.eFocusState )
  148. {
  149. case FOCUS_FAR: bData2 |= 0x01; break;
  150. case FOCUS_NEAR: bData3 = 0x40; break;
  151. case FOCUS_STOP: break;
  152. }
  153. switch( PtzState.eZoomState )
  154. {
  155. case ZOOM_TELE: bData3 |= 0x20; break;
  156. case ZOOM_WIDE: bData3 |= 0x10; break;
  157. case ZOOM_STOP: break;
  158. }
  159. switch( PtzState.eTiltState )
  160. {
  161. case TILT_UP: bData3 |= 0x08; break;
  162. case TILT_DOWN: bData3 |= 0x04; break;
  163. case TILT_STOP: break;
  164. }
  165. switch( PtzState.ePanState )
  166. {
  167. case PAN_LEFT: bData3 |= 0x02; break;
  168. case PAN_RIGHT: bData3 |= 0x01; break;
  169. case PAN_STOP: break;
  170. }
  171. pBuffer[5] = (PtzState.bPanSpeed << 3) | bData2;
  172. pBuffer[6] = bData3;
  173. return CheckSum( pBuffer, 8 );
  174. }
  175. static INT BuildIris( LPBYTE pBuffer, WORD bAddress, BYTE bIris )
  176. {
  177. pBuffer[ 0] = 0x91; // length
  178. pBuffer[ 1] = To14bit( bAddress, 1 ); // address high byte
  179. pBuffer[ 2] = To14bit( bAddress, 0 ); // address low byte
  180. pBuffer[ 3] = 10; // opcode
  181. pBuffer[ 4] = 0x40; // ignore pan
  182. pBuffer[ 5] = 0;
  183. pBuffer[ 6] = 0;
  184. pBuffer[ 7] = 0x40; // ignore tilt
  185. pBuffer[ 8] = 0;
  186. pBuffer[ 9] = 0;
  187. pBuffer[10] = 0x40; // ignore pan velocity & acceleration
  188. pBuffer[11] = 0x40; // ignore tilt velocity & acceleration
  189. pBuffer[12] = 0x10 | ( bIris & 0x01 ) | 0x04; // autofocus + iris 0 bit + ignore zoom bit 0
  190. pBuffer[13] = 0x7F; // zoom
  191. pBuffer[14] = 0; // auto focus
  192. pBuffer[15] = (bIris >> 1) & 0x7F; // iris
  193. pBuffer[16] = 0x0E;//0x0C; // Backlight Comp. Bit & AGC Bit & AutoWhiteBalance
  194. return CheckSum( pBuffer, 18 );
  195. }
  196. static INT CheckSum( LPBYTE pBuffer, INT nLength )
  197. {
  198. INT nCount = nLength - 1;
  199. PBYTE pCheckSum = pBuffer + nCount;
  200. // SUM all bytes in packet
  201. *pCheckSum = *pBuffer++;
  202. while( --nCount )
  203. *pCheckSum += *pBuffer++;
  204. // clear top bit
  205. *pCheckSum &= 0x7F;
  206. return nLength;
  207. }
  208. // conversion
  209. public:
  210. // pan
  211. #define PAN_VALUE_MAX_NEW 128000L // new version
  212. #define PAN_VALUE_MAX_OLD 102400L // old version
  213. static CLgDevicePTZ::PTZ_DEGREE Pan2DegreeNew( DWORD dwPan )
  214. {
  215. return 360L - dwPan * 360L / PAN_VALUE_MAX_NEW;
  216. }
  217. static DWORD Degree2PanNew( CLgDevicePTZ::PTZ_DEGREE Angle )
  218. {
  219. return (DWORD)( ( ( 360L - Angle ) * (PAN_VALUE_MAX_NEW-1) ) / 360L );
  220. }
  221. static CLgDevicePTZ::PTZ_DEGREE Pan2DegreeOld( DWORD dwPan )
  222. {
  223. return 360L - dwPan * 360L / PAN_VALUE_MAX_OLD;
  224. }
  225. static DWORD Degree2PanOld( CLgDevicePTZ::PTZ_DEGREE Angle )
  226. {
  227. return (DWORD)( ( ( 360L - Angle ) * (PAN_VALUE_MAX_OLD-1) ) / 360L );
  228. }
  229. // tilt
  230. static CLgDevicePTZ::PTZ_DEGREE Tilt2Degree( DWORD wTilt )
  231. {
  232. return wTilt * 95L / 27075L - 5L;
  233. }
  234. static DWORD Degree2Tilt( CLgDevicePTZ::PTZ_DEGREE dAngle )
  235. {
  236. return (DWORD)( ( dAngle + 5L ) * 27075L / 95L );
  237. }
  238. // zoom
  239. static BYTE Percent2Zoom( CLgDevicePTZ::PTZ_PERCENT dPercent )
  240. {
  241. return (BYTE)( ( (100L - dPercent) * 0xFE ) / 100L );
  242. }
  243. static CLgDevicePTZ::PTZ_PERCENT Zoom2Percent( BYTE bZoom )
  244. {
  245. return 100L - ( bZoom * 100L / 0xFE );
  246. }
  247. // restrict value
  248. static BOOL ValidatePan( CLgDevicePTZ::PTZ_DEGREE& pan )
  249. {
  250. while( pan < 0 ) pan += 360;
  251. while( pan >= 360 ) pan -= 360;
  252. return TRUE;
  253. }
  254. static BOOL ValidateTilt( CLgDevicePTZ::PTZ_DEGREE& tilt )
  255. {
  256. // restrict tilt within one period
  257. while( tilt < 0 ) tilt += 360;
  258. while( tilt >= 360 ) tilt -= 360;
  259. if( tilt > 90 && tilt < (360-5) )
  260. tilt = ( (tilt-90) < ((360-5)-tilt) ) ? 90 : -5;
  261. else
  262. return TRUE;
  263. return FALSE;
  264. }
  265. static BOOL ValidateZoom( CLgDevicePTZ::PTZ_PERCENT& zoom )
  266. {
  267. if( zoom >= 0 )
  268. {
  269. if( zoom <= 100 ) return TRUE;
  270. else zoom = 100;
  271. }
  272. else zoom = 0;
  273. return FALSE;
  274. }
  275. // iris
  276. static BYTE Percent2Iris( CLgDevicePTZ::PTZ_PERCENT dPercent )
  277. {
  278. return (BYTE)( dPercent * 0x11L / 100L );
  279. }
  280. static CLgDevicePTZ::PTZ_PERCENT Iris2Percent( BYTE bIris )
  281. {
  282. return bIris * 100L / 0x11L;
  283. }
  284. // focus
  285. static BYTE Percent2Focus( CLgDevicePTZ::PTZ_PERCENT dPercent )
  286. {
  287. return (BYTE)( dPercent * 0xFEL / 100L );
  288. }
  289. static CLgDevicePTZ::PTZ_PERCENT Focus2Percent( BYTE bIris )
  290. {
  291. return bIris * 100L / 0xFEL;
  292. }
  293. // pan speed
  294. static BYTE Percent2PanSpeed( CLgDevicePTZ::PTZ_PERCENT dPercent )
  295. {
  296. return (BYTE)( dPercent * 0x0FL / 100L );
  297. }
  298. static CLgDevicePTZ::PTZ_PERCENT PanSpeed2Percent( BYTE bSpeed )
  299. {
  300. return bSpeed * 100L / 0x0FL;
  301. }
  302. // tilt speed
  303. static BYTE Percent2TiltSpeed( CLgDevicePTZ::PTZ_PERCENT dPercent )
  304. {
  305. return (BYTE)( dPercent * 0x0FL / 100L );
  306. }
  307. static CLgDevicePTZ::PTZ_PERCENT TiltSpeed2Percent( BYTE bSpeed )
  308. {
  309. return bSpeed * 100L / 0x0FL;
  310. }
  311. // zoom speed
  312. static BYTE Percent2ZoomSpeed( CLgDevicePTZ::PTZ_PERCENT dPercent )
  313. {
  314. return (BYTE)( dPercent * 0x07L / 100L );
  315. }
  316. static CLgDevicePTZ::PTZ_PERCENT ZoomSpeed2Percent( BYTE bSpeed )
  317. {
  318. return bSpeed * 100L / 0x07L;
  319. }
  320. // values conversion
  321. static BYTE To14bit( WORD wValue, BYTE nDigit )
  322. {
  323. if( nDigit ) return (wValue >> 7) & 0x7F;
  324. return wValue & 0x7F;
  325. }
  326. static DWORD Pan2Value( BYTE b1, BYTE b2, BYTE b3 )
  327. {
  328. return (((DWORD)b1 & 0x07) << 14) | (((DWORD)b2) << 7) | (DWORD)b3;
  329. }
  330. static DWORD Tilt2Value( BYTE b1, BYTE b2, BYTE b3 )
  331. {
  332. return (((DWORD)b1 & 0x01) << 14) | (((DWORD)b2) << 7) | (DWORD)b3;
  333. }
  334. static BYTE Zoom2Value( BYTE b1, BYTE b2 )
  335. {
  336. return ( (b1 >> 2) & 0x01 ) | (b2 << 1);
  337. }
  338. /////////////////////////////////////////// new command ///////////////////////////////////////////////
  339. /*
  340. static INT BuildGetPosition2( LPBYTE pBuffer, WORD bAddress )
  341. {
  342. pBuffer[ 0] = 0x84; // length
  343. pBuffer[ 1] = To14bit( bAddress, 1 ); // address high byte
  344. pBuffer[ 2] = To14bit( bAddress, 0 ); // address low byte
  345. pBuffer[ 3] = 0x13; // opcode
  346. return CheckSum( pBuffer, 5 );
  347. }
  348. static INT BuildGotoPosition2( LPBYTE pBuffer, WORD bAddress, WORD wPan, WORD wTilt, WORD wZoom )
  349. {
  350. pBuffer[ 0] = 0x8B; // length
  351. pBuffer[ 1] = To14bit( bAddress, 1 ); // address high byte
  352. pBuffer[ 2] = To14bit( bAddress, 0 ); // address low byte
  353. pBuffer[ 3] = 0x13; // opcode
  354. pBuffer[ 4] = (BYTE)( wPan >> 7 ) & 0x3F;
  355. pBuffer[ 5] = (BYTE)( wPan ) & 0x7F;
  356. pBuffer[ 6] = (BYTE)( wTilt >> 7 ) & 0x3F;
  357. pBuffer[ 7] = (BYTE)( wTilt ) & 0x7F;
  358. pBuffer[ 8] = (BYTE)( wZoom >> 7 ) & 0x7F;
  359. pBuffer[ 9] = (BYTE)( wZoom ) & 0x7F;
  360. return CheckSum( pBuffer, 11 );
  361. }
  362. static CLgDevicePTZ::PTZ_DEGREE Pan2Degree2( WORD wPan )
  363. {
  364. return 360L - wPan * 360L / 6283L;
  365. }
  366. static WORD Degree2Pan2( CLgDevicePTZ::PTZ_DEGREE dAngle )
  367. {
  368. return ( dAngle * 6283L / 360L ) % 6283;
  369. }
  370. static CLgDevicePTZ::PTZ_DEGREE Tilt2Degree2( WORD wTilt )
  371. {
  372. return 360L - wTilt * 360L / 6283L;
  373. }
  374. static WORD Degree2Tilt2( CLgDevicePTZ::PTZ_DEGREE dAngle )
  375. {
  376. return ( dAngle * 6283L / 360L ) % 6283;
  377. }
  378. static CLgDevicePTZ::PTZ_PERCENT Zoom2Percent2( WORD wZoom )
  379. {
  380. return 50;
  381. }
  382. static WORD Percent2Zoom2( CLgDevicePTZ::PTZ_PERCENT dPercent )
  383. {
  384. return 56;
  385. }
  386. static WORD Pan2Value2( BYTE b1, BYTE b2 )
  387. {
  388. return (((WORD)b1 & 0x3F) << 7) | (WORD)b2;
  389. }
  390. static WORD Tilt2Value2( BYTE b1, BYTE b2 )
  391. {
  392. return (((WORD)b1 & 0x3F) << 7) | (WORD)b2;
  393. }
  394. static WORD Zoom2Value2( BYTE b1, BYTE b2 )
  395. {
  396. return (((WORD)b1 & 0x7F) << 7) | (WORD)b2;
  397. }
  398. */
  399. }PTZ_PHILIPS_G3_AUTODOME;
  400. #ifdef __BCPLUSPLUS__
  401. #pragma warn .inl
  402. #endif
  403. #endif