#include "uart.h"

#include <string.h>
#include <stdarg.h>
#include <stdio.h>

//#define UART0_BPS         115200                             
#define UART0_BPS         230400  

#if UART0_BPS == 115200
  #define UART0_BR0         0x60     //11.0592M / 115200 = 0x0060
  #define UART0_BR1         0x00
#elif UART0_BPS == 230400
  #define UART0_BR0         0x30     //11.0592M / 230400 = 0x0030    
  #define UART0_BR1         0x00
#endif

extern uint8  finger_number;
extern uint16 finger_pos_x[];
extern uint16 finger_pos_y[];

const char *gesture_str[]=
{
  "NO_GESTRUE",            //0x00
  "ST_SCROLL_UP",          //0x01
  "ST_SCROLL_RT",          //0x02
  "ST_SCROLL_DN",          //0x03
  "ST_SCROLL_LT",          //0x04
  "ST_FLICK_SLOW_UP",      //0x05
  "ST_FLICK_SLOW_RT",      //0x06
  "ST_FLICK_SLOW_DN",      //0x07
  "ST_FLICK_SLOW_LT",      //0x08
  "ST_FLICK_FAST_UP",      //0x09
  "ST_FLICK_FAST_RT",      //0x0A
  "ST_FLICK_FAST_DN",      //0x0B
  "ST_FLICK_FAST_LT",      //0x0C
  "ST_CLICK",              //0x0D
  "ST_DOUBLECLICK",        //0x0E
  "UNKNOWN",               //0x0F
  "MT_UP",                 //0x10
  "MT_RT",                 //0x11
  "MT_DN",                 //0x12
  "MT_LT",                  //0x13
  "ZOOM_IN",                //0x14
  "ZOOM_OUT",               //0x15
  "ROTATE_CLOCKWISE",       //0x16
  "ROTATE_COUNTERCLOCKWISE" //0x17
};

void uart0_init(void)
{
  P3SEL |= 0x30;         //P3.4,P3.5 set to UART;
  
  //P3OUT |= 0x08;         //Reset FT232RL 

  UCA0CTL0 = 0x00;       //Default value:No parity check, MSB first, 8bits, UART, Asynchronous mode
  
  UCA0CTL1 |= UCSWRST;    //UART held in reset mode
  UCA0CTL1 |= UCSSEL_1;   //ACLK as clock source.
  
  UCA0BR0 = UART0_BR0;
  UCA0BR1 = UART0_BR1;       
  
  UCA0MCTL &= ~UCOS16;    //disable oversampling modulation
  UCA0CTL1 &= ~UCSWRST;   //UART exit reset mode.
}

void uart0_putchar(uint8 ch)
{
  while(!(UC0IFG & UCA0TXIFG));
  
  UCA0TXBUF = ch;
}

void uart0_puts(char *str) 
{
  while (*str != 0) 
  {
    uart0_putchar(*str++);
  }
}

void uart0_printf(char *fmt,...)
{
  va_list ap;
  char string[256];
  
  va_start(ap,fmt);
  vsprintf(string,fmt,ap);
  uart0_puts(string);
  va_end(ap);
}

void uart1_init(void)
{
  P3SEL |= 0xC0;          //P3.6,P3.7 set to UART;
  
  UCA1CTL0 = 0x00;        //Default value:No parity check, MSB first, 8bits, UART, Asynchronous mode
   
  UCA1CTL1 |= UCSWRST;    //UART held in reset mode
  UCA1CTL1 |= UCSSEL_1;   //ACLK as clock source.
  
  UCA1BR0 = 0x60;
  UCA1BR1 = 0x00;         //11.0592M/115200 = 0x60
  
  UCA1MCTL &= ~UCOS16;    //disable oversampling modulation
  UCA1CTL1 &= ~UCSWRST;   //UART exit reset mode.
}

void uart1_putchar(uint8 ch)
{
  while(!(UC1IFG & UCA1TXIFG));
  
  UCA1TXBUF = ch;
}

void uart1_puts(char *str) 
{
  while (*str != 0) 
  {
    uart1_putchar(*str++);
  }
}

void uart1_printf(char *fmt,...)
{
  va_list ap;
  char string[256];
  
  va_start(ap,fmt);
  vsprintf(string,fmt,ap);
  uart1_puts(string);
  va_end(ap);
}
const char *get_gesture_string(uint8 gesture_id)
{
  if (gesture_id <= ROTATE_COUNTERCLOCKWISE)
  {
    return gesture_str[gesture_id];
  }
  else 
  {
    return NULL;
  }
}
