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.

1858 lines
58 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: 17/11/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: KERNEL.CPP
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of KERNEL.CPP
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include <stdio.h>
  16. #include "kernel.h"
  17. #include "..\lglib\comm\lgsm.h"
  18. #include "..\lglib\lgbitcod.h"
  19. #include "..\lglib\version.h"
  20. #include "screen.h"
  21. #include "Camera.h"
  22. #include "CMigPTZ.h"
  23. #include "CMicPTZ.h"
  24. #include "CPG3AD.h"
  25. #include "CTKS576.h"
  26. #include "CVicon.h"
  27. #include "CPelco.h"
  28. #include "CHebron.h"
  29. static DWORD far* g_Timer = (DWORD far*)0x46CL;
  30. CHAR CKernel::s_pSoftwareVersion[MAX_PATH] = "";
  31. INT CKernel::s_nSoftwareVersion = 0;
  32. #ifdef INCLUDE_WINDOWS_DEFINITIONS
  33. #define TIMER() *g_Timer
  34. #else
  35. #define TIMER() (GetTickCount() * 10 / 182)
  36. #endif
  37. void CKernel::Reset( BOOL bFullReset )
  38. {
  39. m_Detector[0].Reset( bFullReset );
  40. m_Detector[1].Reset( bFullReset );
  41. m_GetFile.Reset();
  42. #ifdef DEBUG_GRAPH_SCREEN
  43. g_Screen.Clear();
  44. #endif
  45. // reset error timings
  46. memset( m_dwTimeErrorOverflowSST, 0, sizeof(m_dwTimeErrorOverflowSST) );
  47. memset( m_dwTimeErrorOverflow, 0, sizeof(m_dwTimeErrorOverflow) );
  48. memset( m_dwTimeErrorSun, 0, sizeof(m_dwTimeErrorSun) );
  49. m_dwTimePower = 0UL;
  50. // reset board
  51. m_pBoard->Reset();
  52. }
  53. BOOL CKernel::Run()
  54. {
  55. BYTE *pBuffer;
  56. INT nCount;
  57. BYTE nMode = LGS::MODE_DETECTOR;
  58. BOOL bRestart = FALSE;
  59. DWORD nBoardIrq = 0;
  60. DWORD nBoardTimer = TIMER();
  61. BOOL bBoardNoIrq = FALSE;
  62. #ifndef COMPILE_AS_GETFILE
  63. BOOL bFastTimerValid = FALSE;
  64. #endif
  65. BOOL bSetHzDataInProgress = FALSE;
  66. BOOL bSetCalibrationTableInProgress = FALSE;
  67. BOOL bPower, bZero, bSun[2];
  68. INT nRange[2];
  69. #ifdef COMPILE_AS_GETFILE
  70. DWORD dwStartTime = TIMER();
  71. BOOL bGetFileRun = FALSE;
  72. // send human readable information
  73. {
  74. // load user settings
  75. AUTO_RESET AutoReset;
  76. BOARD_SETTINGS BoardSettings;
  77. SWITCHER_SETTINGS SwitcherSettings;
  78. CKernel::LoadUserSettings( &AutoReset, &BoardSettings, &SwitcherSettings );
  79. CHAR pText[MAX_PATH];
  80. INT nText;
  81. nText = sprintf( pText, "SOFTWARE VERSION <%s>\r\n", s_pSoftwareVersion );
  82. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  83. nText = sprintf( pText, "ADDRESS <%d>\r\n", m_nAddress );
  84. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  85. nText = sprintf( pText, "SERIAL NUMBER <%s>\r\n", m_pSerialNumber );
  86. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  87. nText = sprintf( pText, "AUTORESET_ENABLE <%d>\r\n", AutoReset.bEnabled );
  88. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  89. nText = sprintf( pText, "AUTORESET_INTERLEAVE <%ld>\r\n", AutoReset.dwInterleave );
  90. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  91. nText = sprintf( pText, "AUTORESET_TIMEOUT <%ld>\r\n", AutoReset.dwTimeout );
  92. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  93. nText = sprintf( pText, "BOARD_TEMPERATURE <%d>\r\n", (INT)(CHAR)BoardSettings.nTemperature );
  94. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  95. nText = sprintf( pText, "BOARD_ZEROSHIFT <%d>\r\n", BoardSettings.nZeroShift );
  96. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  97. nText = sprintf( pText, "SWITCHER_TABLE <%d,%d,%d>\r\n", SwitcherSettings.pTable[0], SwitcherSettings.pTable[1], SwitcherSettings.pTable[2] );
  98. m_pComm485->Write( (PBYTE)pText, nText, 0 ); m_pComm485->Flush();
  99. }
  100. #endif
  101. // update settings
  102. AUTO_RESET AutoReset;
  103. BOARD_SETTINGS BoardSettings;
  104. SWITCHER_SETTINGS SwitcherSettings;
  105. LoadUserSettings( &AutoReset, &BoardSettings, &SwitcherSettings );
  106. SaveUserSettings( &AutoReset, &BoardSettings, &SwitcherSettings);
  107. while( m_bRun )
  108. {
  109. #ifndef INCLUDE_WINDOWS_DEFINITIONS
  110. Sleep(0);
  111. #endif
  112. #ifdef WATCHDOG_ENABLED
  113. // update watchdog timer
  114. m_WatchDog.Update( TIMER() );
  115. #endif
  116. #ifdef COMPILE_AS_GETFILE
  117. if( !bGetFileRun )
  118. {
  119. if( ( (TIMER()) - dwStartTime ) > LGS_GETFILE_WAIT_TIME )
  120. {
  121. // quit
  122. m_bRun = FALSE;
  123. }
  124. }
  125. #endif
  126. #ifndef COMPILE_AS_GETFILE
  127. // clear zero flag
  128. BOOL bInvalidateCamera = FALSE;
  129. // check fast timer
  130. static DWORD dwSlowTime = 0;
  131. if( !bFastTimerValid )
  132. {
  133. if( TIMER() != dwSlowTime )
  134. {
  135. dwSlowTime = TIMER();
  136. bInvalidateCamera = TRUE;
  137. }
  138. }
  139. #endif
  140. // perform work depend on work mode
  141. switch( nMode )
  142. {
  143. case LGS::MODE_BORDER:
  144. case LGS::MODE_DETECTOR:
  145. // check IRQ
  146. if( ( (TIMER()) - nBoardTimer ) > 18UL )
  147. {
  148. if( !nBoardIrq )
  149. {
  150. bBoardNoIrq = TRUE;
  151. #ifdef DEBUG_BOARD_PRINT_NO_IRQ
  152. printf( "NO IRQ %04x\n", m_pBoard->ReadNewBoard() );
  153. #endif
  154. // reset board
  155. m_pBoard->Reset();
  156. #ifndef COMPILE_AS_GETFILE
  157. // fast timer is not valid
  158. bFastTimerValid = FALSE;
  159. #endif
  160. }
  161. nBoardTimer = TIMER();
  162. nBoardIrq = 0;
  163. }
  164. // check board
  165. if( m_pBoard->GetData( &bZero, bSun, nRange, &bPower ) )
  166. {
  167. // perform linearization
  168. nRange[0] = m_Calibrator[0].Calibrate( nRange[0] );
  169. nRange[1] = m_Calibrator[1].Calibrate( nRange[1] );
  170. // update voltage state
  171. m_Voltage.Invalidate( bPower );
  172. nBoardIrq++;
  173. #ifndef COMPILE_AS_GETFILE
  174. // fast timer is valid
  175. bFastTimerValid = TRUE;
  176. #endif
  177. #ifdef DEBUG_BOARD_SET_TEST_BIT
  178. m_pBoard->SetTestBit( TRUE );
  179. #endif
  180. #ifdef LGS_MIRROR
  181. if( m_bWaitFirstZero && bZero )
  182. {
  183. m_bWaitFirstZero = FALSE;
  184. m_nDetector = 0;
  185. }
  186. if( !m_bWaitFirstZero )
  187. {
  188. if( bZero )
  189. m_nDetector = m_nAngle = 0;
  190. else
  191. {
  192. m_nAngle++;
  193. if( m_nAngle == LGS_MAX_DETECTOR_ANGLES )
  194. {
  195. m_nDetector = 1 - m_nDetector;
  196. m_nAngle = 0;
  197. bZero = TRUE;
  198. }
  199. }
  200. m_Tracker[0].Invalidate();
  201. #ifdef COMM_WITHOUT_INTERRUPTS
  202. m_pComm485->Invalidate();
  203. m_pCameraComm->Invalidate();
  204. #endif
  205. m_Detector[m_nDetector].Invalidate( bZero, bSun[0], nRange[0], TRUE );
  206. }
  207. #else
  208. m_Tracker[0].Invalidate();
  209. #ifdef COMM_WITHOUT_INTERRUPTS
  210. m_pComm485->Invalidate();
  211. m_pCameraComm->Invalidate();
  212. #endif
  213. m_Tracker[1].Invalidate();
  214. #ifdef COMM_WITHOUT_INTERRUPTS
  215. m_pComm485->Invalidate();
  216. m_pCameraComm->Invalidate();
  217. #endif
  218. m_Detector[0].Invalidate( bZero, bSun[0], nRange[0], m_Detector[1].IsAllowFlip() );
  219. #ifdef COMM_WITHOUT_INTERRUPTS
  220. m_pComm485->Invalidate();
  221. m_pCameraComm->Invalidate();
  222. #endif
  223. m_Detector[1].Invalidate( bZero, bSun[1], nRange[1], m_Detector[0].IsAllowFlip() );
  224. #endif
  225. #ifdef DEBUG_BOARD_SET_TEST_BIT
  226. m_pBoard->SetTestBit( FALSE );
  227. #endif
  228. #ifndef COMPILE_AS_GETFILE
  229. bInvalidateCamera = TRUE;
  230. #endif
  231. }
  232. break;
  233. default:
  234. case LGS::MODE_FILESYSTEM:
  235. case LGS::MODE_GETFILE:
  236. if( m_pBoard->GetData( &bZero, bSun, nRange, &bPower ) )
  237. {
  238. // perform linearization
  239. nRange[0] = m_Calibrator[0].Calibrate( nRange[0] );
  240. nRange[1] = m_Calibrator[1].Calibrate( nRange[1] );
  241. // update voltage state
  242. m_Voltage.Invalidate( bPower );
  243. #ifndef COMPILE_AS_GETFILE
  244. bInvalidateCamera = TRUE;
  245. #endif
  246. nBoardIrq++;
  247. #ifndef COMPILE_AS_GETFILE
  248. // fast timer is valid
  249. bFastTimerValid = TRUE;
  250. #endif
  251. }
  252. break;
  253. }
  254. // try to work in windows
  255. #ifndef INCLUDE_WINDOWS_DEFINITIONS
  256. bFastTimerValid = FALSE;
  257. #endif
  258. #ifndef COMPILE_AS_GETFILE
  259. // process camera commands
  260. if( bInvalidateCamera )
  261. {
  262. #ifdef COMM_WITHOUT_INTERRUPTS
  263. m_pComm485->Invalidate();
  264. m_pCameraComm->Invalidate();
  265. #endif
  266. #ifdef DEBUG_BOARD_SET_TEST_BIT_2
  267. m_pBoard->SetTestBit2( TRUE );
  268. #endif
  269. // process camera commands
  270. m_pCamera->Invalidate( TIMER(), m_pBoard->GetTimer(), bFastTimerValid );
  271. #ifdef DEBUG_BOARD_SET_TEST_BIT_2
  272. m_pBoard->SetTestBit2( FALSE );
  273. #endif
  274. }
  275. // process relay commands
  276. m_Relay.Invalidate( m_pBoard->GetTimer(), TIMER() );
  277. #endif
  278. //process communication commands
  279. #ifdef COMM_WITHOUT_INTERRUPTS
  280. m_pComm485->Invalidate();
  281. m_pCameraComm->Invalidate();
  282. #endif
  283. // auto reset
  284. if( m_AutoReset.IsNeedToReset() )
  285. {
  286. bRestart = TRUE;
  287. m_bRun = FALSE;
  288. }
  289. // check for new commands from host
  290. if( m_Protocol.Read( &pBuffer, &nCount, 0, FALSE ) )
  291. {
  292. // check address
  293. BYTE bAddress = 0;
  294. if( nCount && ( (m_nAddress == *pBuffer) || (LGS_EXCLUSIVE_ADDRESS == *pBuffer) ) )
  295. {
  296. // store address
  297. bAddress = *pBuffer;
  298. // update pointers
  299. nCount--;
  300. pBuffer++;
  301. // check remainig count
  302. if( nCount )
  303. {
  304. BYTE pSendBuffer[1024];
  305. PBYTE pSendBufferPtr = pSendBuffer;
  306. int nSendCount = 2;
  307. // set address
  308. *pSendBufferPtr++ = bAddress;
  309. BYTE bHostCommand = *pBuffer++;
  310. // Restart if set hz data was not finished correctly
  311. if( ( bSetHzDataInProgress && bHostCommand != LGS::CMD_SET_HZ_DATA ) ||
  312. ( bSetCalibrationTableInProgress && bHostCommand != LGS::CMD_SET_CALIBRATION_TABLE ) )
  313. {
  314. bRestart = TRUE;
  315. m_bRun = FALSE;
  316. }
  317. // get command from buffer
  318. switch( bHostCommand )
  319. {
  320. #ifndef EMBEDDED_CAMERA_DISABLE
  321. case LGS::CMD_CAMERA:
  322. // build CMD_MODE command
  323. *pSendBufferPtr++ = LGS::CMD_CAMERA;
  324. // process command
  325. m_pCamera->OnProtocolLgsCameraCommand( pBuffer, nCount, pSendBufferPtr, nSendCount );
  326. // write command
  327. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  328. break;
  329. #endif
  330. case LGS::CMD_RELAY:
  331. // build CMD_MODE command
  332. *pSendBufferPtr++ = LGS::CMD_RELAY;
  333. // process command
  334. m_Relay.OnProtocolLgsRelayCommand( pBuffer, nCount, pSendBufferPtr, nSendCount );
  335. // write command
  336. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  337. break;
  338. case LGS::CMD_MODE:
  339. {
  340. // get command parameters
  341. BOOL bSetMode = FALSE;
  342. BYTE nNewMode = nMode;
  343. LGS::GetMode( pBuffer, &bSetMode, &nNewMode );
  344. // build CMD_MODE command
  345. *pSendBufferPtr++ = LGS::CMD_MODE;
  346. nSendCount += LGS::BuildMode( pSendBufferPtr, FALSE, nMode );
  347. // write command
  348. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  349. // if needs to switch mode
  350. if( bSetMode && nMode != nNewMode )
  351. {
  352. switch( nNewMode )
  353. {
  354. case LGS::MODE_GETFILE:
  355. #ifdef COMPILE_AS_GETFILE
  356. bGetFileRun = TRUE;
  357. #endif
  358. break;
  359. case LGS::MODE_BORDER:
  360. case LGS::MODE_DETECTOR:
  361. case LGS::MODE_FILESYSTEM:
  362. m_Detector[0].BorderEnable( nNewMode == LGS::MODE_BORDER );
  363. m_Detector[1].BorderEnable( nNewMode == LGS::MODE_BORDER );
  364. #ifdef COMPILE_AS_GETFILE
  365. bGetFileRun = FALSE;
  366. dwStartTime = TIMER();
  367. #endif
  368. break;
  369. #ifdef DEBUG_ENABLE_SEND_BOARDDATA
  370. case LGS::MODE_SENDBOARDDATA1:
  371. case LGS::MODE_SENDBOARDDATA2:
  372. DoSendBoardData( nNewMode - LGS::MODE_SENDBOARDDATA1 );
  373. break;
  374. #endif
  375. }
  376. // reset after switching mode
  377. if( nMode != LGS::MODE_BORDER && nMode != LGS::MODE_DETECTOR )
  378. Reset( TRUE );
  379. // store new mode
  380. nMode = nNewMode;
  381. // reset IRQ checking
  382. nBoardIrq = 0;
  383. nBoardTimer = TIMER();
  384. }
  385. }
  386. break;
  387. case LGS::CMD_BORDER:
  388. {
  389. // get command parameters
  390. BYTE bError;
  391. INT nAngle, nRange0, nRange1;
  392. LGS::GetBorder( pBuffer, &bError, &nAngle, &nRange0, &nRange1 );
  393. // build CMD_BORDER command
  394. *pSendBufferPtr++ = LGS::CMD_BORDER;
  395. if( nMode != LGS::MODE_BORDER )
  396. nSendCount += LGS::BuildBorder( pSendBufferPtr, LGS::BD_ERRORMODE, 0, 0, 0 );
  397. else
  398. {
  399. BYTE bError = ( (!m_Detector[0].GetBorderRange( nAngle, &nRange0 )) ||
  400. (!m_Detector[1].GetBorderRange( nAngle, &nRange1 )) ) ? LGS::BD_NOTREADY : 0;
  401. if( bError )
  402. nSendCount += LGS::BuildBorder( pSendBufferPtr, bError, 0, 0, 0 );
  403. else
  404. nSendCount += LGS::BuildBorder( pSendBufferPtr, 0, nAngle, nRange0 >> 1, nRange1 >> 1 );
  405. }
  406. // write command
  407. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  408. }
  409. break;
  410. case LGS::CMD_STATE:
  411. {
  412. // build CMD_STATE command
  413. *pSendBufferPtr++ = LGS::CMD_STATE;
  414. if( nMode != LGS::MODE_DETECTOR )
  415. nSendCount += LGS::BuildError( pSendBufferPtr, 0, LGS::ER_MODE, 0 );
  416. else
  417. {
  418. for( int i = 0; i < 2; i++ )
  419. {
  420. CDetector *pDetector = m_Detector + i;
  421. #ifdef LGS_MIRROR
  422. CTracker *pTracker = m_Tracker + 0;
  423. #else
  424. CTracker *pTracker = m_Tracker + i;
  425. #endif
  426. BYTE bDevice = i ? LGS::ID_DEVICE1 : LGS::ID_DEVICE0;
  427. // assume that no errors
  428. BYTE bErrorMask = LGS::ER_NONE;
  429. INT nErrorAngle = 0;
  430. // check for tracker overflow
  431. if( ( m_dwTimeErrorOverflow[i] < TIMER() ) && pTracker->GetLastErrorOverflow() )
  432. {
  433. m_dwTimeErrorOverflow[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  434. bErrorMask |= LGS::ER_OVERFLOW;
  435. }
  436. // check for tracker scan to scan overflow
  437. if( ( m_dwTimeErrorOverflowSST[i] < TIMER() ) && pTracker->GetLastErrorOverflowSST() )
  438. {
  439. m_dwTimeErrorOverflowSST[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  440. bErrorMask |= LGS::ER_SSTOVERFLOW;
  441. }
  442. // check for detector sun errors
  443. if( ( m_dwTimeErrorSun[i] < TIMER() ) && pDetector->GetLastErrorSun() )
  444. {
  445. m_dwTimeErrorSun[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  446. bErrorMask |= LGS::ER_SUNBIT;
  447. }
  448. // check for detector angle errors
  449. if( pDetector->GetLastErrorAngle() )
  450. {
  451. bErrorMask |= LGS::ER_ANGLE;
  452. nErrorAngle = pDetector->GetLastErrorAngleValue();
  453. }
  454. // check for kernel error
  455. if( bBoardNoIrq )
  456. {
  457. bErrorMask |= LGS::ER_NOIRQ;
  458. bBoardNoIrq = FALSE;
  459. }
  460. // if error exist
  461. if( bErrorMask )
  462. nSendCount += LGS::BuildError( pSendBufferPtr, bDevice, bErrorMask, nErrorAngle );
  463. // place target information
  464. CTracker::TARGET target;
  465. while( pTracker->GetTarget( &target ) )
  466. {
  467. #ifdef LGS_MIRROR
  468. #ifdef LGS_TEST_SEND_TARGET_WIDTH
  469. nSendCount += LGS::BuildTarget( pSendBufferPtr, bDevice, target.bType, target.nAngle << 1, target.nRange, target.nWidth );
  470. #else
  471. nSendCount += LGS::BuildTarget( pSendBufferPtr, bDevice, target.bType, target.nAngle << 1, target.nRange );
  472. #endif
  473. #else
  474. #ifdef LGS_TEST_SEND_TARGET_WIDTH
  475. nSendCount += LGS::BuildTarget( pSendBufferPtr, bDevice, target.bType, target.nAngle, target.nRange, target.nWidth );
  476. #else
  477. nSendCount += LGS::BuildTarget( pSendBufferPtr, bDevice, target.bType, target.nAngle, target.nRange );
  478. #endif
  479. #endif
  480. if( nSendCount >= 20 )
  481. break;
  482. }
  483. if( nSendCount >= 20 )
  484. break;
  485. }
  486. }
  487. #ifndef EMBEDDED_CAMERA_DISABLE
  488. // add camera tags
  489. m_pCamera->OnProtocolLgsStateCommand( pSendBufferPtr, nSendCount );
  490. #endif
  491. // write command
  492. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  493. }
  494. break;
  495. case LGS::CMD_STATE_2:
  496. // prepare response
  497. OnCmdState2( pBuffer, nCount, pSendBufferPtr, nSendCount, nMode, bBoardNoIrq );
  498. // write command
  499. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  500. break;
  501. case LGS::CMD_GET_WIDTH_TABLE:
  502. // prepare response
  503. OnCmdGetWidthTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  504. // write command
  505. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  506. break;
  507. case LGS::CMD_SET_WIDTH_TABLE:
  508. // prepare response
  509. OnCmdSetWidthTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  510. // write command
  511. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  512. break;
  513. case LGS::CMD_GET_ZOOM_TABLE:
  514. // prepare response
  515. OnCmdGetZoomTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  516. // write command
  517. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  518. break;
  519. case LGS::CMD_SET_ZOOM_TABLE:
  520. // prepare response
  521. OnCmdSetZoomTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  522. // write command
  523. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  524. // update camera settings
  525. m_pCamera->SaveZoomTable();
  526. // reset after file operation
  527. Reset( TRUE );
  528. // reset IRQ checking
  529. nBoardIrq = 0;
  530. nBoardTimer = TIMER();
  531. break;
  532. case LGS::CMD_GET_TILT_TABLE:
  533. // prepare response
  534. OnCmdGetTiltTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  535. // write command
  536. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  537. break;
  538. case LGS::CMD_SET_TILT_TABLE:
  539. // prepare response
  540. OnCmdSetTiltTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  541. // write command
  542. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  543. // update camera settings
  544. m_pCamera->SaveTiltTable();
  545. // reset after file operation
  546. Reset( TRUE );
  547. // reset IRQ checking
  548. nBoardIrq = 0;
  549. nBoardTimer = TIMER();
  550. break;
  551. case LGS::CMD_GET_LRF_DISABLE:
  552. // prepare response
  553. OnCmdGetLrfDisable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  554. // write command
  555. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  556. break;
  557. case LGS::CMD_SET_LRF_DISABLE:
  558. // prepare response
  559. OnCmdSetLrfDisable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  560. // write command
  561. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  562. // reset after file operation
  563. Reset( TRUE );
  564. // reset IRQ checking
  565. nBoardIrq = 0;
  566. nBoardTimer = TIMER();
  567. break;
  568. case LGS::CMD_GET_HZ_DATA:
  569. m_pShadow->OnCmdGetHzData( pBuffer, nCount, pSendBufferPtr, nSendCount );
  570. // write command
  571. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  572. break;
  573. case LGS::CMD_SET_HZ_DATA:
  574. {
  575. BOOL bPendingSave = m_pShadow->OnCmdSetHzData( pBuffer, nCount, pSendBufferPtr, nSendCount );
  576. // write command
  577. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  578. // save
  579. if( bPendingSave )
  580. {
  581. // save data
  582. m_pShadow->Save();
  583. // reset after file operation
  584. Reset( TRUE );
  585. // reset IRQ checking
  586. nBoardIrq = 0;
  587. nBoardTimer = TIMER();
  588. #ifdef LGS_MIRROR
  589. bRestart = TRUE;
  590. m_bRun = FALSE;
  591. #endif
  592. }
  593. }
  594. break;
  595. case LGS::CMD_GET_HZ_ENABLE:
  596. m_pShadow->OnCmdGetHzEnable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  597. // write command
  598. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  599. break;
  600. case LGS::CMD_SET_HZ_ENABLE:
  601. {
  602. BOOL bPendingSave = m_pShadow->OnCmdSetHzEnable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  603. // write command
  604. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  605. // save
  606. if( bPendingSave )
  607. {
  608. // save data
  609. m_pShadow->Save();
  610. // reset after file operation
  611. Reset( TRUE );
  612. // reset IRQ checking
  613. nBoardIrq = 0;
  614. nBoardTimer = TIMER();
  615. // set data finished correctly
  616. bSetHzDataInProgress = FALSE;
  617. }
  618. else
  619. bSetHzDataInProgress = TRUE;
  620. }
  621. break;
  622. case LGS::CMD_GET_CALIBRATION_TABLE:
  623. OnCmdGetCalibrationTable( pBuffer, nCount, pSendBufferPtr, nSendCount );
  624. // write command
  625. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  626. break;
  627. case LGS::CMD_SET_CALIBRATION_TABLE:
  628. {
  629. INT nDevice = 0;
  630. BOOL bPendingSave = OnCmdSetCalibrationTable( pBuffer, nCount, pSendBufferPtr, nSendCount, nDevice );
  631. // write command
  632. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  633. // save
  634. if( bPendingSave )
  635. {
  636. // save data
  637. switch( nDevice )
  638. {
  639. case 0: m_Calibrator[nDevice].Save( "lt0.ini" ); break;
  640. case 1: m_Calibrator[nDevice].Save( "lt1.ini" ); break;
  641. }
  642. // reset after file operation
  643. Reset( TRUE );
  644. // reset IRQ checking
  645. nBoardIrq = 0;
  646. nBoardTimer = TIMER();
  647. // set data finished correctly
  648. bSetCalibrationTableInProgress = FALSE;
  649. }
  650. else
  651. bSetCalibrationTableInProgress = TRUE;
  652. }
  653. break;
  654. case LGS::CMD_QUIT:
  655. *pSendBufferPtr++ = LGS::CMD_QUIT;
  656. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  657. m_bRun = FALSE;
  658. break;
  659. case LGS::CMD_RESTART:
  660. *pSendBufferPtr++ = LGS::CMD_RESTART;
  661. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  662. bRestart = TRUE;
  663. m_bRun = FALSE;
  664. break;
  665. case LGS::CMD_GET_SERIAL_NUMBER:
  666. // reply command
  667. *pSendBufferPtr++ = LGS::CMD_GET_SERIAL_NUMBER;
  668. // reply data
  669. nSendCount += LGS::BuildSerialNumber( pSendBufferPtr, m_nAddress, m_nSerialNumber, m_pSerialNumber );
  670. // write into port
  671. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  672. break;
  673. case LGS::CMD_GET_SOFTWARE_VERSION:
  674. // reply command
  675. *pSendBufferPtr++ = LGS::CMD_GET_SOFTWARE_VERSION;
  676. // reply data
  677. nSendCount += LGS::BuildSoftwareVersion( pSendBufferPtr, (PBYTE)s_pSoftwareVersion, s_nSoftwareVersion );
  678. // write into port
  679. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  680. break;
  681. case LGS::CMD_GET_VOLTAGE:
  682. // reply command
  683. *pSendBufferPtr++ = LGS::CMD_GET_VOLTAGE;
  684. // reply data
  685. nSendCount += LGS::BuildEvtVoltage( pSendBufferPtr, m_Voltage.GetVoltage() );
  686. // write into port
  687. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  688. break;
  689. #ifndef EMBEDDED_CAMERA_DISABLE
  690. case LGS::CMD_GET_CAMERA_PARAMETERS:
  691. {
  692. // reply command
  693. *pSendBufferPtr++ = LGS::CMD_GET_CAMERA_PARAMETERS;
  694. // reply data
  695. nSendCount += LGS::BuildCameraParameters( pSendBufferPtr, &m_pCamera->GetParameters() );
  696. // write into port
  697. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  698. }
  699. break;
  700. case LGS::CMD_SET_CAMERA_PARAMETERS:
  701. {
  702. LGS::CAMERA_PARAMETERS cp;
  703. LGS::GetCameraParameters( pBuffer, &cp );
  704. // reply command
  705. *pSendBufferPtr++ = LGS::CMD_SET_CAMERA_PARAMETERS;
  706. // reply data
  707. nSendCount += LGS::BuildCameraParameters( pSendBufferPtr, &cp );
  708. // write into port
  709. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  710. // store serial number
  711. SaveSettings( (PCHAR)m_pSerialNumber, m_nAddress, &cp, &m_Relay.GetParameters(), &m_Voltage.GetParameters(), &m_LRFDisable );
  712. // restart program
  713. m_bRun = FALSE;
  714. }
  715. break;
  716. #endif
  717. case LGS::CMD_GET_VOLTAGE_PARAMETERS:
  718. {
  719. // reply command
  720. *pSendBufferPtr++ = LGS::CMD_GET_VOLTAGE_PARAMETERS;
  721. // reply data
  722. nSendCount += LGS::BuildVoltageParameters( pSendBufferPtr, &m_Voltage.GetParameters() );
  723. // write into port
  724. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  725. }
  726. break;
  727. case LGS::CMD_SET_VOLTAGE_PARAMETERS:
  728. {
  729. LGS::VOLTAGE_PARAMETERS vp;
  730. // store voltage parameters
  731. LGS::GetVoltageParameters( pBuffer, &vp );
  732. // reply command
  733. *pSendBufferPtr++ = LGS::CMD_SET_VOLTAGE_PARAMETERS;
  734. // reply data
  735. nSendCount += LGS::BuildVoltageParameters( pSendBufferPtr, &vp );
  736. // write into port
  737. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  738. // store serial number
  739. SaveSettings( (PCHAR)m_pSerialNumber, m_nAddress, &m_pCamera->GetParameters(), &m_Relay.GetParameters(), &vp, &m_LRFDisable );
  740. // update voltage prameters
  741. m_Voltage.SetParameters( &vp );
  742. // reset after file operation
  743. Reset( TRUE );
  744. // reset IRQ checking
  745. nBoardIrq = 0;
  746. nBoardTimer = TIMER();
  747. }
  748. break;
  749. case LGS::CMD_GET_RELAY_PARAMETERS:
  750. {
  751. // reply command
  752. *pSendBufferPtr++ = LGS::CMD_GET_RELAY_PARAMETERS;
  753. // reply data
  754. nSendCount += LGS::BuildRelayParameters( pSendBufferPtr, &m_Relay.GetParameters() );
  755. // write into port
  756. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  757. }
  758. break;
  759. case LGS::CMD_SET_RELAY_PARAMETERS:
  760. {
  761. LGS::RELAY_PARAMETERS rp;
  762. LGS::GetRelayParameters( pBuffer, &rp );
  763. // reply command
  764. *pSendBufferPtr++ = LGS::CMD_SET_RELAY_PARAMETERS;
  765. // reply data
  766. nSendCount += LGS::BuildRelayParameters( pSendBufferPtr, &rp );
  767. // write into port
  768. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  769. // store serial number
  770. SaveSettings( (PCHAR)m_pSerialNumber, m_nAddress, &m_pCamera->GetParameters(), &rp, &m_Voltage.GetParameters(), &m_LRFDisable );
  771. // update relay parameters
  772. m_Relay.SetParameters( &rp );
  773. // reset after file operation
  774. Reset( TRUE );
  775. // reset IRQ checking
  776. nBoardIrq = 0;
  777. nBoardTimer = TIMER();
  778. }
  779. break;
  780. case LGS::CMD_SET_ADDRESS:
  781. {
  782. // reply command
  783. *pSendBufferPtr++ = LGS::CMD_SET_ADDRESS;
  784. // parse parameters
  785. BYTE pSerialNumber[MAX_PATH];
  786. BYTE nAddress, nCount;
  787. LGS::GetAddress( pBuffer, &nAddress, &nCount, pSerialNumber );
  788. // only if serial number is equal
  789. if( (nCount == m_nSerialNumber) && !memcmp( pSerialNumber, m_pSerialNumber, m_nSerialNumber ) )
  790. {
  791. // change address
  792. m_nAddress = nAddress;
  793. // prepare answer message
  794. nSendCount += LGS::BuildAddress( pSendBufferPtr, m_nAddress, m_nSerialNumber, m_pSerialNumber );
  795. // send reply
  796. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  797. // store serial number
  798. SaveSettings( (PCHAR)m_pSerialNumber, m_nAddress, &m_pCamera->GetParameters(), &m_Relay.GetParameters(), &m_Voltage.GetParameters(), &m_LRFDisable );
  799. // reset after file operation
  800. Reset( TRUE );
  801. // reset IRQ checking
  802. nBoardIrq = 0;
  803. nBoardTimer = TIMER();
  804. }
  805. }
  806. break;
  807. case LGS::CMD_SET_COMM_SPEED:
  808. {
  809. // reply command
  810. *pSendBufferPtr++ = LGS::CMD_SET_COMM_SPEED;
  811. // parse parameters
  812. LONG nSpeed;
  813. LGS::GetCommSpeed( pBuffer, &nSpeed );
  814. // prepare answer message
  815. nSendCount += LGS::BuildCommSpeed( pSendBufferPtr, nSpeed );
  816. // send reply
  817. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  818. // reinitialize communication if nessessary
  819. #ifndef COMPILE_AS_GETFILE
  820. if( m_pComm485->GetBaudRate() != nSpeed )
  821. #endif
  822. {
  823. if( !m_pComm485->SetParameters( nSpeed, 8L, CComm485::_NOPARITY ) )
  824. return FALSE;
  825. // store new comm speed
  826. FILE *pFile = fopen( "SERIAL.INI", "wt" );
  827. if( pFile )
  828. {
  829. fprintf( pFile, "%ld\n", nSpeed );
  830. fclose( pFile );
  831. }
  832. // reset after file operation
  833. Reset( TRUE );
  834. // reset IRQ checking
  835. nBoardIrq = 0;
  836. nBoardTimer = TIMER();
  837. }
  838. }
  839. break;
  840. case LGS::CMD_PARAMETERS:
  841. {
  842. // reply command
  843. *pSendBufferPtr++ = LGS::CMD_PARAMETERS;
  844. // parse input command
  845. BYTE bSet, nDevice;
  846. LGS::LGS_PARAMETERS parameters;
  847. LGS::GetParameters( pBuffer, &bSet, &nDevice, &parameters );
  848. // return parameters
  849. if( bSet )
  850. {
  851. m_Detector[ nDevice ].SetParameters( &parameters );
  852. nSendCount += LGS::BuildCmdSetParameters( pSendBufferPtr, nDevice, &parameters );
  853. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  854. // reset after file operation
  855. Reset( TRUE );
  856. // reset IRQ checking
  857. nBoardIrq = 0;
  858. nBoardTimer = TIMER();
  859. }
  860. else
  861. {
  862. m_Detector[ nDevice ].GetParameters( &parameters );
  863. nSendCount += LGS::BuildCmdSetParameters( pSendBufferPtr, nDevice, &parameters );
  864. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  865. }
  866. }
  867. break;
  868. case LGS::CMD_RESET:
  869. *pSendBufferPtr++ = LGS::CMD_RESET;
  870. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  871. nMode = LGS::MODE_DETECTOR;
  872. Reset( FALSE );
  873. // reset IRQ checking
  874. nBoardIrq = 0;
  875. nBoardTimer = TIMER();
  876. #ifdef LGS_MIRROR
  877. bRestart = TRUE;
  878. m_bRun = FALSE;
  879. #endif
  880. break;
  881. case LGS::CMD_FULL_RESET:
  882. *pSendBufferPtr++ = LGS::CMD_FULL_RESET;
  883. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  884. nMode = LGS::MODE_DETECTOR;
  885. Reset( TRUE );
  886. // reset IRQ checking
  887. nBoardIrq = 0;
  888. nBoardTimer = TIMER();
  889. #ifdef LGS_MIRROR
  890. bRestart = TRUE;
  891. m_bRun = FALSE;
  892. #endif
  893. break;
  894. case LGS::CMD_FULL_BORDER:
  895. {
  896. *pSendBufferPtr++ = LGS::CMD_FULL_BORDER;
  897. // read command data
  898. INT nAngle;
  899. LGS::GetCmdFullBorder( pBuffer, &nAngle );
  900. // prepare answer
  901. INT nRange1 = 0;
  902. INT nRange2 = 0;
  903. BOOL bReady = m_Detector[1].GetFullBorderRange( nAngle, &nRange2 ) &&
  904. m_Detector[0].GetFullBorderRange( nAngle, &nRange1 );
  905. if( bReady )
  906. nSendCount += LGS::BuildEvtFullBorder( pSendBufferPtr, TRUE, nAngle, nRange1, nRange2 );
  907. else
  908. // use percent from detector 2 because it always comes later
  909. nSendCount += LGS::BuildEvtFullBorder( pSendBufferPtr, FALSE, nRange2, 0, 0 );
  910. // write back
  911. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  912. }
  913. break;
  914. case LGS::CMD_GETFILE:
  915. {
  916. // build CMD_GETFILE command
  917. *pSendBufferPtr++ = LGS::CMD_GETFILE;
  918. if( nMode != LGS::MODE_GETFILE )
  919. {
  920. nSendCount += LGS::BuildGetFile( pSendBufferPtr, LGS::GF_ERRORMODE, 0, 0, 0 );
  921. }
  922. else
  923. {
  924. // get command parameters
  925. BYTE bError, bOperation, pData[100];
  926. INT nData;
  927. LGS::GetGetFile( pBuffer, &bError, &bOperation, pData, &nData );
  928. // perform command
  929. if( m_GetFile.OnCommand( bOperation, pData, nData ) )
  930. nSendCount += LGS::BuildGetFile( pSendBufferPtr, 0, ( bOperation & LGS::GF_OP_INDEX ) | LGS::GF_OP_OK, NULL, 0 );
  931. else
  932. nSendCount += LGS::BuildGetFile( pSendBufferPtr, LGS::GF_ERRORFILE, bOperation, NULL, 0 );
  933. }
  934. // write unswer
  935. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  936. }
  937. break;
  938. #ifndef LGS_DISABLE_FILE_SYSTEM
  939. case LGS::CMD_FS_SUB_PACKET:
  940. {
  941. // get incoming sub packet
  942. LGS::FS_DATA_EX InFsData;
  943. LGS::GetFSSubPacket( pBuffer, &InFsData );
  944. switch( InFsData.nCommand )
  945. {
  946. case LGS::FS_CMD_SEND_CMD:
  947. // build CMD_GETFILE command
  948. *pSendBufferPtr++ = LGS::CMD_FS_SUB_PACKET;
  949. if( nMode != LGS::MODE_FILESYSTEM )
  950. {
  951. LGS::FS_DATA_EX OutFsData;
  952. OutFsData.nCommand = LGS::FS_CMD_SEND_EVT;
  953. OutFsData.sendevt.nError = LGS::FS_ERROR_MODE;
  954. OutFsData.sendevt.nOrder = InFsData.sendcmd.nOrder;
  955. nSendCount += LGS::BuildFSSubPacket( pSendBufferPtr, &OutFsData );
  956. }
  957. else
  958. {
  959. // perform action
  960. memcpy( ((PBYTE)&m_FileSystemPacket) + InFsData.sendcmd.nPosition, InFsData.sendcmd.pSubBuffer, InFsData.sendcmd.nSubSize );
  961. // if last sub packet of packet
  962. if( InFsData.sendcmd.nLast )
  963. {
  964. LG_FS_PACKET_EX PacketEx;
  965. memcpy( &PacketEx, &m_FileSystemPacket, sizeof( m_FileSystemPacket ) );
  966. memset( &m_FileSystemPacket, 0, sizeof( m_FileSystemPacket ) );
  967. CLgFileSystemServer::OnPacket( &PacketEx, &m_FileSystemPacket );
  968. }
  969. // prepare response
  970. LGS::FS_DATA_EX OutFsData;
  971. OutFsData.nCommand = LGS::FS_CMD_SEND_EVT;
  972. OutFsData.sendevt.nError = LGS::FS_ERROR_SUCCESS;
  973. OutFsData.sendevt.nOrder = InFsData.sendcmd.nOrder;
  974. nSendCount += LGS::BuildFSSubPacket( pSendBufferPtr, &OutFsData );
  975. }
  976. // write unswer
  977. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  978. break;
  979. case LGS::FS_CMD_RECEIVE_CMD:
  980. // build CMD_GETFILE command
  981. *pSendBufferPtr++ = LGS::CMD_FS_SUB_PACKET;
  982. if( nMode != LGS::MODE_FILESYSTEM )
  983. {
  984. LGS::FS_DATA_EX OutFsData;
  985. OutFsData.nCommand = LGS::FS_CMD_RECEIVE_EVT;
  986. OutFsData.receiveevt.nError = LGS::FS_ERROR_MODE;
  987. OutFsData.receiveevt.nOrder = InFsData.receivecmd.nOrder;
  988. nSendCount += LGS::BuildFSSubPacket( pSendBufferPtr, &OutFsData );
  989. }
  990. else
  991. {
  992. // prepare sub packet
  993. LGS::FS_DATA_EX OutFsData;
  994. OutFsData.nCommand = LGS::FS_CMD_RECEIVE_EVT;
  995. OutFsData.receiveevt.nError = LGS::FS_ERROR_SUCCESS;
  996. OutFsData.receiveevt.nOrder = InFsData.receivecmd.nOrder;
  997. OutFsData.receiveevt.nLast = ( m_FileSystemPacket.nSize - InFsData.receivecmd.nPosition ) <= 30;
  998. OutFsData.receiveevt.nPosition = InFsData.receivecmd.nPosition;
  999. OutFsData.receiveevt.nSubSize = OutFsData.receiveevt.nLast ? ( m_FileSystemPacket.nSize - InFsData.receivecmd.nPosition ) : 30;
  1000. memcpy( OutFsData.receiveevt.pSubBuffer, ((PBYTE)&m_FileSystemPacket) + InFsData.receivecmd.nPosition, OutFsData.receiveevt.nSubSize );
  1001. nSendCount += LGS::BuildFSSubPacket( pSendBufferPtr, &OutFsData );
  1002. }
  1003. // write unswer
  1004. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  1005. break;
  1006. default:
  1007. // command is wrong!!!
  1008. *pSendBufferPtr++ = LGS::CMD_UNSUPPORTED;
  1009. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  1010. break;
  1011. }
  1012. }
  1013. break;
  1014. #endif
  1015. default:
  1016. *pSendBufferPtr++ = LGS::CMD_UNSUPPORTED;
  1017. m_Protocol.Write( pSendBuffer, nSendCount, 0, FALSE );
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. #ifdef COMPILE_AS_GETFILE
  1025. bRestart;
  1026. return FALSE;
  1027. #else
  1028. return bRestart;
  1029. #endif
  1030. }
  1031. #ifdef DEBUG_ENABLE_SEND_BOARDDATA
  1032. BOOL CKernel::DoSendBoardData( INT nDevice )
  1033. {
  1034. if( !m_pComm485->SetParameters( LGS_SEND_BOARDDATA_BAUDRATE, 8L, CComm485::_NOPARITY ) )
  1035. return FALSE;
  1036. DWORD nBoardIrq = 0;
  1037. DWORD nBoardTimer = TIMER();
  1038. BOOL bPower, bZero, bSun[2];
  1039. INT nRange[2];
  1040. BYTE pData[ ( LGS_MAX_SEND_BOARDDATA_PART * 13 ) + 10 ];
  1041. CLgBitCode Encoder( pData );
  1042. INT nPart = 0;
  1043. nDevice = ( (nDevice == 0 || nDevice == 1) ? nDevice : 0 );
  1044. while( m_bRun )
  1045. {
  1046. #ifdef WATCHDOG_ENABLED
  1047. // update watchdog timer
  1048. m_WatchDog.Update( TIMER() );
  1049. #endif
  1050. // check timer
  1051. if( ( (TIMER()) - nBoardTimer ) > 18UL )
  1052. {
  1053. if( !nBoardIrq )
  1054. {
  1055. #ifdef DEBUG_BOARD_PRINT_NO_IRQ
  1056. printf( "NO IRQ %04x\n", m_pBoard->ReadNewBoard() );
  1057. #endif
  1058. // reset board
  1059. m_pBoard->Reset();
  1060. }
  1061. nBoardTimer = TIMER();
  1062. nBoardIrq = 0;
  1063. }
  1064. // get new data
  1065. if( m_pBoard->GetData( &bZero, bSun, nRange, &bPower ) )
  1066. {
  1067. // increase IRQ counter
  1068. nBoardIrq++;
  1069. // add next 13 bits of data
  1070. DWORD dwData = (DWORD)( nRange[nDevice] | ( bSun[nDevice] ? 1 << 11 : 0 ) | ( bZero ? 1 << 12 : 0 ) );
  1071. if( Encoder.Encode24( dwData, 13 ) )
  1072. {
  1073. // increase part number
  1074. nPart++;
  1075. // if numbe rof partes exeed max partes
  1076. if( nPart >= LGS_MAX_SEND_BOARDDATA_PART )
  1077. {
  1078. // prepare check sum in 14th byte of packet
  1079. BYTE bCheckSum = 0;
  1080. for( int i = 0; i < (LGS_MAX_SEND_BOARDDATA_PART * 13); i++ )
  1081. bCheckSum += pData[i];
  1082. pData[LGS_MAX_SEND_BOARDDATA_PART * 13] = bCheckSum;
  1083. // write into stream
  1084. m_pComm485->Write( pData, (LGS_MAX_SEND_BOARDDATA_PART * 13) + 1, 0 );
  1085. // reset part
  1086. nPart = 0;
  1087. // zero memory
  1088. memset( pData, 0, (LGS_MAX_SEND_BOARDDATA_PART * 13) + 1 );
  1089. }
  1090. // restart encoder
  1091. Encoder.SetBuffer( pData + ( nPart * 13 ) );
  1092. }
  1093. }
  1094. #ifdef COMM_WITHOUT_INTERRUPTS
  1095. m_pComm485->Invalidate();
  1096. m_pCameraComm->Invalidate();
  1097. #endif
  1098. }
  1099. return TRUE;
  1100. }
  1101. #endif
  1102. /////////////////////// FILE SYSTEM REQUESTS HANDLERS //////////////////////////
  1103. #ifndef LGS_DISABLE_FILE_SYSTEM
  1104. struct ffblk g_ffBlock;
  1105. LG_FS_RESULT CKernel::DoFindFirst( PDWORD pHandle, PLG_FS_PATH pPath, PLG_FS_FIND_DATA pFindData )
  1106. {
  1107. // add termination 0
  1108. TCHAR pPathWithNull[MAX_PATH];
  1109. memcpy( pPathWithNull, pPath->pBuffer, pPath->nSize );
  1110. pPathWithNull[ pPath->nSize ] = 0;
  1111. // open find first
  1112. if( findfirst( pPathWithNull, &g_ffBlock, FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_DIREC | FA_ARCH ) == 0 )
  1113. {
  1114. g_ffBlock.ff_attrib;
  1115. pFindData->dwFileAttributes = ( ( g_ffBlock.ff_attrib & FA_RDONLY ) ? LG_FS_FA_READONLY : 0 ) |
  1116. ( ( g_ffBlock.ff_attrib & FA_HIDDEN ) ? LG_FS_FA_HIDDEN : 0 ) |
  1117. ( ( g_ffBlock.ff_attrib & FA_SYSTEM ) ? LG_FS_FA_SYSTEM : 0 ) |
  1118. ( ( g_ffBlock.ff_attrib & FA_DIREC ) ? LG_FS_FA_DIRECTORY : 0 ) |
  1119. ( ( g_ffBlock.ff_attrib & FA_ARCH ) ? LG_FS_FA_ARCHIVE : 0 ) ;
  1120. pFindData->dwFileSizeHigh = 0;
  1121. pFindData->dwFileSizeLow = g_ffBlock.ff_fsize;
  1122. pFindData->Name.nSize = strlen( g_ffBlock.ff_name ) + 1;
  1123. memcpy( pFindData->Name.pBuffer, g_ffBlock.ff_name, pFindData->Name.nSize );
  1124. *pHandle = 0;
  1125. return LG_FS_RESULT_SUCCESS;
  1126. }
  1127. *pHandle = LG_FS_INVALID_HANDLE;
  1128. return LG_FS_RESULT_ERROR_NO_MORE_FILES;
  1129. }
  1130. LG_FS_RESULT CKernel::DoFindNext( DWORD dwHandle, PLG_FS_FIND_DATA pFindData )
  1131. {
  1132. dwHandle;
  1133. // open find first
  1134. if( findnext( &g_ffBlock ) == 0 )
  1135. {
  1136. pFindData->dwFileAttributes = ( ( g_ffBlock.ff_attrib & FA_RDONLY ) ? LG_FS_FA_READONLY : 0 ) |
  1137. ( ( g_ffBlock.ff_attrib & FA_HIDDEN ) ? LG_FS_FA_HIDDEN : 0 ) |
  1138. ( ( g_ffBlock.ff_attrib & FA_SYSTEM ) ? LG_FS_FA_SYSTEM : 0 ) |
  1139. ( ( g_ffBlock.ff_attrib & FA_DIREC ) ? LG_FS_FA_DIRECTORY : 0 ) |
  1140. ( ( g_ffBlock.ff_attrib & FA_ARCH ) ? LG_FS_FA_ARCHIVE : 0 ) ;
  1141. pFindData->dwFileSizeHigh = 0;
  1142. pFindData->dwFileSizeLow = g_ffBlock.ff_fsize;
  1143. pFindData->Name.nSize = strlen( g_ffBlock.ff_name ) + 1;
  1144. memcpy( pFindData->Name.pBuffer, g_ffBlock.ff_name, pFindData->Name.nSize );
  1145. return LG_FS_RESULT_SUCCESS;
  1146. }
  1147. return LG_FS_RESULT_ERROR_NO_MORE_FILES;
  1148. }
  1149. LG_FS_RESULT CKernel::DoFindClose( DWORD dwHandle )
  1150. {
  1151. dwHandle;
  1152. return LG_FS_RESULT_SUCCESS;
  1153. }
  1154. #endif
  1155. extern BOOL g_bShowErrors;
  1156. void CKernel::OnCmdState2( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount, BYTE& nMode, BOOL& bBoardNoIrq )
  1157. {
  1158. // build CMD_STATE_2 command
  1159. *pSendBufferPtr++ = LGS::CMD_STATE_2;
  1160. // if mode is wrong
  1161. if( nMode != LGS::MODE_DETECTOR )
  1162. {
  1163. // build reply
  1164. nSendCount += LGS::BuildEvtState2Flags( pSendBufferPtr, LGS::S2_ITF_ERROR_MODE );
  1165. return;
  1166. }
  1167. BYTE bRequestType = LGS::S2_RT_TARGET_POLAR;
  1168. if( nReadCount )
  1169. nReadCount -= LGS::GetCmdState2( pReadBufferPtr, &bRequestType );
  1170. // flags
  1171. BYTE bFlags = 0;
  1172. // check for ANGLE and ZERO MARK errors
  1173. if( g_bShowErrors && m_Detector[0].GetLastErrorAngle() )
  1174. {
  1175. INT nErrorAngle = m_Detector[0].GetLastErrorAngleValue();
  1176. if( nErrorAngle == -1 )
  1177. bFlags |= LGS::S2_ITF_ERROR_NO_ZERO_MARK;
  1178. else
  1179. nSendCount += LGS::BuildEvtState2ErrorAngle( pSendBufferPtr, nErrorAngle );
  1180. }
  1181. // check for NO IRQ
  1182. if( g_bShowErrors && bBoardNoIrq )
  1183. {
  1184. bFlags |= LGS::S2_ITF_ERROR_NO_IRQ;
  1185. // clear no irq flag
  1186. bBoardNoIrq = FALSE;
  1187. }
  1188. // check for POWER changes
  1189. if( g_bShowErrors )
  1190. m_Voltage.OnProtocolLgsState2Command( bFlags );
  1191. #ifndef EMBEDDED_CAMERA_DISABLE
  1192. // add camera flags
  1193. m_pCamera->OnProtocolLgsState2Command( bFlags );
  1194. #endif
  1195. // apply flags
  1196. if( bFlags )
  1197. nSendCount += LGS::BuildEvtState2Flags( pSendBufferPtr, bFlags );
  1198. for( int i = 0; i < 2; i++ )
  1199. {
  1200. CDetector *pDetector = m_Detector + i;
  1201. #ifdef LGS_MIRROR
  1202. CTracker *pTracker = m_Tracker + 0;
  1203. #else
  1204. CTracker *pTracker = m_Tracker + i;
  1205. #endif
  1206. // assume that no errors
  1207. BYTE bDeviceFlags = 0;
  1208. // check for tracker overflow
  1209. if( ( m_dwTimeErrorOverflow[i] < TIMER() ) && pTracker->GetLastErrorOverflow() )
  1210. {
  1211. m_dwTimeErrorOverflow[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  1212. bDeviceFlags |= LGS::S2_ITDF_ERROR_OVERFLOW;
  1213. }
  1214. // check for tracker scan to scan overflow
  1215. if( ( m_dwTimeErrorOverflowSST[i] < TIMER() ) && pTracker->GetLastErrorOverflowSST() )
  1216. {
  1217. m_dwTimeErrorOverflowSST[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  1218. bDeviceFlags |= LGS::S2_ITDF_ERROR_SST_OVERFLOW;
  1219. }
  1220. // check for detector sun errors
  1221. if( ( m_dwTimeErrorSun[i] < TIMER() ) && pDetector->GetLastErrorSun() )
  1222. {
  1223. m_dwTimeErrorSun[i] = TIMER() + LGS_COMM_ERROR_SEND_PERIOD;
  1224. bDeviceFlags |= LGS::S2_ITDF_ERROR_SUN;
  1225. }
  1226. // apply device flags
  1227. if( g_bShowErrors && bDeviceFlags )
  1228. nSendCount += LGS::BuildEvtState2DeviceFlags( pSendBufferPtr, i, bDeviceFlags );
  1229. // place target information
  1230. CTracker::TARGET target;
  1231. while( pTracker->GetTarget( &target ) )
  1232. {
  1233. BYTE bType;
  1234. switch( target.bType )
  1235. {
  1236. case LGS::TT_WIDE:
  1237. case LGS::TT_NORMAL:
  1238. bType = ( target.bType == LGS::TT_NORMAL ) ? LGS::S2_ITT_NORMAL : LGS::S2_ITT_WIDE;
  1239. if( ( bRequestType & LGS::S2_RT_TARGET_MASK ) == LGS::S2_RT_TARGET_DEKART )
  1240. {
  1241. #ifdef LGS_MIRROR
  1242. const double a2r = asin(1.0) * 4.0 / ( LGS_MAX_DETECTOR_ANGLES << 2 );
  1243. double a = ( target.nAngle << 1 ) * a2r;
  1244. #else
  1245. const double a2r = asin(1.0) * 4.0 / LGS_MAX_DETECTOR_ANGLES;
  1246. double a = target.nAngle * a2r;
  1247. #endif
  1248. INT x = target.nRange * cos( a );
  1249. INT y = target.nRange * sin( a );
  1250. #ifdef LGS_MIRROR
  1251. nSendCount += ((bRequestType&LGS::S2_RW_MASK)==LGS::S2_RW_ENABLE) ?
  1252. LGS::BuildEvtState2TargetDekartW( pSendBufferPtr, i, bType, x, y, target.nWidth ) :
  1253. LGS::BuildEvtState2TargetDekart( pSendBufferPtr, i, bType, x, y );
  1254. #else
  1255. nSendCount += ((bRequestType&LGS::S2_RW_MASK)==LGS::S2_RW_ENABLE) ?
  1256. LGS::BuildEvtState2TargetDekartW( pSendBufferPtr, i, bType, x, y, target.nWidth << 1 ) :
  1257. LGS::BuildEvtState2TargetDekart( pSendBufferPtr, i, bType, x, y );
  1258. #endif
  1259. }
  1260. else
  1261. #ifdef LGS_MIRROR
  1262. nSendCount += ((bRequestType&LGS::S2_RW_MASK)==LGS::S2_RW_ENABLE) ?
  1263. LGS::BuildEvtState2TargetPolarW( pSendBufferPtr, i, bType, target.nAngle << 1, target.nRange, target.nWidth << 1 ) :
  1264. LGS::BuildEvtState2TargetPolar( pSendBufferPtr, i, bType, target.nAngle << 1, target.nRange );
  1265. #else
  1266. nSendCount += ((bRequestType&LGS::S2_RW_MASK)==LGS::S2_RW_ENABLE) ?
  1267. LGS::BuildEvtState2TargetPolarW( pSendBufferPtr, i, bType, target.nAngle, target.nRange, target.nWidth ) :
  1268. LGS::BuildEvtState2TargetPolar( pSendBufferPtr, i, bType, target.nAngle, target.nRange );
  1269. #endif
  1270. break;
  1271. case LGS::TT_FLIP:
  1272. nSendCount += LGS::BuildEvtState2Flip( pSendBufferPtr, i, target.nAngle );
  1273. break;
  1274. case LGS::TT_BLIND:
  1275. if( !g_bShowErrors )
  1276. break;
  1277. switch( target.nAngle )
  1278. {
  1279. case LGS_BLIND_TYPE_MR_ENTER:
  1280. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_START | LGS::S2_ITB_MEDIUM_RANGE );
  1281. break;
  1282. case LGS_BLIND_TYPE_MR_EXIT:
  1283. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_STOP | LGS::S2_ITB_MEDIUM_RANGE );
  1284. break;
  1285. case LGS_BLIND_TYPE_180_DEGRESE_ENTER:
  1286. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_START | LGS::S2_ITB_180_DEGREE );
  1287. break;
  1288. case LGS_BLIND_TYPE_180_DEGRESE_EXIT:
  1289. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_STOP | LGS::S2_ITB_180_DEGREE );
  1290. break;
  1291. case LGS_BLIND_TYPE_FS_ENTER:
  1292. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_START | LGS::S2_ITB_FOG_SPIKES );
  1293. break;
  1294. case LGS_BLIND_TYPE_FS_EXIT:
  1295. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_STOP | LGS::S2_ITB_FOG_SPIKES );
  1296. break;
  1297. case LGS_BLIND_TYPE_PD_ENTER:
  1298. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_START | LGS::S2_ITB_PD );
  1299. break;
  1300. case LGS_BLIND_TYPE_PD_EXIT:
  1301. nSendCount += LGS::BuildEvtState2Blind( pSendBufferPtr, i, LGS::S2_ITB_STOP | LGS::S2_ITB_PD );
  1302. break;
  1303. }
  1304. break;
  1305. // case LGS::TT_CAMERA:
  1306. // break;
  1307. // case LGS::TT_TEST:
  1308. // break;
  1309. }
  1310. if( nSendCount >= 20 )
  1311. break;
  1312. }
  1313. if( nSendCount >= 20 )
  1314. break;
  1315. }
  1316. }
  1317. void CKernel::OnCmdGetWidthTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1318. {
  1319. // unused variables
  1320. pReadBufferPtr; nReadCount;
  1321. // build CMD_GET_WIDHT_TABLE command
  1322. *pSendBufferPtr++ = LGS::CMD_GET_WIDTH_TABLE;
  1323. PLONG pNativeTable = m_Detector[0].GetFilterWidth()->GetTable();
  1324. // prepare table
  1325. LGS::WIDTH_TABLE table;
  1326. table.nCount = LGS::WIDTH_TABLE_SIZE;
  1327. for( INT i = 0; i < LGS::WIDTH_TABLE_SIZE; i++ )
  1328. table.pTable[i] = ( pNativeTable[i] * 100L ) >> 7;
  1329. // write command
  1330. nSendCount += LGS::BuildEvtGetWidthTable( pSendBufferPtr, &table );
  1331. }
  1332. void CKernel::OnCmdSetWidthTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1333. {
  1334. // build CMD_SET_WIDHT_TABLE command
  1335. *pSendBufferPtr++ = LGS::CMD_SET_WIDTH_TABLE;
  1336. // read table
  1337. LGS::WIDTH_TABLE table;
  1338. nReadCount -= LGS::GetCmdSetWidthTable( pReadBufferPtr, &table );
  1339. FILE *pFile = fopen( "width.ini", "wt" );
  1340. if( pFile )
  1341. {
  1342. for( INT i = 0; i < table.nCount; i++ )
  1343. fprintf( pFile, "%d ", table.pTable[i] );
  1344. fclose( pFile );
  1345. }
  1346. // write command
  1347. nSendCount += LGS::BuildEvtSetWidthTable( pSendBufferPtr, &table );
  1348. // restart program
  1349. m_bRun = FALSE;
  1350. }
  1351. void CKernel::OnCmdGetZoomTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1352. {
  1353. // unused variables
  1354. pReadBufferPtr; nReadCount;
  1355. // build CMD_GET_WIDHT_TABLE command
  1356. *pSendBufferPtr++ = LGS::CMD_GET_ZOOM_TABLE;
  1357. // write command
  1358. nSendCount += LGS::BuildZoomTable( pSendBufferPtr, &m_pCamera->GetZoomTable() );
  1359. }
  1360. void CKernel::OnCmdSetZoomTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1361. {
  1362. // build CMD_SET_WIDHT_TABLE command
  1363. *pSendBufferPtr++ = LGS::CMD_SET_ZOOM_TABLE;
  1364. // read table
  1365. nReadCount -= LGS::GetZoomTable( pReadBufferPtr, &m_pCamera->GetZoomTable() );
  1366. // write command
  1367. nSendCount += LGS::BuildZoomTable( pSendBufferPtr, &m_pCamera->GetZoomTable() );
  1368. }
  1369. void CKernel::OnCmdGetTiltTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1370. {
  1371. // unused variables
  1372. pReadBufferPtr; nReadCount;
  1373. // build CMD_GET_WIDHT_TABLE command
  1374. *pSendBufferPtr++ = LGS::CMD_GET_TILT_TABLE;
  1375. // write command
  1376. nSendCount += LGS::BuildTiltTable( pSendBufferPtr, &m_pCamera->GetTiltTable() );
  1377. }
  1378. void CKernel::OnCmdSetTiltTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1379. {
  1380. // build CMD_SET_WIDHT_TABLE command
  1381. *pSendBufferPtr++ = LGS::CMD_SET_TILT_TABLE;
  1382. // read table
  1383. nReadCount -= LGS::GetTiltTable( pReadBufferPtr, &m_pCamera->GetTiltTable() );
  1384. // write command
  1385. nSendCount += LGS::BuildTiltTable( pSendBufferPtr, &m_pCamera->GetTiltTable() );
  1386. }
  1387. void CKernel::OnCmdGetCalibrationTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1388. {
  1389. // read table
  1390. BYTE nDevice = *pReadBufferPtr++;
  1391. nReadCount --;
  1392. // build CMD_GET_LINEARIZATION_TABLE command
  1393. *pSendBufferPtr++ = LGS::CMD_GET_CALIBRATION_TABLE;
  1394. *pSendBufferPtr++ = nDevice;
  1395. nSendCount++;
  1396. // process command
  1397. m_Calibrator[nDevice?1:0].OnCmdGetCalibrationTable( pReadBufferPtr, nReadCount, pSendBufferPtr, nSendCount );
  1398. }
  1399. BOOL CKernel::OnCmdSetCalibrationTable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount, INT& nDevice )
  1400. {
  1401. // read table
  1402. nDevice = *pReadBufferPtr++;
  1403. nReadCount --;
  1404. // build CMD_SET_LINEARIZATION_TABLE command
  1405. *pSendBufferPtr++ = LGS::CMD_SET_CALIBRATION_TABLE;
  1406. *pSendBufferPtr++ = nDevice;
  1407. nSendCount++;
  1408. // process command
  1409. return m_Calibrator[nDevice?1:0].OnCmdSetCalibrationTable( pReadBufferPtr, nReadCount, pSendBufferPtr, nSendCount );
  1410. }
  1411. void CKernel::OnCmdGetLrfDisable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1412. {
  1413. // unused variables
  1414. pReadBufferPtr; nReadCount;
  1415. // build CMD_GET_LRF_DISABLE command
  1416. *pSendBufferPtr++ = LGS::CMD_GET_LRF_DISABLE;
  1417. // write command
  1418. nSendCount += LGS::BuildLRFDisable( pSendBufferPtr, &m_LRFDisable );
  1419. }
  1420. void CKernel::OnCmdSetLrfDisable( PBYTE& pReadBufferPtr, INT& nReadCount, PBYTE& pSendBufferPtr, INT& nSendCount )
  1421. {
  1422. // read table
  1423. nReadCount -= LGS::GetLRFDisable( pReadBufferPtr, &m_LRFDisable );
  1424. // store serial number
  1425. SaveSettings( (PCHAR)m_pSerialNumber, m_nAddress, &m_pCamera->GetParameters(), &m_Relay.GetParameters(), &m_Voltage.GetParameters(), &m_LRFDisable );
  1426. // build CMD_SET_LRF_DISABLE command
  1427. *pSendBufferPtr++ = LGS::CMD_SET_LRF_DISABLE;
  1428. // write command
  1429. nSendCount += LGS::BuildLRFDisable( pSendBufferPtr, &m_LRFDisable );
  1430. // restart program
  1431. m_bRun = FALSE;
  1432. }
  1433. typedef enum SETTING_TYPE
  1434. {
  1435. ST_NONE = -1,
  1436. ST_SN = 0,
  1437. ST_ADDRESS,
  1438. ST_CAMERA_TYPE,
  1439. ST_CAMERA_SHIFT,
  1440. ST_CAMERA_AUTO,
  1441. ST_CAMERA_DELAY,
  1442. ST_RELAY_NC,
  1443. ST_RELAY_AUTO,
  1444. ST_RELAY_DELAY,
  1445. ST_VOLTAGE_CONTROL,
  1446. ST_MASTER_DISABLED,
  1447. ST_SLAVE_DISABLED,
  1448. ST_AUTORESET_ENABLE,
  1449. ST_AUTORESET_INTERLEAVE,
  1450. ST_AUTORESET_TIMEOUT,
  1451. ST_BOARD_TEMPERATURE,
  1452. ST_BOARD_ZEROSHIFT,
  1453. ST_BOARD_ENCODERSTEPS,
  1454. ST_BOARD_GEARRATIO,
  1455. ST_BOARD_MAXRANGE,
  1456. ST_SWITCHER_TABLE,
  1457. ST_SWITCHER_REPLY_TIMEOUT,
  1458. ST_SWITCHER_COMMAND_TIMEOUT,
  1459. ST_SWITCHER_AUTO_MODE,
  1460. ST_SWITCHER_AUTO_ENTER_TIMEOUT,
  1461. ST_SWITCHER_AUTO_SWITCH_TIMEOUT,
  1462. }SETTING_TYPE;
  1463. LPCTSTR m_pSetting[] =
  1464. {
  1465. { "SN" },
  1466. { "ADDRESS" },
  1467. { "CAMERA_TYPE" },
  1468. { "CAMERA_SHIFT" },
  1469. { "CAMERA_AUTO" },
  1470. { "CAMERA_DELAY" },
  1471. { "RELAY_NC" },
  1472. { "RELAY_AUTO" },
  1473. { "RELAY_DELAY" },
  1474. { "VOLTAGE_CONTROL" },
  1475. { "MASTER_DISABLED" },
  1476. { "SLAVE_DISABLED" },
  1477. { "AUTORESET_ENABLE" },
  1478. { "AUTORESET_INTERLEAVE" },
  1479. { "AUTORESET_TIMEOUT" },
  1480. { "BOARD_TEMPERATURE" },
  1481. { "BOARD_ZEROSHIFT" },
  1482. { "BOARD_ENCODERSTEPS" },
  1483. { "BOARD_GEARRATIO" },
  1484. { "BOARD_MAXRANGE" },
  1485. { "SWITCHER_TABLE" },
  1486. { "SWITCHER_REPLY_TIMEOUT" },
  1487. { "SWITCHER_COMMAND_TIMEOUT" },
  1488. { "SWITCHER_AUTO_MODE" },
  1489. { "SWITCHER_AUTO_ENTER_TIMEOUT" },
  1490. { "SWITCHER_AUTO_SWITCH_TIMEOUT" }
  1491. };
  1492. BOOL CKernel::LoadSettings( LPTSTR pSerialNumber, PBYTE pSerialNumberSize, PBYTE pAddress, LGS::PCAMERA_PARAMETERS pCameraParameters, LGS::PRELAY_PARAMETERS pRelayParameters, LGS::PVOLTAGE_PARAMETERS pVoltageParameters, LGS::PLRF_DISABLE pLRFDisable )
  1493. {
  1494. // read serial number
  1495. FILE *pFile = fopen( "SN.INI", "rt" );
  1496. if( !pFile )
  1497. {
  1498. // fill with default sn
  1499. *pSerialNumberSize = 14;
  1500. memcpy( pSerialNumber, "NOSERIALNUMBER", *pSerialNumberSize );
  1501. return FALSE;
  1502. }
  1503. CHAR pLine[MAX_PATH];
  1504. while( fgets( pLine, MAX_PATH, pFile ) )
  1505. {
  1506. SETTING_TYPE eType = ST_NONE;
  1507. PCHAR pTemp = NULL;
  1508. for( INT i = 0; i < sizeof( m_pSetting ) / sizeof( *m_pSetting ); i++ )
  1509. {
  1510. pTemp = strstr( pLine, m_pSetting[i] );
  1511. if( pTemp )
  1512. {
  1513. eType = (SETTING_TYPE)i;
  1514. pTemp = strstr( pTemp, "=" );
  1515. if( pTemp )
  1516. pTemp = strtok( pTemp + 1, " \t\n\r" );
  1517. break;
  1518. }
  1519. }
  1520. if( eType == ST_NONE || !pTemp )
  1521. continue;
  1522. switch( eType )
  1523. {
  1524. case ST_SN: *pSerialNumberSize = strlen( pTemp ); memcpy( pSerialNumber, pTemp, *pSerialNumberSize ); break;
  1525. case ST_ADDRESS: *pAddress = atoi( pTemp ); break;
  1526. case ST_CAMERA_TYPE: pCameraParameters->eType = (LGS::E_CAMERA_TYPE)atoi( pTemp ); break;
  1527. case ST_CAMERA_SHIFT: pCameraParameters->nShift = (WORD)atoi( pTemp ); break;
  1528. case ST_CAMERA_AUTO: pCameraParameters->bAuto = (BOOL)atoi( pTemp ); break;
  1529. case ST_CAMERA_DELAY: pCameraParameters->nAutoDelay = (WORD)atoi( pTemp ); break;
  1530. case ST_RELAY_NC: pRelayParameters->bNormallyClose = (WORD)atoi( pTemp ); break;
  1531. case ST_RELAY_AUTO: pRelayParameters->bAuto = (BOOL)atoi( pTemp ); break;
  1532. case ST_RELAY_DELAY: pRelayParameters->nAutoDelay = (WORD)atoi( pTemp ); break;
  1533. case ST_VOLTAGE_CONTROL: pVoltageParameters->bControl = (BOOL)atoi( pTemp ); break;
  1534. case ST_MASTER_DISABLED: pLRFDisable->bMaster = (BOOL)atoi( pTemp ); break;
  1535. case ST_SLAVE_DISABLED: pLRFDisable->bSlave = (BOOL)atoi( pTemp ); break;
  1536. }
  1537. }
  1538. fclose( pFile );
  1539. return TRUE;
  1540. }
  1541. BOOL CKernel::SaveSettings( LPCTSTR pSerialNumber, INT nAddress, LGS::PCAMERA_PARAMETERS pCameraParameters, LGS::PRELAY_PARAMETERS pRelayParameters, LGS::PVOLTAGE_PARAMETERS pVoltageParameters, LGS::PLRF_DISABLE pLRFDisable )
  1542. {
  1543. FILE *pFile = fopen( "SN.INI", "wt" );
  1544. if( !pFile )
  1545. return FALSE;
  1546. LPCTSTR pFormatStr = "%s = %s\n";
  1547. LPCTSTR pFormatInt = "%s = %d\n";
  1548. fprintf( pFile, pFormatStr, m_pSetting[ST_SN], pSerialNumber );
  1549. fprintf( pFile, pFormatInt, m_pSetting[ST_ADDRESS], nAddress );
  1550. fprintf( pFile, pFormatInt, m_pSetting[ST_CAMERA_TYPE], pCameraParameters->eType );
  1551. fprintf( pFile, pFormatInt, m_pSetting[ST_CAMERA_SHIFT], pCameraParameters->nShift );
  1552. fprintf( pFile, pFormatInt, m_pSetting[ST_CAMERA_AUTO], pCameraParameters->bAuto );
  1553. fprintf( pFile, pFormatInt, m_pSetting[ST_CAMERA_DELAY], pCameraParameters->nAutoDelay );
  1554. fprintf( pFile, pFormatInt, m_pSetting[ST_RELAY_AUTO], pRelayParameters->bAuto );
  1555. fprintf( pFile, pFormatInt, m_pSetting[ST_RELAY_DELAY], pRelayParameters->nAutoDelay );
  1556. fprintf( pFile, pFormatInt, m_pSetting[ST_RELAY_NC], pRelayParameters->bNormallyClose );
  1557. fprintf( pFile, pFormatInt, m_pSetting[ST_VOLTAGE_CONTROL], pVoltageParameters->bControl );
  1558. fprintf( pFile, pFormatInt, m_pSetting[ST_MASTER_DISABLED], pLRFDisable->bMaster );
  1559. fprintf( pFile, pFormatInt, m_pSetting[ST_SLAVE_DISABLED], pLRFDisable->bSlave );
  1560. fclose( pFile );
  1561. return TRUE;
  1562. }
  1563. BOOL CKernel::LoadUserSettings( AUTO_RESET *pAutoReset, BOARD_SETTINGS *pBoard, SWITCHER_SETTINGS *pSwitcher )
  1564. {
  1565. pSwitcher->pTable[0] = LGS_MIRROR_ANGLE_OF_VIEW * 1 / 4;
  1566. pSwitcher->pTable[1] = LGS_MIRROR_ANGLE_OF_VIEW * 2 / 4;
  1567. pSwitcher->pTable[2] = LGS_MIRROR_ANGLE_OF_VIEW * 3 / 4;
  1568. pSwitcher->nReplyTimeout = 100L;
  1569. pSwitcher->nCommandTimeout = 500L;
  1570. pSwitcher->nAutoMode = 0;
  1571. pSwitcher->nAutoEnterTimeout = 60;
  1572. pSwitcher->nAutoSwitchTimeout = 3;
  1573. pAutoReset->bEnabled = FALSE;
  1574. pAutoReset->dwInterleave = 2000UL;
  1575. pAutoReset->dwTimeout = 60000UL;
  1576. pBoard->nZeroShift = 0;
  1577. pBoard->nTemperature = 20;
  1578. pBoard->nEncoderSteps = 6;
  1579. pBoard->nGearRatio = 61;
  1580. // pBoard->nMaxRange = 2048;
  1581. // read serial number
  1582. FILE *pFile = fopen( "USER.INI", "rt" );
  1583. if( !pFile )
  1584. return FALSE;
  1585. CHAR pLine[MAX_PATH];
  1586. while( fgets( pLine, MAX_PATH, pFile ) )
  1587. {
  1588. SETTING_TYPE eType = ST_NONE;
  1589. PCHAR pTemp = NULL;
  1590. for( INT i = 0; i < sizeof( m_pSetting ) / sizeof( *m_pSetting ); i++ )
  1591. {
  1592. pTemp = strstr( pLine, m_pSetting[i] );
  1593. if( pTemp )
  1594. {
  1595. eType = (SETTING_TYPE)i;
  1596. pTemp = strstr( pTemp, "=" );
  1597. if( pTemp )
  1598. pTemp = strtok( pTemp + 1, " \t\n\r" );
  1599. break;
  1600. }
  1601. }
  1602. if( eType == ST_NONE || !pTemp )
  1603. continue;
  1604. switch( eType )
  1605. {
  1606. case ST_AUTORESET_ENABLE: pAutoReset->bEnabled = (BOOL)atoi( pTemp ); break;
  1607. case ST_AUTORESET_INTERLEAVE: pAutoReset->dwInterleave = atol( pTemp ); break;
  1608. case ST_AUTORESET_TIMEOUT: pAutoReset->dwTimeout = atol( pTemp ); break;
  1609. case ST_BOARD_TEMPERATURE: pBoard->nTemperature = atoi( pTemp ); break;
  1610. case ST_BOARD_ZEROSHIFT: pBoard->nZeroShift = atoi( pTemp ); break;
  1611. case ST_BOARD_ENCODERSTEPS: pBoard->nEncoderSteps = atoi( pTemp ); break;
  1612. case ST_BOARD_GEARRATIO: pBoard->nGearRatio = atoi( pTemp ); break;
  1613. // case ST_BOARD_MAXRANGE: pBoard->nMaxRange = atoi( pTemp ); break;
  1614. case ST_SWITCHER_TABLE: sscanf( pTemp, "%d,%d,%d", pSwitcher->pTable + 0, pSwitcher->pTable + 1, pSwitcher->pTable + 2 );
  1615. case ST_SWITCHER_REPLY_TIMEOUT: pSwitcher->nReplyTimeout = atol( pTemp ); break;
  1616. case ST_SWITCHER_COMMAND_TIMEOUT: pSwitcher->nCommandTimeout = atol( pTemp ); break;
  1617. case ST_SWITCHER_AUTO_MODE: pSwitcher->nAutoMode = atoi( pTemp ); break;
  1618. case ST_SWITCHER_AUTO_ENTER_TIMEOUT: pSwitcher->nAutoEnterTimeout = atoi( pTemp ); break;
  1619. case ST_SWITCHER_AUTO_SWITCH_TIMEOUT: pSwitcher->nAutoSwitchTimeout = atoi( pTemp ); break;
  1620. }
  1621. }
  1622. fclose( pFile );
  1623. return TRUE;
  1624. }
  1625. BOOL CKernel::SaveUserSettings( AUTO_RESET *pAutoReset, BOARD_SETTINGS *pBoard, SWITCHER_SETTINGS *pSwitcher )
  1626. {
  1627. FILE *pFile = fopen( "USER.INI", "wt" );
  1628. if( !pFile )
  1629. return FALSE;
  1630. LPCTSTR pFormatInt = "%s=%d\n";
  1631. LPCTSTR pFormatArr = "%s=%d,%d,%d\n";
  1632. fprintf( pFile, pFormatInt, m_pSetting[ST_AUTORESET_ENABLE], (INT)pAutoReset->bEnabled );
  1633. fprintf( pFile, pFormatInt, m_pSetting[ST_AUTORESET_INTERLEAVE], (INT)pAutoReset->dwInterleave );
  1634. fprintf( pFile, pFormatInt, m_pSetting[ST_AUTORESET_TIMEOUT], (INT)pAutoReset->dwTimeout );
  1635. fprintf( pFile, pFormatInt, m_pSetting[ST_BOARD_TEMPERATURE], (INT)(CHAR)pBoard->nTemperature );
  1636. fprintf( pFile, pFormatInt, m_pSetting[ST_BOARD_ZEROSHIFT], (INT)pBoard->nZeroShift );
  1637. fprintf( pFile, pFormatInt, m_pSetting[ST_BOARD_ENCODERSTEPS], (INT)pBoard->nEncoderSteps );
  1638. fprintf( pFile, pFormatInt, m_pSetting[ST_BOARD_GEARRATIO], (INT)pBoard->nGearRatio );
  1639. fprintf( pFile, pFormatArr, m_pSetting[ST_SWITCHER_TABLE], pSwitcher->pTable[0], pSwitcher->pTable[1], pSwitcher->pTable[2] );
  1640. fprintf( pFile, pFormatInt, m_pSetting[ST_SWITCHER_REPLY_TIMEOUT], pSwitcher->nReplyTimeout );
  1641. fprintf( pFile, pFormatInt, m_pSetting[ST_SWITCHER_COMMAND_TIMEOUT], pSwitcher->nCommandTimeout );
  1642. fprintf( pFile, pFormatInt, m_pSetting[ST_SWITCHER_AUTO_MODE], pSwitcher->nAutoMode );
  1643. fprintf( pFile, pFormatInt, m_pSetting[ST_SWITCHER_AUTO_ENTER_TIMEOUT], pSwitcher->nAutoEnterTimeout );
  1644. fprintf( pFile, pFormatInt, m_pSetting[ST_SWITCHER_AUTO_SWITCH_TIMEOUT], pSwitcher->nAutoSwitchTimeout );
  1645. fclose( pFile );
  1646. return TRUE;
  1647. }