//=============================================================================
// (c) Copyright 2005 Diamond Systems Corporation. Use of this source code
// is subject to the terms of Diamond Systems' Software License Agreement.
// Diamond Systems provides no warranty of proper performance if this
// source code is modified.
//
// File: DSCDIOFunctions.c   v5.9
// Desc: Sample program that demonstrates how to use the digital I/O functions
// Created by	KL
//=============================================================================

#include <stdio.h>

#ifdef _WIN32
#ifndef _WIN32_WCE
#include <conio.h>
#endif

#include <windows.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "dscud.h"
#endif

// DOS
#ifdef __BORLANDC__
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "../../../../current/dev/source/dscud.h"
#endif

// Linux and QNX
#if defined(linux) || defined(__QNXNTO__) || defined(_WRS_VXWORKS_5_X)
#include <stdlib.h>
#include <math.h>
#include <time.h>
// diamond driver includes
#include "dscud.h"

#ifdef _WRS_VXWORKS_5_X
#include <selectLib.h>
#define main HERCDSCDIOFunctions
#else
#include <sys/time.h>
#endif

static int kbhit()
{
	struct timeval timeout;
	fd_set rfds;

	timeout.tv_sec = 0;
	timeout.tv_usec = 0;

	FD_ZERO(&rfds);
	FD_SET(0, &rfds);

	if ( select(0+1, &rfds, NULL, NULL, &timeout) > 0 )
		return getchar();

	return 0;
}
#endif


// var declarations
BYTE result;   // returned error code
DSCB dscb;     // handle used to refer to the board
DSCCB dsccb;   // structure containing board settings
BYTE port;     // port used for digital I/O
BYTE output_b; // BYTE sent to digital output
BYTE input_b;  // BYTE receiving digital input
BYTE config_byte;       // DIO configuration byte
ERRPARAMS errorParams;  // spits out errors
int intBuff;   // integer variable

//=============================================================================
// Name: main()
// Desc: Starting function that calls the driver functions used
//
//		 NOTE: By convention, you should capture the BYTE return value for each
//		 driver API call, and check the error code.
//
//	     I. Driver Initialization
//	    II. Board Initialization
//	   III. DIO Input and Output
//	    IV. Cleanup
//
//=============================================================================

int main( void )
{
	//=========================================================================
	// I. DRIVER INITIALIZATION
	//
	//    Initializes the DSCUD library.
	//
	//    STEPS TO FOLLOW:
	//
	//	  1. initialize the driver, using the driver version for validation
	//=========================================================================

	if( dscInit( DSC_VERSION ) != DE_NONE )
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInit error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );		
		return 0;
	}

	//=========================================================================
	// II. BOARD INITIALIZATION
	//
	//	   Initialize the HERCULES board. This function passes the various
	//	   hardware parameters to the driver and resets the hardware.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. set the board type to DSC_HERC
	//	   2. set the base address 
	//	   3. set the interrupt level
	//	   4. intialize and register the board with the driver, after which
	//		  the struct, dscb, now holds the handle for the board
	//=========================================================================

	printf( "\nHERCULES BOARD INITIALIZATION:\n" );

	dsccb.boardtype = DSC_HERCEBX;

	printf("Enter the base address in hex (default 0x240): ");
	scanf("%hx", &dsccb.base_address );	

	dsccb.int_level = 5;

	if(dscInitBoard(dsccb.boardtype, &dsccb, &dscb) != DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInitBoard error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );		
		return 0;
	}

	//=========================================================================
	// III. DIO INPUT AND OUTPUT
	//
	//		Perform a set of byte-level inputs and outputs. Start by configuring
	//		the DIO ports. Call dscSetConfig to configure input/output mode. 
	//		Then send an output byte value of 0 to the selected digital I/O port.
	//		Read from that same port again to verify that the correct value was
	//		exchanged. For word and bit-level operations, the steps are nearly
	//		identical with a few minor changes to function names and the size
	//		of the data to be transferred.
	//
	//
	//		STEPS TO FOLLOW:
	//
	//		1. configure the DIO settings
	//		2. set the digital I/O port to 0
	//		3. send a byte out to the board through port 0
	//		4. receive a byte from the board through port 0 and display the
	//		   result
	//		5. repeat steps 2-3 for 5 iterations
	//		6. repeat steps 2-4 until a key is pressed
	//=========================================================================

	printf( "\nDIO INPUT AND OUTPUT:\n" );

	printf("Enter configuration Byte in hex (see user manual): ");
	scanf("%x", &intBuff);

	config_byte = (BYTE) intBuff;

	if ( dscDIOSetConfig(dscb, &config_byte ) != DE_NONE )
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscDIOSetConfig error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );		
		return 0;
	}

	do
	{
		printf("Enter the output port in decimal (0 - 5): ");
		scanf("%d", &intBuff ) ;
		port = (BYTE) intBuff;

		printf("Enter the value to output in decimal (0 - 255): ");
		scanf("%d", &intBuff);
		output_b = (BYTE) intBuff;

		if ( dscDIOOutputByte( dscb, port, (BYTE)( output_b ) ) != DE_NONE )
		{
			dscGetLastError(&errorParams);
			fprintf( stderr, "dscDIOOutputByte error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );		
			return 0;
		}

		if ( dscDIOInputByte( dscb, port, &input_b ) != DE_NONE )
		{
			dscGetLastError(&errorParams);
			fprintf( stderr, "dscDIOInputByte error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );		
			return 0;
		}

		printf( "Byte value sent to port: %d, Byte value received from port: %d\n", (int)output_b, (int)input_b );  
		printf("\nEnter 'q' to exit or any key to continue \n");

	} while(getchar() != 'q' );
	
	//=========================================================================
	// IV. CLEANUP
	//
	//	   Cleanup any remnants left by the program and free the resources used
	//	   by the driver.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. free the driver resources
	//=========================================================================

	dscFree();

	printf( "\nDSCDIOFunctions completed.\n" );

	return 0;
} // end main()



#ifdef _WIN32_WCE
int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	main();

	return 0;
}

#endif
