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.
235 lines
4.4 KiB
235 lines
4.4 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 19/04/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM: LgAlarm
|
|
// FILE: LgAlarm.cpp
|
|
// AUTHOR: Chizhevskiy Sergey
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file for implemenation of class CLgAlarm
|
|
//----------------------------------------------------------------------------
|
|
#include "StdAfx.h"
|
|
#include "..\LgLib\Version.h"
|
|
#include "..\LgLib\LgLogger.h"
|
|
#include "LgAlarm.h"
|
|
|
|
CLgAlarm::CLgAlarm()
|
|
{
|
|
// init registers
|
|
m_dwVersion = ::GetVersion();
|
|
|
|
// set defaults
|
|
m_bEnabled = TRUE;
|
|
m_bAlarmOn = FALSE;
|
|
|
|
// stop buzzer
|
|
BuzzerOn( FALSE );
|
|
|
|
// reset
|
|
m_bWaveAlarm = FALSE;
|
|
memset( m_pWaveFile, 0, sizeof(m_pWaveFile) );
|
|
m_bWaveOn = FALSE;
|
|
}
|
|
|
|
CLgAlarm::~CLgAlarm()
|
|
{
|
|
Destroy();
|
|
}
|
|
|
|
void CLgAlarm::Enable( BOOL bEnable )
|
|
{
|
|
if( ( !bEnable ) && m_bEnabled )
|
|
Stop();
|
|
|
|
m_bEnabled = bEnable;
|
|
}
|
|
|
|
void CLgAlarm::Beep()
|
|
{
|
|
if( m_bEnabled )
|
|
m_EventBeep.Set();
|
|
}
|
|
|
|
void CLgAlarm::Start()
|
|
{
|
|
if( m_bEnabled )
|
|
{
|
|
m_bAlarmOn = TRUE;
|
|
|
|
if( m_bWaveAlarm )
|
|
{
|
|
if( !m_bWaveOn )
|
|
{
|
|
PlaySound( m_pWaveFile, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP );
|
|
m_bWaveOn = TRUE;
|
|
}
|
|
}
|
|
else
|
|
m_EventAlarm.Set();
|
|
}
|
|
}
|
|
|
|
void CLgAlarm::Stop()
|
|
{
|
|
m_bAlarmOn = FALSE;
|
|
|
|
if( m_bWaveAlarm )
|
|
{
|
|
if( m_bWaveOn )
|
|
{
|
|
PlaySound( NULL, NULL, SND_PURGE );
|
|
m_bWaveOn = FALSE;
|
|
}
|
|
}
|
|
else
|
|
m_EventAlarm.Set();
|
|
}
|
|
|
|
DWORD CLgAlarm::ThreadProc()
|
|
{
|
|
HANDLE phEvent[] = { CLgThread::GetThreadStopEvent(), m_EventAlarm, m_EventBeep };
|
|
|
|
BOOL bVoice = TRUE;
|
|
BOOL bAlarmOn = FALSE;
|
|
|
|
while(TRUE)
|
|
{
|
|
switch( ::WaitForMultipleObjects( 3, phEvent, FALSE, m_bAlarmOn ? 0 : INFINITE ) )
|
|
{
|
|
case WAIT_OBJECT_0+0:
|
|
goto THREAD_EXIT;
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
bAlarmOn = m_bAlarmOn;
|
|
break;
|
|
|
|
case WAIT_OBJECT_0+2:
|
|
// make beep during 50 ms
|
|
if( Beep( 1500, 50UL, CLgThread::GetThreadStopEvent() ) )
|
|
goto THREAD_EXIT;
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
break;
|
|
|
|
default:
|
|
goto THREAD_EXIT;
|
|
}
|
|
|
|
if( bAlarmOn )
|
|
{
|
|
// change frequency
|
|
if( Beep( bVoice ? 1500 : 500, 100UL, CLgThread::GetThreadStopEvent() ) )
|
|
goto THREAD_EXIT;
|
|
bVoice = !bVoice;
|
|
}
|
|
}
|
|
|
|
THREAD_EXIT:
|
|
BuzzerOn( FALSE );
|
|
return 0UL;
|
|
}
|
|
|
|
BOOL CLgAlarm::Beep( DWORD dwFreq, DWORD dwDuration, HANDLE hEvent )
|
|
{
|
|
BOOL dwResult = FALSE;
|
|
|
|
// if Win95, Win98, WinMe
|
|
if( m_dwVersion & 0x80000000 )
|
|
{
|
|
BuzzerFrequency( dwFreq );
|
|
// on buzzer
|
|
BuzzerOn( TRUE );
|
|
// wait
|
|
if( hEvent ) dwResult = ::WaitForSingleObject( hEvent, dwDuration ) == WAIT_OBJECT_0;
|
|
else ::Sleep( dwDuration );
|
|
// off buzzer
|
|
BuzzerOn( FALSE );
|
|
}
|
|
else
|
|
{
|
|
::Beep( dwFreq, dwDuration );
|
|
}
|
|
|
|
return dwResult;
|
|
}
|
|
|
|
void CLgAlarm::BuzzerOn( BOOL bOn )
|
|
{
|
|
if( m_dwVersion & 0x80000000 )
|
|
{
|
|
if( bOn ) Outp( BEEPER_PPI_PORT, Inp( BEEPER_PPI_PORT ) | 3 );
|
|
else Outp( BEEPER_PPI_PORT, Inp( BEEPER_PPI_PORT ) & ~3 );
|
|
}
|
|
}
|
|
|
|
void CLgAlarm::BuzzerFrequency( INT nFrequency )
|
|
{
|
|
if( m_dwVersion & 0x80000000 )
|
|
{
|
|
Outp( BEEPER_SET_FREQUENCY_PORT, nFrequency );
|
|
Outp( BEEPER_SET_FREQUENCY_PORT, nFrequency >> 8 );
|
|
}
|
|
}
|
|
|
|
INT CLgAlarm::Outp(WORD port, INT databyte)
|
|
{
|
|
return ( m_dwVersion & 0x80000000 ) ? _outp( port, databyte ) : 0;
|
|
}
|
|
|
|
int CLgAlarm::Inp(WORD port)
|
|
{
|
|
return ( m_dwVersion & 0x80000000 ) ? _inp( port ) : 0;
|
|
}
|
|
|
|
BOOL CLgAlarm::Create( LPCTSTR pWaveFile )
|
|
{
|
|
m_bWaveAlarm = FALSE;
|
|
|
|
// check file existent
|
|
if( pWaveFile )
|
|
{
|
|
// determine file existence
|
|
DWORD dwFileAttr = ::GetFileAttributes( pWaveFile );
|
|
m_bWaveAlarm = ( dwFileAttr != (((DWORD)-1))/*INVALID_FILE_ATTRIBUTES*/ );
|
|
|
|
// copy file name
|
|
if( m_bWaveAlarm )
|
|
_tcscpy( m_pWaveFile, pWaveFile );
|
|
}
|
|
|
|
// init buzzer
|
|
Outp( BEEPER_SET_TIMER_CHANNEL_PORT, 0xB6 );
|
|
|
|
// init buzzer frequency
|
|
BuzzerFrequency( 1500 );
|
|
|
|
// stop buzzer
|
|
BuzzerOn( FALSE );
|
|
|
|
// set defaults
|
|
m_bEnabled = TRUE;
|
|
m_bAlarmOn = FALSE;
|
|
|
|
// start thread
|
|
return CLgThread::Start( FALSE );
|
|
}
|
|
|
|
void CLgAlarm::Destroy()
|
|
{
|
|
// stop thread
|
|
CLgThread::Stop( TRUE );
|
|
|
|
// stop buzzer
|
|
BuzzerOn( FALSE );
|
|
|
|
// stop sound (if any)
|
|
if( m_bWaveOn )
|
|
{
|
|
PlaySound( NULL, NULL, SND_PURGE );
|
|
m_bWaveOn = FALSE;
|
|
}
|
|
}
|