#include "app_step.h"
#include "nrf_gpio.h"
#include "usr_config.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_mt.h"
#include "app_host.h"
#include "app_charge.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "nrf_delay.h"
#include "app_flash.h"
#include "hal_ser_imu_mode_manage.h"
#include "ble_comm.h"
#include "exception.h"

static uint8_t realStepMode = 0;        //是否为实时计步模式
static uint32_t realStepCur_L = 0;      //左鞋步数记录点
static uint32_t realStepAdd_R = 0;      //右鞋步数
static uint8_t realStepTimCnt = 0;      //连接右鞋计时
static uint8_t  realStepHeartCnt = 0;   //游戏模式心跳计时

/********************** 函数声明区 *************************/
uint32_t app_step_GetStep_L(void)
{
	return mFlash.mStep.stepCur[0];
}

uint32_t app_step_GetStep_R(void)
{ 
	return mFlash.mStep.stepCur[1];
}

//右鞋实时计步发送
static void app_step_RealSendProcess(void)
{
	if(-1 != hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP)){
		uint8_t buf[8];
		uint8_t L = 0;
		buf[L++] = BLE_REALTIMESTEP_ENTER;
		buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>24);
		buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>16);
		buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
		buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
		buf[L++] = (uint8_t)(realStepAdd_R>>24);
		buf[L++] = (uint8_t)(realStepAdd_R>>16);
		buf[L++] = (uint8_t)(realStepAdd_R>>8);
		buf[L++] = (uint8_t)(realStepAdd_R>>0);
//		SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]-realStepCur_L:%d,realStepAdd_R:%d\n",(mFlash.mStep.stepCur[0]-realStepCur_L),realStepAdd_R);
		BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
	}
}

//右鞋实时计步连接管理
void app_step_RealConnectProcess(void)
{
	 if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != -1){
		  if(realStepHeartCnt < 255)realStepHeartCnt++;
			if(realStepHeartCnt >= 60){//与手机断开连接
				hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
				if(realStepTimCnt > 0){ realStepTimCnt = 0; //从机在发步数
					uint8_t mod = BLE_REALTIMESTEP_HANDUP;
					BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //挂起从机实时步数
				}
			}else{
				if(realStepTimCnt>0) realStepTimCnt--;
				if(realStepTimCnt==0) BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
			}
		}else{
				if(realStepTimCnt>0){ realStepTimCnt = 0;
						BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
				}
     }
}

//接收右鞋实时步数并发送到手机
void cb_BLE_Host_R_REALTIMESTEP(void* handle)
{
	BLE_Client_Rx_t* target = handle;
	#if DEBUG_STEP
	extern step_count_t mIstep_count_t_right;
	
	mIstep_count_t_right.F_val_now = ((uint32_t)target->pDat[1]<<8)|((uint32_t)target->pDat[2]<<0);
	mIstep_count_t_right.F_val_now_B = ((uint32_t)target->pDat[3]<<8)|((uint32_t)target->pDat[4]<<0);
	mIstep_count_t_right.F_filter_val = ((uint32_t)target->pDat[5]<<8)|((uint32_t)target->pDat[6]<<0);
	mIstep_count_t_right.F_flag_step = ((uint32_t)target->pDat[7]<<8)|((uint32_t)target->pDat[8]<<0);
	hal_step_matlib_Test(0x01);
	if(mIstep_count_t_right.F_flag_step > 0)mIstep_count_t_right.F_flag_step =0;
	#else
	if(target->pDat[0]==BLE_REALTIMESTEP_ENTER){
		  realStepAdd_R = ((uint32_t)target->pDat[1]<<24) | (uint32_t)(target->pDat[2]<<16) | (uint32_t)(target->pDat[3]<<8) | ((uint32_t)target->pDat[4]<<0);
		  app_step_RealSendProcess();
//		  SEGGER_RTT_printf(0,">>>>>>realStepAdd_R:%ld\n",realStepAdd_R);
			realStepTimCnt = 3;
	}
	#endif
}

//接收手机命令
void cb_BLE_Client_R_REALTIMESTEP(void* handle)
{
	BLE_Client_Rx_t* target = handle;
	uint8_t cmd = target->pDat[0];

	if(cmd==BLE_REALTIMESTEP_ENTER){
		realStepHeartCnt = 0;
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
	}
	else if(cmd==BLE_REALTIMESTEP_EXIT){
		hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
	}
	
	BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&cmd,1);
	
	if(cmd==BLE_REALTIMESTEP_ENTER && realStepMode==BLE_REALTIMESTEP_EXIT){
			realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
		  realStepAdd_R = 0;
	}
	realStepMode = cmd;
}

void app_step_Init(void)
{
	if(0 == mFlash.isHost)Process_Start(100,"step_RealSend",app_step_RealSendProcess);
	Process_Start(1000,"step_RealConnect",app_step_RealConnectProcess);
	
	BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
}


