/**
  ******************************************************************************
  * @file    dio.c
  * @author  hskim
  * @version v0.7
  * @date    2019-06-07
  * @brief   This file provides Digital Input and Output's fuction.
  ******************************************************************************
  */





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "pca10040.h"
#include "dio.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "sysmgr.h"

#include "main.h"



uint8_t BT5ButtonList[2] = BUTTONS_LIST;


stHSDOut HSDOut[3];
stHSDIOTask HSDIOTask;


uint32_t TimeSlice[10];
static int TS = 0;


stHSDIn DIn[2];


void DioTask()
{
	DITask();
}




int DITask()
{
	int i;
	uint16_t readDI;

	if( HSDIOTask.DIRunFlag < HSDIOTask.DIRunPeriodTime )
		return 0;
	HSDIOTask.DIRunFlag = 0;

	for( i=0; i<1; i++)
	{
		//DInput[i].in  = (uint8_t)HAL_GPIO_ReadPin(DIPinMap[i].Port,  DIPinMap[i].Pin);
		//if( DIPinMap[i].HighActive == LowActive )
		//	DInput[i].in = ~DInput[i].in&0x01;
	}

    return 0;
}






int DOTask()
{

#if 0
	int i;
	int DoIndex;

	if( !HSDIOTask.RunFlag )
		return 0;
	HSDIOTask.RunFlag = 0;

	HSDOutProcess(DOut, DOPinMap, 21 );
#endif

    return 0;

}





void HSDInInit()
{


    uint32_t i;
    for (i = 0; i < BUTTONS_NUMBER; ++i)
    {
        nrf_gpio_cfg_input(BT5ButtonList[i], BUTTON_PULL);
    }

	HSDIOTask.DIRunPeriodTime = 25;
	HSDIOTask.DISecCount =1000/HSDIOTask.DIRunPeriodTime;


	DIn[0].Pressed		 = Key_0_Pressed;
	DIn[0].Released		 = Key_0_Released;
	DIn[1].Pressed		 = Key_1_Pressed;
	DIn[1].Released		 = Key_1_Released;

	DIn[0].longPressTimeOne = 5;	//jakuja 
	DIn[0].LongPressedOne	= DevRegLongPressedOne;

#if 0	
	DIn[2].Pressed		 = Key_2_Pressed;
	DIn[2].Released		 = Key_2_Released;
	DIn[3].Pressed		 = Key_3_Pressed;
	DIn[3].Released		 = Key_3_Released;
	DIn[4].Pressed		 = Key_4_Pressed;
	DIn[4].Released		 = Key_4_Released;
#endif


}






void HSDInRun()
{
	
	int Len;

	int DiIndex;

	for( DiIndex=0; DiIndex<BUTTONS_NUMBER; DiIndex++)
	{
		if( nrf_gpio_pin_read(BT5ButtonList[DiIndex]) == false)		// BUTTON PUSH
		{
			DIn[DiIndex].in = 1;
			
			if( DIn[DiIndex].softRelease == 1 )
			{
				return;
			}


			if( DIn[DiIndex].onFlag == 0 )
			{
				DIn[DiIndex].pressCount++;
				DIn[DiIndex].Pressed(&DIn[DiIndex]);
				
				NRF_LOG_INFO("==> Key_%d Pressed", BT5ButtonList[DiIndex]);
				
			}else{

				if( DIn[DiIndex].longPressTimeKeep != 0 )
				{
					if( DIn[DiIndex].pressedSec >= DIn[DiIndex].longPressTimeKeep )
					{
						DIn[DiIndex].LongPressedKeep(&DIn[DiIndex]);
					}
				}

				if( DIn[1].pressTime == 20 )		//pressTime 25ms Tick
				{
					if( DIn[0].onFlag == 0 )
						Trigger_Emg();
					else{
						Trigger_DUF();
					}
				}

				if( DIn[DiIndex].pressTime % HSDIOTask.DISecCount == 0 )	// count up 0.5 sec
				{
					DIn[DiIndex].pressedSec++;


					if( DIn[0].onFlag == 1 && DIn[1].pressedSec == 2 )
					{

						//SystemTimer.SEND_TIMER = 0;						
						//SystemManager.FWUpdate = 1;
						//Buzzer_Select(4);
					}

					if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <= 5 )		//pressTime 25ms Tick
					{
						SystemTimer.SEND_TIMER = 0;
					}

					if( DIn[0].pressedSec == 5 )
					{
						Trigger_RegDev();
					}


					if( DIn[DiIndex].longPressTimeOne != 0 )
					{
						if( DIn[DiIndex].pressedSec >= DIn[DiIndex].longPressTimeOne )
						{
							DIn[DiIndex].longPressCount++;
							DIn[DiIndex].softRelease = 1;
							DIn[DiIndex].LongPressedOne(&DIn[DiIndex]);
							
							NRF_LOG_INFO("==> Key_%d LongPressedOne", BT5ButtonList[DiIndex]);
						}
					}
				}
			}
			
			DIn[DiIndex].onFlag = 1;
			DIn[DiIndex].pressTime++;

		}else{


			if( DIn[DiIndex].onFlag == 1 )
			{
				if( DiIndex == 0 )						// TEST BUTTON
				{
					//if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
					if( DIn[0].pressTime >= 2 && DIn[0].pressTime < 100 ){
						if( SystemManager.FWUpdate == 0 )
							Trigger_LineTest();
					}else{
						NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);

						DIn[DiIndex].in = 0;
						DIn[DiIndex].pressTime = 0;
						DIn[DiIndex].onFlag = 0;
						DIn[DiIndex].pressedSec = 0;
						DIn[DiIndex].softRelease = 0;
						DIn[DiIndex].prevTime = 0;
					
						
					}
				}

				if( DiIndex == 1 )						// EMG BUTTON
				{
					//if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
					if( DIn[1].pressTime < 25 ){
						NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);

						DIn[DiIndex].in = 0;
						DIn[DiIndex].pressTime = 0;
						DIn[DiIndex].onFlag = 0;
						DIn[DiIndex].pressedSec = 0;
						DIn[DiIndex].softRelease = 0;
						DIn[DiIndex].prevTime = 0;
				
						
					}
				}

				DIn[DiIndex].Released(&DIn[DiIndex]);

				NRF_LOG_INFO("==> Key_%d Released", BT5ButtonList[DiIndex]);
			}

			
			DIn[DiIndex].in = 0;
			DIn[DiIndex].pressTime = 0;
			DIn[DiIndex].onFlag = 0;
			DIn[DiIndex].pressedSec = 0;
			DIn[DiIndex].softRelease = 0;
			DIn[DiIndex].prevTime = 0;

		}

	}

}





void Trigger_Emg()
{

	NRF_LOG_INFO("# EMG Pressed");

	SystemManager.EmgSendButton = BUTTON_EMG;
	SystemTimer.EMG_SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;


	if(!advertising_flag)
		advertising_start();
	
	Buzzer_Select(BUZZER_EMG);

}


void Trigger_LineTest()
{

	NRF_LOG_INFO("# Test Pressed");

	SystemManager.EmgSendButton = BUTTON_TEST;
	SystemTimer.EMG_SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;

	if(!advertising_flag)
		advertising_start();


	Buzzer_Select(BUZZER_TEST);

}


void Trigger_RegDev()
{

	NRF_LOG_INFO("# DevReg Pressed");

	SystemManager.EmgSendButton = BUTTON_REG;
	SystemTimer.EMG_SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;

	if(!advertising_flag)
		advertising_start();

	Buzzer_Select(BUZZER_REG);

}


void Trigger_DUF()
{
	NRF_LOG_INFO("# DevReg Pressed");

	SystemTimer.SEND_TIMER = 0; 					
	SystemManager.FWUpdate = 1;
	Buzzer_Select(BUZZER_BOOT_LOADER);
}



char Key1Flag = 0;

void Key_0_Pressed(stHSDIn* pDin)
{


	Key1Flag = 0;

	

}

void Key_0_Released(stHSDIn* pDin)
{

	if( DIn[0].pressedSec < 1 )//jakuja 
		return;


	
	
	if( Key1Flag == 1 )
		return;

#if 0	
	SystemManager.EmgSendButton = BUTTON_TEST;
	NRF_LOG_INFO("@@@@@@@ Test Pressed");

	SystemTimer.SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;
#endif

}

void DevRegLongPressedOne(stHSDIn* pDin)
{

	Key1Flag = 1;

#if 0	
	SystemManager.EmgSendButton = BUTTON_REG;

	
	NRF_LOG_INFO("@@@@@@@ DevReg Pressed");
	
	SystemTimer.SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;
#endif


}




void Key_1_Pressed(stHSDIn* pDin)
{

	if( DIn[0].onFlag == 1 )
	{
		SystemManager.FWUpdate = 1;
		Buzzer_Select(BUZZER_BOOT_LOADER);
	}
	
}

void Key_1_Released(stHSDIn* pDin)
{

	if( DIn[1].pressedSec < 1 )//jakuja 
		return;
	
#if 0
	NRF_LOG_INFO("@@@@@@@ EMG Pressed");

	SystemManager.EmgSendButton = BUTTON_EMG;
	SystemTimer.SEND_RUN = 1;
	SystemTimer.SEND_TIMER = 0;
#endif

}






#if 0
void Key_2_Pressed(stHSDIn* pDin)
{
	uint8_t sendKey[2];
	sendKey[0] = 0x51;	
	 keys_send(1, &sendKey[0]);
	
}

void Key_2_Released(stHSDIn* pDin)
{
	//NRF_LOG_INFO("Key_2_Released");

}


void Key_3_Pressed(stHSDIn* pDin)
{
	//NRF_LOG_INFO("Key_3_Pressed");

}

void Key_3_Released(stHSDIn* pDin)
{
	//NRF_LOG_INFO("Key_3_Released");

}



void Key_4_Pressed(stHSDIn* pDin)
{
	//NRF_LOG_INFO("Key_4_Pressed");
}//

void Key_4_Released(stHSDIn* pDin)
{
	//NRF_LOG_INFO("Key_4_Released");
}
#endif
