/******************************************************************
srtksamp.c

	Run on any Z-World board

   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.

	Demonstrates the use of the Simplified Real-Time Kernel (SRTK)
	and costatements. The SRTK is a multi-tasking operating system
   with 3 levels of task priority.

	The high priority task is the function srtk_hightask(). This
	function is defined by the user, but called from the SRTK every
	25 ms. The low priority task, srtk_lowtask(), is called every
	100 ms and can be preempted by the high priority task. 	  

	A background task could be run that could be preempted by
	either SRTK task.
**********************************************************************/

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

int HCOUNT, LCOUNT;

main(){

		HCOUNT = LCOUNT = 0;

		VdInit();							// virtual driver needed by SRTK
		init_srtkernel();  				// initialize the SRTK

		printf("HCOUNT LCOUNT \n");

		// loop while SRTK does the work
		while(1){ /* background processing would go  here */};
}


//-------------- The Real Time Kernel Tasks ---------------- 


// This high priority task is executed every 25 ms

#funcchain _srtk_hightask srtk_hightask

srtk_hightask(){

	// Increment counter on each pass
	HCOUNT++;
}


// This low priority task is executed every 100 ms

#funcchain _srtk_lowtask srtk_lowtask

srtk_lowtask(){

	// Increment counter on each pass
	LCOUNT++;

	costate{

		// Wait for 1/2 second to elapsed 
		waitfor(DelayMs(500));

		// Output counts
		goxy(0,1);  
		printf("%3d    %3d", HCOUNT, LCOUNT);
	}

	costate{

		// When HCOUNT nears 1000, reset counters
		waitfor(  HCOUNT >= 960 );
		HCOUNT = 0;
		LCOUNT = 0;
	}
}


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