#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "NUC1xx.h"
#include "DrvUART.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "LCD_Driver.h"
uint8_t  read_buf[17];
uint8_t  i;
uint8_t  j;
char     TEXT[17];
char     TEXT1[16] = "Pressure:    hpa";
char     TEXT2[16] = "Hum(a/s):      %";
char     TEXT3[16] = "Temp    :      c";
char     TEXT4[16] = "RainDrop:       ";
char     SENSOR1[16] = "                ";
char     SENSOR2[16] = "                ";
char     SENSOR3[16] = "                ";
char     SENSOR4[16] = "                ";
char     SENSOR5[16] = "                ";
char     SENSOR6[16] = "                ";
int 		 len;
int 		 cont;
int 		 pointer[15];
double 	 ucByte3f;
long 		 ucByte1 = 0;
long 		 ucByte2 = 0;
long 		 ucByte3 = 0;
long		 ucByte4 = 0;
long		 ucByte5 = 0;
/*---------Function Delay-----------------*/
void delay(void){
int j;
for(j=0;j<3000000;j++);
}
int Delay(int ms){
	while(ms--);
	return 0;}
/*---------System Restarter--------------*/
void callableMain()
{
	
	STR_UART_T sParam;
   	
	DrvGPIO_InitFunction(E_FUNC_UART0);	// Set UART pins

	/* UART Setting */
  sParam.u32BaudRate 		= 9600;
  sParam.u8cDataBits 		= DRVUART_DATABITS_8;
  sParam.u8cStopBits 		= DRVUART_STOPBITS_1;
  sParam.u8cParity 		  = DRVUART_PARITY_NONE;
  sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;

	/* Set UART Configuration */
 	if(DrvUART_Open(UART_PORT0,&sParam) != E_SUCCESS);
	
	DrvGPIO_SetBit(E_GPA,13);
	DrvGPIO_ClrBit(E_GPC,12);
	DrvGPIO_ClrBit(E_GPC,13);
	DrvGPIO_ClrBit(E_GPC,14);
	DrvGPIO_ClrBit(E_GPC,15);
	
	//looping program here
	DrvUART_Read (UART_PORT0, read_buf, 16);
		len  = 0;							//reset len  
		cont = 0;							//reset the pointer step
		for(i=0;i<17;i++){
		TEXT[i]=read_buf[i];
		 }
		len = strlen(TEXT);		//the length of TEXT char
		for(j=0;j<len;j++){		
		if(TEXT[j]==';'){		  //find serial divider ";"
		pointer[cont] = j;		
		cont++;
		}
		}
		strncpy(SENSOR1, TEXT+0, (pointer[0]));
		strncpy(SENSOR2, TEXT+(pointer[0]+1), (pointer[1]-pointer[0]-1));
		strncpy(SENSOR3, TEXT+(pointer[1]+1), (pointer[2]-pointer[1]-1));
		strncpy(SENSOR4, TEXT+(pointer[2]+1), (pointer[3]-pointer[2]-1));
		strncpy(SENSOR5, TEXT+(pointer[3]+1), (pointer[4]-pointer[3]-1));
		strncpy(SENSOR6, TEXT+(pointer[4]+1), (len-pointer[4]-1));
		ucByte1 = strtol(SENSOR1, NULL, 16);
		ucByte2 = strtol(SENSOR2, NULL, 16);
		ucByte3 = strtol(SENSOR3, NULL, 16);
		ucByte4 = strtol(SENSOR4, NULL, 16);
		ucByte5 = strtol(SENSOR5, NULL, 16);
		ucByte3f = (double)ucByte3/1000;
		sprintf(TEXT1+0,"Pressure:%1.3f hPa",ucByte3f);
		print_lcd(0,TEXT1);
		sprintf(TEXT2+0,"Hum(S/A):%02d%%/%02d%%",ucByte1,ucByte4);
		print_lcd(1,TEXT2);	
		sprintf(TEXT3+0,"Temp    :%02d C    ",ucByte5);
		print_lcd(2,TEXT3);	
		sprintf(TEXT4+0,"RainDrop:%02d %%   ",ucByte2);
		print_lcd(3,TEXT4);
		Delay(100000000);                  
  }
/*---------MAIN function------------------*/
/*----------------------------------------*/
int main(void){
		UNLOCKREG();
	SYSCLK->PWRCON.XTL12M_EN = 1;
	DrvSYS_Delay(5000);
  DrvSYS_Open(48000000);
	LOCKREG();
	
	DrvGPIO_Open (E_GPB, 11, E_IO_OUTPUT);	//mengaktifkan Buzzer
	DrvGPIO_Open (E_GPA, 12, E_IO_OUTPUT);	//mengaktifkan RGB LED Biru
	DrvGPIO_Open (E_GPA, 13, E_IO_OUTPUT);	//mengaktifkan RGB LED Hijau
	DrvGPIO_Open (E_GPA, 14, E_IO_OUTPUT);	//mengaktifkan RGB LED Merah
	DrvGPIO_Open (E_GPC, 12, E_IO_OUTPUT);	//mengaktifkan LED1
	DrvGPIO_Open (E_GPC, 13, E_IO_OUTPUT);	//mengaktifkan LED2
	DrvGPIO_Open (E_GPC, 14, E_IO_OUTPUT);	//mengaktifkan LED3
	DrvGPIO_Open (E_GPC, 15, E_IO_OUTPUT);	//mengaktifkan LED4

	
	Initial_pannel();                 // initialize LCD panel
	clr_all_pannal();                 // clear LCD panel	
	DrvGPIO_ClrBit(E_GPD,14); 				// mengaktifkan latar LCD 	
	print_lcd(0, "Weather Station "); 
	print_lcd(1, "                "); 
	print_lcd(2, " Adrie Sentosa  "); 
	print_lcd(3, "NRP: 2212100078 "); 
	Delay(50000000);									//10 detik
	 while(1) {
		callableMain();
		DrvGPIO_ClrBit(E_GPA,13);
		Delay(1000);
	}
	}

