/*
	KPITPSCN.C

	This program demonstrates how to use the default interpolating
	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 vdriver.lib
#use op71l.lib
#use op71hw.lib
#use kp_op71.lib

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

	auto int i,j;

	glInit();						// turn on backlight
	op71BackLight(1);
	op71SetContrast(30);

	glBlankScreen();
	glSetBrushType(GL_SET);

	for (i = 320/16+320/32; i < 320-320/32; i += 320/16) {
	  glPlotLine(i,0,i,240);
	  hitwd();
	}
	for (i = 240/16+240/32; i < 240-240/32-1; i += 240/16) {
	  glPlotLine(0,i,320,i);
	  hitwd();
	}

	kpInit(kpItpStChgFn);	//	initialize the keypad modules for default usages
	VdInit();

	combined = 0;	//	no key is being pressed
	msMark = MS_TIMER;
	while (1) {
		hitwd();			//	hit watchdog so controller doesn't reset
		while (MS_TIMER - msMark < 500) hitwd();
		msMark = MS_TIMER;
		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("released R:%2d,C:%2d\n",(char)keyPressed>>4,keyPressed&15);
			} else {							//	rising edge (key pressed)
				++combined;					//	one more key pressed
				//	decode the row and column
				printf("pressed R:%2d,C:%2d\n",(char)keyPressed>>4,keyPressed&15);
			}
		}
	}		
}