/*---------------------------------------------------------------------*/
/* APLIC  - V_3.1.0  02/02/93                                          */
/*                                                                     */
/* Copyright (C) Cyclades Corporation, 1992/1993.                      */
/*                                                                     */
/* This program was compiled and tested in Microsoft C V 5.0 and is    */ 
/* provided by Cyclades as an example.				       */ 
/*---------------------------------------------------------------------*/

/* include files */

#include <stdio.h>
#include <string.h>

#include "..\library\cyclom.h"


#define	MAX_CHANNEL	8


/* global variables */

struct ChannelParam	ch_par, ch_par1;


/* main routine */

main()
{
unsigned short	i,j;
unsigned short	channel;
unsigned char	keystroke;
char 		w_char;
ushort		nchan;
ushort		poll_channel;

	printf("                          -------------------------------\n");
	printf("                              Cyclades Corporation.\n");
	printf("                            Application Program example.\n");
	printf("                          -------------------------------\n\n");

	/* initializes the library */	

	if ( ( nchan = (ushort) Cyc_init(D0,IRQ12) ) == -1) {
		printf("Board not found. Stopping.\n");
		exit(1);
	}
	
	printf("%d-channel card found and initialized.\n\n",nchan);
	
	/* open all 8 channels */ 

	printf("Opening channel");
	for ( i = 1 ; i <= MAX_CHANNEL ; i++ ) { 
		ch_par.channel = i;
		ch_par.char_len = CHAR_7;
		ch_par.parity_check = PAR_CHECK_ON;
		ch_par.parity = EVEN_PARITY;
		ch_par.stopbits = ONE_STOP_BIT;
		ch_par.baudrate = BAUD_1200;
		if ( Cyc_open(&ch_par) == -1 ) {
			printf("\nError : cannot open channel %d.\n",i);
			fatal();
		}
		Cyc_curr_settings(i,&ch_par1);
		if (ch_par.baudrate != ch_par1.baudrate) {
			printf("\nError programming channel %d.\n",i);
			fatal();
		} else {
			printf(" %d,",i);
		}
	}
	printf("\n");

	/* write a message in all terminals */
	
	printf("Sending message to channel");
	for ( i = 1 ; i <= MAX_CHANNEL ; i++ ) {
	  Cyc_write_str(i,"\n\r\n\r\n\r");
	  Cyc_write_str(i,"You can type:\n\r");
	  Cyc_write_str(i,"	<Ctrl-A>, to close your channel.\n\r");
	  Cyc_write_str(i,"	<Ctrl-B>, to get the application screen.\n\r");
	  Cyc_write_str(i,"	<Ctrl-C>, to block all other channels.\n\r");
	  Cyc_write_str(i,"	<Ctrl-D>, to unblock other channels.\n\r");
	  printf(" %d,",i);
	}
	printf("\n\n");

	printf("This application stay reading all terminals.\n");
	printf("If the user types:\n\n");
	printf("	<Ctrl-A>, his channel is closed.\n");
	printf("	<Ctrl-B>, he will get application screen.\n");
	printf("	<Ctrl-C>, other channels will be blocked.\n");
	printf("	<Ctrl-D>, unblock other channels.\n");
	printf("\n\n");

	/* stay in loop waiting for a keystroke */

	poll_channel = 0;

	while (1) {
		channel = poll_channel;	/* poll channels */
		while ( Cyc_read_ch(&channel,&keystroke) == -1 ) {
			if(kbhit()) {
				printf("Ok, finishing... \n\n"); 
				fatal(1);
			}
		}
		
		if ( keystroke == 0x01 ) {
		  Cyc_write_str(channel,"\n\r\n\rYour terminal is closed!\n\r");
		  Cyc_close(channel,CL_WAIT);
		  printf("channel %d closed\n",channel);
		  continue;
		}
		if ( keystroke == 0x02 ) {
		  printf("Channel %d. sending screen...\n",channel);
		  Cyc_write_str(channel,"\n\r\n\n");
		  Cyc_write_str(channel,"\t+-----------------------------------------------------------+\n\r");
		  Cyc_write_str(channel,"\t|                                                           |\n\r");
		  Cyc_write_str(channel,"\t|                          Cyclom                           |\n\r");
		  Cyc_write_str(channel,"\t|                                                           |\n\r");
		  Cyc_write_str(channel,"\t|                     Application Screen                    |\n\r");
		  Cyc_write_str(channel,"\t|                                                           |\n\r");
		  Cyc_write_str(channel,"\t+-----------------------------------------------------------+\n\r\n\r");
		  continue;
		}

		if ( keystroke == 0x03 ) {
		  printf("Channel %d blocked all other terminals.\n",channel);
		  poll_channel = channel;
		  for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
		  	if (i != channel) {
			  	Cyc_write_str(i,"\n\rTerminal blocked!");
			} else {
				Cyc_write_str(i,"\n\rYou have blocked all other terminals!\n\r");
			}
		  }
		  continue;
		}

		if ( keystroke == 0x04 ) {
		  printf("Channel %d unblocked other terminals.\n",channel);
		  poll_channel = 0;
		  for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
		  	if (i != channel) {
			  	Cyc_write_str(i,"\n\rTerminal unblocked.\n\r");
			} else {
				Cyc_write_str(i,"\n\rYou have unblocked all other terminals.\n\r");
			}
		  }
		  continue;
		}
		
		printf ("Character %c (0x%x) received from channel %d\n",
			 keystroke,keystroke,channel);
		Cyc_write_str(channel,"\n\rCharacter received: ");
		Cyc_write_ch(channel,keystroke);
		Cyc_write_str(channel,"\n\r");
	}
	
}

fatal()
{
unsigned short	i;

	printf ("Closing all channels...\n");
	for ( i = 1 ; i <= MAX_CHANNEL ; i++ ) { 
		Cyc_close(i,CL_HANGUP);
	}
	printf ("Reseting library...\n\n");
	Cyc_reset(IRQ10);
	exit(1);
}

