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.

235 lines
4.4 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 19/04/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM: LgAlarm
  8. // FILE: LgAlarm.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file for implemenation of class CLgAlarm
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "..\LgLib\LgLogger.h"
  17. #include "LgAlarm.h"
  18. CLgAlarm::CLgAlarm()
  19. {
  20. // init registers
  21. m_dwVersion = ::GetVersion();
  22. // set defaults
  23. m_bEnabled = TRUE;
  24. m_bAlarmOn = FALSE;
  25. // stop buzzer
  26. BuzzerOn( FALSE );
  27. // reset
  28. m_bWaveAlarm = FALSE;
  29. memset( m_pWaveFile, 0, sizeof(m_pWaveFile) );
  30. m_bWaveOn = FALSE;
  31. }
  32. CLgAlarm::~CLgAlarm()
  33. {
  34. Destroy();
  35. }
  36. void CLgAlarm::Enable( BOOL bEnable )
  37. {
  38. if( ( !bEnable ) && m_bEnabled )
  39. Stop();
  40. m_bEnabled = bEnable;
  41. }
  42. void CLgAlarm::Beep()
  43. {
  44. if( m_bEnabled )
  45. m_EventBeep.Set();
  46. }
  47. void CLgAlarm::Start()
  48. {
  49. if( m_bEnabled )
  50. {
  51. m_bAlarmOn = TRUE;
  52. if( m_bWaveAlarm )
  53. {
  54. if( !m_bWaveOn )
  55. {
  56. PlaySound( m_pWaveFile, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP );
  57. m_bWaveOn = TRUE;
  58. }
  59. }
  60. else
  61. m_EventAlarm.Set();
  62. }
  63. }
  64. void CLgAlarm::Stop()
  65. {
  66. m_bAlarmOn = FALSE;
  67. if( m_bWaveAlarm )
  68. {
  69. if( m_bWaveOn )
  70. {
  71. PlaySound( NULL, NULL, SND_PURGE );
  72. m_bWaveOn = FALSE;
  73. }
  74. }
  75. else
  76. m_EventAlarm.Set();
  77. }
  78. DWORD CLgAlarm::ThreadProc()
  79. {
  80. HANDLE phEvent[] = { CLgThread::GetThreadStopEvent(), m_EventAlarm, m_EventBeep };
  81. BOOL bVoice = TRUE;
  82. BOOL bAlarmOn = FALSE;
  83. while(TRUE)
  84. {
  85. switch( ::WaitForMultipleObjects( 3, phEvent, FALSE, m_bAlarmOn ? 0 : INFINITE ) )
  86. {
  87. case WAIT_OBJECT_0+0:
  88. goto THREAD_EXIT;
  89. case WAIT_OBJECT_0+1:
  90. bAlarmOn = m_bAlarmOn;
  91. break;
  92. case WAIT_OBJECT_0+2:
  93. // make beep during 50 ms
  94. if( Beep( 1500, 50UL, CLgThread::GetThreadStopEvent() ) )
  95. goto THREAD_EXIT;
  96. break;
  97. case WAIT_TIMEOUT:
  98. break;
  99. default:
  100. goto THREAD_EXIT;
  101. }
  102. if( bAlarmOn )
  103. {
  104. // change frequency
  105. if( Beep( bVoice ? 1500 : 500, 100UL, CLgThread::GetThreadStopEvent() ) )
  106. goto THREAD_EXIT;
  107. bVoice = !bVoice;
  108. }
  109. }
  110. THREAD_EXIT:
  111. BuzzerOn( FALSE );
  112. return 0UL;
  113. }
  114. BOOL CLgAlarm::Beep( DWORD dwFreq, DWORD dwDuration, HANDLE hEvent )
  115. {
  116. BOOL dwResult = FALSE;
  117. // if Win95, Win98, WinMe
  118. if( m_dwVersion & 0x80000000 )
  119. {
  120. BuzzerFrequency( dwFreq );
  121. // on buzzer
  122. BuzzerOn( TRUE );
  123. // wait
  124. if( hEvent ) dwResult = ::WaitForSingleObject( hEvent, dwDuration ) == WAIT_OBJECT_0;
  125. else ::Sleep( dwDuration );
  126. // off buzzer
  127. BuzzerOn( FALSE );
  128. }
  129. else
  130. {
  131. ::Beep( dwFreq, dwDuration );
  132. }
  133. return dwResult;
  134. }
  135. void CLgAlarm::BuzzerOn( BOOL bOn )
  136. {
  137. if( m_dwVersion & 0x80000000 )
  138. {
  139. if( bOn ) Outp( BEEPER_PPI_PORT, Inp( BEEPER_PPI_PORT ) | 3 );
  140. else Outp( BEEPER_PPI_PORT, Inp( BEEPER_PPI_PORT ) & ~3 );
  141. }
  142. }
  143. void CLgAlarm::BuzzerFrequency( INT nFrequency )
  144. {
  145. if( m_dwVersion & 0x80000000 )
  146. {
  147. Outp( BEEPER_SET_FREQUENCY_PORT, nFrequency );
  148. Outp( BEEPER_SET_FREQUENCY_PORT, nFrequency >> 8 );
  149. }
  150. }
  151. INT CLgAlarm::Outp(WORD port, INT databyte)
  152. {
  153. return ( m_dwVersion & 0x80000000 ) ? _outp( port, databyte ) : 0;
  154. }
  155. int CLgAlarm::Inp(WORD port)
  156. {
  157. return ( m_dwVersion & 0x80000000 ) ? _inp( port ) : 0;
  158. }
  159. BOOL CLgAlarm::Create( LPCTSTR pWaveFile )
  160. {
  161. m_bWaveAlarm = FALSE;
  162. // check file existent
  163. if( pWaveFile )
  164. {
  165. // determine file existence
  166. DWORD dwFileAttr = ::GetFileAttributes( pWaveFile );
  167. m_bWaveAlarm = ( dwFileAttr != (((DWORD)-1))/*INVALID_FILE_ATTRIBUTES*/ );
  168. // copy file name
  169. if( m_bWaveAlarm )
  170. _tcscpy( m_pWaveFile, pWaveFile );
  171. }
  172. // init buzzer
  173. Outp( BEEPER_SET_TIMER_CHANNEL_PORT, 0xB6 );
  174. // init buzzer frequency
  175. BuzzerFrequency( 1500 );
  176. // stop buzzer
  177. BuzzerOn( FALSE );
  178. // set defaults
  179. m_bEnabled = TRUE;
  180. m_bAlarmOn = FALSE;
  181. // start thread
  182. return CLgThread::Start( FALSE );
  183. }
  184. void CLgAlarm::Destroy()
  185. {
  186. // stop thread
  187. CLgThread::Stop( TRUE );
  188. // stop buzzer
  189. BuzzerOn( FALSE );
  190. // stop sound (if any)
  191. if( m_bWaveOn )
  192. {
  193. PlaySound( NULL, NULL, SND_PURGE );
  194. m_bWaveOn = FALSE;
  195. }
  196. }