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.
309 lines
8.1 KiB
309 lines
8.1 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgDeviceGPIOLPT.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgDeviceGPIOLPT.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LgDeviceGpioLPT.h"
|
|
#include "..\KernelIO\KernelIO.h"
|
|
|
|
DWORD CLgDeviceGPIOLPT::s_bControlBits[13] =
|
|
{
|
|
GPIO_LPT_I_ASK,
|
|
GPIO_LPT_I_BUZY,
|
|
GPIO_LPT_I_PAPEREND,
|
|
GPIO_LPT_I_SELECT,
|
|
GPIO_LPT_I_ERROR,
|
|
GPIO_LPT_IO_DATA_0,
|
|
GPIO_LPT_IO_DATA_1,
|
|
GPIO_LPT_IO_DATA_2,
|
|
GPIO_LPT_IO_DATA_3,
|
|
GPIO_LPT_IO_DATA_4,
|
|
GPIO_LPT_IO_DATA_5,
|
|
GPIO_LPT_IO_DATA_6,
|
|
GPIO_LPT_IO_DATA_7,
|
|
};
|
|
|
|
BOOL CLgDeviceGPIOLPT::s_bInvertBits[13] =
|
|
{
|
|
TRUE,
|
|
FALSE,
|
|
TRUE,
|
|
TRUE,
|
|
TRUE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE,
|
|
FALSE
|
|
};
|
|
|
|
CLgDeviceGPIOLPT::CLgDeviceGPIOLPT()
|
|
{
|
|
m_BaseAddress = 0;
|
|
m_dwInputState = 0;
|
|
m_dwOutputState = 0;
|
|
m_bDataOutput = TRUE;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::CLgDeviceGPIOLPT" );
|
|
}
|
|
|
|
CLgDeviceGPIOLPT::~CLgDeviceGPIOLPT()
|
|
{
|
|
Destroy();
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::~CLgDeviceGPIOLPT" );
|
|
}
|
|
|
|
BOOL CLgDeviceGPIOLPT::Create( LPCTSTR pPort, BOOL bDataOutput )
|
|
{
|
|
// store port direction
|
|
m_bDataOutput = bDataOutput;
|
|
|
|
if( m_bDataOutput )
|
|
{
|
|
if( !CLgDeviceGPIO::Create( 5, 8 ) )
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if( !CLgDeviceGPIO::Create( 13, 0 ) )
|
|
return FALSE;
|
|
}
|
|
|
|
// initialize driver
|
|
if( !InitializeWinIo() )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgDeviceGPIOLPT::Create", "Can't initialize WinIO, probably no drivers" );
|
|
return FALSE;
|
|
}
|
|
|
|
// set base address
|
|
if( !_tcsnicmp( pPort, _T("LPT1"), 4 ) )
|
|
m_BaseAddress = 0x378;
|
|
else
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "Only LPT1 is implemented" );
|
|
return FALSE;
|
|
}
|
|
|
|
// store current input state
|
|
if( !GetPortVal( m_BaseAddress, &m_dwSavedState, 4 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
|
|
m_dwOutputState = 0;
|
|
|
|
// initialize parallel port
|
|
if( m_bDataOutput )
|
|
{
|
|
DWORD dwValue;
|
|
|
|
// set data as output
|
|
if( !GetPortVal( m_BaseAddress + 2, &dwValue, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
if( !SetPortVal( m_BaseAddress + 2, dwValue & ~0x20, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
|
|
// store initial state of inputs
|
|
if( !GetPortVal( m_BaseAddress + 1, &dwValue, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
m_dwInputState = dwValue;
|
|
|
|
// clear all outputs
|
|
if( !SetPortVal( m_BaseAddress, m_dwOutputState, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DWORD dwValue;
|
|
|
|
// set data as input
|
|
if( !GetPortVal( m_BaseAddress + 2, &dwValue, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
if( !SetPortVal( m_BaseAddress + 2, dwValue | 0x20, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
|
|
// store initial state of data inputs
|
|
if( !GetPortVal( m_BaseAddress + 1, &dwValue, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
m_dwInputState = dwValue;
|
|
if( !GetPortVal( m_BaseAddress, &dwValue, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
|
|
}
|
|
m_dwInputState |= (dwValue << 8);
|
|
}
|
|
|
|
// init gisteresis
|
|
for( INT i = 0 ; i < 13; i++ )
|
|
m_pGisterezis[i].bSavedOn = ( (m_dwInputState & s_bControlBits[i]) == s_bControlBits[i] );
|
|
|
|
LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::Create", "%d,%d,%d,%d,%d,"
|
|
"%d,%d,%d,%d,%d,"
|
|
"%d,%d,%d",
|
|
m_pGisterezis[0].bSavedOn, m_pGisterezis[1].bSavedOn, m_pGisterezis[2].bSavedOn, m_pGisterezis[3].bSavedOn, m_pGisterezis[4].bSavedOn,
|
|
m_pGisterezis[5].bSavedOn, m_pGisterezis[6].bSavedOn, m_pGisterezis[7].bSavedOn, m_pGisterezis[8].bSavedOn, m_pGisterezis[9].bSavedOn,
|
|
m_pGisterezis[10].bSavedOn, m_pGisterezis[11].bSavedOn, m_pGisterezis[12].bSavedOn );
|
|
|
|
/*
|
|
if( !CLgDeviceGPIO::InitInputState( 0, m_dwInputState ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "CLgDeviceGPIO::InitInputState failed" );
|
|
return FALSE;
|
|
}
|
|
*/
|
|
return TRUE;
|
|
}
|
|
|
|
void CLgDeviceGPIOLPT::Destroy()
|
|
{
|
|
// deitialize driver
|
|
ShutdownWinIo();
|
|
}
|
|
|
|
BOOL CLgDeviceGPIOLPT::ReadWriteProc()
|
|
{
|
|
INT nCmd = GetCmd();
|
|
switch( nCmd )
|
|
{
|
|
case CMD_GET_INPUT:
|
|
// avalible for new commands
|
|
ReleaseCmd();
|
|
// call handler
|
|
OnEvtGetInput( m_Input, s_bInvertBits[m_Input] ? !m_pGisterezis[m_Input].bSavedOn : m_pGisterezis[m_Input].bSavedOn );
|
|
break;
|
|
|
|
case CMD_GET_INPUT_ALL:
|
|
{
|
|
// avalible for new commands
|
|
ReleaseCmd();
|
|
// prepare input list
|
|
for( INT i = 0; i < GetInputCount(); i++ )
|
|
GetInputPins()->pPin[i].bOn = ( s_bInvertBits[i] ? !m_pGisterezis[i].bSavedOn : m_pGisterezis[i].bSavedOn );
|
|
// call handler
|
|
OnEvtGetInputAll( GetInputPins() );
|
|
}
|
|
break;
|
|
|
|
case CMD_SET_OUTPUT:
|
|
{
|
|
ReleaseCmd();
|
|
|
|
if( m_Output.bOn ) m_dwOutputState |= ( 1 << m_Output.nIndex );
|
|
else m_dwOutputState &= ~( 1 << m_Output.nIndex );
|
|
|
|
SetPortVal( m_BaseAddress, m_dwOutputState, 1 );
|
|
|
|
OnEvtSetOutput( m_Output.nIndex, m_Output.bOn );
|
|
}
|
|
break;
|
|
|
|
case CMD_GET_OUTPUT_ALL:
|
|
{
|
|
// avalible for new commands
|
|
ReleaseCmd();
|
|
// prepare input list
|
|
for( INT i = 0; i < GetOutputCount(); i++ )
|
|
{
|
|
DWORD dwBit = 1 << i;
|
|
GetOutputPins()->pPin[i].bOn = (m_dwOutputState & dwBit ) == dwBit;
|
|
}
|
|
// call handler
|
|
OnEvtGetOutputAll( GetOutputPins() );
|
|
}
|
|
break;
|
|
|
|
case CMD_NONE:
|
|
break;
|
|
|
|
default:
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgDeviceGPIOLPT::ReadWriteProc", "received unsupported command %d", nCmd );
|
|
ReleaseCmd();
|
|
OnEvtCommandError( nCmd, _T("UNSUPPORTED") );
|
|
break;
|
|
}
|
|
|
|
// get current time
|
|
LARGE_INTEGER liTime;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&liTime );
|
|
|
|
// read state of control bits
|
|
DWORD dwInputState = 0;
|
|
INT nBitCount = 5;
|
|
if( !GetPortVal( m_BaseAddress+1, &dwInputState, 1 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::ReadWriteProc", "GetPortVal failed" );
|
|
}
|
|
if( !m_bDataOutput )
|
|
{
|
|
DWORD dwDataPort = 0;
|
|
if( !GetPortVal( m_BaseAddress, &dwDataPort, 4 ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::ReadWriteProc", "GetPortVal failed" );
|
|
}
|
|
dwInputState |= ( dwDataPort << 8 );
|
|
nBitCount += 8;
|
|
}
|
|
|
|
// check changes
|
|
if( dwInputState != m_dwInputState )
|
|
{
|
|
DWORD dwChangedBits = dwInputState ^ m_dwInputState;
|
|
for( INT i = 0; i < nBitCount; i++ )
|
|
{
|
|
DWORD dwBit = s_bControlBits[i];
|
|
if( dwChangedBits & dwBit )
|
|
{
|
|
// update times
|
|
m_pGisterezis[i].bCurrentOn = (dwInputState & dwBit) == dwBit;
|
|
m_pGisterezis[i].liTime.QuadPart = liTime.QuadPart + GPIO_GISTERESIS_TIME;
|
|
}
|
|
}
|
|
|
|
// store new state
|
|
m_dwInputState = dwInputState;
|
|
}
|
|
|
|
// resolve gisteresis times
|
|
for( INT i = 0; i < nBitCount; i++ )
|
|
{
|
|
if( m_pGisterezis[i].liTime.QuadPart > liTime.QuadPart )
|
|
{
|
|
if( m_pGisterezis[i].bCurrentOn != m_pGisterezis[i].bSavedOn )
|
|
{
|
|
m_pGisterezis[i].bSavedOn = m_pGisterezis[i].bCurrentOn;
|
|
|
|
// notify about performation
|
|
OnEvtInputChange( i, s_bInvertBits[i] ? !m_pGisterezis[i].bSavedOn : m_pGisterezis[i].bSavedOn );
|
|
}
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|