/*
	KPDEFLT.C

	This program demonstrates how to use the default scanning key
	interpretation routines and screen printing capabilities of the
	KP library. It uses glprintf to display messages. Please refer
	to online help for a descriptin of the glprintf() function.
	
	This program displays the number of keys being pressed as well
	as the rows and columns of the last key pressed and last key
	released.
*/

#use eziopk24.lib
#use wintek.lib					//	specify underlying LCD
#use glcd.lib						//	specify high level graphics library
#use kp.lib

main() {
	auto struct _fontInfo fi;	//	declare struct to hold font information
	auto int keyPressed;			//	to store key code returned
	auto int combined;			//	number of keys pressed

	eioBrdInit(0);	
	glInit();		//	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.
	
	glFontInit(		//	initialize the font info structure needed later
		&fi,			//	pointer to the font info structure to fill
		6,				//	horizontal size (in pixels) of each character
		8,				//	vertical size (in pixels) of each character
		32,			//	beginning code of printable characters
		127,			//	ending code of printable characters
		engFont6x8R	//	address of font bitmap in root memory
		);

	combined = 0;	//	no key is being pressed
		//	print the form
	glPrintf(0,0,&fi,"Keypad Status\nTotal #:\nLast Pressed: \nLast Released:");
	glSetBrushType(GL_BLOCK);	//	new pixels totally overwrites old ones
	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
			kdiSetDnI(1);
			if (keyPressed & 0x100) {	//	falling edge (key released)
				--combined;					//	one fewer key pressed
				//	decode the row and column
				glPrintf(14*6,24,&fi,"R:%1d,C:%1d",(char)keyPressed>>3,keyPressed&7);
			} else {							//	rising edge (key pressed)
				++combined;					//	one more key pressed
				//	decode the row and column
				glPrintf(14*6,16,&fi,"R:%1d,C:%1d",(char)keyPressed>>3,keyPressed&7);
			}
			glPrintf(14*6,8,&fi,"%1d",combined);	//	display number of
																//	simultaneous keys
			kdiSetDnI(0);
		}
	}		
}