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.

174 lines
4.6 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: LgNetDeviceGPIOCOM.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgNetDeviceGPIOCOM.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgNetDeviceGPIOCOM.h"
  16. CLgNetDeviceGPIOCOM::CLgNetDeviceGPIOCOM()
  17. {
  18. }
  19. CLgNetDeviceGPIOCOM::~CLgNetDeviceGPIOCOM()
  20. {
  21. Destroy();
  22. }
  23. BOOL CLgNetDeviceGPIOCOM::Create( LPCTSTR pPort, BOOL bSelfTest, INT nSelfTestTimeout )
  24. {
  25. if( !CLgDeviceGPIOCOM::Create( pPort, bSelfTest, nSelfTestTimeout ) )
  26. return FALSE;
  27. // CLgNetDevice::SetID( 1 );
  28. m_OutMsg.SetSenderID( CLgNetDevice::GetID() );
  29. // attach to itself
  30. return CLgThread::Start( FALSE );
  31. }
  32. void CLgNetDeviceGPIOCOM::Destroy()
  33. {
  34. // nessesary to do it, because "vtable" will be cleared before
  35. // parent's destructor will be called
  36. CLgThread::Stop( TRUE );
  37. CLgDeviceGPIOCOM::Destroy();
  38. }
  39. DWORD CLgNetDeviceGPIOCOM::ThreadProc()
  40. {
  41. BOOL bLoop = TRUE;
  42. while( bLoop )
  43. {
  44. // as fast as possible but without overheating :)
  45. switch( ::WaitForSingleObject( CLgThread::GetThreadStopEvent(), 1UL ) )
  46. {
  47. default:
  48. case WAIT_FAILED:
  49. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgNetDeviceGPIOCOM::ThreadProc"), _T("WaitForSingleObject failed with 0x%08X"), ::GetLastError() );
  50. case WAIT_OBJECT_0:
  51. bLoop = FALSE;
  52. continue;
  53. case WAIT_TIMEOUT:
  54. break;
  55. }
  56. // check input command
  57. if( CLgDevice::IsReadyForCmd() && CLgNetDevice::PopMessage( m_InMsg ) )
  58. {
  59. m_OutMsg.SetDistinationID( m_InMsg.GetSenderID() );
  60. // depend on command
  61. BYTE bCommand;
  62. if( m_InMsg.GetCommand( &bCommand ) )
  63. {
  64. CLgDeviceGPIO::GPIO_PIN pin;
  65. switch( bCommand )
  66. {
  67. case CLgDeviceGPIO::CMD_GET_OUTPUT:
  68. if( m_InMsg.GetOutput( &pin ) )
  69. CmdGetOutput( pin.nIndex );
  70. break;
  71. case CLgDeviceGPIO::CMD_GET_OUTPUT_ALL:
  72. CmdGetOutputAll();
  73. break;
  74. case CLgDeviceGPIO::CMD_SET_OUTPUT:
  75. if( m_InMsg.GetOutput( &pin ) )
  76. CmdSetOutput( pin.nIndex, pin.bOn );
  77. break;
  78. case CLgDeviceGPIO::CMD_GET_INPUT:
  79. if( m_InMsg.GetInput( &pin ) )
  80. CmdGetInput( pin.nIndex );
  81. break;
  82. case CLgDeviceGPIO::CMD_GET_INPUT_ALL:
  83. CmdGetInputAll();
  84. break;
  85. default:
  86. OnEvtCommandError( -1, _T("UNKNOWN COMMAND") );
  87. break;
  88. }
  89. }
  90. }
  91. // call port processing routine
  92. CLgDeviceGPIOCOM::ReadWriteProc();
  93. }
  94. return 0UL;
  95. }
  96. void CLgNetDeviceGPIOCOM::OnEvtGetOutput( INT nOutput, BOOL bOn )
  97. {
  98. m_OutMsg.BuildEvtGetOutput( nOutput, bOn );
  99. SendMessageToParent( m_OutMsg );
  100. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtGetOutput", "%d,%d", nOutput, bOn );
  101. }
  102. void CLgNetDeviceGPIOCOM::OnEvtSetOutput( INT nOutput, BOOL bOn )
  103. {
  104. m_OutMsg.BuildEvtSetOutput( nOutput, bOn );
  105. SendMessageToParent( m_OutMsg );
  106. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtSetOutput", "%d,%d", nOutput, bOn );
  107. }
  108. void CLgNetDeviceGPIOCOM::OnEvtGetInput( INT nInput, BOOL bOn )
  109. {
  110. m_OutMsg.BuildEvtGetInput( nInput, bOn );
  111. SendMessageToParent( m_OutMsg );
  112. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtGetInput", "%d,%d", nInput, bOn );
  113. }
  114. void CLgNetDeviceGPIOCOM::OnEvtInputChange( INT nInput, BOOL bOn )
  115. {
  116. m_OutMsg.BuildEvtInputChange( nInput, bOn );
  117. SendMessageToParent( m_OutMsg );
  118. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtInputChange", "%d,%d", nInput, bOn );
  119. }
  120. void CLgNetDeviceGPIOCOM::OnEvtGetOutputAll( PGPIO_PINS pPins )
  121. {
  122. m_OutMsg.BuildEvtGetOutputAll( pPins );
  123. SendMessageToParent( m_OutMsg );
  124. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtGetOutputAll" );
  125. }
  126. void CLgNetDeviceGPIOCOM::OnEvtGetInputAll( PGPIO_PINS pPins )
  127. {
  128. m_OutMsg.BuildEvtGetInputAll( pPins );
  129. SendMessageToParent( m_OutMsg );
  130. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtGetInputAll" );
  131. }
  132. void CLgNetDeviceGPIOCOM::OnEvtCommandError( INT nCommand, LPCTSTR pFormat, ... )
  133. {
  134. va_list marker;
  135. va_start( marker, pFormat );
  136. m_OutMsg.BuildEvtCommandError( nCommand, pFormat, marker );
  137. va_end( marker );
  138. CLgNetObject::SendMessageToParent( m_OutMsg );
  139. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgNetDeviceGPIOCOM::OnEvtCommandError" );
  140. }