/*--------------------------------------------------------
FILENAME
 	parallel.c

DESCRIPTION	
	Test program to read the parallel port

AUTHOR
	Requires DriverLINX Port I/O Driver Interface files
	dlportio.h and dlportio.lib

LOG
	Modifier 	Date    		Change
	--------   	----    		--------
	D. Cline	10/09/01		Created

--------------------------------------------------------*/
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winioctl.h>
#include <time.h>
#include "dlportio.h"

#define			BASE				0x378
#define			STATUSIN			BASE+1
#define			WATER_LRSE1_MASK	0x40	//Status bit 7/ pin 10/ Parallel ack line
#define			WATER_LRSS1_MASK	0x20	//Status bit 6/	pin 12/ Paper end

//void bits(unsigned int sgn, unsigned char *p);

void bits(unsigned int sgn, unsigned char *p)
{
  int i;
  unsigned int j;
        
  j = 0x80;
  for (i = 0; i < 8; i++) {
    p[i] = ' ';
    p[i] = ((sgn & (j >> i))==(j >> i) ? '1' : '0');
  }
  p[8] = '\0';
}


void main(int argc, const char* argv[])
{
	int k = '0';
	unsigned char databytechar[8];
	char databyte;
	unsigned int statusin = STATUSIN;

	printf("Hit 'x' to exit\n");	
	do {	
	
		databyte = (char) DlPortReadPortUchar(statusin);
		bits((unsigned int)databyte, databytechar);
		printf("Register 0x%x %6d:MSB %s LSB\r",statusin, databyte,databytechar); 

		if(!(WATER_LRSE1_MASK & databyte)) // Water sense will be active low
			printf("Water LRSE1 active\n"); 
		if(!(WATER_LRSS1_MASK & databyte)) // Water sense will be active low
			printf("Water LRSS1 active\n"); 

	if(_kbhit()) {
		k = getche();

		switch(k) {
			case '1':
				statusin = 0x279;
				break;
			case '2':
				statusin = 0x379;
				break;
			case '3':
				statusin = 0x3bd;
				break;
			case 'x':
			case 'X':
				exit(0);
				break;
			default:
				printf("\nInvalid key %c\n",k);
			break;
		}
		printf("\nHit 'x' to exit\n");
	}
	
	Sleep(25);
   } while (k != 'x' && k != 'X'); //any hit breaks loop
	
	exit(0);
}
