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.

266 lines
5.6 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: 05/02/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Alarm
  8. // FILE: Alarm.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file for implemenation of class CAlarm
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "Alarm.h"
  16. #include <ConIO.h>
  17. #include "..\..\LgLib\Version.h"
  18. CAlarm::CAlarm()
  19. {
  20. // m_fEnabled = FALSE;
  21. // return;
  22. m_dwVersion = ::GetVersion();
  23. m_fEnabled = TRUE;
  24. Outp( BEEPER_SET_TIMER_CHANNEL_PORT, 0XB6 );
  25. Outp( BEEPER_SET_FREQUENCY_PORT, 1500 );
  26. Outp( BEEPER_SET_FREQUENCY_PORT, 1500 >> 8 );
  27. m_BeeperOff = Inp( BEEPER_PPI_PORT );
  28. m_BeeperOn = m_BeeperOff | 3 ;
  29. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  30. DWORD dwThreadID;
  31. m_hEventClose = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  32. m_hEventBeep = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  33. m_hEventAlarm = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  34. m_hThreadBeep = ::CreateThread( NULL, 0, ThreadProcAlarmBeep, this, 0, &dwThreadID );
  35. m_hEventWave = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  36. m_hThreadWave = ::CreateThread( NULL, 0, ThreadProcAlarmWave, this, 0, &dwThreadID );
  37. }
  38. CAlarm::~CAlarm()
  39. {
  40. if( m_hThreadBeep )
  41. {
  42. ::ResetEvent( m_hEventAlarm );
  43. ::ResetEvent( m_hEventBeep );
  44. ::SetEvent( m_hEventClose );
  45. ::WaitForSingleObject( m_hThreadBeep, 1000UL );
  46. ::CloseHandle( m_hThreadBeep );
  47. }
  48. if( m_hThreadWave )
  49. {
  50. ::ResetEvent( m_hEventWave );
  51. ::SetEvent( m_hEventClose );
  52. ::WaitForSingleObject( m_hThreadWave, 1000UL );
  53. ::CloseHandle( m_hThreadWave );
  54. }
  55. if( m_hEventClose )
  56. {
  57. ::CloseHandle( m_hEventClose );
  58. }
  59. if( m_hEventBeep )
  60. {
  61. ::CloseHandle( m_hEventBeep );
  62. }
  63. if( m_hEventAlarm )
  64. {
  65. ::CloseHandle( m_hEventAlarm );
  66. }
  67. if( m_hEventWave )
  68. {
  69. ::CloseHandle( m_hEventWave );
  70. }
  71. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  72. }
  73. void CAlarm::Enable( BOOL fEnable )
  74. {
  75. return;
  76. if( fEnable )
  77. m_fEnabled = TRUE;
  78. else
  79. {
  80. if( m_fEnabled )
  81. {
  82. AlarmEnd();
  83. WaveStop();
  84. }
  85. m_fEnabled = FALSE;
  86. }
  87. }
  88. void CAlarm::Beep()
  89. {
  90. if( m_fEnabled )
  91. {
  92. if( m_hThreadBeep )
  93. ::SetEvent( m_hEventBeep );
  94. else
  95. {
  96. Outp( BEEPER_PPI_PORT, m_BeeperOn );
  97. ::Sleep( 20 );
  98. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  99. }
  100. }
  101. }
  102. void CAlarm::AlarmBegin()
  103. {
  104. if( m_fEnabled )
  105. {
  106. if( m_hThreadBeep )
  107. ::SetEvent( m_hEventAlarm );
  108. else
  109. Outp( BEEPER_PPI_PORT, m_BeeperOn );
  110. }
  111. }
  112. void CAlarm::AlarmEnd()
  113. {
  114. if( m_hThreadBeep )
  115. ::ResetEvent( m_hEventAlarm );
  116. else
  117. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  118. }
  119. DWORD CAlarm::ThreadProcBeep()
  120. {
  121. __try
  122. {
  123. HANDLE phEvent[] = { m_hEventClose, m_hEventAlarm, m_hEventBeep };
  124. BOOL bVoice = TRUE;
  125. while(TRUE)
  126. {
  127. switch( ::WaitForMultipleObjects( 3, phEvent, FALSE, INFINITE ) )
  128. {
  129. case WAIT_OBJECT_0+0: goto THREAD_EXIT;
  130. case WAIT_OBJECT_0+1:
  131. if( bVoice )
  132. {
  133. Outp( BEEPER_SET_FREQUENCY_PORT, 1500 );
  134. Outp( BEEPER_SET_FREQUENCY_PORT, 1500 >> 8 );
  135. bVoice = FALSE;
  136. }
  137. else
  138. {
  139. Outp( BEEPER_SET_FREQUENCY_PORT, 500 );
  140. Outp( BEEPER_SET_FREQUENCY_PORT, 500 >> 8 );
  141. bVoice = TRUE;
  142. }
  143. Outp( BEEPER_PPI_PORT, m_BeeperOn );
  144. if( ::WaitForSingleObject( m_hEventClose, 100 ) == WAIT_OBJECT_0 ) goto THREAD_EXIT;
  145. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  146. break;
  147. case WAIT_OBJECT_0+2:
  148. Outp( BEEPER_PPI_PORT, m_BeeperOn );
  149. if( ::WaitForSingleObject( m_hEventClose, 20 ) == WAIT_OBJECT_0 ) goto THREAD_EXIT;
  150. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  151. break;
  152. }
  153. }
  154. THREAD_EXIT:
  155. Outp( BEEPER_PPI_PORT, m_BeeperOff );
  156. }
  157. __except(1)
  158. {
  159. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CAlarm::ThreadProcBeep"), "CAlarm::ThreadProcBeep" );
  160. }
  161. return 0UL;
  162. }
  163. DWORD CAlarm::ThreadProcWave()
  164. {
  165. return 0;
  166. BOOL bResult = FALSE;
  167. __try
  168. {
  169. HANDLE phEvent[] = { m_hEventClose, m_hEventWave };
  170. while(TRUE)
  171. {
  172. LPCTSTR pFileName;
  173. switch( ::WaitForMultipleObjects( 2, phEvent, FALSE, INFINITE ) )
  174. {
  175. case WAIT_OBJECT_0+0: goto THREAD_EXIT;
  176. case WAIT_OBJECT_0+1:
  177. __try
  178. {
  179. bResult = m_WaveQueue.GetMessage( pFileName );
  180. }
  181. __except( 1 )
  182. {
  183. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CAlarm::ThreadProcWave"), "m_WaveQueue.GetMessage" );
  184. }
  185. if( bResult )
  186. {
  187. __try
  188. {
  189. sndPlaySound( pFileName, SND_SYNC | SND_NODEFAULT );
  190. }
  191. __except( 1 )
  192. {
  193. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CAlarm::ThreadProcWave"), "sndPlaySound" );
  194. }
  195. ::SetEvent( m_hEventWave );
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. __except(1)
  202. {
  203. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CAlarm::ThreadProcWave"), "CAlarm::ThreadProcWave" );
  204. }
  205. THREAD_EXIT:
  206. return 0UL;
  207. }
  208. INT CAlarm::Outp(WORD port, INT databyte)
  209. {
  210. if( m_dwVersion & 0x80000000 )
  211. return _outp( port, databyte );
  212. return 0;
  213. }
  214. int CAlarm::Inp(WORD port)
  215. {
  216. if( m_dwVersion & 0x80000000 )
  217. return _inp( port );
  218. return 0;
  219. }
  220. void CAlarm::WaveStart( LPCTSTR *ppFileNames, int nCount, BOOL bStopPrev )
  221. {
  222. return;
  223. if( m_fEnabled )
  224. {
  225. if( bStopPrev )
  226. WaveStop();
  227. if( m_WaveQueue.IsEmpty() )
  228. {
  229. for( int i = 0; i < nCount; i++ )
  230. m_WaveQueue.PostMessage( ppFileNames[i] );
  231. ::SetEvent( m_hEventWave );
  232. }
  233. }
  234. }
  235. void CAlarm::WaveStop()
  236. {
  237. return;
  238. sndPlaySound( NULL, SND_NODEFAULT );
  239. m_WaveQueue.Clear();
  240. }