///////////////////////////////////////////////////////////////////////
// ADSmartIO.cpp
// 720020-1153x
//
// Applied Data Systems
// www.applieddata.net
//
// Description
// -----------
// This helper class wraps the ADSmartIO functions
//
// History
// -------
// 720020-11531 - July 3, 2003 : ctacke
//   - Initial release
//
// 720020-11532 - July 18, 2003 : ctacke
//   - Added debug checking at proc load
//   - added runtime checks for proc availability
//   - added loads for backlight functions missing in rev 1
//
///////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "adsmartio.h"

ADSmartIO::ADSmartIO()
{
	hSmartIO	= NULL;
	loaded		= FALSE;

	RETAILMSG(TRUE,(L"\r\nLoading SMARTIO.DLL...\r\n"));
	hSmartIO = LoadLibrary((L"SMARTIO.dll"));

	if(hSmartIO == NULL)
	{	
		RETAILMSG(TRUE,(L"\r\nCan't find SMARTIO.DLL\r\n"));
		RETAILMSG(TRUE,(L"\r\nLoading KEYBDDR.DLL...\r\n"));
		hSmartIO = LoadLibrary((L"KEYBDDR.dll"));

		if(hSmartIO == NULL)
		{	
			RETAILMSG(TRUE,(L"\r\nFailed to load ADSmartIO library!\r\n"));
			return;
		}
		else
		{	
			RETAILMSG(TRUE,(L"\r\nKEYBDDR.DLL Loaded successfully...\r\n"));
		}
	}
	else
	{	
		RETAILMSG(TRUE,(L"\r\nSMARTIO.DLL Loaded successfully...\r\n"));
	}

	if(hSmartIO!=NULL)
	{	
		DEBUGMSG(TRUE,(_T("Loading ADSmartIO functions\r\n")));

		SIOSelectOption = (SIOSELECTOPTION)	GetProcAddress(hSmartIO,(L"SIOSelectOption"));
		if(SIOSelectOption == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOSelectOption: %i"), GetLastError())); }

		SIOInitDDRA		= (SIOINITDDRA)		GetProcAddress(hSmartIO,(L"SIOInitDDRA"));
		if(SIOInitDDRA == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOInitDDRA: %i"), GetLastError())); }

		SIOInitDDRB		= (SIOINITDDRB)		GetProcAddress(hSmartIO,(L"SIOInitDDRB"));
		if(SIOInitDDRB == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOInitDDRB: %i"), GetLastError())); }

		SIOInitDDRC		= (SIOINITDDRC)		GetProcAddress(hSmartIO,(L"SIOInitDDRC"));
		if(SIOInitDDRC == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOInitDDRC: %i"), GetLastError())); }

		SIOInitDDRD		= (SIOINITDDRD)		GetProcAddress(hSmartIO,(L"SIOInitDDRD"));
		if(SIOInitDDRD == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOInitDDRD: %i"), GetLastError())); }

		SIOReadPortA	= (SIOREADPORTA)	GetProcAddress(hSmartIO,(L"SIOReadPortA"));
		if(SIOReadPortA == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadPortA: %i"), GetLastError())); }

		SIOReadPortB	= (SIOREADPORTB)	GetProcAddress(hSmartIO,(L"SIOReadPortB"));
		if(SIOReadPortB == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadPortB: %i"), GetLastError())); }

		SIOReadPortC	= (SIOREADPORTC)	GetProcAddress(hSmartIO,(L"SIOReadPortC"));
		if(SIOReadPortC == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadPortC: %i"), GetLastError())); }

		SIOReadPortD	= (SIOREADPORTD)	GetProcAddress(hSmartIO,(L"SIOReadPortD"));
		if(SIOReadPortD == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadPortD: %i"), GetLastError())); }

		SIOWritePortA	= (SIOWRITEPORTA)	GetProcAddress(hSmartIO,(L"SIOWritePortA"));
		if(SIOWritePortA == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOWritePortA: %i"), GetLastError())); }

		SIOWritePortB	= (SIOWRITEPORTB)	GetProcAddress(hSmartIO,(L"SIOWritePortB"));
		if(SIOWritePortB == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOWritePortB: %i"), GetLastError())); }

		SIOWritePortC	= (SIOWRITEPORTC)	GetProcAddress(hSmartIO,(L"SIOWritePortC"));
		if(SIOWritePortC == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOWritePortC: %i"), GetLastError())); }

		SIOWritePortD	= (SIOWRITEPORTD)	GetProcAddress(hSmartIO,(L"SIOWritePortD"));
		if(SIOWritePortD == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOWritePortD: %i"), GetLastError())); }

		SIOBackLightON	= (SIOBACKLIGHTON)	GetProcAddress(hSmartIO,(L"SIOBackLightON"));
		if(SIOBackLightON == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOBackLightON: %i"), GetLastError())); }
		
		SIOBackLightOFF	= (SIOBACKLIGHTOFF)	GetProcAddress(hSmartIO,(L"SIOBackLightOFF"));
		if(SIOBackLightOFF == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOBackLightOFF: %i"), GetLastError())); }
		
		SIOBackLightPwm	= (SIOBACKLIGHTPWM)	GetProcAddress(hSmartIO,(L"SIOBackLightPwm"));
		if(SIOBackLightPwm == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOBackLightPwm: %i"), GetLastError())); }
		
		SIOReadKeypad	= (SIOREADKEYPAD)GetProcAddress(hSmartIO,(L"SIOReadKeypad"));
		if(SIOReadKeypad == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadKeypad: %i"), GetLastError())); }

		SIOBackLightVEEPwm	= (SIOBACKLIGHTVEEPWM)	GetProcAddress(hSmartIO,(L"SIOBackLightVEEPwm"));
		if(SIOBackLightVEEPwm == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOBackLightVEEPwm: %i"), GetLastError())); }

		SIOSetKeyPadSize	= (SIOSETKEYPADSIZE)	GetProcAddress(hSmartIO,(L"SIOSetKeyPadSize"));
		if(SIOSetKeyPadSize == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOSetKeyPadSize: %i"), GetLastError())); }

		SIOAnalogConversion = (SIOANALOGCONVERSION)	GetProcAddress(hSmartIO,(L"SIOAnalogConversion"));
		if(SIOAnalogConversion == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOAnalogConversion: %i"), GetLastError())); }

		SIOFirmwareVersion	= (SIOFIRMWAREVERSION)	GetProcAddress(hSmartIO,(L"SIOFirmwareVersion"));
		if(SIOFirmwareVersion == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOFirmwareVersion: %i"), GetLastError())); }

		SIOReadDeviceVersion	= (SIOREADDEVICEVERSION)	GetProcAddress(hSmartIO,(L"SIOReadDeviceVersion"));
		if(SIOReadDeviceVersion == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadDeviceVersion: %i"), GetLastError())); }

		SIOReadDevicePartType	= (SIOREADDEVICEPARTTYPE)	GetProcAddress(hSmartIO,(L"SIOReadDevicePartType"));
		if(SIOReadDevicePartType == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOReadDevicePartType: %i"), GetLastError())); }

		SIOChargerOn = (SIOCHARGERON)	GetProcAddress(hSmartIO,(L"SIOChargerOn"));
		if(SIOChargerOn == NULL) { DEBUGMSG(TRUE, (_T("  *Failed to load SIOChargerOn: %i"), GetLastError())); }

		DEBUGMSG(TRUE,(_T(".")));

		DEBUGMSG(TRUE,(_T("  ok\r\n")));
	}

	loaded = TRUE;

	return;
}

ADSmartIO::~ADSmartIO()
{
	if(hSmartIO)
	{
		FreeLibrary(hSmartIO);
	}
}

UINT ADSmartIO::AnalogConversion (BYTE channel)
{
	if(loaded)
	{
		if(SIOAnalogConversion != NULL)
			return SIOAnalogConversion(channel);
		else
			RETAILMSG(TRUE, (_T("SIOAnalogConversion function not available\r\n")));
	}

	return 0xFFFF;
}

UINT ADSmartIO::FirmwareVersion()
{
	if(loaded)
	{
		if(SIOFirmwareVersion != NULL)
			return SIOFirmwareVersion();
		else
			RETAILMSG(TRUE, (_T("SIOFirmwareVersion function not available\r\n")));
	}

	return 0xFFFF;
}

void ADSmartIO::InitDDRA(BYTE mask)
{
	if(loaded)
	{
		if(SIOInitDDRA != NULL)
			SIOInitDDRA(mask);
		else
			RETAILMSG(TRUE, (_T("SIOInitDDRA function not available\r\n")));
	}
}

void ADSmartIO::InitDDRB(BYTE mask)
{
	if(loaded)
	{
		if(SIOInitDDRB != NULL)
			SIOInitDDRB(mask);
		else
			RETAILMSG(TRUE, (_T("SIOInitDDRB function not available\r\n")));
	}
}

void ADSmartIO::InitDDRC(BYTE mask)
{
	if(loaded)
	{
		if(SIOInitDDRC != NULL)
			SIOInitDDRC(mask);
		else
			RETAILMSG(TRUE, (_T("SIOInitDDRC function not available\r\n")));
	}
}

void ADSmartIO::InitDDRD(BYTE mask)
{
	if(loaded)
	{
		if(SIOInitDDRD != NULL)
			SIOInitDDRD(mask);
		else
			RETAILMSG(TRUE, (_T("SIOInitDDRD function not available\r\n")));
	}
}

UINT ADSmartIO::PartType()
{
	if(loaded)
	{
		if(SIOReadDevicePartType != NULL)
			return SIOReadDevicePartType();
		else
			RETAILMSG(TRUE, (_T("SIOReadDevicePartType function not available\r\n")));
	}

	return 0xFFFF;
}

UINT ADSmartIO::ReadEEPROM(BYTE blockNumber, BYTE dataBytes[16])
{
	if(loaded)
	{
		if(SIOReadEEPROM != NULL)
			return SIOReadEEPROM(blockNumber, dataBytes);
		else
			RETAILMSG(TRUE, (_T("SIOReadEEPROM function not available\r\n")));
	}

	return 0xFFFF;
}

BYTE ADSmartIO::ReadKeypad()
{
	if(loaded)
	{
		if(SIOReadKeypad != NULL)
			return SIOReadKeypad();
		else
			RETAILMSG(TRUE, (_T("SIOReadKeypad function not available\r\n")));
	}

	return 0xFF;
}

void ADSmartIO::WritePortA(BYTE value)
{
	if(loaded)
	{
		if(SIOWritePortA != NULL)
			SIOWritePortA(value);
		else
			RETAILMSG(TRUE, (_T("SIOWritePortA function not available\r\n")));
	}
}

void ADSmartIO::WritePortB(BYTE value)
{
	if(loaded)
	{
		if(SIOWritePortB != NULL)
			SIOWritePortB(value);
		else
			RETAILMSG(TRUE, (_T("SIOWritePortB function not available\r\n")));
	}
}

void ADSmartIO::WritePortC(BYTE value)
{
	if(loaded)
	{
		if(SIOWritePortC != NULL)
			SIOWritePortC(value);
		else
			RETAILMSG(TRUE, (_T("SIOWritePortC function not available\r\n")));
	}
}

void ADSmartIO::WritePortD(BYTE value)
{
	if(loaded)
	{
		if(SIOWritePortD != NULL)
			SIOWritePortD(value);
		else
			RETAILMSG(TRUE, (_T("SIOWritePortD function not available\r\n")));
	}
}

BYTE ADSmartIO::ReadPortA()
{
	if(loaded)
	{
		if(SIOReadPortA != NULL)
			return SIOReadPortA();
		else
			RETAILMSG(TRUE, (_T("SIOReadPortA function not available\r\n")));
	}

	return 0xFF;
}

BYTE ADSmartIO::ReadPortB()
{
	if(loaded)
	{
		if(SIOReadPortB != NULL)
			return SIOReadPortB();
		else
			RETAILMSG(TRUE, (_T("SIOReadPortB function not available\r\n")));
	}

	return 0xFF;
}

BYTE ADSmartIO::ReadPortC()
{
	if(loaded)
	{
		if(SIOReadPortC != NULL)
			return SIOReadPortC();
		else
			RETAILMSG(TRUE, (_T("SIOReadPortC function not available\r\n")));
	}

	return 0xFF;
}

BYTE ADSmartIO::ReadPortD()
{
	if(loaded)
	{
		if(SIOReadPortD != NULL)
			return SIOReadPortD();
		else
			RETAILMSG(TRUE, (_T("SIOReadPortD function not available\r\n")));
	}

	return 0xFF;
}

void ADSmartIO::SelectOption(BYTE option)
{
	if(loaded)
	{
		if(SIOSelectOption != NULL)
			SIOSelectOption(option);
		else
			RETAILMSG(TRUE, (_T("SIOSelectOption function not available\r\n")));
	}
}

void ADSmartIO::SetBacklightPwm(BYTE value)
{
	if(loaded)
	{
		if(SIOBackLightPwm != NULL)
			SIOBackLightPwm(value);
		else
			RETAILMSG(TRUE, (_T("SIOBackLightPwm function not available\r\n")));
	}
}

void ADSmartIO::SetBacklightState(BOOL value)
{
	if(loaded)
	{
		if(value)
		{
			if(SIOBackLightON != NULL)
				SIOBackLightON();
			else
				RETAILMSG(TRUE, (_T("SIOBacklightON function not available\r\n")));
		}
		else
		{
			if(SIOBackLightOFF != NULL)
				SIOBackLightOFF();
			else
				RETAILMSG(TRUE, (_T("SIOBacklightOFF function not available\r\n")));
		}
	}
}

void ADSmartIO::SetBacklightVEE(BYTE value)
{
	if(loaded)
	{
		if(SIOBackLightVEEPwm != NULL)
			SIOBackLightVEEPwm(value);
		else
			RETAILMSG(TRUE, (_T("SIOBackLightVEEPwm function not available\r\n")));
	}
}

BOOL ADSmartIO::SetKeyPadSize(BYTE x, BYTE y)
{
	if(loaded)
	{
		if(SIOSetKeyPadSize != NULL)
			return SIOSetKeyPadSize(x, y);
		else
			RETAILMSG(TRUE, (_T("SIOSetKeyPadSize function not available\r\n")));
	}

	return FALSE;
}

BOOL ADSmartIO::SetSSPTimeout(ULONG timeout)
{
	if(loaded)
	{
		if(SIOSetSSPTimeout != NULL)
			return SIOSetSSPTimeout(timeout);
		else
			RETAILMSG(TRUE, (_T("SIOSetSSPTimeout function not available\r\n")));
	}

	return FALSE;
}

void ADSmartIO::ChargerOn(BOOL value)
{
	if(loaded)
	{
		if(SIOChargerOn != NULL)
			SIOChargerOn(value);
		else
			RETAILMSG(TRUE, (_T("SIOChargerOn function not available\r\n")));
	}
}