/*
	SC1Z1232.C
	
	CM71xx/Z1 and CM72xx/Z2

	Demonstration of serial port z0 of the Z180 as a diagnostic port.

	PC terminal or communication program should be set for 9600 baud, 
	8 data bit, 1 stop, no parity. If the terminal program in windows is
	used, select terminal font under terminal preference option.

	This program will cause communication with Dynamic C to be lost. When
	this happens, connect the PC serial cable to the "PROGRAM" serial
	port on the evaluation board. Once the terminal program is running,	
	type a short string terminated with <CR> and the same string is
	sent back from the SmartCore preceded by "hello >> ".
*/

#define IBAUD 9600/1200		// baud rate
									// with modem either 2400 or 1200
									// without modem => 19200,9600, 4800, etc
#define TBUFSIZE 384			// size of transmit buffer
#define RBUFSIZE 384			// size of receive buffer

char MODE = 4;					// 8 data, no parity, 1 stop
char NO_MODEM = 0;			// we don't want modem
char ECHO  = 1;				// we do want character echo

char *menu[] = {	"Z-World Engineering.",
						"This program shows the use of Z180 port 1 for",
						"RS232 communication. Type a short string,    ",
						"terminated with <CR>, and the same string is ",
						"sent back from the SmartCore.                "};
main(){

int  i,j;
int  count;
char tbuf[TBUFSIZE];		// transmit buffer
char rbuf[RBUFSIZE];		// receive buffer
char buf[RBUFSIZE+1];	// dummy buffer for receiving a complete command
	 
// Since serial port 1 is the default programming port with the SmartCore,
// communication with Dynamic C is lost when the Z1 port is initialized 
// for this application.						

	Dinit_z1(rbuf,tbuf,RBUFSIZE,TBUFSIZE, MODE, IBAUD, NO_MODEM, ECHO );

	showmenu();

	count = 8;  
	for(;;){			// endless loop constantly monitoring for new command line

		runwatch();
		hitwd();

		if( Dread_z1(buf,ENTER) != 0 ){		// wait for string terminated with CR

			if( count == 0 ){
				count = 8;
				showmenu();
			}

			Dz1send_prompt();
			Dwrite_z1( "hello >> ", 9 );
			Dwrite_z1( buf, strlen(buf) );
			Dz1send_prompt();

			count--;
		}
	}
}

// Clear the screen on the dumb terminal.

void clear_screen(){

	while( !Dwrite_z1("\x1b[2J",4) ){}
}

// Display a message on the dumb terminal.

showmenu(){
int i;

	clear_screen();

	for( i=0;i<5;i++ )	{
		Dz1send_prompt();
		while( !Dwrite_z1(menu[i], strlen(menu[i])) ) hitwd();
	}

	Dz1send_prompt();
}