#include "soft_flow.h"
#include "gpio.h"
#include "common.h"
#include "soft_uart.h"
#include "soft_flash.h"
#include "soft_p_2_c.h"
#include "string.h"
#include "soft_water_device.h"
#include "soft_seed_device.h"
#include "soft_timer.h"
#include "soft_test.h"
#include "tim.h"

flow_hl flow_dev1;
flow_hl flow_dev2;

void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
{
    //__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
    if ( GPIO_Pin == EXIT_1T_Pin )
    {
        if(HAL_GPIO_ReadPin( GPIOB, EXIT_1T_Pin ) == GPIO_PIN_SET)
        {
            flow_count_add(1,EXTI_UP);
        }
        else if (HAL_GPIO_ReadPin( GPIOB, EXIT_1T_Pin ) == GPIO_PIN_RESET)
        {
            flow_count_add(1,EXTI_FALL);
        }
    }

    if ( GPIO_Pin == EXIT_2T_Pin )
    {
        if(HAL_GPIO_ReadPin( GPIOB, EXIT_2T_Pin ) == GPIO_PIN_SET)
        {
            flow_count_add(2,EXTI_UP);
        }
        else if (HAL_GPIO_ReadPin( GPIOB, EXIT_2T_Pin ) == GPIO_PIN_RESET)
        {
            flow_count_add(2,EXTI_FALL);
        }       
    }
}


/**
  * @file    flow_count_add
  * @brief   外部中断计算触发数
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void flow_count_add(uint8_t flow_num,uint8_t exti_status)
{
    flow_hl *flow_temp = NULL;
    
    if(flow_num == 1)
    {
        flow_temp = &flow_dev1;
    }
    else
    {
        flow_temp = &flow_dev2;
    }

    if(exti_status == EXTI_UP)
    {
        if(Get_Systimer_Us() - flow_temp->falling_delay_time > 700)
        {
            flow_temp->falling_delay_falg = true;

            if(flow_temp->falling_delay_falg == true && flow_temp->rising_delay_flag == true)
            {
                flow_temp->falling_delay_falg = false;
                flow_temp->rising_delay_flag = false;
                flow_temp->irq_count++;

                if ( flow_temp->irq_count - flow_temp->irq_last_count >= 5 && flow_temp->irq_flag == 0)
                {   
                        flow_temp->speed = ( 1000.0f * ( flow_temp->irq_count - flow_temp->irq_last_count ) ) / FlOW_KP *
                                ( 1000000.0f / ( Get_Systimer_Us() - flow_temp->last_time ) ); //ml/min
                    flow_temp->irq_last_count = flow_temp->irq_count;
                    flow_temp->last_time = Get_Systimer_Us();
                }
            }

            flow_temp->rising_delay_time = Get_Systimer_Us();
        }
    }
    else if(exti_status == EXTI_FALL)
    {
        if(Get_Systimer_Us() - flow_temp->rising_delay_time > 700)
        {
            flow_temp->rising_delay_flag = true;
            flow_temp->falling_delay_time = Get_Systimer_Us();
        }
    }
}


/**
  * @file    L1L2_GPIO_check
  * @brief   34IO端口检测
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
GPIO_PinState L3_status = GPIO_PIN_SET;
GPIO_PinState L4_status = GPIO_PIN_SET;
void L1L2_GPIO_check()
{
    static int l1l2_time = 0;
    if ( HAL_GetTick() - l1l2_time < 100 )
    {
        return;
    }
    l1l2_time = HAL_GetTick();

    // 抛球信号 接PMUA1
    // 抛球反馈信号接 L1
    // 断药L2
    if(uavinf.uavtype == JET_UAV_TYPE)
    {
        static bool jet_active = false;
        static uint32_t jet_time = 0;
        static bool jet_ack_flag = false;

        GPIO_PinState Dev_ack = HAL_GPIO_ReadPin( GPIOB, EXIT_3T_Pin );
        L3_status = GPIO_PIN_SET;//L3给默认值
        L4_status = HAL_GPIO_ReadPin( GPIOB, EXIT_4T_Pin );

        //上电持续高电平检测
        if (Dev_ack == 1 && jet_active == false)
        {
            //持续一段时间2s
            if (HAL_GetTick() - jet_time > 2000)
            {
                jet_active = true;
            }
        }
        //检测到低电平 
        else if (Dev_ack == 0 && jet_active == true)
        {
            //抛物机回应已抛出
            if(jet_ack_flag == true)
            {
                pmu_set_ack(JET_UAV_TYPE,1,0,0);

                jet_ack_flag = false;
            }
        }
        else
        {
            jet_time = HAL_GetTick();
            jet_ack_flag = true;
        }
    }
    else if(uavinf.uavtype == VK_ALL_IN_ONE)
    {
        //Tim4_init_pwmout(); //pwm输出初始化

        Dev.Arm_Link.connect_status = COMP_NORMAL;
        Dev.Arm.facid = FAC_VK_ALLINONE;
        Dev.Arm_Link.recv_time = HAL_GetTick();
    }
    else
    {
        L3_status = HAL_GPIO_ReadPin( GPIOB, EXIT_3T_Pin );
        L4_status = HAL_GPIO_ReadPin( GPIOB, EXIT_4T_Pin );
    }
    
}



/**
  * @file    flow_function
  * @brief   流量计相关函数
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void flow_function(void)
{
    if ( Get_Systimer_Us() - flow_dev1.last_time >= 300000 )
    {
        if(Get_Systimer_Us() - flow_dev1.cal_delay_time > 200000)//在增加一个延时，中断里赋值lasttime有问题
        {
            flow_dev1.irq_flag = 1;
            flow_dev1.speed = ( 1000.0f * ( flow_dev1.irq_count - flow_dev1.irq_last_count ) ) / FlOW_KP *
                            ( 1000000.0f / ( Get_Systimer_Us() - flow_dev1.last_time ) ); //ml/min  *60 /60          
            flow_dev1.irq_last_count = flow_dev1.irq_count;
            flow_dev1.last_time = Get_Systimer_Us();
            flow_dev1.irq_flag = 0;
        }
    }
    else
    {
        flow_dev1.cal_delay_time = Get_Systimer_Us();
    }
 

    if ( Get_Systimer_Us() - flow_dev2.last_time >= 300000 )
    {
        if(Get_Systimer_Us() - flow_dev2.cal_delay_time > 200000)
        {
            flow_dev2.irq_flag = 1;
            flow_dev2.speed = ( 1000.0f * ( flow_dev2.irq_count - flow_dev2.irq_last_count ) ) / FlOW_KP *
                        ( 1000000.0f / ( Get_Systimer_Us() - flow_dev2.last_time ) ); //ml/min        
            flow_dev2.irq_last_count = flow_dev2.irq_count;
            flow_dev2.last_time = Get_Systimer_Us();
            flow_dev2.irq_flag = 0;
        } 
    }
    else
    {
        flow_dev2.cal_delay_time = Get_Systimer_Us();
    }

}

mimo_flow flow_mimo1;
mimo_flow flow_mimo2;
/**
  * @file    can_recv_mimor_flow_function
  * @brief   恩曌流量计检测
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void can_recv_mimor_flow_function(uint32_t CanID, uint8_t data[], uint8_t len)
{

    Dev.Flow.facid = FAC_MIMO_SIG;
    Dev.Flow_Link1.connect_status = COMP_NORMAL;
    Dev.Flow_Link1.recv_time = HAL_GetTick();

    if(Dev.Flow_Link2.connect_status == COMP_NORMAL)
    {
        Dev.Flow.facid = FAC_MIMO_DOU;
    }
    if(CanID == CAN_MIMO_FLOW)
    {
        switch (data[0])
        {
        case 0:

            memcpy(&flow_mimo1.ch,&data[0],8);

            if(flow_mimo1.last_total_ml > flow_mimo1.total_ml)
            {
                flow_mimo1.overturn_count++;
            }

            flow_mimo1.last_total_ml = flow_mimo1.total_ml;

            break;
        case 1:
            Dev.Flow_Link2.connect_status = COMP_NORMAL;
            Dev.Flow_Link2.recv_time = HAL_GetTick();

            memcpy(&flow_mimo2.ch,&data[0],8);
            
            if(flow_mimo2.last_total_ml > flow_mimo2.total_ml)
            {
                flow_mimo2.overturn_count++;
            }

            flow_mimo2.last_total_ml = flow_mimo2.total_ml;
            break;
        default:
            break;
        }
    }
    else if(CanID == CAN_MIMO_FLOW_K)
    {
        if(data[1] == 0xFB && data[2] == 0x01)
        switch (data[2])
        {
        case 0:
            if(data[4] == 0XD1)
            {
               flow_mimo1.flow_k = data[5] * 256 + data[6]; 
            }
            break;
        case 1:
            if(data[4] == 0XD1)
            {
                flow_mimo2.flow_k = data[5] * 256 + data[6];
            }
            break;
        default:
            break;
        }
    }
}


vk_flow flow_vkdev1;
vk_flow flow_vkdev2;

