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.

261 lines
6.3 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: SensorSPRU.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of SensorSPRU.cpp
  13. //----------------------------------------------------------------------------
  14. // SensorSPRU.cpp: implementation of the CSensorSPRU class.
  15. //////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. //#include "chicago.h"
  18. #include "SensorSPRU.h"
  19. #include "EventQueue.h"
  20. #include "..\..\LgLib\Version.h"
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char THIS_FILE[]=__FILE__;
  24. #define new DEBUG_NEW
  25. #endif
  26. //////////////////////////////////////////////////////////////////////
  27. // Construction/Destruction
  28. //////////////////////////////////////////////////////////////////////
  29. CSensorSPRU::CSensorSPRU()
  30. {
  31. m_nType = DTR;
  32. m_strName = "DTR";
  33. m_bBitMask = m_bBitState = m_bBitAlarm = 0;
  34. m_bBitEnable = 0x3F;
  35. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  36. m_pBitPosition[i] = SDPOINT( 0.1, ((double)i+1.0) / 8.0 );
  37. m_bBitInactive = 0;
  38. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorSPRU::CSensorSPRU") );
  39. }
  40. CSensorSPRU::~CSensorSPRU()
  41. {
  42. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorSPRU::~CSensorSPRU") );
  43. }
  44. static void PrepareBitString( BYTE byte, LPSTR pBitString, const int bitCount = DTR_BIT_COUNT )
  45. {
  46. for( int i = bitCount - 1; i >= 0; i-- )
  47. *pBitString++ = ( ( byte >> i ) & 1 ) + '0';
  48. *pBitString = 0;
  49. }
  50. CString CSensorSPRU::GetEventString( int nEvent, BYTE bitState )
  51. {
  52. CHAR pBitString[7];
  53. CString str;
  54. switch( nEvent )
  55. {
  56. case LGS_EVENT_NORMAL:
  57. str.Format( "%s normally working", m_strName );
  58. break;
  59. case LGS_EVENT_TIMEOUT_APPEAR:
  60. str.Format( "%s not responsed", m_strName );
  61. break;
  62. case LGS_EVENT_ALARM_SHOW:
  63. PrepareBitString( bitState, pBitString );
  64. str.Format( "%s alarm appears (%s)", m_strName, pBitString );
  65. break;
  66. case LGS_EVENT_ALARM_HIDE:
  67. PrepareBitString( bitState, pBitString );
  68. str.Format( "%s alarm disappears (%s)", m_strName, pBitString );
  69. break;
  70. }
  71. return str;
  72. }
  73. COLORREF CSensorSPRU::GetEventColor( int nEvent )
  74. {
  75. switch( nEvent )
  76. {
  77. case LGS_EVENT_NORMAL: return RGB( 0x00, 0xFF, 0x00 );
  78. case LGS_EVENT_TIMEOUT_APPEAR: return RGB( 0xFF, 0xFF, 0x00 );
  79. case LGS_EVENT_ALARM_SHOW: return RGB( 0xFF, 0x00, 0x00 );
  80. case LGS_EVENT_ALARM_HIDE: return RGB( 0x00, 0xFF, 0x00 );
  81. }
  82. return RGB( 0xFF, 0xFF, 0xFF );
  83. }
  84. void CSensorSPRU::PaintBits(CDC *pDC, CRect &viewRect, BOOL bInverse )
  85. {
  86. static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
  87. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  88. {
  89. if( bits[i] & GetBitMask() )
  90. {
  91. CPoint p = CalcDrawPoint( viewRect, viewRect.Size(), GetBitPosition(i), 1.0 );
  92. if( !viewRect.PtInRect(p) ) continue;
  93. COLORREF color = 0x000000;
  94. if( bits[i] & GetBitEnable() )
  95. {
  96. if( GetState() == TIMEOUT )
  97. {
  98. color = GetStateColor( GetState(), bInverse );
  99. }
  100. else
  101. {
  102. if( bits[i] & GetBitState() )
  103. {
  104. color = GetStateColor( ALARM, bInverse );
  105. }
  106. else if( bits[i] & GetBitAlarm() )
  107. {
  108. color = GetStateColor( POSTALARM, bInverse );
  109. }
  110. else
  111. {
  112. color = GetStateColor( NORMAL, bInverse );
  113. }
  114. }
  115. }
  116. else
  117. color = GetStateColor( DISABLE, bInverse );
  118. CRect rect;
  119. rect.SetRect( p.x - 4, p.y - 4, p.x + 4, p.y + 4 );
  120. pDC->FillSolidRect( &rect, color );
  121. }
  122. }
  123. }
  124. void CSensorSPRU::Reset()
  125. {
  126. SetBitState(0);
  127. SetBitAlarm(0);
  128. if( ( GetBitMask() & GetBitEnable() ) == GetBitMask() )
  129. CSensor::SetState( UNKNOWN );
  130. else
  131. CSensor::SetState( DISABLE );
  132. }
  133. void CSensorSPRU::Enable(BOOL bEnable)
  134. {
  135. CSensor::Enable( bEnable );
  136. if( bEnable ) SetBitEnable( 0xFF );
  137. else SetBitEnable( 0x00 );
  138. }
  139. void CSensorSPRU::SetBitEnable( BYTE bBitEnable )
  140. {
  141. m_bBitEnable = bBitEnable;
  142. if( GetState() != UNKNOWN && GetState() != TIMEOUT )
  143. {
  144. if( GetBitEnable() & GetBitMask() & GetBitState() )
  145. {
  146. if( CSensor::GetState() != ALARM )
  147. CSensor::SetState( ALARM );
  148. }
  149. else
  150. if( GetBitEnable() & GetBitMask() & GetBitAlarm() )
  151. {
  152. if( CSensor::GetState() != POSTALARM )
  153. CSensor::SetState( POSTALARM );
  154. }
  155. else
  156. if( ( GetBitEnable() & GetBitMask() ) == GetBitMask() )
  157. {
  158. if( CSensor::GetState() != NORMAL )
  159. CSensor::SetState( NORMAL );
  160. }
  161. else
  162. {
  163. if( CSensor::GetState() != DISABLE )
  164. CSensor::SetState( DISABLE );
  165. }
  166. }
  167. }
  168. void CSensorSPRU::Enable( BOOL bEnable, int nBit )
  169. {
  170. static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
  171. int nIndex = 0;
  172. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  173. {
  174. if( bits[i] & GetBitMask() )
  175. {
  176. if( nIndex == nBit )
  177. {
  178. BYTE bMask = GetBitEnable();
  179. if( bEnable ) bMask |= bits[i];
  180. else bMask &= ( 0xFF ^ bits[i] );
  181. SetBitEnable( bMask & GetBitMask() );
  182. return;
  183. }
  184. nIndex++;
  185. }
  186. }
  187. }
  188. int CSensorSPRU::GetBitIndex( LPSDPOINT pPoint )
  189. {
  190. static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
  191. double dMin = DBL_MAX;
  192. int iMin = -1;
  193. int nIndex = 0;
  194. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  195. if( bits[i] & GetBitMask() )
  196. {
  197. double d = sqrt( pow( m_pBitPosition[i].x - pPoint->x, 2 ) +
  198. pow( m_pBitPosition[i].y - pPoint->y, 2 ) );
  199. if( d < dMin )
  200. {
  201. iMin = nIndex;
  202. dMin = d;
  203. }
  204. nIndex++;
  205. }
  206. return iMin;
  207. }
  208. void CSensorSPRU::SetInactiveTime( int iBitMask, DWORD minTime, DWORD maxTime )
  209. {
  210. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  211. {
  212. if( iBitMask & ( 1 << i ) )
  213. {
  214. m_pBitMinTime[ i ] = minTime;
  215. m_pBitMaxTime[ i ] = maxTime;
  216. m_bBitInactive |= ( 1 << i );
  217. }
  218. }
  219. }
  220. void CSensorSPRU::CheckInactiveTime( LPBYTE pByte, DWORD dwCT )
  221. {
  222. if( (*pByte) & m_bBitInactive )
  223. {
  224. for( int i = 0; i < DTR_BIT_COUNT; i++ )
  225. {
  226. if( m_bBitInactive & ( 1 << i ) & (*pByte) )
  227. {
  228. if( ( m_pBitMinTime[i] < dwCT ) && ( dwCT < m_pBitMaxTime[i] ) )
  229. (*pByte) &= ( 0xFF^(1 << i ) );
  230. else
  231. if( dwCT >= m_pBitMaxTime[i] )
  232. m_bBitInactive &= ( 0xFF^(1 << i ) );
  233. }
  234. }
  235. }
  236. }