/**************************************************************************

	Rn_buzzer.c

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

	This program demonstrates the control of the buzzer on the controller,
   it uses both API functions buzzer() and buzzerActivate().

	Note: The Buzzer is mono-tone, however by toggling the control bit
	      faster or slower, some pitch and motor boat affects can be
	      obtained.

**************************************************************************/
#class auto
#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


nodebug
void msDelay(unsigned int delay)
{
	auto unsigned long done_time;

	done_time = MS_TIMER + delay;
   while( (long) (MS_TIMER - done_time) < 0 );
}


void main()
{
	auto int i, delay, loop, counter;
	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();


	printf("\n\n");

	// Main loop to control the buzzer
	for(loop = 0; loop < 2; loop++)
	{
		// Generate 3 long beeps
		printf("\n\nGenerate 3 beeps\n\n");
		for(i=0; i < 3; i++)
		{
			for(delay = 0; delay < 8000; delay++);
			buzzer(1);
			for(delay = 0; delay < 10000; delay++);
			buzzer(0);

		}
		msDelay(3000);

		// Make the buzzer go up in pitch
		printf("Make the buzzer goes up in pitch\n\n");
		for(i=0; i < 800; i++)
		{
			for(delay = 800; delay > i; delay--);
			buzzer(1);
			for(delay = 800; delay > i; delay--);
			buzzer(0);
		}
		msDelay(3000);

		// Make the buzzer go down in pitch
		printf("Make the buzzer goes down in pitch\n\n");
		for(i=0; i < 1000; i++)
		{
			for(delay = 0; delay < i; delay++);
			buzzer(1);
			for(delay = 0; delay < i; delay++);
			buzzer(0);
		}
		msDelay(3000);

      printf("Use non-blocking buzzerActivate\n\n");
      buzzerActivate(5000);
      counter = 0;
    	for(i=0; i < 600; i++)
		{
      	printf("Code Executing while buzzer is ON = %d     \r", counter++);
		}
		msDelay(3000);
	}
}