/*

	mmserial.c

*/
#include <dos.h>

struct point {
					unsigned char button;
					int x;
					int y;	};

/* local functions */

unsigned int getser();
unsigned char putser(unsigned char);
struct point get_coord();

extern unsigned int comport;

/********************************************************************
**
**    Routines to initialize serial port, send and receive bytes
**    from the serial port, and receive a coordinate.
**
********************************************************************* */


/********************************************************************
**
**	Get character from serial port
**
********************************************************************* */

unsigned int getser()  

{
int timeout;
/* wait for character, but not forever */
timeout = 0;
while(((inp(comport + 5) & 0x01) == 0) && (timeout < 1999))
	timeout++;
if (timeout > 1998)
	return(0xffff);
else
	return((unsigned int)((inp(comport))&0x00ff)); /* return input character */
}	/* getser */

/********************************************************************
**
**	Send character out to serial port
**
********************************************************************* */

unsigned char putser(c)

unsigned char c;
{
int timeout;

/* wait for character, but not forever */
timeout = 0;
while(((inp(comport + 5) & 0x20) == 0) && (timeout < 1999))
	timeout++;
if (timeout > 1998)
	return(0x00);
else{
	outp(comport,c);	/* output character */
	return(c);	}	/* return output character */

}	/* putser */

/********************************************************************
**
**
**
********************************************************************* */

struct point get_coord()	/* get a coordinate from TYPE 10 digitizer */

{
unsigned int itemp;
unsigned char temp[5];
int i,x;
struct point rcvd_coord;
i=0;
do {	/* synchronize data input */
			if (i == 100)
			{
					rcvd_coord.button = 255;
					rcvd_coord.x = 0;
					rcvd_coord.y = 0;
					return(rcvd_coord);
			}
	itemp = getser();
	if(!(itemp & 0xff00))
		temp[0] = (unsigned char)(itemp);
	else	{	/* BAD data fraom getser() */
		rcvd_coord.button = 255;
		rcvd_coord.x = 0;
		rcvd_coord.y = 0;
		return(rcvd_coord);	}
		i++;
} while (((temp[0]) & 0x80) == 0);

/* get next 4 bytes */
for (x =1; x <= 4; x++) {
	itemp = getser();
	if(!(itemp & 0xff00))
		temp[x] = (unsigned char)(itemp);
	else	{	/* BAD data from getser() */
		rcvd_coord.button = 255;
		rcvd_coord.x = 0;
		rcvd_coord.y = 0;
		return(rcvd_coord);	}
}

rcvd_coord.button = (temp[0] & 0x07);

rcvd_coord.x = (unsigned int)((temp[2] & 0x7f) << 7) +
					(unsigned int)(temp[1] & 0x7f);

rcvd_coord.y = (unsigned int)((temp[4] & 0x7f) << 7) +
					(unsigned int)(temp[3] & 0x7f);

return(rcvd_coord);

}	/* get_coord */

/********************************************************************
**
** convert ascii hex equivalent char to an integer
**
********************************************************************* */

int htoi( char *s)

{
unsigned int temp[4];
int i;
char *ch;

for ( i = 0;i <=3; i++ )
			temp[i] = 0;
for ( i = 0; i <= strlen(s)-1; i++ )
{
		ch = ( (char *)( (s-1) + ((strlen(s))-i) ) );
		if ((*ch > 0x2f) && (*ch < 0x3f))
			temp[i] =( (unsigned int)(*ch-0x30));
		else
		{
				if ( (*ch-87) < 10 || (*ch-87) > 15 )
				{
				/**	illegal hex address  **/
						printf(" Error Illegal Hex address specified!");
						printf("\n Please check your serial port address!");
						return(0);
				}
				else
						temp[i] = ((unsigned int)(*ch - 87));
		}
}
comport = (((( (temp[3]*16 + temp[2])*16 )+temp[1] )*16 )+temp[0] );
return(1);

}

















                                     