#include "ad7291.h"
#define AD7291_VTG_DATA 10; 

/**
 * @brief ad7291_init function
 */
e_ad7291_ret_sts ad7291_init(ad7291_dev_t* ad7291_hdl)
{
    e_ad7291_ret_sts sts;
    int16_t temp=1;
    temp=temp;
    sts = null_ptr_check(ad7291_hdl);
    if(sts==AD7291_SCS)
    {
        sts=AD7291_SetRegisterValue(ad7291_hdl, AD7291_REG_COMMAND, AD7291_COMMAND_DELAY |0xFF00);
        if(sts==AD7291_SCS)
        {
            sts=AD7291_SCS;
        }
    }
    else
    {
        sts=AD7291_COM_ERR;
    }
    return (sts);
}

/**
 * @brief ad7291 null poniter check function
 */
int8_t null_ptr_check(ad7291_dev_t* ad7291_hdl)
{
    int8_t sts;
    if ((ad7291_hdl == NULL) || (ad7291_hdl->read == NULL) || (ad7291_hdl->write == NULL))
    {
        sts=AD7291_NULL_PTR;
    }
    else
    {
        sts=AD7291_SCS;
    }
    return sts;
}

/**
 * @brief ad7291 get voltage conversion result function
 */
e_ad7291_ret_sts AD7291_GetVoltageConversionResults(ad7291_dev_t* ad7291_hdl,e_ad7291_chnl chnl,float* data)
{
    chnl = 0x20;
    unsigned short commandValue = 0;
    unsigned char mask = 0x80;
    mask=mask;
    unsigned char channelsNumber = 0;
    channelsNumber = channelsNumber;
    unsigned short voltageData[16] = {0, };
    unsigned char channel = 0;
    channel = channel;
    unsigned char channelAddress = 0;

    AD7291_GetRegisterValue(ad7291_hdl,AD7291_REG_COMMAND,
            (unsigned short*)&commandValue,
            1);

#if 1
    commandValue = (commandValue & 0xFF00) >> 8;
    while(mask)
    {
        if(commandValue & mask)
        {
            channelsNumber++;
        }
        mask = mask >> 1;
    }

    if(channelsNumber)
    {
        AD7291_GetRegisterValue(ad7291_hdl,AD7291_REG_VOLTAGE,
                voltageData,
                channelsNumber);
    }
    for(channel = 0; channel < channelsNumber; channel++)
    {
        channelAddress = (voltageData[channel] & 0xF000) >> 12;
        data[channelAddress] = (float)((float)(voltageData[channel] & 0x0FFF) * 2.5) / 4096 ;

    }
#endif
    //AD7291_GetRegisterValue(ad7291_hdl,AD7291_REG_VOLTAGE,
    //                      voltageData,
    //                    chnl);

    //    channelAddress = (voltageData[chnl] & 0xF000) >> 12;
    //    data[channelAddress] = (float)((float)(voltageData[chnl] & 0x0FFF) * 2.5) / 4096 ;
    //for(channel = 0; channel < channelsNumber; channel++)
    // {
    //   channelAddress = (voltageData[channel] & 0xF000) >> 12;
    // data[channelAddress] = (float)((float)(voltageData[channel] & 0x0FFF) * 2.5) / 4096 ;
    //}


    return AD7291_SCS;
}

/**
 * @brief ad7291 Set Register Value function
 */
e_ad7291_ret_sts AD7291_SetRegisterValue(ad7291_dev_t* ad7291_hdl , unsigned char registerAddress,unsigned short registerValue)
{

    e_ad7291_ret_sts sts;
    unsigned char sendData[2]={0};
    // sendData[0] = registerAddress;
    sendData[0] = ((registerValue & 0xFF00) >> 8);
    sendData[1] = ((registerValue & 0x00FF));
    sts=ad7291_hdl->write(ad7291_hdl->io_intf_hdle,ad7291_hdl->slave_address,registerAddress,sendData,2);
    return(sts);
}

/**
 * @brief ad7291 get Register Value function
 */
void AD7291_GetRegisterValue(ad7291_dev_t* ad7291_hdl,unsigned char registerAddress,
        unsigned short* registerValue,
        unsigned char readsNumber)
{
    unsigned char receiveBuffer[16] = {0};
    unsigned char read = 0;
    uint8_t testBuffer=1;
    e_ad7291_ret_sts sts=AD7291_ERR;
    sts=null_ptr_check(ad7291_hdl);
    if(sts!=AD7291_SCS)
    {
        sts=AD7291_NULL_PTR;
    }
    else
    {
        sts=ad7291_hdl->write(ad7291_hdl->io_intf_hdle,ad7291_hdl->slave_address,registerAddress,&testBuffer,1);
        if(sts == AD7291_SCS)
        {
            sts = AD7291_SCS;
        }else
        {
            sts = AD7291_ERR;
        }
        sts=ad7291_hdl->read(ad7291_hdl->io_intf_hdle,ad7291_hdl->slave_address,registerAddress,receiveBuffer,15);
        if(sts == AD7291_SCS)
        {
            sts = AD7291_SCS;
        }else
        {
            sts = AD7291_ERR;
        }
    }

    for(read = 0; read < readsNumber; read++)
    {
        registerValue[read] = ((unsigned short)receiveBuffer[read * 2] << 8) +
            receiveBuffer[read * 2 + 1];
    }
}

/**
 * @brief ad7291 get voltage convertion function
 */
e_ad7291_ret_sts ad7291_get_voltage_convertion(ad7291_dev_t* ad7291_hdl,e_ad7291_chnl chnl,float *data)
{
    e_ad7291_ret_sts sts;
    sts = AD7291_GetVoltageConversionResults(ad7291_hdl,chnl,data);
    return (sts);
}

/**
 * @brief ad7291 Self test function
 */
e_ad7291_ret_sts ad7291_self_test(ad7291_dev_t *ad7291_hdl)
{
    e_ad7291_ret_sts sts = 0;
    uint8_t write_data = 26;
    uint8_t read_data = 0;
    uint8_t AD7291_REG_ADDR = 0x00;

    sts=ad7291_hdl->write(ad7291_hdl->io_intf_hdle,ad7291_hdl->slave_address,AD7291_REG_ADDR,(uint8_t*)&write_data,1);
    {
        sts=ad7291_hdl->read(ad7291_hdl->io_intf_hdle,ad7291_hdl->slave_address,AD7291_REG_ADDR,(uint8_t *)&read_data,1);
        {
            if(write_data == read_data)
            {
                sts = AD7291_SCS;
            }
            else
            {
                sts = AD7291_ERR;
            }
        }
    }
    return sts;
}
