//---------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#pragma hdrstop

//#include "../../core/api.h"
#include "ktapi.h"

#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

//---------------------------------------------------------------------------
#define BIT0		0x01
#define BIT1		0x02
#define BIT2		0x04
#define BIT3		0x08
#define BIT4		0x10
#define BIT5		0x20
#define BIT6		0x40
#define BIT7		0x80

		
#define LCD_RS		BIT0
#define LCD_R_W		BIT1
#define	LCD_E		BIT2



unsigned char LCD_ReadNibble(unsigned RS)
{
unsigned char Data=0;

		
		// d0-d3 output d4-d7 input
		
		
		
		if (RS==0)
		{
			SetClrGPIO(LCD_E |LCD_RS ,0);
			SetClrGPIO(LCD_R_W,1);
		}
		else
		{
			SetClrGPIO(LCD_E ,0);
			SetClrGPIO(LCD_R_W | LCD_RS ,1);
		}
			

		SetClrGPIO(LCD_E ,1);
	

		// 100ns
		usleep(1);
		ReadGPIO(0xf0,&Data);
		
				
		SetClrGPIO(LCD_E ,0);
		
		return Data >> 4;

}



void LCD_WriteNibble(unsigned char RS,unsigned char Val)
{
		// All output
		SetGPIODir(0xff);
		

		
		if (RS==0)
		{
			SetClrGPIO(LCD_R_W|LCD_E |LCD_RS ,0);
		}
		else
		{
			SetClrGPIO(LCD_R_W|LCD_E ,0);
			SetClrGPIO(LCD_RS ,1);
		}
			

		SetClrGPIO(Val&0xf0,1);
		SetClrGPIO((~Val)&0xf0,0);

		usleep(1);
		
			
		SetClrGPIO(LCD_E ,1);
		
		usleep(1);

		SetClrGPIO(LCD_E ,0);
	
}


unsigned char LCD_Read(unsigned char RS)
{
unsigned char Data;
	SetGPIODir(0x0f);
	
	Data=LCD_ReadNibble(RS)<<4;
	Data|=LCD_ReadNibble(RS);

	return Data;
}

void LCD_Wait(void)
{
unsigned char Data;
unsigned int TimeOut=1000;

	//usleep(10000);
	//return;

	do
	{
		Data=LCD_Read(0);
//		printf("%02x-",Data);
		Data&=0x80;
		
		
	}
	while((Data==0x80)&&(--TimeOut!=0));
	//printf("\n");
	
}

void LCD_Write(unsigned char RS,unsigned char Val)
{
	LCD_Wait();
	LCD_WriteNibble(RS,Val&0xf0);
	LCD_WriteNibble(RS,(Val<<4)&0xf0);

}

void LCD_Clear(void)
{
	// Display Clear
	LCD_Write(0, BIT0 );
}

void LCD_String(unsigned char Offset,char * Text)
{
	LCD_Write(0, BIT7  | Offset );
	
	while(*Text!=0x00)
	{
		LCD_Write(1, *Text++);
	}
}

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
char * Name;
char * OS_String;
int i;
_pHWM_Item HM_Items;
_API_ * BIN_KT_API;

float fVal_1;
float fVal_2;
char String[0x100];

	if (KT_API_Open(sfInternal))
	{
		Get_KT_API(&BIN_KT_API);
		printf("KTAPI Verion %d.%03d.%d.%d",BIN_KT_API->VersionMajor,BIN_KT_API->VersionMinor,BIN_KT_API->Build,BIN_KT_API->Type);
		
		Name="N/A";
		GetBoardName(&Name);
		printf(" - %s",Name);

		
		OS_String="N/A";
		KT_API_Get_OS_String(&OS_String);
		printf(" (%s)\n",OS_String);		
		
		printf("\n");
	/*
		if (ReadHWMonitorItems(&HM_Items))
		{             
			while(HM_Items->ID!=_HWM_Last)
			{            
				
				switch(HM_Items->ID & _HWM_Type_Mask)
				{
				case _HWM_Type_Voltage:  
					printf("%-10s: %8.3f V\n",HM_Items->Name,HM_Items->Value);
					break;
				case _HWM_Type_Temperature:
					printf("%-10s: %8.1f C\n",HM_Items->Name,HM_Items->Value);
					break;
				case _HWM_Type_Fan:
					printf("%-10s: %8.0f RPM\n",HM_Items->Name,HM_Items->Value);
					break;
				default:
					printf("%-10s: %8.3f\n",HM_Items->Name,HM_Items->Value);
					break;
					
				}
				HM_Items++;
				
			}
		}*/



		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(100);
		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(100);
		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(100);
		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(100);
		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(1000*5);
		LCD_WriteNibble(0,BIT5 | BIT4);
		usleep(1000*100);
		LCD_WriteNibble(0,BIT5 | BIT4);

		// Set 4 bit mode
		LCD_WriteNibble(0,BIT5 );
		
		
		LCD_Write(0,BIT5 | BIT3);
		
		// Display Off
		LCD_Write(0, BIT3 );

		// Display On
		LCD_Write(0, BIT3|BIT2);
		
		
		// Display Clear
		LCD_Write(0, BIT0 );
		
		// Enter mode set
		LCD_Write(0, BIT2 | BIT1  );//| BIT1 );//| BIT0);

		// Enter mode set
		LCD_Write(0, BIT4 | BIT2 );//|BIT3| BIT2);
		
				

		LCD_Write(0, BIT6 );
		LCD_Write(0, BIT7 );


		while(!kbhit())
		{
			ReadHWMonitorItems(NULL);
			
			GetHWMonitorItem(_HWM_CPU_Temp,&fVal_1);
			sprintf(String,"CPU Temp : %3.1f C",fVal_1);
			LCD_String(0,String);
			
			
			GetHWMonitorItem(_HWM_CPU_Fan,&fVal_1);
			sprintf(String,"CPU Fan  : %4.0f RPM",fVal_1);
			LCD_String(0x40,String); 
			
/*			
			LCD_String(0,String);
			LCD_String(0x40,"Line2");
			LCD_String(20,"Line3");
			LCD_String(0x40+20,"Line4");
			*/
		}
		
		LCD_Clear();
		
		
		/*
		LCD_String(0x40,"abcdef");
		LCD_String(14,"abcdef");
		LCD_String(0x40+14,"abcdef");
		*/
		KT_API_Close();
	}

	return 0;
}
//---------------------------------------------------------------------------
