/***************************************************************************
	fun.c
	
	Z-World, 2001
	This sample program is for the LCD MSCG12232 Display Module.

  	This program demonstrates the drawing primitive features of the
  	graphic library (i.e. lines, circles, polygons).

  	Instructions:
  	-------------
  	1. Run and compile this program.
  	2. Watch the LCD display as it goes through the various graphic demo's.

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

// Structure to hold 12x16 font information
fontInfo fi12x16;

// General use MsDelay function 
nodebug
void msDelay(unsigned int ams)
{
	auto unsigned long t0;
	
	for (t0=MS_TIMER; MS_TIMER<ams+t0;);
}


main()
{
	auto int led, increment, mask, time_delay, loop, i, j, y;

	// Initialize the controller
	brdInit();	

	// Initialize the LCD display
	glInit();

	// Initial the font information structure
	glXFontInit(&fi12x16, 12, 16, 32, 127, Font12x16);	//	initialize basic font

	increment = 0;
	while(1)
	{
		// Costate to toggle the LED's
		costate
		{
			mask = 0x01;
			increment++;
			for(led = 0; led <= 6; led++)
			{
				if(increment & mask)
			 		ledOut(led, 1);
				else
					ledOut(led, 0);
				mask = mask << 1;
			}
			waitfor(DelayMs(75));	
		}

		// Costate to demonstrate the graphic primitives
		costate
		{
			// Display the initial sign-on message
			glBlankScreen();
			glPrintf (0,  0,   &fi12x16, "Welcome to");
			glPrintf (0,  16,  &fi12x16, "Z-World!");
			waitfor(DelayMs(1000));	
			

			// With using the line algorithm draw a circular pattern, with
			// using the buffer LOCK/UNLOCK speed feature.
			glBlankScreen();
			glBuffLock();
			for(j = 0; j < LCD_YS; j++)
			{
				glPlotLine(0, j, LCD_XS - 1, LCD_YS - 1 - j);
				if((j % 4) == 0)
				{
					glBuffUnlock();
					glBuffLock();
				}	
			}
			for(i = LCD_XS - 1; i >= 0; --i)
			{

				glPlotLine(i, 0, LCD_XS - 1 - i, LCD_YS - 1);
				if((i % 4) == 0)
				{
					glBuffUnlock();
					glBuffLock();		
				}
			}
			glBuffUnlock();


			// Plot circle
			glBlankScreen();
			glPlotCircle (60, 16, 14);
			waitfor(DelayMs(750));	

			// Fill the circle that was just plotted
			glFillCircle (60, 16, 14);
			waitfor(DelayMs(500));	


			// Draw a series of circles (going from small to large)			
			glBlankScreen();
			for (y = 1; y < 60-4; y+=4)
			{
				glPlotCircle(60,16,y);
				waitfor(DelayMs(200));	
			}

			// Draw a series of circles (going from large to small)
			glBlankScreen();
			for (y = (60-4);  y >= 0; y-=4)
			{
				glPlotCircle(60,16,y);
				waitfor(DelayMs(10));	
			}

			// Draw a series of polygons (going from large to small)  					
			glBlankScreen();
			for (y = 1; y < 16; y+=2)
			{
				glPlotPolygon(4, 122-y,y,  y,y,  y,32-y,  122-y,32-y);
				waitfor(DelayMs(300));	
			}
		
			// Plot 4 polygons using the buffer LOCK/UNLOCK feature to speed-up
			// the display process.
			glBlankScreen();
			glBuffLock();
			glPlotPolygon(4, 0, 0,   16,0,    16,15,   0, 15);
			glPlotPolygon(4, 0, 16,  16,16,   16,31,   0, 31);
			glPlotPolygon(4, 105, 0,   121,0,    121,15,   105, 15);
			glPlotPolygon(4, 105, 16,  121,16,   121,31,   105, 31);
			glBuffUnlock();			
			waitfor(DelayMs(700));	

			// Fill the polygons with using the buffer LOCK/UNLOCK speed feature
			glBuffLock();
			glFillPolygon(4, 0, 0,   16,0,    16,15,   0, 15);
			glFillPolygon(4, 0, 16,  16,16,   16,31,   0, 31);
			glFillPolygon(4, 105, 0,   121,0,    121,15,   105, 15);
			glFillPolygon(4, 105, 16,  121,16,   121,31,   105, 31);
			glBuffUnlock();
			waitfor(DelayMs(700));
		}
	}
}