//---------------------------------------------------------------------------
#include <stdio.h>

#pragma hdrstop

#ifndef linux
#include "..\..\core\api.h"
#else
#include "../../core/api.h"
#endif // linux
//---------------------------------------------------------------------------


#ifndef linux

#include <conio.h>

#else


#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;
}
#endif // linux

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


          
tGetBoardName GetBoardName;

tGetGPIOConfig GetGPIOConfig;
tSetGPIOConfig SetGPIOConfig;
tGetGPIOValue GetGPIOValue;
tSetGPIOValue SetGPIOValue;
tGetGPIOCap GetGPIOCap;



void __cdecl DebugOut(char *Text)
{
	printf("%s",Text);
}


int main(int argc, char* argv[])
{
char * Name;
unsigned char Val;
int i;
int Status;
unsigned int uiVal;

	
	Proc_DebugOut=DebugOut;
	
	if (KT_API_Open(sfDISK))
	{
		printf("KTAPI Verion %d.%03d.%d.%d",KT_API->VersionMajor,KT_API->VersionMinor,KT_API->Build,KT_API->Type);
		
		if ( KT_API_GetFunction("GetBoardName",&GetBoardName))
		{	
			GetBoardName(&Name);
			printf(" - %s",Name);		
		}
		
		if (OS_String[0]!=0x00)
		{
			printf(" (%s)",OS_String);		
		}
		
		printf("\n");
		
		Status =_API_se_OK;
		Status&=KT_API_GetFunction("GetGPIOConfig",&GetGPIOConfig);
		Status&=KT_API_GetFunction("SetGPIOConfig",&SetGPIOConfig);
		Status&=KT_API_GetFunction("GetGPIOValue",&GetGPIOValue);
		Status&=KT_API_GetFunction("SetGPIOValue",&SetGPIOValue);
		Status&=KT_API_GetFunction("GetGPIOCap",&GetGPIOCap);
		

		if (Status)
		{
			printf("Functions OK\n");
			for(i=0;i<18;i++)
			{
				SetGPIOConfig(i,eGPIO_Output);
//				SetGPIOConfig(i,eGPIO_Input | eGPIO_Pulldown);
//				SetGPIOConfig(i,eGPIO_Input | eGPIO_Pullup);
				SetGPIOValue(i,0);
			}
			
			for(i=0;i<18;i++)
			{
				if (i>9)
					printf("%d ",i/10);
				else
					printf("  ");
			}
			printf("\n");
			for(i=0;i<18;i++)
			{
				printf("%d ",i % 10);
			}
			printf("\n");
			for(i=0;i<18;i++)
				printf("--");
			
			printf("\n");
			
			while(!kbhit())
			{
				for(i=0;i<18;i++)
				{
					uiVal=-1;
					GetGPIOValue(i,&uiVal);
					printf("%d ",uiVal);
				}
				printf("\r");
			}
			
			
			
		}

	
		

		KT_API_Close();
	}


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