//
// Created by ilya on 20.06.24.
//
#include <string.h>
#include "Amplifier_AW87579.h"
#include "SystemDelayInterface.h"

#define IfNotOkRet(FUN) if(FUN == false){return false;}

bool xAmplifierAW87579_SetSistemControl(tI2cIO *i2c, tAW87579_SYSCTR values){
    uint8_t I2C_Out[2] = {0};
    I2C_Out[0] = AW87579_REG_SYSCTRL;
    I2C_Out[1] = 0x00|
            ((values.EN_SW & 0b1) << 6)|
            (0b1<<5)|
            ((values.EN_BOOST & 0b1) << 4)|
            ((values.EN_PA & 0b1) << 3)|
            ((values.RCV_MODE & 0b1) << 2)|
            (values.EN_HVBAT & 0b1) << 0;
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetBatterySafeguard(tI2cIO *i2c, tAW87579_BATSAFE values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_BATSAFE;
    I2C_Out[1] = 0x00|
                 ((values.BAT_SFGD_DEGLITCH & 0b11) << 5)|
                 ((values.BAT_SFGD_VTH & 0b11) << 3)|
                 ((values.EN_BAT_SFGD & 0b1) << 2)|
                 (values.BAT_SFGD_LEVEL & 0b11) << 0;
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetBoostOutputVoltage(tI2cIO *i2c, tAW87579_BSTVOUT values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_BSTOVR;
    I2C_Out[1] = 0x00|
            ((values.BST_VOUT & 0b11111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetBoostControl(tI2cIO *i2c, tAW87579_BSTCPR2 values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_BSTCPR2;
    I2C_Out[1] = 0x00|
            ((values.BST_IPEAK & 0b1111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetGainControl(tI2cIO *i2c, tAW87579_PAGR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGR;
    I2C_Out[1] = 0x00|
            ((values.PA_GAIN & 0b11111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetAGC3OutputPower(tI2cIO *i2c, tAW87579_PAGC3OPR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGC3OPR;
    I2C_Out[1] = 0x00|
            (0b010 << 5)|
            ((values.PD_AGC3 & 0b1) << 4)|
            ((values.AGC3_OUTPUT_POWER & 0b1111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetAGC3Parameter(tI2cIO *i2c, tAW87579_PAGC3PR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGC3PR;
    I2C_Out[1] = 0x00|
            ((values.AGC3_REL_TIME & 0b111) << 5)|
            ((values.AGC3_ATT_TIME & 0b111) << 2)|
            (0b10 << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetAG2OutputPower(tI2cIO *i2c, tAW87579_PAGC2OPR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGC2OPR;
    I2C_Out[1] = 0x00|
            ((values.AGC2_OUTPUT_POWER & 0b1111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetAGC2Parameter(tI2cIO *i2c, tAW87579_PAGC2PR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGC2PR;
    I2C_Out[1] = 0x00|
            ((values.AGC2_ATT_TIME & 0b111) << 2);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetAGC1Parameter(tI2cIO *i2c, tAW87579_PAGC1PR values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_PAGC1PR;
    I2C_Out[1] = 0x00|
            (0b01001 << 3)|
            ((values.AGC1_ATT_TIME & 0b11) << 1)|
            ((values.PD_AGC1 & 0b1) << 0);
    bool res =  I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetADPMode(tI2cIO *i2c, tAW87579_ADP_MODE values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_ADP_MODE;
    I2C_Out[1] = 0x00|
            ((values.AGC1_ATT_TIMEA & 0b1) << 3)|
            ((values.ADPBOOST_MODE & 0b111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    return res;
}

bool xAmplifierAW87579_SetADPBoost(tI2cIO *i2c, tAW87579_ADPBST_TIME1 values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_ADPBST_TIME1;
    I2C_Out[1] = 0x00|
            ((values.ADP_BST_TIME2 & 0b1111) << 4)|
            ((values.ADP_BST_TIME1 & 0b1111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}

bool xAmplifierAW87579_SetADPBoostVTH(tI2cIO *i2c, tAW87579_ADPBST_VTH values){
    uint8_t I2C_Out[3] = {0};
    I2C_Out[0] = AW87579_REG_ADPBST_VTH;
    I2C_Out[1] = 0x00|
            ((values.ADP_LOW_STEP & 0b11) << 6)|
            ((values.SET_BOOST_VTH2 & 0b111) << 3)|
            ((values.SET_BOOST_VTH1 & 0b111) << 0);
    bool res = I2cWrite(i2c,AW87579_Addres_Write,I2C_Out,2,AW87579_IO_TIMEOUT);
    SystemDelayMs(10);
    return res;
}



void xAmplifierAW87579_GetGainControlReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
}

void xAmplifierAW87579_GetBoostOutputVoltageReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg=AW87579_REG_BSTOVR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_BSTOVR;
}

void xAmplifierAW87579_GetBoostControlReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_BSTCPR2;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_BSTCPR2;
}

void xAmplifierAW87579_GetADPBoostVthReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_ADPBST_VTH;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_ADPBST_VTH;
}

void xAmplifierAW87579_GetADPBoostModeReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_ADP_MODE;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_ADP_MODE;
}

void xAmplifierAW87579_GetADG1ModeReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGC1PR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_PAGC1PR;
}

void xAmplifierAW87579_GetADG3ModeOpReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGC3OPR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_PAGC3OPR;
}

void xAmplifierAW87579_GetADG3ModeReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGC3PR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_PAGC3PR;
}

void XfcProtMethod_GetAGC2OprParamModeReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGC2OPR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_PAGC2OPR;
}

void XfcProtMethod_GetEnBoostReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_SYSCTRL;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_SYSCTRL;
}

void XfcProtMethod_GetAGC2ParamModeReg(tI2cIO *i2c, uint8_t *res){
    uint8_t reg = AW87579_REG_PAGC2PR;
    I2cWrite( i2c, AW87579_Addres_Write | 0, &reg, 1, AW87579_IO_TIMEOUT);
    I2cRead(i2c, AW87579_Addres_Read | 1, res, 1, AW87579_IO_TIMEOUT);
    reg=AW87579_REG_PAGC2PR;
}


bool xAmplifierAW87579_SetAll(tI2cIO *i2c, tAW87579_RegsList list){
    IfNotOkRet(xAmplifierAW87579_SetSistemControl(i2c,list.sysctr))
    IfNotOkRet(xAmplifierAW87579_SetBatterySafeguard(i2c, list.batsafe))
    IfNotOkRet(xAmplifierAW87579_SetBoostOutputVoltage(i2c, list.bstvout))
    IfNotOkRet(xAmplifierAW87579_SetBoostControl(i2c, list.bstcpr2))
    IfNotOkRet(xAmplifierAW87579_SetGainControl(i2c, list.pagr))
    IfNotOkRet(xAmplifierAW87579_SetAGC3OutputPower(i2c,list.pagc3Opr))
    IfNotOkRet(xAmplifierAW87579_SetAGC3Parameter(i2c, list.pagc3Pr))
    IfNotOkRet(xAmplifierAW87579_SetAG2OutputPower(i2c, list.pagc2Opr))
    IfNotOkRet(xAmplifierAW87579_SetAGC2Parameter(i2c, list.pagc2Pr))
    IfNotOkRet(xAmplifierAW87579_SetAGC1Parameter(i2c, list.pagc1Pr))
    IfNotOkRet(xAmplifierAW87579_SetADPMode(i2c,list.adpMode))
    IfNotOkRet(xAmplifierAW87579_SetADPBoost(i2c, list.adpbstTime1))
    IfNotOkRet(xAmplifierAW87579_SetADPBoostVTH(i2c, list.adpbstVth))
    return true;
}
