|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: SensorSPRU.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of SensorSPRU.cpp
//----------------------------------------------------------------------------
// SensorSPRU.cpp: implementation of the CSensorSPRU class.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "chicago.h"
#include "SensorSPRU.h"
#include "EventQueue.h"
#include "..\..\LgLib\Version.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSensorSPRU::CSensorSPRU(){ m_nType = DTR; m_strName = "DTR"; m_bBitMask = m_bBitState = m_bBitAlarm = 0; m_bBitEnable = 0x3F; for( int i = 0; i < DTR_BIT_COUNT; i++ ) m_pBitPosition[i] = SDPOINT( 0.1, ((double)i+1.0) / 8.0 ); m_bBitInactive = 0;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorSPRU::CSensorSPRU") );}
CSensorSPRU::~CSensorSPRU(){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSensorSPRU::~CSensorSPRU") );}
static void PrepareBitString( BYTE byte, LPSTR pBitString, const int bitCount = DTR_BIT_COUNT ){ for( int i = bitCount - 1; i >= 0; i-- ) *pBitString++ = ( ( byte >> i ) & 1 ) + '0'; *pBitString = 0;}
CString CSensorSPRU::GetEventString( int nEvent, BYTE bitState ){ CHAR pBitString[7]; CString str; switch( nEvent ) { case LGS_EVENT_NORMAL: str.Format( "%s normally working", m_strName ); break; case LGS_EVENT_TIMEOUT_APPEAR: str.Format( "%s not responsed", m_strName ); break; case LGS_EVENT_ALARM_SHOW: PrepareBitString( bitState, pBitString ); str.Format( "%s alarm appears (%s)", m_strName, pBitString ); break; case LGS_EVENT_ALARM_HIDE: PrepareBitString( bitState, pBitString ); str.Format( "%s alarm disappears (%s)", m_strName, pBitString ); break; } return str;}
COLORREF CSensorSPRU::GetEventColor( int nEvent ){ switch( nEvent ) { case LGS_EVENT_NORMAL: return RGB( 0x00, 0xFF, 0x00 ); case LGS_EVENT_TIMEOUT_APPEAR: return RGB( 0xFF, 0xFF, 0x00 ); case LGS_EVENT_ALARM_SHOW: return RGB( 0xFF, 0x00, 0x00 ); case LGS_EVENT_ALARM_HIDE: return RGB( 0x00, 0xFF, 0x00 ); } return RGB( 0xFF, 0xFF, 0xFF );}
void CSensorSPRU::PaintBits(CDC *pDC, CRect &viewRect, BOOL bInverse ){ static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
for( int i = 0; i < DTR_BIT_COUNT; i++ ) { if( bits[i] & GetBitMask() ) { CPoint p = CalcDrawPoint( viewRect, viewRect.Size(), GetBitPosition(i), 1.0 ); if( !viewRect.PtInRect(p) ) continue;
COLORREF color = 0x000000;
if( bits[i] & GetBitEnable() ) { if( GetState() == TIMEOUT ) { color = GetStateColor( GetState(), bInverse ); } else { if( bits[i] & GetBitState() ) { color = GetStateColor( ALARM, bInverse ); } else if( bits[i] & GetBitAlarm() ) { color = GetStateColor( POSTALARM, bInverse ); } else { color = GetStateColor( NORMAL, bInverse ); } } } else color = GetStateColor( DISABLE, bInverse );
CRect rect; rect.SetRect( p.x - 4, p.y - 4, p.x + 4, p.y + 4 ); pDC->FillSolidRect( &rect, color ); } }}
void CSensorSPRU::Reset(){ SetBitState(0); SetBitAlarm(0);
if( ( GetBitMask() & GetBitEnable() ) == GetBitMask() ) CSensor::SetState( UNKNOWN ); else CSensor::SetState( DISABLE );}
void CSensorSPRU::Enable(BOOL bEnable){ CSensor::Enable( bEnable ); if( bEnable ) SetBitEnable( 0xFF ); else SetBitEnable( 0x00 );}
void CSensorSPRU::SetBitEnable( BYTE bBitEnable ) { m_bBitEnable = bBitEnable; if( GetState() != UNKNOWN && GetState() != TIMEOUT ) { if( GetBitEnable() & GetBitMask() & GetBitState() ) { if( CSensor::GetState() != ALARM ) CSensor::SetState( ALARM ); } else if( GetBitEnable() & GetBitMask() & GetBitAlarm() ) { if( CSensor::GetState() != POSTALARM ) CSensor::SetState( POSTALARM ); } else if( ( GetBitEnable() & GetBitMask() ) == GetBitMask() ) { if( CSensor::GetState() != NORMAL ) CSensor::SetState( NORMAL ); } else { if( CSensor::GetState() != DISABLE ) CSensor::SetState( DISABLE ); } }}
void CSensorSPRU::Enable( BOOL bEnable, int nBit ){ static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 }; int nIndex = 0; for( int i = 0; i < DTR_BIT_COUNT; i++ ) { if( bits[i] & GetBitMask() ) { if( nIndex == nBit ) { BYTE bMask = GetBitEnable(); if( bEnable ) bMask |= bits[i]; else bMask &= ( 0xFF ^ bits[i] ); SetBitEnable( bMask & GetBitMask() ); return; } nIndex++; } }}
int CSensorSPRU::GetBitIndex( LPSDPOINT pPoint ){ static const BYTE bits[DTR_BIT_COUNT] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 }; double dMin = DBL_MAX; int iMin = -1; int nIndex = 0; for( int i = 0; i < DTR_BIT_COUNT; i++ ) if( bits[i] & GetBitMask() ) { double d = sqrt( pow( m_pBitPosition[i].x - pPoint->x, 2 ) + pow( m_pBitPosition[i].y - pPoint->y, 2 ) ); if( d < dMin ) { iMin = nIndex; dMin = d; }
nIndex++; }
return iMin;}
void CSensorSPRU::SetInactiveTime( int iBitMask, DWORD minTime, DWORD maxTime ){ for( int i = 0; i < DTR_BIT_COUNT; i++ ) { if( iBitMask & ( 1 << i ) ) { m_pBitMinTime[ i ] = minTime; m_pBitMaxTime[ i ] = maxTime; m_bBitInactive |= ( 1 << i ); } }}
void CSensorSPRU::CheckInactiveTime( LPBYTE pByte, DWORD dwCT ){ if( (*pByte) & m_bBitInactive ) { for( int i = 0; i < DTR_BIT_COUNT; i++ ) { if( m_bBitInactive & ( 1 << i ) & (*pByte) ) { if( ( m_pBitMinTime[i] < dwCT ) && ( dwCT < m_pBitMaxTime[i] ) ) (*pByte) &= ( 0xFF^(1 << i ) ); else if( dwCT >= m_pBitMaxTime[i] ) m_bBitInactive &= ( 0xFF^(1 << i ) ); } } }}
|