/*******************************************************************
   echoterm.c
   Z-Word Inc 2003

	This sample program is for any RabbitNet board.

	Description
	===========
	This program demonstrates a simple character echo to any
	RabbitNet board through a serial terminal on the master controller.
   A character is sent to the RabbitNet board found by using the rn_find()
   to search for a board that matches criteria. If is found, the character
   will be returned back plus the status of the board.  Otherwise, the
   status byte will indicate a no connection.

	Connections
	===========
   1. Connect RabbitNet and power supply cables to the controller
   	and RN boards as described in the RabbitNet Manual.
	2. Connect PC (tx) to the controller on an available tx port
   	such as TXB.
  	3. Connect PC (rx) to the controller on an available rx port
   	such as RXB.
	4. Connect PC GND to GND.
   5. Configure the controller serial port below to match your
   	connections.

	Tera Term setup
	===============
	1. Startup Tera Term on your PC.
	2. Configure the serial parameters for the following:
	   a) Baud Rate of 19200, 8 bits, no parity and 1 stop bit.
	   b) Enable the "Local Echo" option.
	   c) Set line feed options to:  Receive = CR     Transmit = CR+LF
 	3. Select File-->Log-->Path and Filename
		The next steps are important, if not done if will overwrite previously
		saved calibration data when using same the filename.
   	a) Enable the File APPEND option located at the bottom of dialog box.
   	b) Select the OPEN option at the right-hand side of dialog box.
   	c) Tera Term is now ready to log all data received on the serial
   		port to your specified file.

	Instructions
	============
	1. Compile and run this program.

*******************************************************************/
#class auto

#define MATCHFLAG  RN_MATCH_PORT|RN_MATCH_PRDID
#define MATCHPORT 0           //port 0 or port 1 on the controller
#define MATCHPID RN1100			//RN1100 DI/0 board; RN1200 for AD board

/////
// configure your serial port connection here
//	presently configured serial port E
///
#define seropen	serEopen
#define serclose	serEclose
#define serputs	serEputs
#define serputc	serEputc
#define serwrite	serEwrite
#define sergetc	serEgetc
#define serrdFlush serErdFlush
#define serwrFlush serEwrFlush
#define serrdFree	serErdFree
#define serwrFree	serEwrFree
#define SXSR SESR
#define EINBUFSIZE 255
#define EOUTBUFSIZE 255

#define INBUFSIZE EINBUFSIZE
#define OUTBUFSIZE EOUTBUFSIZE
#define BAUDRATE 19200l


main()
{
	int device0;
	char statusbyte, sendbyte, recbyte;
	int i;
	char sendbuf[128];
	auto rn_search newdev;

	brdInit();                 //initialize controller
	seropen(BAUDRATE);
	serrdFlush();
	serwrFlush();

	memset(sendbuf, '\x0', sizeof(sendbuf));
	while(serrdFree() != INBUFSIZE) sergetc();
	serputs("Starting . . .\n\r\x0");

   rn_init(RN_PORTS, 1);      //initialize controller RN ports

   //search for device match
	newdev.flags = MATCHFLAG;
	newdev.ports = MATCHPORT;
	newdev.productid = MATCHPID;
   if ((device0 = rn_find(&newdev)) == -1)
   {
		serputs("no board connection at port 0\n\r\x0");
		while (serwrFree() != OUTBUFSIZE);
	 	while((RdPortI(SXSR)&0x08) || (RdPortI(SXSR)&0x04));
		serclose();
		exit(0);
	}

	while (1)
	{
		for (sendbyte='A'; sendbyte<='Z'; sendbyte++)
		{
			statusbyte = rn_echo(device0, sendbyte, &recbyte);
			if( sendbyte == recbyte )
			{
				ledOut(2,1);
				for (i=0; i<4000; i++);
				ledOut(2,0);
				for (i=0; i<6000; i++);
			}
			else
			{
				ledOut(3,1);
				for (i=0; i<4000; i++);
				ledOut(3,0);
				for (i=0; i<6000; i++);
			}
			sprintf(sendbuf,"send %c, status %x rec %c\n", sendbyte, statusbyte, recbyte);
			serwrite(sendbuf, sizeof(sendbuf));
		}
		while (serwrFree() != OUTBUFSIZE);
  	 	while((RdPortI(SXSR)&0x08) || (RdPortI(SXSR)&0x04));
	}
	serclose();
}

