/******************************************************************************
 *
 * Copyright:
 *    (C) 2008 Embedded Artists AB
 *
 * File:
 *    main.c
 *
 * Description:
 *    Sample application that demonstrates how to use the QVGA LCD
 *
 *****************************************************************************/

/******************************************************************************
 * Includes
 *****************************************************************************/
#include "general.h"
#include <printf_P.h>
#include <ea_init.h>
#include <stdlib.h>
#include "ea_lcd/lcd_hw.h"
#include "ea_lcd/lcd_grph.h"
#include "delay.h"
#include "sdram.h"
#include "lpc24xx.h"
 

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/



/*****************************************************************************
 * Local variables
 ****************************************************************************/


/* EA QVGA OLED portrait mode display parameters */
static tLcdParams ea_QVGA_OLED_v1 =
{
    33,        /* Horizontal back porch */
    26,       /* Horizontal front porch */
    32,       /* HSYNC pulse width */
    240,      /* Pixels per line */
    4,        /* Vertical back porch */
    8,       /* Vertical front porch */
    4,        /* VSYNC pulse width */
    320,      /* Lines per panel */
    0,        /* Do not invert output enable */
    1,        /* Invert panel clock */
    1,        /* Invert HSYNC */
    1,        /* Invert VSYNC */
    1,        /* AC bias frequency (not used) */
    16,       /* Bits per pixel */
    LCD_ADTFT,    /* panel type */
    0,        /* Single panel display */
};

lcd_color_t const COLORS_TAB[16] = {BLACK,
                             NAVY,
                             DARK_GREEN,
                             DARK_CYAN,
                             MAROON,
                             PURPLE,
                             OLIVE,
                             LIGHT_GRAY,
                             DARK_GRAY,
                             BLUE,
                             GREEN,
                             CYAN,
                             RED,
                             MAGENTA,
                             YELLOW,
                             WHITE
                            };

/*****************************************************************************
 * Implementation of local functions
 ****************************************************************************/

/*****************************************************************************
 *
 * Description:
 *    Generate a random number.
 *
 ****************************************************************************/
static tU16
random(tU16 seed)
{
  tU16 temp;
  
  temp = rand();
  temp = temp % seed;
  return temp;
}


/*****************************************************************************
 *
 * Description:
 *    Demonstrates the use of simple line function.
 *
 ****************************************************************************/
static void
lineDemo(void)
{
  tU32 x0, y0;
  tU32 x1, y1;
  tU16 color;   
  tU32 no;
  const tU32 CENTER_X = 120;
  const tU32 CENTER_Y = 160;
  
  for(no=0; no<100; no++)
  {
    x0 = random(240);
    y0 = random(320);
    color = random(15)+1;
    
    x1 = 2*CENTER_X - x0;
    y1 = 2*CENTER_Y - y0;
    
    lcd_line(x0, y0, x1, y1, COLORS_TAB[color]);
    mdelay(10);
  }
}


/*****************************************************************************
 *
 * Description:
 *    Demonstrates the use of the draw rectangle function
 *
 ****************************************************************************/
static void
rectangleDemo(void)
{
  tU32 x0, y0;
  tU32 x1, y1;
  tU16 color;
  tU32 no;
  
  for(no=0; no<50; no++)
  {
    x0 = random(240);
    y0 = random(320);
    color = random(15)+1;
    
    x1 = x0 + random(150);
    y1 = y0 + random(100);
    
    lcd_fillRect(x0, y0, x1, y1, COLORS_TAB[color]);
    mdelay(20);
  }
}


/*****************************************************************************
 *
 * Description:
 *    Demonstrates the use of draw circle function
 *
 ****************************************************************************/
static void
circleDemo(void)
{
  tU32 x0, y0;
  tU8  radius;
  tU16 color;
  tU32 no;
  
  for(no=0; no<100; no++)
  {
    x0 = random(240);
    y0 = random(320);
    color = random(16);
    radius = random(50);
    
    lcd_circle(x0, y0, radius, COLORS_TAB[color]);
    mdelay(10);
  }
}

/*****************************************************************************
 * Implementation of public functions
 ****************************************************************************/

/*****************************************************************************
 *
 * Description:
 *    The main-function. 
 *
 * Returns:
 *    Always 0, since return value is not used.
 *
 ****************************************************************************/
int
main(void)
{
  int test = 0;
  //initialize printf()-functionality
  eaInit();

  // initialize external SDRAM
  sdramInit();
  
  
  //print welcome message
  printf("\n*********************************************************");
  printf("\n*");
  printf("\n* (C) 2008 Embedded Artists AB");
  printf("\n*");
  printf("\n* Welcome to this program that demonstrates how to use the");
  printf("\n* LCD QVGA.");
  printf("\n*");
  printf("\n*********************************************************\n\n");

#if 0  
static tLcdParams ea_QVGA_v2 =
{
    28,        /* Horizontal back porch */
    10,       /* Horizontal front porch */
    2,       /* HSYNC pulse width */
    240,      /* Pixels per line */
    3,        /* Vertical back porch */
    2,       /* Vertical front porch */
    2,        /* VSYNC pulse width */
    320,      /* Lines per panel */
    0,        /* Do not invert output enable */
    1,        /* Invert panel clock */
    1,        /* Invert HSYNC */
    1,        /* Invert VSYNC */
    1,        /* AC bias frequency (not used) */
    16,       /* Bits per pixel */
    LCD_ADTFT,    /* LCD panel type */
    0,        /* Single panel display */
};
#endif

  
#ifdef TEST_LCDPARAMS
  
  printf("Horizontal back porch (%d):\t", ea_QVGA_v2.hbp);
  ea_QVGA_v2.hbp = consolGetIntNum();
  
  printf("Horizontal front porch (%d):\t", ea_QVGA_v2.hfp);
  ea_QVGA_v2.hfp = consolGetIntNum();  
  
  printf("HSYNC pulse width (%d):\t\t", ea_QVGA_v2.hsw);
  ea_QVGA_v2.hsw = consolGetIntNum();    
  
  printf("Pixels per line (%d):\t\t", ea_QVGA_v2.ppl);
  ea_QVGA_v2.ppl = consolGetIntNum();      
  
  printf("Vertical back porch (%d):\t", ea_QVGA_v2.vbp);
  ea_QVGA_v2.vbp = consolGetIntNum();        
  
  printf("Vertical front porch (%d):\t", ea_QVGA_v2.vfp);
  ea_QVGA_v2.vfp = consolGetIntNum();  
  
  printf("VSYNC pulse width (%d):\t\t", ea_QVGA_v2.vsw);
  ea_QVGA_v2.vsw = consolGetIntNum();    
  
  printf("Lines per panel (%d):\t\t", ea_QVGA_v2.lpp);
  ea_QVGA_v2.lpp = consolGetIntNum();     
  
  printf("Invert ouput enable (%d):\t", ea_QVGA_v2.ioe);
  ea_QVGA_v2.ioe = consolGetIntNum();       
  
  printf("Invert panel clock (%d):\t\t", ea_QVGA_v2.ipc);
  ea_QVGA_v2.ipc = consolGetIntNum();   
  
  printf("Invert HSYNC (%d):\t\t", ea_QVGA_v2.ihs);
  ea_QVGA_v2.ihs = consolGetIntNum();   
  
  printf("Invert VSYNC (%d):\t\t", ea_QVGA_v2.ivs);
  ea_QVGA_v2.ivs = consolGetIntNum();   
  
  
  printf("\n\nThe display will be initialized with the following values\n\n");  
  
  printf("Horizontal back porch:\t%d\n", ea_QVGA_v2.hbp);  
  printf("Horizontal front porch:\t%d\n", ea_QVGA_v2.hfp); 
  printf("HSYNC pulse width:\t%d\n", ea_QVGA_v2.hsw);  
  printf("Pixels per line:\t%d\n", ea_QVGA_v2.ppl);
  printf("Vertical back porch:\t%d\n", ea_QVGA_v2.vbp);
  printf("Vertical front porch:\t%d\n", ea_QVGA_v2.vfp);
  printf("VSYNC pulse width:\t%d\n", ea_QVGA_v2.vsw); 
  printf("Lines per panel:\t%d\n", ea_QVGA_v2.lpp);
  printf("Invert ouput enable:\t%d\n", ea_QVGA_v2.ioe);  
  printf("Invert panel clock:\t%d\n", ea_QVGA_v2.ipc);
  printf("Invert HSYNC:\t\t%d\n", ea_QVGA_v2.ihs); 
  printf("Invert VSYNC:\t\t%d\n", ea_QVGA_v2.ivs);
  
#endif
  //initialize LCD  
  lcdInit(&ea_QVGA_OLED_v1); 
  lcdTurnOn();
  
  while(1)
  {
    lcd_fillScreen(BLACK);
    lineDemo();

    lcd_fillScreen(BLACK);
    rectangleDemo();

    lcd_fillScreen(BLACK);
    circleDemo();
  }

  return 0;
}
