/*
	KPQ.C

	This program demonstrates how to use the queuing key
	interpretation routines. The kpQ... driver (vs. the kpDef... and
	kpMulti... drivers) is particularly well suited to applications
	in which the keypad scan routine is asynchronous to the key
	event consumer.

	In this sample program, the keypad scanning is performed as a
	part of _srtk_lowtask, which is called every 100ms and async. to
	the kpQGetKey call in the main program.
	
*/

#define RUNKERNEL 1

#use vdriver.lib
#use srtk.lib
#use op71l.lib
#use kp_op71.lib
#use op71hw.lib
#funcchain _srtk_lowtask kpScanState

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

	kpQInit();	//	initialize the keypad modules for default usages
	VdInit();	//	initialize the virtual driver after initializing the keypad!!
	glInit();	// turn on backlight
	op71BackLight(1);
	op71SetContrast(35);
	init_srtkernel();

	combined = 0;	//	no key is being pressed
	while (1) {
		do {
			keyPressed = kpQGetKey();		//	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);
				}
			}
		} while (keyPressed != -1);
	}		
}