/******************************************************************************
 *
 * 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 "touch.h"
 

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



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

/* EA QVGA portrait mode TFT display parameters */
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 */
};

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
                            };

#define BACKLIGHT_Y   (DISPLAY_HEIGHT - 60)
#define SLIDER_Y      (BACKLIGHT_Y + 35)
#define SLIDER_MARGIN 20
#define SLIDER_H      20
#define BACKLIGHT_PERC_MAX 35

static tU16 lastSliderX = SLIDER_MARGIN; 
                            
void calibrateStart(void);
void lcdSetBacklight(tU8 percent);

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


/***********************************************************************
 *
 * Function: adjustBacklight
 *
 * Purpose: Adjust the backlight
 *
 * Parameters: 
 *      x : x value of slider
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
static void adjustBacklight(tU16 x)
{
  tU16 perc = x - SLIDER_MARGIN;
  perc = perc * BACKLIGHT_PERC_MAX / (DISPLAY_WIDTH-2*SLIDER_MARGIN);
  perc = 100 - perc;
  
  lcdSetBacklight(perc);
}

/***********************************************************************
 *
 * Function: moveSlider
 *
 * Purpose: Move the slider
 *
 * Parameters: 
 *      x : x value
 *      y : y value
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
static void moveSlider(tU16 x, tU16 y)
{
  if (y < SLIDER_Y-SLIDER_H/2 || y > SLIDER_Y+SLIDER_H/2)
    return;

  if (x < SLIDER_MARGIN || x > DISPLAY_WIDTH-SLIDER_MARGIN-1)
    return; 

  /* erase old */
  lcd_line(lastSliderX, SLIDER_Y-SLIDER_H/2, lastSliderX, SLIDER_Y+SLIDER_H/2, WHITE);
  
  lcd_line(lastSliderX, SLIDER_Y, lastSliderX, SLIDER_Y, BLACK);
  lcd_line(lastSliderX, SLIDER_Y+1, lastSliderX, SLIDER_Y+1, BLACK);   

  lcd_line(x, SLIDER_Y-SLIDER_H/2, x, SLIDER_Y+SLIDER_H/2, BLUE); 

  lastSliderX = x;

  adjustBacklight(x);

}

/***********************************************************************
 *
 * Function: drawBacklightArea
 *
 * Purpose: Draw the area for the backlight control
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
static void drawBacklightArea(void)
{

  lcd_line(0, BACKLIGHT_Y, DISPLAY_WIDTH-1, BACKLIGHT_Y, BLACK);
  lcd_putString(65, BACKLIGHT_Y+6, "Backlight control");

  lcd_line(SLIDER_MARGIN, SLIDER_Y, DISPLAY_WIDTH-SLIDER_MARGIN-1, SLIDER_Y, BLACK);
  lcd_line(SLIDER_MARGIN, SLIDER_Y+1, DISPLAY_WIDTH-SLIDER_MARGIN-1, SLIDER_Y+1, BLACK);

  moveSlider(lastSliderX, SLIDER_Y);
}



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

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

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

  //initialize LCD

  lcdInit(&ea_QVGA_v2); 
  lcdTurnOn();
  
  touch_init();
  
  lcd_fillScreen(WHITE);
  calibrateStart();
  lcd_fillScreen(WHITE);  
  
  drawBacklightArea();

  while(1) {
    tS32 x, y, z = 0;

    touch_xyz(&x, &y, &z);
    if (y < BACKLIGHT_Y) 
    {
      lcd_point(x, y, BLACK);
    }
    else 
    {
      moveSlider(x, y);      
    }
  }  
  
#if 0  
  while(1)
  {
    lcd_fillScreen(BLACK);
    lineDemo();

    lcd_fillScreen(BLACK);
    rectangleDemo();

    lcd_fillScreen(BLACK);
    circleDemo();
  }
#endif  

  return 0;
}
