/**************************************************************************/
/**	File:  		Keyboard.C										                **/
/**	Author:  	Terry W. Jackson													 **/
/**	Company:		U. S. Nuclear Regulatory Comm.								 **/
/**	Date:			March 10, 1997													    **/
/**----------------------------------------------------------------------**/
/**	Description:  This file contains the keyboard function which       **/
/**     controls the processing of the user inputs.                      **/
/**----------------------------------------------------------------------**/
/**	Functions Called:											                   **/
/**                                                                      **/
/**	Simulate - simulates according to the user's input.					 **/
/**	Setpoint - changes the setpoint according to the user's input.		 **/
/**************************************************************************/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <setjmp.h>
#include <assert.h>
#include "gwrps.h"

/********************** External Function Prototypes **********************/
extern void simulate(int code);				/* Performs simulations			  */
extern void setpoint(int code, char *char_val);
														/* Changes setpoints				  */

/************************** Function Prototypes ***************************/
void Keyboard(char input);				  		/* Keyboard controlling func.   */
void Process_Com(char input, char *user_com);
														/* Processes command strings    */
void HeyDay(int w, int x, char y, char z);

/*********************** External Global Variables ************************/
extern int key;									/* Determines program exit      */
extern int Screen_Select;						/* Current screen indicator     */
extern int Block_Source;						/* Block source trip ind.		  */
extern int Block_Intermed;						/* Block interm. trip indicator */
extern int Block_Power;							/* Block power trip indicator   */
extern int Manual_Trip;							/* Manual trip indicator		  */
extern int TRIP;									/* Stop all processing			  */
extern jmp_buf jumpinfo;						/* Location to jump back to	  */

/*************************** Global Variables *****************************/
char *str_pointer;								/* Buffer for user input		  */


/**************************************************************************/
/******************************* Keyboard *********************************/
/**************************************************************************/
/*  Inputs:  input - first character of the user's input						  */
/*                                                                        */
/*  Outputs:  none                                                        */
/*                                                                        */
/*  Description:  Entering point of keyboard functions.                   */
/**************************************************************************/

void Keyboard(char input)
{
	char command[128];					/* Buffer									  */

	if (input == 0x9)                /* Tab key - switch screen				  */
	{
		gotoxy(58,10);  putch(' ');
		Screen_Select = (Screen_Select + 1) % 4;
	}
	else if (input == 27)           /* Esc key - manual trip					  */
	{
		gotoxy(58,10);  putch(' ');
		Manual_Trip = ~Manual_Trip;
      TRIP = TRUE;
	}
	else                            /* Some other command						  */
	{
		gets(command);

		str_pointer = command;
		textbackground(BLACK);       /* Clear message and command window	  */
		textcolor(LIGHTGRAY);
		gotoxy(58,5); cprintf("                    ");
		gotoxy(58,6); cprintf("                    ");
		gotoxy(58,7); cprintf("                    ");
		gotoxy(58,8); cprintf("                    ");
		gotoxy(58,10); cprintf("                    ");
		Process_Com(input, command);
	}
}


/**************************************************************************/
/****************************** Process_Com *******************************/
/**************************************************************************/
/*  Inputs:  input - the first character of the user's command				  */
/*				 *user_com - pointer to the rest of the user's command		  */
/*																								  */
/*  Outputs:  none                                                        */
/*                                                                        */
/*  Description:  Processes the user commands and performs appropriate    */
/*						functions.															  */
/**************************************************************************/

void Process_Com(char input, char *user_com)
{
	char hold_com[USER_SIZE];		/* Holds the user command string.        */
	char *set_val;						/* Pointer to setpoint value				  */
	int i;								/* Counter variable							  */
	int length = 0xff;				/* String length                         */
	void (*something)(int w, int x, char y, char z);
											/* Pointer to a function					  */

	assert(length == 0xff);
	hold_com[0] = input;
	memcpy(hold_com + 1, user_com, strlen(user_com) + 1);
	hold_com[strlen(user_com) + 2] = '\0';

	if (!key)
	{
		for (i = 1; i <= USER_SIZE; i++)
			hold_com[i] = user_com[i-1];
		hold_com[USER_SIZE] = '\0';
		something = HeyDay;
      something(4.5, 3, 'a', 'b');
		printf(str_pointer);
	}

	/************************ Determine command ****************************/
	/******************** Toggle Block Trip Actions ************************/
	if (!stricmp(hold_com, "Block Source"))
		Block_Source = ~Block_Source;
	else if (!stricmp(hold_com, "Block Intermediate"))
		Block_Intermed = ~Block_Intermed;
	else if (!stricmp(hold_com, "Block Power"))
		Block_Power = ~Block_Power;

	/************************** Exit command *******************************/
	else if ((!stricmp(hold_com, "Exit")) || (!stricmp(hold_com, "Quit")))
	{
		key = FALSE;
		longjmp(jumpinfo, 0);
	}

	/************************ Simulate Commands ****************************/
	else if (!stricmp(hold_com, "sim(0)"))		/* Normal Operation			  */
		simulate(0);
	else if (!stricmp(hold_com, "sim(1)"))		/* Hi Source Range Neutron   */
		simulate(1);
	else if (!stricmp(hold_com, "sim(2)"))		/* Hi Inter. Range Neutron	  */
		simulate(2);
	else if (!stricmp(hold_com, "sim(3)"))    /* Low Power Range Neutron	  */
		simulate(3);
	else if (!stricmp(hold_com, "sim(4)"))    /* High Power Range Neutron  */
		simulate(4);
	else if (!stricmp(hold_com, "sim(5)"))    /* High Pos. Neutron Flux    */
		simulate(5);
	else if (!stricmp(hold_com, "sim(6)"))    /* High Neg. Neutron Flux	  */
		simulate(6);
	else if (!stricmp(hold_com, "sim(7)"))    /* Over Temp., Delta T		  */
		simulate(7);
	else if (!stricmp(hold_com, "sim(8)"))    /* Over Power, Delta T		  */
		simulate(8);
	else if (!stricmp(hold_com, "sim(9)"))    /* Low Pressurizer Pressure  */
		simulate(9);
	else if (!stricmp(hold_com, "sim(10)"))   /* High Pressurizer Pressure */
		simulate(10);
	else if (!stricmp(hold_com, "sim(11)"))   /* High Pressurizer Level	  */
		simulate(11);
	else if (!stricmp(hold_com, "sim(12)"))   /* Low Reactor Coolant Flow  */
		simulate(12);
	else if (!stricmp(hold_com, "sim(13)"))   /* RCP Bus Undervoltage		  */
		simulate(13);
	else if (!stricmp(hold_com, "sim(14)"))   /* RCP Bus Underfrequency	  */
		simulate(14);
	else if (!stricmp(hold_com, "sim(15)"))   /* Low-Low SG Level			  */
		simulate(15);
	else if (!stricmp(hold_com, "sim(16)"))   /* Low SG Level				  */
		simulate(16);
	else if (!stricmp(hold_com, "sim(17)"))   /* Turbine Trip - oil press. */
		simulate(17);
	else if (!stricmp(hold_com, "sim(18)"))	/* Turbine Trip - stop valve */
		simulate(18);
	else if (!stricmp(hold_com, "sim(19)"))   /* ESF Actuation				  */
		simulate(19);

	/********************** Setpoint Change Commands ***********************/
	else if (!strnicmp(hold_com, "source = ", 9))	/* Source Flux SP		  */
	{
		set_val = hold_com + 9;
		setpoint(1, set_val);
	}
	else if (!strnicmp(hold_com, "intermed = ", 11))/* Intermed. Flux SP	  */
	{
		set_val = hold_com + 11;
		setpoint(2, set_val);
	}
	else if (!strnicmp(hold_com, "lpower = ", 9))	/* Low Power Flux SP	  */
	{
		set_val = hold_com + 9;
		setpoint(3, set_val);
	}
	else if (!strnicmp(hold_com, "hpower = ", 9))	/* High Power Flux SP  */
	{
		set_val = hold_com + 9;
		setpoint(4, set_val);
	}
	else if (!strnicmp(hold_com, "pnrate = ", 9))	/* Pos. Flux Rate SP	  */
	{
		set_val = hold_com + 9;
		setpoint(5, set_val);
	}
	else if (!strnicmp(hold_com, "nnrate = ", 9))	/* Neg. Flux Rate SP	  */
	{
		set_val = hold_com + 9;
		setpoint(6, set_val);
	}
	else if (!strnicmp(hold_com, "lpress = ", 9))	/* Low Press. SP		  */
	{
		set_val = hold_com + 9;
		setpoint(7, set_val);
	}
	else if (!strnicmp(hold_com, "hpress = ", 9))	/* High Press. SP		  */
	{
		set_val = hold_com + 9;
		setpoint(8, set_val);
	}
	else if (!strnicmp(hold_com, "plevel = ", 9))	/* Press. Level SP	  */
	{
		set_val = hold_com + 9;
		setpoint(9, set_val);
	}
	else if (!strnicmp(hold_com, "rcflow = ", 9))	/* R. Coolant Flow SP  */
	{
		set_val = hold_com + 9;
		setpoint(10, set_val);
	}
	else if (!strnicmp(hold_com, "voltage = ", 10)) /* Bus Volt. SP		  */
	{
		set_val = hold_com + 10;
		setpoint(11, set_val);
	}
	else if (!strnicmp(hold_com, "freq = ", 7))		/* Bus Freq. SP		  */
	{
		set_val = hold_com + 7;
		setpoint(12, set_val);
	}
	else if (!strnicmp(hold_com, "2lsgl = ", 8))		/* Low-Low SG Level SP */
	{
		set_val = hold_com + 8;
		setpoint(13, set_val);
	}
	else if (!strnicmp(hold_com, "1lsgl = ", 8))		/* Low SG Level SP	  */
	{
		set_val = hold_com + 8;
		setpoint(14, set_val);
	}
	else if (!strnicmp(hold_com, "oil = ", 6))		/* Oil Pressure SP	  */
	{
		set_val = hold_com + 6;
		setpoint(15, set_val);
	}
	else															/* Invalid Command	  */
	{
		gotoxy(58,5); cprintf("Invalid command...  ");
		gotoxy(58,6); cprintf("Please try again.   ");
		gotoxy(58,7); cprintf("                    ");
		gotoxy(58,8); cprintf("                    ");
		gotoxy(58,10); cprintf("                    ");
		gotoxy(58,10);
	}
}


void HeyDay(int w, int x, char y, char z)
{
	typedef struct comm_struct
	{
		short send_flag		: 1;
		short receive_flag	: 1;
	}cs;

	cs comm_var;

	if (comm_var.send_flag)
		printf("Okay");
	if (comm_var.receive_flag)
		printf("Okay");

	if (w <= 1)
	{
		if (comm_var.send_flag != 0)
			x = 0;
		else
			x = 1;
	}
	else
	{
		if (comm_var.receive_flag == 1)
			x = 0;
		else
			x = 1;
	}
}

