/*
	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 op71p.lib
#use op71hw.lib
#use kp_op71.lib

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

	glInit();
	op71BackLight(1);
	op71SetContrast(60);
	kpDefInit();	//	initialize the keypad modules for default usages


	combined = 0;	//	no key is being pressed
	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
				printf("depressed R:%1d,C:%1d\n",(char)keyPressed>>3,keyPressed&7);
			} else {							//	rising edge (key pressed)
				++combined;					//	one more key pressed
				//	decode the row and column
				printf("pressed R:%1d,C:%1d\n",(char)keyPressed>>3,keyPressed&7);
			}
		}
	}		
}