/*
	DEMOSRTK.C

   If using Dynamic C for DOS, make sure CPLC.LIB and RTK.LIB are
   commented out with an '*' and uncomment out VDRIVER.LIB and SRTK.LIB
   in LIB.DIR.

	Run on any board - Simplified Real-time Kernel version
	of demo_rt.c  ( cdemo_rt.c, mgdemort.c, ... )

*/

#use vdriver.lib     		// Required
#use srtk.lib					//  to
#define RUNKERNEL 1			//  run SRTK

char comment_flag;

main() 
{
    vat_init();
    comment_flag=0;
    VdInit();					// virtual driver needed by SRTK
	 init_srtkernel();		// initialize the SRTK
    backgnd();             // this runs after everything else 
}


shared float temp; 			// temperature in degrees C 
shared float settemp;		// temperature setting 
shared float volume;			// amount of liquid in vat 
shared char heater;			// 1 - heater on 
shared char intake;			// 1 - intake valve open 
shared char level;			// 1 - water level above switch 
shared int station;
shared int xtrain,ytrain;
shared int waiting;
char comment[60];
 

/* simulate the target system 
   the outflow rate is 10 units/tick
   the inflow rate is 25 units/tick when intake is open 
   the temperature of the intake liquid is 20
   the settemp must be higher
   the capacity of the vessel 1000 units of liquid
   the capacity of the heater is to raise the temperature 1 degree per tick
*/

vat_init()
{
    temp=30.0; 
    volume=1000.0; 
    settemp=56.0; 
    station=0;
}

#funcchain _srtk_hightask srtk_hightask

srtk_hightask(){
   runwatch ();
	costate{
   	if (intake) 
      	temp=0.985 * temp + 20.0 * 0.015; // dilute with 15 liters at 20C 

    	if (heater) 
      	temp = temp + 1.0;

	   if (intake) 
   	   volume = volume + 15.0; 
	   else 
   	   volume = volume - 10.0;

	   if (volume > 1000.0) 
   	   level = 0; 
	   else 
   	   level = 1;

		intake = level;          // if level low, enable intake valve 
   	heater = temp < settemp; // if temp low, turn on heater 
	}
}

#funcchain _srtk_lowtask srtk_lowtask

srtk_lowtask(){

    int k;
	
	 costate{	
		while (comment_flag) waitfor(DelayMs(175L));// if display not updated */
	   strcpy (comment, "Begin at Sacramento, Ca.");
  		comment_flag = 1;

		xtrain = 30; ytrain = 1;	// at Sacramento station 
   	waitfor(DelayMs(500L));    // wait in Sacramento station 
	   while (comment_flag)  waitfor(DelayMs(175L));	// wait for display update */
	   strcpy (comment, "Leave Sacramento");
		comment_flag=1;
    
    	// Sacto to Davis 
	   for (k = 31; k < 48; k++){ 
		  	xtrain = k; 
			waitfor(DelayMs(175L));
		} 
		while (comment_flag)  waitfor(DelayMs(175L)); 
		strcpy (comment, "Stop at Davis");
		comment_flag = 1;
		waitfor(DelayMs(1000L));	// wait at Davis station 
		while (comment_flag)waitfor(DelayMs(175L)) ; 
		strcpy (comment, "Leave Davis");
		comment_flag=1;

		for (k = 48; k < 63; k++) { xtrain=k; waitfor(DelayMs(175L)); }
		while (comment_flag)  waitfor(DelayMs(175L)); 
		strcpy (comment, "Stop at Oakland");
		comment_flag = 1;
		waitfor(DelayMs(1000L));	// wait at Oakland station 
		while (comment_flag)  waitfor(DelayMs(175L)); 
		strcpy (comment, "Leave Oakland");
		comment_flag = 1;

	   for (k=1; k<4; k++) {ytrain = k;  waitfor(DelayMs(700L));}
		while (comment_flag)  waitfor(DelayMs(175L)); 
		strcpy (comment, "Arrive at San Jose");
		comment_flag = 1;
		waitfor(DelayMs(1000L));	// wait at San Jose Stations 
	}
}

backgnd()
{
   int k;
   int intakeold, heaterold,xtold,ytold;
   float tempold,volumeold,setold;

   k=0;
   xtold=70; ytold=2;
   setold=0.;
   goxy(0,0);
   printf("Real-time kernel demo");

   goxy(0,1); printf("temp=");
   goxy(0,2); printf("volume=");
   goxy(30,0); printf("Sacramento");
   goxy(45,0); printf("Davis");
   goxy(60,0); printf("Oakland");
   goxy(60,4); printf("San Jose");
   goxy(25,4); printf("F4 (DOS only)/ arrows to change ");
	
	while (1)
   {
   	if (xtold != xtrain || ytold != ytrain)
      {
      	goxy (xtold,ytold); printf(" ");
         xtold = xtrain; ytold = ytrain;
         goxy (xtold,ytold); printf("T");
      }

      if(setold != settemp)    
      {
         goxy(0,4); 
         printf("set temperature=%5.2f",setold=settemp);
      }
    
      if (temp != tempold) 
      {
         goxy(8,1); 
         printf("%8.2f",temp); 
         tempold=temp;
      }
    
      if (volume != volumeold) 
      {
         goxy(8,2); 
         printf("%8.2f",volumeold=volume);
      }

      if (intake != intakeold || heaterold != heater) 
      {
         goxy(0,3); 
         printf("intake=%d  heater=%d",intake,heater);
         heaterold = heater; 
         intakeold = intake;
      }

      if(comment_flag) 
      {
         goxy(30,2); 
         printf ("                        ");
         goxy(30,2); 
         printf("%s",comment); 
         comment_flag = 0;
      }

      if(kbhit())
      {
         k=getchar();
         if(k==0xc8 || k==0x38) settemp = settemp + 1.0;
         if(k==0x32 || k==0xd0) settemp = settemp - 1.0;
      }
   }
}

goxy(int x, int y)
{ 
    printf("\x1b=%c%c",x+32,y+32);
}
