#include "board.h"
#include "myRadio.h"
#include "myRadio_gpio.h"

/**-------------------------radio include----------------------------------**/
#include "24l01.h"
/**-------------------------radio include end----------------------------------**/

static int8_t rfTxPower;
static uint32_t rfFrequence;
static uint32_t rfBaudrate;
static rfRxCallBack rxCb;
static uint8_t rfRxBuffer[255];
static uint32_t rf_handle;
static uint8_t rf_workProcess;
static uint8_t chipType;
/**-------------------------radio params----------------------------------**/
static bool rf_irq;
/**-------------------------radio params end----------------------------------**/
void myRadio_delay(uint32_t time_ms)
{
    uint32_t i, j;
    i = time_ms;
    while (i --)
    {
        for ( j = 0; j < 1000; j++)
        {
            ;
        }
    }
}
/**
 * @brief IO口中断回调
 *      IO口产生中断后会执行该函数
 *      用于接收射频工作的中断响应
 * 
 * @param index 
 */
void myRadio_gpioCallback(uint8_t index)
{
    rf_irq = true;
}
/**
 * @brief 射频初始化
 * 
 * @param agr0 
 * @param agr1_ptr 无线工作状态响应回调
 *          产生回调给外部使用，@rfRxCallBack
 */
void myRadio_init(int agr0, void *agr1_ptr)
{
    myRadio_gpio_init(myRadio_gpioCallback);
    
/**-------------------------radio init----------------------------------**/
    NRF24L01_Init();
/**-------------------------radio init end----------------------------------**/
    RF_EXT_PA_TO_IDLE();
    if ((rfRxCallBack )agr1_ptr)
    {
        rxCb = (rfRxCallBack )agr1_ptr;
    }
    rf_handle = 0xe5;
}
/**
 * @brief 射频底层执行程序
 *      要放在主循环中执行
 * 
 */
void myRadio_process(void)
{
    if (rf_handle == 0)
    {
        return;
    }
    status_tu status;
    rfRxPacket_ts rfRxPacket;
    uint8_t ucPayload[50];
    if (rf_irq == false)
    {
        return;
    }
    rf_irq = false;
    status = NRF24L01_readStatus();
    if (status.statusUint.rx_dr)
    {
        rfRxPacket.rssi = NRF24L01_getRssi();
        NRF24L01_getRxPacket(ucPayload, 12);
        memset(rfRxPacket.payload, 0, sizeof(rfRxPacket.payload));
        memcpy(rfRxPacket.payload, ucPayload, 12);
        rfRxPacket.len = 12;
        if (rxCb)
        {
            rxCb(RX_STA_SECCESS, rfRxPacket);
        }
    }
    if (status.statusUint.tx_ds)
    {
        if (rxCb)
        {
            NRF24L01_WriteStrobe(FLUSH_TX);//清除TX FIFO寄存器 
            rxCb(TX_STA_SECCESS, rfRxPacket);
        }
    }
}
/**
 * @brief 退出射频进入休眠
 * 
 */
void myRadio_abort(void)
{
    if (rf_handle == 0)
    {
        return;
    }
    RF_EXT_PA_TO_IDLE();
    rf_workProcess = 0;
    NRF24L01_powerDown();
}
/**
 * @brief 获取射频工作中心频率
 * 
 * @return uint32_t 
 */
uint32_t myRadio_getFrequency(void)
{
    if (rf_handle == 0)
    {
        return 0;
    }
    return rfFrequence;
}
/**
 * @brief 设置射频工作中心频率
 * 
 * @param freq 
 *      具体频点，单位：Hz
 */
void myRadio_setFrequency(uint32_t freq)
{
    if (rf_handle == 0)
    {
        return;
    }
    rfFrequence = freq;
    NRF24L01_setChannl(rfFrequence);
}
/**
 * @brief 获取发射功率
 * 
 * @return int8_t 
 */
int8_t myRadio_getTxPower(void)
{
    if (rf_handle == 0)
    {
        return 0;
    }
    return rfTxPower;
}
/**
 * @brief 设置发射功率
 * 
 * @param power 
 *          单位：dbm
 */
void myRadio_setTxPower(int8_t power)
{
    if (rf_handle == 0)
    {
        return;
    }
    rfTxPower = power;
    NRF24L01_setRfPower(rfTxPower);
}
/**
 * 获取射频波特率
 * @param : br->
*/
uint32_t myRadio_getBaudrate(void)
{
    if (rf_handle == 0)
    {
        return 0;
    }
    return rfBaudrate;
}
/**
 * 设置射频波特率
 * @param : br->
*/
void myRadio_setBaudrate(uint32_t br)
{
    if (rf_handle == 0)
    {
        return;
    }
    rfBaudrate = br;
    NRF24L01_setRfBaudrate(br);
}
/**
 * @brief 设置模组型号
 * 
 * @param type 
 */
void myRadio_setChipType(uint8_t type)
{
    chipType = type;
}
/**
 * @brief 获取模组型号
 * 
 * @return uint8_t 
 */
uint8_t myRadio_getChipType(void)
{
    return chipType;
}
int16_t myRadio_getRssi(void)
{
    return 0;
}
/**
 * @brief 无线发送数据包
 * 
 * @param packet 
 */
void myRadio_transmit(rfTxPacket_ts *packet)
{
    if (rf_handle == 0)
    {
        return;
    }
    RF_EXT_PA_TO_TX();
    if (rf_workProcess != 1)
    {
        NRF24L01_RfMode(RF_PRIM_TX);
    }
    rf_workProcess = 1;
    
    NRF24L01_TxPacket(packet->payload, packet->len);
}
/**
 * @brief 进入无线接收
 * 
 */
void myRadio_receiver(void)
{ 
    if (rf_handle == 0)
    {
        return;
    }
    RF_EXT_PA_TO_RX();
    rf_workProcess = 0;
    NRF24L01_RfMode(RF_PRIM_RX);
}
void myRadio_setCtrl(controlMode_te mode, uint32_t value)
{
    if (rf_handle == 0)
    {
        return;
    }
    myRadio_init(0, 0);
    switch (mode)
    {
    case RADIO_EXT_CONTROL_TX_UNMODULATED:
    {
        RF_EXT_PA_TO_TX();
        myRadio_setTxPower(rfTxPower);
        myRadio_setFrequency(rfFrequence);
        NRF24L01_carrierWave();
    }
        break;
    case RADIO_EXT_CONTROL_TX_MODULATED:
    {
        RF_EXT_PA_TO_TX();
        RF_EXT_PA_TO_TX();
        myRadio_setTxPower(rfTxPower);
        myRadio_setFrequency(rfFrequence);
        NRF24L01_carrierWave();
    }
        break;
    case RADIO_EXT_CONTROL_RX_SENSITIVITY:
    {
        RF_EXT_PA_TO_RX();
        myRadio_setFrequency(rfFrequence);
        myRadio_receiver();
    }
        break;
    
    default:
        break;
    }
}

/**-------------------------radio funtion----------------------------------**/

/**-------------------------radio funtion end----------------------------------**/
