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.

212 lines
5.0 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: EventQueue.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of EventQueue.cpp
  13. //----------------------------------------------------------------------------
  14. // EventQueue.cpp: implementation of the CEventQueue class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. //#include "chicago.h"
  19. #include "AuxLib.h"
  20. #include "..\..\LgLib\stdafx.h"
  21. #include "EventQueue.h"
  22. #include "..\..\LgLib\Version.h"
  23. /*
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char THIS_FILE[]=__FILE__;
  27. #define new DEBUG_NEW
  28. #endif
  29. */
  30. //////////////////////////////////////////////////////////////////////
  31. // Construction/Destruction
  32. //////////////////////////////////////////////////////////////////////
  33. CEventQueue::CEventQueue( int nBufferSize )
  34. {
  35. m_Begin = 0;
  36. m_End = 0;
  37. m_nBufferSize = nBufferSize;
  38. m_pBuffer = new EVENT[ m_nBufferSize ];
  39. m_hMutex = ::CreateMutex( NULL, FALSE, NULL );
  40. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventQueue::CEventQueue") );
  41. }
  42. CEventQueue::~CEventQueue()
  43. {
  44. ::CloseHandle( m_hMutex );
  45. delete [] m_pBuffer;
  46. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventQueue::~CEventQueue") );
  47. }
  48. BOOL CEventQueue::PostEvent( BYTE event, BYTE data8, WORD data16, DWORD data32, COLORREF color, LPSTR pFormat, ... )
  49. {
  50. ::WaitForSingleObject( m_hMutex, INFINITE );
  51. int newEnd = ( m_End + 1 ) % m_nBufferSize;
  52. if( m_Begin == newEnd )
  53. {
  54. ::ReleaseMutex( m_hMutex );
  55. return FALSE;
  56. }
  57. m_pBuffer[ m_End ].id = event;
  58. m_pBuffer[ m_End ].data8 = data8;
  59. m_pBuffer[ m_End ].data16 = data16;
  60. m_pBuffer[ m_End ].data32 = data32;
  61. m_pBuffer[ m_End ].color = color;
  62. // place text
  63. if( pFormat )
  64. {
  65. const int nMaxCount = sizeof(m_pBuffer[ m_End ].pText);
  66. va_list pMarker;
  67. va_start( pMarker, pFormat );
  68. int iCount = _vsntprintf( m_pBuffer[ m_End ].pText, nMaxCount, pFormat, pMarker );
  69. if( iCount < 0 ) iCount = nMaxCount;
  70. va_end( pMarker );
  71. m_pBuffer[m_End].pText[iCount] = 0;
  72. }
  73. else
  74. m_pBuffer[m_End].pText[0] = 0;
  75. SYSTEMTIME st;
  76. ::GetLocalTime( &st );
  77. ::SystemTimeToFileTime( &st, (LPFILETIME)&m_pBuffer[ m_End ].time );
  78. m_End = newEnd;
  79. ::ReleaseMutex( m_hMutex );
  80. return TRUE;
  81. }
  82. BOOL CEventQueue::PostEvent( EVENT &event )
  83. {
  84. ::WaitForSingleObject( m_hMutex, INFINITE );
  85. int newEnd = ( m_End + 1 ) % m_nBufferSize;
  86. if( m_Begin == newEnd )
  87. {
  88. ::ReleaseMutex( m_hMutex );
  89. return FALSE;
  90. }
  91. m_pBuffer[ m_End ] = event;
  92. m_End = newEnd;
  93. ::ReleaseMutex( m_hMutex );
  94. return TRUE;
  95. }
  96. BOOL CEventQueue::PostEventImageCapture( CSensor* pSensor, HBITMAP hBitmap )
  97. {
  98. EVENT event;
  99. BuildEventImageCapture( &event, pSensor, hBitmap );
  100. return PostEvent( event );
  101. }
  102. /*
  103. //try to optimize queue :-)
  104. BOOL CEventQueue::PostEvent( EVENT &event )
  105. {
  106. ::WaitForSingleObject( m_hMutex, INFINITE );
  107. if( m_nCount + 1 > m_nSize )
  108. {
  109. int nSize = m_nSize + 128;
  110. LPEVENT pBuffer = new EVENT[ nSize ];
  111. ::CopyMemory( pBuffer, m_pBuffer + m_nPos, sizeof(EVENT) * ( m_nSize - m_nPos ) );
  112. ::CopyMemory( pBuffer + m_nSize - m_nPos, m_pBuffer + m_nPos, sizeof(EVENT) * ( m_nSize - m_nPos ) );
  113. delete [] m_pBuffer;
  114. m_nSize = nSize;
  115. m_pBuffer = pBuffer;
  116. m_nPos = 0;
  117. }
  118. m_pBuffer[ m_nPos ] = event;
  119. m_nPos = ( m_nPos + 1 ) % m_nSize;
  120. m_nCount++;
  121. ::ReleaseMutex( m_hMutex );
  122. return FALSE;
  123. }
  124. */
  125. BOOL CEventQueue::GetEvent(EVENT &event)
  126. {
  127. ::WaitForSingleObject( m_hMutex, INFINITE );
  128. if( m_Begin == m_End )
  129. {
  130. ::ReleaseMutex( m_hMutex );
  131. return FALSE;
  132. }
  133. event = m_pBuffer[ m_Begin ];
  134. m_Begin = ( m_Begin + 1 ) % m_nBufferSize;
  135. ::ReleaseMutex( m_hMutex );
  136. return TRUE;
  137. }
  138. void CEventQueue::Clear()
  139. {
  140. ::WaitForSingleObject( m_hMutex, INFINITE );
  141. m_Begin = m_End;
  142. ::ReleaseMutex( m_hMutex );
  143. }
  144. BOOL CEventQueue::IsEmpty()
  145. {
  146. ::WaitForSingleObject( m_hMutex, INFINITE );
  147. BOOL bIsEmpty = ( m_Begin == m_End );
  148. ::ReleaseMutex( m_hMutex );
  149. return bIsEmpty;
  150. }
  151. void GetEventString( LPEVENT pEvent, CString& str )
  152. {
  153. SYSTEMTIME st;
  154. ::FileTimeToSystemTime( (LPFILETIME)&pEvent->time, &st );
  155. str.Format( "%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s",
  156. st.wMonth, st.wDay, st.wYear,
  157. st.wHour, st.wMinute, st.wSecond, pEvent->pText );
  158. }
  159. void CEventQueue::BuildEventTime( PLARGE_INTEGER pTime )
  160. {
  161. FILETIME SystemTime;
  162. ASSERT(pTime);
  163. ::GetSystemTimeAsFileTime( &SystemTime );
  164. ::FileTimeToLocalFileTime( &SystemTime, (LPFILETIME)pTime );
  165. }
  166. void CEventQueue::BuildEventImageCapture( PEVENT pEvent, CSensor* pSensor, HBITMAP hBitmap )
  167. {
  168. BuildEventTime( &pEvent->time );
  169. pEvent->id = LGS_EVENT_IMAGE_CAPTURE;
  170. pEvent->pSensor = pSensor;
  171. pEvent->hBitmap = hBitmap;
  172. }