//
// Smpl_UART0_Arduino : read serial monitor char from arduino
//
// Arduino module
// comm port = 9600, N, 1, 8, 1
// pin3 : RXD   to NUC140 UART0-TX (GPB1)
// pin4 : TXD   to NUC140 UART0-RX (GPB0)

#include <stdio.h>
#include <string.h>
#include "NUC1xx.h"
#include "DrvUART.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "LCD_Driver.h"
/*---------Function Delay-----------------*/
void delay(void){
int j;
for(j=0;j<3000;j++);
}
int Delay(int ms){
	while(ms--);
	return 0;}
/*---------MAIN function------------------*/
/*----------------------------------------*/
int32_t main()
{
	uint8_t  read_buf[17];
	uint8_t  i;
	char     TEXT1[16] = "                ";
	char     TEXT2[16] = "                ";
	char     SENSOR1[16] = "                ";
	char     SENSOR2[16] = "                ";
	char     SENSOR3[16] = "                ";
	char     SENSOR4[16] = "                ";
	char     SENSOR5[16] = "                ";
	char     SENSOR6[16] = "                ";
	long 		 ucByte1 = 0;
	long 		 ucByte2 = 0;
	long 		 ucByte3 = 0;
	long		 ucByte4 = 0;
	long		 ucByte5 = 0;
	long		 ucByte6 = 0;
	
	STR_UART_T sParam;

	UNLOCKREG();
	SYSCLK->PWRCON.XTL12M_EN = 1;
	DrvSYS_Delay(5000);
  DrvSYS_Open(48000000);
	LOCKREG();
   	
	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;

	DrvGPIO_ClrBit(E_GPD,14); 							// mengaktifkan latar LCD 	
	Initial_pannel();                 // initialize LCD panel
	clr_all_pannal();                 // clear LCD panel	
	print_lcd(0, "Smpl_UART0_Ardu "); // print title

	/* Set UART Configuration */
 	if(DrvUART_Open(UART_PORT0,&sParam) != E_SUCCESS);
	/*
	/////////read serial data////////////////
		DrvUART_Read (UART_PORT0, read_buf, 2);
		Delay(100000);
		////////transfer data to TEXT1///////////
		for(i=0;i<17;i++){
		TEXT1[i]=read_buf[i];	
		}
		////////manipulate data////////////////
		///////////////////////////////////////
		///////clipping data//////////////////
		strncpy(SENSOR1, TEXT1+0, 2);
		strncpy(SENSOR2, TEXT1+3, 2);
		strncpy(SENSOR3, TEXT1+6, 2);
		strncpy(SENSOR4, TEXT1+9, 2);
		strncpy(SENSOR5, TEXT1+12, 2);
		strncpy(SENSOR6, TEXT1+15, 2);
		//////encoding data////////////////
		//////HEX to DEC////////////////
		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);
		ucByte6 = strtol(SENSOR6, NULL, 16);
		//////printing data////////////////
		sprintf(SENSOR1,"%d",ucByte1);
		sprintf(SENSOR2,"%d",ucByte2);
		sprintf(SENSOR3,"%d",ucByte3);
		sprintf(SENSOR4,"%d",ucByte4);
		sprintf(SENSOR5,"%d",ucByte5);
		sprintf(SENSOR6,"%d",ucByte6);
		//////display data to LCD////////////////
		print_lcd(1,SENSOR1);	
		print_lcd(2,SENSOR3);	
		print_lcd(3,SENSOR6);
		*/

  while(1) {
		DrvUART_Read (UART_PORT0, read_buf, 17);
		//Delay(100000);
		////////transfer data to TEXT1///////////
		for(i=0;i<17;i++){
		TEXT1[i]=read_buf[i];	
		}
		////////manipulate data////////////////
		///////////////////////////////////////
		///////clipping data//////////////////
		strncpy(SENSOR1, TEXT1+0, 2);
		strncpy(SENSOR2, TEXT1+3, 2);
		strncpy(SENSOR3, TEXT1+6, 2);
		strncpy(SENSOR4, TEXT1+9, 2);
		strncpy(SENSOR5, TEXT1+12, 2);
		strncpy(SENSOR6, TEXT1+15, 2);
		//////encoding data////////////////
		//////HEX to DEC////////////////
		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);
		ucByte6 = strtol(SENSOR6, NULL, 16);
		//////printing data////////////////
	//	sprintf(SENSOR1,"%d",ucByte1);
		//sprintf(SENSOR2,"%d",ucByte2);
		//sprintf(SENSOR3,"%d",ucByte3);
		//sprintf(SENSOR4,"%d",ucByte4);
		//sprintf(SENSOR5,"%d",ucByte5);
		//sprintf(SENSOR6,"%d",ucByte6);
		//////display data to LCD////////////////
		print_lcd(1,SENSOR1);	
		print_lcd(2,SENSOR3);	
		print_lcd(3,SENSOR6);	

	}
}

