/*
	FKSAMP.C

	Run on PK22xx/Little Star or PK21xx/Rugged Giant.

	Skeleton sample program for using new five key functions.	

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

#use fk.lib
#use vdriver.lib
#use srtk.lib
#nointerleave
#define RUNKERNEL 1
float f,temp;
int menuing;
int i;
unsigned u,iter;
char strng[11];
struct tm Time;

main(){
	strcpy(strng,"abcdefghi");
	f = 25.34;
	i = 15;
	u = 5;
	menuing = 1;
	VdInit();							// virtual driver needed by SRTK
	init_srtkernel();  				// initialize the SRTK
	up_beepvol(1);						// required for PK21xx/Rugged Giant beeper
	while(1){; /* loop while SRTK works */ }
}

#funcchain _srtk_lowtask fivekey_lowtask

int fivekey_lowtask(){
	segchain _srtk_hightask { lc_kget(1); }
	
	fk_monitorkeypad();
	if(menuing){
		PutMenus();
		fk_helpmsg(fk_default_help);		// puts up help messages
	}
	else{

		// simulate a monitored value
		costate{
			waitfor(DelaySec(2L));
			temp = 70.0 + sin( 2*PI*(float)(SEC_TIMER%32L)/32L );
			lcd_printf(0,"temp = %5.2f", temp);
		}
	}
	user_keys();							// process user keys
}

int PutMenus(){
	extern char fk_newmenu;
	int choice;
	static long SecSinceMidNight;
	static int DaysSince1980;
	auto long tmpLong;
	
	costate{
		for(;;){
			lcd_erase();
			lcd_printf(0l, "time/date menu");
			fk_newmenu=0;	

			for(;;){									// begin menu
				tm_rd(&Time);
				tmpLong = mktime(&Time);
				DaysSince1980 = (int)(tmpLong / (24L*60L*60L)) ;
				SecSinceMidNight = tmpLong % (24L*60L*60L);
				waitfor(fk_item_date("Date: ",&DaysSince1980));
				if (fk_newmenu) break; 
				waitfor(fk_item_time("Time: ",&SecSinceMidNight));
				if (fk_newmenu) break;
			}						 					// end menu
			tmpLong = 24L*60L*60L*DaysSince1980+SecSinceMidNight;
			mktm(&Time,tmpLong);
			lcd_erase();
			lcd_printf(0x00000000l,"I/O Menu1");
			fk_newmenu=0;	

			for(;;){			  						// begin menu
				waitfor(fk_item_enum("enum sel %8s",
				 &choice,"first","second","third","last",0));
				if(fk_newmenu) break;
				waitfor(fk_item_int("integer = %4d",
				 &i,10,1000));
				if(fk_newmenu) break;	
				waitfor(fk_item_uint("uint = %2u",
				 &u,0,10));
				if(fk_newmenu) break;
				waitfor(fk_item_alpha("Alpha = ",
				 strng,strlen(strng)));
				if(fk_newmenu) break;
				waitfor(fk_item_float("Float =%8.4f",
				 &f,-900.,9999.));
				if(fk_newmenu) break;		
			}			 								// end menu
		}
	}
	return 1;
}

int user_keys(){
	extern int fk_tkey;
	extern char help_block[50];

	costate{
		waitfor(fk_tkey > 6);
		switch(fk_tkey){
	
			// user defined actions here
			case 7:	// f1
			case 8:	// f2
			case 9:	// f3
			case 10:	// f4
			case 11: // del
				if(menuing){
					up_beep(80);
					lcd_savscrn(help_block); 
					lcd_erase_line(0);
					lcd_erase_line(1);
					lcd_printf(0x01000000,"Hit Add Key for Menu");
					menuing = 0;
				}
				break;
			case 12: // add
				if(!menuing){
					up_beep(80);
					lcd_resscrn(help_block);
					menuing = 1;
				}
				break;
			default:
		}
	}								// end of costatement
}

