/*
KPDEFLT.C

This program demonstrates how to use the default scanning key
interpretation routines of the KP library. It uses glprintf to
display messages. Please refer to glprintf.c to see how to print
text on the screen.

This program display the number of keys being pressed as well
as the rows and columns of the last key pressed and last key
released on the LCD screen.

*/

#use eziolp31.lib
#use tl_lp31.lib
#use tl.lib
#use kp.lib

main() {
	auto int keyPressed;			//	to store key code returned
	auto int combined;			//	number of keys pressed

	eioBrdInit(0);
	lcdInit();		//	initialize the software and hardware modules
	kpDefInit();	//	initialize the keypad modules for default usages

	//	the following call works for both standard and deluxe versions of
	//	the compiler because the font bitmap is in root memory.
	
	combined = 0;	//	no key is being pressed
		//	print the form
	lcdPos(0,0);
	lcdPrintf("keypad status:");
	kpScanState();	//	scan the state of the keypad, no interpretation
	kpDefGetKey();	//	interpret the state just scanned
	while (1) {
		hitwd();			//	hit watchdog so controller doesn't reset
		kpScanState();	//	scan the state of the keypad, no interpretation
		keyPressed = kpDefGetKey();	//	interpret the state just scanned
												//	and put the code into keyPressed
		if (keyPressed == -1) {			//	-1 means no key activity is detected
		} else {								//	otherwise, some activity took place
			if (keyPressed & 0x100) {	//	falling edge (key released)
				--combined;					//	one fewer key pressed
				//	decode the row and column
				lcdPos(1,0);
				lcdPrintf("R:%1d,C:%1d",(char)keyPressed>>3,keyPressed&7);
			} else {							//	rising edge (key pressed)
				++combined;					//	one more key pressed
				//	decode the row and column
				lcdPos(1,0);
				lcdPrintf("R:%1d,C:%1d",(char)keyPressed>>3,keyPressed&7);
			}
			lcdPos(1,10);
			lcdPrintf("%1d",combined);	//	display number of
												//	simultaneous keys
		}
	}		
}