//-------------------------------------------------------------------------------------------------------
//本文件是雷管驱动的底层程序，不对外
//-------------------------------------------------------------------------------------------------------

#include "LGDriverAPI.h"
#include "LGDriver.h"
#include "board.h"



#define Long_Time 650 
#define Short_Time 400 


//============================================================================
//接收波形转换
//============================================================================
static void LG_Receive_Switch(void)
{
    LGDRV_PHA_H();
    Lib_delay_us(1000);
    LGDRV_PHA_L();
    Lib_delay_us(1000);
}


static u8 LG_ReceiveBit()     //扫描用接收位
{
    u8 ReturnBit = 0, ValueCnt = 0;
    u16 OffsetTime = 0;

    LGDRV_PHA_H();

    ValueCnt = Lib_CountFeedBackData(&OffsetTime);

    LGDRV_PHA_L();  //这个放到了Lib_CountFeedBackData里面了

    if(ValueCnt > 0)
    {
        ReturnBit = 1;       
        PA11_1(); 
        
//        if(950 > OffsetTime)
//            Lib_delay_us(950 - OffsetTime);
//        else
//            Lib_delay_us(950);
        
        Lib_delay_us(950);
        
        PA11_0();
        
    }
    else
    {
        ReturnBit = 0;

//        if(500 > OffsetTime)
//            Lib_delay_us(500 - OffsetTime);
//        else
//            Lib_delay_us(500);
        Lib_delay_us(500);
        
    }

    return ReturnBit;
}

//============================================================================
//发送一个字节
//输入参数：要发送的数据
//============================================================================
static void LG_SendByte(u8 dat)
{
    u8 i;
    u16 time_h, time_l;

    for(i = 0; i < 8; i++)
    {
        if(dat & 0x80)
        {
            time_h = Short_Time;

            time_l = Long_Time;
        }
        else
        {
            time_h = Long_Time;

            time_l = Short_Time;
        }

        LGDRV_PHA_H();
        Lib_delay_us(time_h);
        LGDRV_PHA_L();
        Lib_delay_us(time_l);

        dat <<= 1;
    }
}

//============================================================================
//发送一个字节 校时专用，仅高低时间和通用发送有差异
//输入参数：要发送的数据
//============================================================================
static void LG_SendByte_CaliTime(u16 dat)
{
    u16 i;
    //u16 time_h = 496 - Close_Pmos_Delay * 2, time_l = 496 - Open_Pmos_Delay * 2; //150ms=150ms 1000=1000.07 12000=12000.70

    for(i = 0; i < dat; i++)
    {
        LGDRV_PHA_H();
        Lib_delay_us(496);
        LGDRV_PHA_L();
        Lib_delay_us(Lib_SetDelayData(Lib_CaliTime));

        Lib_ResetWdg();
    }
}

//============================================================================
//接收一个字节
//返回值：接收到的数据
//============================================================================
static u8 LG_ReceiveByte(void)
{
    u8  BitValue, ReturnData = 0, i;

    for (i = 0; i < 8; i++)
    {
        BitValue = LG_ReceiveBit();
        ReturnData <<= 1;
        ReturnData  += BitValue;
    }
    return ReturnData;
}


//============================================================================
//接收参数
//返回值：接收到的数据
//============================================================================
u8 LG_ReceiveParameter(u8 *Parameter, u8 Length)
{
    u8 i = 0;
    u8 ret = 0;

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(1);
    //接收转换
    LG_Receive_Switch();

    for(i = 0; i < Length; i++)
    {
        Parameter[i] = LG_ReceiveByte();
    }

    ret = LG_ReceiveBit();

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(0);

    LGDRV_PHA_H();

    return ret;
}

//接收单bitAck
u8 LG_ReceiveAck(void)
{
    u8 ret = 0;
    u8 i;

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(1);
    //接收转换
    LG_Receive_Switch();

    for(i = 0; i < 1; i++)
    {
        ret += LG_ReceiveBit();
    }

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(0);

    LGDRV_PHA_H();

    return ret;
}

//接收单bit数据
u16 LG_ReceiveSingleBit(u8 *Parameter, u16 Length, u8 bit)
{
    u16 i;
    u8 j = 0;
    u16 ret = 0;

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(1);
    //接收转换
    LG_Receive_Switch();

    for(i = 0; i < Length; i++)
    {
        j = LG_ReceiveBit();

        if(j > 0)
        {
            Parameter[i] |= bit;
            ret++;
        }
        else
        {
            Parameter[i] &= ~bit;
        }
    }

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(0);

    LGDRV_PHA_H();

    return ret;
}

//延时起爆波形:总计发送660个波形，雷管数量状态位+补发波形 总计660个波形  雷管累计到652个波形会起爆
u16 LG_ReceiveDetBomming(u8 *pStatus, u16 Length, u8 allow)
{
    u16 j = 0;
    u16 i = 0;
    u16 ret = 0, tmp = 0;

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(0);
    //接收转换
    LG_Receive_Switch();

    //按实际雷管数据接收数据位
    for(i = 0; i < (Length + 1); i++)
    {
        j = LG_ReceiveBit();

        if(j > 0)
        {
            tmp++;

            //记录起爆反馈状态
            pStatus[i] |= 0x04; //cBoomStatus
        }
        else
        {
            //记录起爆反馈状态
            pStatus[i] &= ~0x04; //~cBoomStatus
        }
    }

    //存储实际雷管数量
    ret = tmp;

    //OUTA输出高低电压标志 1:输出电压 0:输出高电压
    Lib_SetPowerOutAhSwitch(0);

    //起爆数量不对 且 不允许起爆，JQ版本
#if 0   
    //直接起爆，不判断数量，YM版本
    if((Length > tmp) && ((Length - tmp) > allow))
    {
        //中止起爆，主循环查询更新状态
        LGDRV_PHA_H();
        return ret;
    }
#endif
    
    //预留2 0：正常模式，真实对码，真实起爆；1：真实对码，模拟起爆；2：全0对码，真实起爆；3：全0对码，模拟起爆
    if((1 == ConfigData.Reserved2) || (3 == ConfigData.Reserved2))
    {
        if(0xFF == allow)
        {
            LGDRV_PHA_H();
            return ret;
        }           
    }

    
    for(i = 0; i < (660 - (Length + 1)); i++) //必爆651 发送660
    {
        LGDRV_PHA_H();
        Lib_delay_us(600);
        LGDRV_PHA_L();
        Lib_delay_us(400);

        Lib_ResetWdg();
    }

    LGDRV_PHA_H();

    return ret;
}

//发送延时起爆用来清除波形计数--接收1个bit
void LG_CleanWave_Receive(void)
{
    //接收转换
    LG_Receive_Switch();

    LG_ReceiveBit();
}

//============================================================================
//发送功能指令
//输入参数：要发送的功能码数据
//============================================================================
void LG_SendCommand(u8 Function, u8 *Parameter, u8 Length)
{
    u8 i = 0;

    LGDRV_PHA_H();
    Lib_delay_ms(11);
    
    /*******开始信号********/
    LGDRV_PHA_L();
    Lib_delay_us(1500);
    LGDRV_PHA_H();
    /***********************/
 
    
    LG_SendByte(Function);//发送指令

    for(i = 0; i < Length; i++)
    {
        LG_SendByte(Parameter[i]);//发送参数
    }

    LGDRV_PHA_H();
}



//校时指令
void LG_SendCorrectTimeCommand(u8 Function, u16 time)
{
    LGDRV_PHA_H();
    Lib_delay_ms(11);
    LGDRV_PHA_L();
    Lib_delay_us(1500);
    LGDRV_PHA_H();

    LG_SendByte(Function);

    time += 100;

    LG_SendByte_CaliTime(time);

    LGDRV_PHA_H();
}
