#include "usr_config.h"
#include "hal_battery.h"
#include "nrf_drv_saadc.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_led.h"
#include "hal_ser_imu_mode_manage.h"
#include "app_overturn.h"
#include "hal_wearshoes.h"

#define acc1iValue_noshoes 1200

#define acc0Value  -1400

#define acc1iValue -1400
#define acc1aValue 1800
#define acc2iValue -1500
#define acc2aValue 400				
				
/************************ 踮脚显示电量 ***********************************/
static uint8_t flag_move =0;

void app_BatDispaly_Process_N(void)        
{
		int16_t acc[3] = {0,0,0};
	  ser_imu_data_t data;
		static uint32_t cnt = 0;
					
		if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_STANDBY)) return;//停止状态
					
		//获取最新一组前脚加速度
		if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
			hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
			acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
		}
	  
//		SEGGER_RTT_printf(0,"0:acc[0]=%d,acc[1]=%d,acc[2]=%d\n",acc[0],acc[1],acc[2]);
		if(acc[2] > acc1iValue_noshoes){
				cnt++;
				if(cnt ==(1500/StandByPower_Interval) ){ 
//					  SEGGER_RTT_printf(0,">>>>>>>0:acc[0]=%d,acc[1]=%d,acc[2]=%d\n",acc[0],acc[1],acc[2]);
						if(GetBatteryPersent()>20){
								LED_Start(LED_OVERTURN,COLOR_GREEN);
						}else{
								LED_Start(LED_OVERTURN,COLOR_ORANGE);
						}
						Process_SetHoldOn(app_BatDispaly_Process_N,1);
				}
				if(cnt >= (10000/StandByPower_Interval)){
						LED_Stop(LED_OVERTURN);
						Process_SetHoldOn(app_BatDispaly_Process_N,0);
				}
		}else{
				if(cnt>0){ cnt = 0;
						LED_Stop(LED_OVERTURN);
						Process_SetHoldOn(app_BatDispaly_Process_N,0);
				}
				if(1== flag_move){
					 flag_move =0;
					 LED_Stop(LED_OVERTURN);
					 Process_SetHoldOn(app_BatDispaly_Process_N,0);
				}
		}
}

void app_BatDispaly_Process(void)
{
		int16_t acc[3] = {0,0,0};
	  ser_imu_data_t data;
		static uint32_t cnt = 0;
		static int16_t         acc0 = acc0Value;
		static int16_t         acc1i = acc1iValue;
		static int16_t         acc1a = acc1aValue;
		static int16_t         acc2i = acc2iValue;
		static int16_t         acc2a = acc2aValue;

		if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_NORMAL)) return;//走路状态
	
		//获取最新一组前脚加速度
	  if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
			hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
			acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
		}
		
//     SEGGER_RTT_printf(0,"1:acc[0]=%d,acc[1]=%d,acc[2]=%d\n",acc[0],acc[1],acc[2]);
		if(acc[0] < acc0 &&  
			(acc[1] > acc1i && acc[1] < acc1a) &&  
			(acc[2] < acc2a && acc[2] > acc2i)){
				cnt++;
				if(cnt==(1500/StandByPower_Interval)){ 
						if(GetBatteryPersent()>20){
							LED_Start(LED_OVERTURN,COLOR_GREEN);
						}else{
							LED_Start(LED_OVERTURN,COLOR_ORANGE);
						}
						Process_SetHoldOn(app_BatDispaly_Process,1);
						acc0 = acc0Value;
						acc1i = acc1iValue;
						acc1a = acc1aValue;
						acc2i = acc2iValue;
						acc2a = acc2aValue;
				}
				if(cnt == (10000/StandByPower_Interval)){
						LED_Stop(LED_OVERTURN);
						Process_SetHoldOn(app_BatDispaly_Process,0);
						acc0 = acc0Value-200;
						acc1i = acc1iValue;
						acc1a = acc1aValue;
						acc2i = acc2iValue;
						acc2a = acc2aValue;
				}
		}else{
				if(cnt>0){ cnt = 0;
						LED_Stop(LED_OVERTURN);
						Process_SetHoldOn(app_BatDispaly_Process,0);
						acc0 = acc0Value;
						acc1i = acc1iValue;
						acc1a = acc1aValue;
						acc2i = acc2iValue;
						acc2a = acc2aValue;
				}
				if(flag_move == 0){
					 flag_move =1;
					 LED_Stop(LED_OVERTURN);
					 Process_SetHoldOn(app_BatDispaly_Process,0);
					 acc0 = acc0Value;
					 acc1i = acc1iValue;
					 acc1a = acc1aValue;
					 acc2i = acc2iValue;
					 acc2a = acc2aValue;
				}
		}
}


void app_overturn_Init(void)
{
	Process_Start(StandByPower_Interval,"BatDispaly",app_BatDispaly_Process);
	Process_Start(StandByPower_Interval,"BatDispaly_N",app_BatDispaly_Process_N);
}
