/***************************************************************************
	Rn_keybasic.c

 	Z-World, 2003
	This sample program is for the Rabbitnet LCD MSCG12232 Display Module.

   Sample program to demonstrate the keypad functions. This program
	will display the following in the STDIO display window:

	1. Displays default ASCII keypad return values.
	2. Displays custom ASCII keypad return values.
	3. Demonstrates keypad repeat functionality.

	Instructions:
	1. Compile and run this program.
	2. Press each key on the controller keypad at least once when prompted.
	3. When the "Key Repeat" prompt appears, press the key and hold it down
	   for a least 1-2 seconds to see the "Key Repeat" feature in action.

***************************************************************************/
#class auto		// Change default: local vars now stored on stack.
#memmap xmem  // Required to reduce root memory usage


//////
// Search criteria for Rabbitnet driver
//////
#define MATCHFLAG RN_MATCH_PRDID	//set flag to search for product ID
#define MATCHPID  RN1500			//RN1500 Display



void main (	void	)
{
	unsigned	wKey;		//	User Keypress
	int keyflag, keypad_active, done;
	auto int device0, status;
   auto rn_search newdev;

	//------------------------------------------------------------------------
	// Initialize the controller
	//------------------------------------------------------------------------
	brdInit();			// Initialize the controller

   rn_init(RN_PORTS, 1);      // Initialize controller RN ports

   // Verify that the Rabbitnet display board is connected
	newdev.flags = MATCHFLAG;
	newdev.productid = MATCHPID;
   if ((device0 = rn_find(&newdev)) == -1)
   {
   	printf("\n no device found\n");
      exit(0);
   }

   // Initialize Display and Keypad low-level drivers
   // Note: Functions brdInit() and rn_init() must executed before initializing
   //       display and keypad drivers.
   glInit();
   keyInit();
	keypadDef();	// Use the default keypad ASCII return values

	printf("\n\n");
	printf("LCD/Keypad Sample Program\n");
	printf("---------------------------\n");

	for(;;)
	{

		costate
		{								//	Process Keypad Press/Hold/Release
			keyProcess ();
			waitfor ( DelayMs(10) );
		}

		costate
		{
			//------------------------------------------------------------------------------------------
			//	Default Key demo
			//------------------------------------------------------------------------------------------

			printf("\n\nPress all keys on the keypad to see Default ASCII return values!\n\n\n");
			keypadDef();
			keyflag = 0;
			done = FALSE;

			while(!done)
			{
				waitfor ( wKey = keyGet() );	//	Wait for Keypress
				printf("KEY PRESSED = %c\n\r", wKey);
				switch(wKey)
				{
					case 'R': 	keyflag |= 0x01; 	break;
					case 'E':	keyflag |= 0x02;	break;
					case 'D':	keyflag |= 0x04;	break;
					case '-':	keyflag |= 0x08;	break;
					case '+':	keyflag |= 0x10;	break;
					case 'U':	keyflag |= 0x20;	break;
					case 'L':	keyflag |= 0x40;	break;
				}
				if(keyflag == 0x7F)
				{
					done = TRUE;
				}
			}

			//------------------------------------------------------------------------------------------
			//	Custom Key demo
			//------------------------------------------------------------------------------------------

			printf("\n\nReconfigured keypad for custom ASCII return values...Press all keys again!\n\n\n");
			keyConfig ( 3,'D',0, 0, 0,  0, 0 );
			keyConfig ( 6,'G',0, 0, 0,  0, 0 );
			keyConfig ( 2,'C',0, 0, 0,  0, 0 );
			keyConfig ( 5,'F',0, 0, 0,  0, 0 );
			keyConfig ( 1,'B',0, 0, 0,  0, 0 );
			keyConfig ( 4,'E',0, 0, 0,  0, 0 );
			keyConfig ( 0,'A',0, 0, 0,  0, 0 );
			keyflag = 0;
			done = FALSE;
			while(!done)
			{
				waitfor ( wKey = keyGet() );	//	Wait for Keypress
				printf("KEY PRESSED = %c\n\r", wKey);
				switch(wKey)
				{
					case 'A': 	keyflag |= 0x01;	break;
					case 'B':	keyflag |= 0x02;	break;
					case 'C':	keyflag |= 0x04;	break;
					case 'D':	keyflag |= 0x08;	break;
					case 'E':	keyflag |= 0x10;	break;
					case 'F':	keyflag |= 0x20;	break;
					case 'G':	keyflag |= 0x40;	break;
				}
				if(keyflag == 0x7F)
				{
					done = TRUE;
				}

			}

			//------------------------------------------------------------------------------------------
			//	Key repeat demo
			//------------------------------------------------------------------------------------------

			printf("\n\nReconfigured keys back to the default with fast repeat enabled\n\n\r");
			keypadDef();

			// Setup for FAST key repeat after holding down key for 150 ticks.
			//
			// Note: The keyConfig function "cCntHold" parameter along with the delay
			// between keyprocess determines the initial KEY hold time before it starts
			// to repeat. The delay being used to control the hold time is in the first
			// costatement in this program:
			//
			//
			//		costate
			//		{								//	Process Keypad Press/Hold/Release
			//			keyProcess ();
			//			waitfor ( DelayMs(10) );
			//		}
			//
			keyConfig (  3,'R',0, 100, 1,  1, 1 );
			keyConfig (  6,'E',0, 100, 1,  1, 1 );
			keyConfig (  2,'D',0, 100, 1,  1, 1 );
			keyConfig (  5,'+',0, 100, 1,  1, 1 );
			keyConfig (  1,'U',0, 100, 1,  1, 1 );
			keyConfig (  4,'-',0, 100, 1,  1, 1 );
			keyConfig (  0,'L',0, 100, 1,  1, 1 );

			keyflag = 0;
			done = FALSE;
			while(!done)
			{
				waitfor ( wKey = keyGet() );	//	Wait for Keypress
				printf("KEY PRESSED = %c\n\r", wKey);
				switch(wKey)
				{
					case 'R': 	keyflag |= 0x01;	break;
					case 'E':	keyflag |= 0x02;	break;
					case 'D':	keyflag |= 0x04;	break;
					case '+':	keyflag |= 0x08;	break;
					case 'U':	keyflag |= 0x10;	break;
					case '-':	keyflag |= 0x20;	break;
					case 'L':	keyflag |= 0x40;	break;
				}
				if(keyflag == 0x7F)
				{
					done = TRUE;
				}
			}
		}
	}
}