/********************** 头文件 *************************/
#include "bsp_esb.h"
#include "nrf_esb.h"
#include "nrf_error.h"
#include "nrf_esb_error_codes.h"
#include "hal_imu.h"

/********************** 变量区 *************************/
static nrf_esb_payload_t        tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00);
static nrf_esb_payload_t        rx_payload;

/********************** 环形缓存区 *************************/
static const int  RxLen = 1024;
static volatile unsigned char RxBuf[RxLen];
static volatile unsigned char* RxW=RxBuf;
static volatile unsigned char* RxR=RxBuf;

void ESB_Push(unsigned char* p,int len)
{
	volatile unsigned char *W=RxW; //这里要与上面指针相同
	if(len<=0) return;
	for(int i=0;i<len;i++){
		W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf;        //取下一位置(到顶转到底)
		if(W!=RxR){*RxW=*(p+i); RxW=W;}
		else break;
	}
}

unsigned int ESB_CheckLen(void) //检查RX接收了多少数据
{   
	unsigned int Len; //short 
	volatile unsigned char *W=RxW; volatile unsigned char *R=RxR; 
	if(W>=R)Len=W-R;else Len=(W+RxLen)-R;         //这样正确(中途中断改变也变不了结果)
    return Len; 
}

unsigned char ESB_ReadByte(void)   //读RX中数锯,地指加一,和丢弃
{	
	unsigned char R=*RxR;       //读数
	if(RxR!=RxW){	if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
	return R; 
}

unsigned char ESB_CheckByte(unsigned short n) //看RX中数锯,地指不变, 
{  	
	volatile unsigned char *R=RxR+n; 
	if(R>=(RxBuf+RxLen))R-=RxLen; 
	return *R; 
}	

void ESB_Discard(unsigned short n) //丢弃RX数据几位  
{	
	while(n){ n--;  
		if(RxR==RxW) return; 
		if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标 
	}	
}
//static void Radio_State(void)
//{
//        switch(NRF_RADIO->STATE)
//        {
//                case RADIO_STATE_STATE_Disabled:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Disabled\n");
//                        break;
//                case RADIO_STATE_STATE_RxRu:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxRu\n");
//                        break;
//                case RADIO_STATE_STATE_RxIdle:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxIdle\n");
//                        break;
//                case RADIO_STATE_STATE_Rx:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Rx\n");
//                        break;
//                case RADIO_STATE_STATE_RxDisable:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxDisable\n");
//                        break;
//                case RADIO_STATE_STATE_TxRu:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxRu\n");
//                        break;
//                case RADIO_STATE_STATE_TxIdle:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxIdle\n");
//                        break;
//                case RADIO_STATE_STATE_Tx:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Tx\n");
//                        break;
//                case RADIO_STATE_STATE_TxDisable:
//                        SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxDisable\n");
//                        break;
//        }
//}

#define ESB_COMMAND_LENGTH_MAX  200
void ESB_SendBuff(unsigned char *p,int L)//发送缓存
{
	uint8_t i;
	if(L>ESB_COMMAND_LENGTH_MAX) return;
	
	
//	
	(void) nrf_esb_stop_rx();
	
//	Radio_State();
	
	for(i=0;i<L;i++){
		tx_payload.data[i] = p[i];
	}
	tx_payload.noack = false;
	tx_payload.length = L;
	nrf_esb_write_payload(&tx_payload);
}

//*****************************************************************//

/********************** 函数声明区 *************************/
void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
{
    switch (p_event->evt_id)
    {
        case NRF_ESB_EVENT_TX_SUCCESS:
//			SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_SUCCESS\n");
			(void) nrf_esb_flush_tx();
            (void) nrf_esb_start_rx();
            break;
        case NRF_ESB_EVENT_TX_FAILED:
//			SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_FAILED\n");
            (void) nrf_esb_flush_tx();
            (void) nrf_esb_start_rx();
            break;
        case NRF_ESB_EVENT_RX_RECEIVED:
//			SEGGER_RTT_printf(0,"NRF_ESB_EVENT_RX_RECEIVED\n");
            while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS){
                if(rx_payload.length > 0){
					#if DEBUG_ESB_INT
					SEGGER_RTT_printf(0,"INT_ESB:"); for(int i=0;i<rx_payload.length;i++){SEGGER_RTT_printf(0,"%02X ",rx_payload.data[i]);} SEGGER_RTT_printf(0,"\r\n");
					#endif
					
					IMU_SetRssi(rx_payload.rssi);
					if(rx_payload.data[0]==0xAA && rx_payload.data[1]==(uint8_t)(~rx_payload.data[2])){
						if(rx_payload.data[3]==0){ //IMU data		
							IMU_SetSlaveData(rx_payload.data,rx_payload.length);
						}else
						ESB_Push(rx_payload.data,rx_payload.length);
					}
                }
            }
            break;
    }
}

#define  ADDR ESB_ADDR

uint32_t ESB_Init(void)
{
    uint32_t err_code;
    uint8_t base_addr_0[4] = {0x01, 0x01, ADDR, ADDR};
    uint8_t base_addr_1[4] = {0x01, 0x01, ADDR, ADDR};
    uint8_t addr_prefix[8] = {0x01, 0x01, 0x01, 0x01, ADDR, ADDR, ADDR, ADDR };

    nrf_esb_config_t nrf_esb_config         = NRF_ESB_DEFAULT_CONFIG;
//    nrf_esb_config.protocol                 = NRF_ESB_PROTOCOL_ESB_DPL;
	nrf_esb_config.payload_length           = 250;
//    nrf_esb_config.retransmit_delay         = 600;
//	nrf_esb_config.payload_length           = ESB_COMMAND_LENGTH_MAX;
    nrf_esb_config.bitrate                  = NRF_ESB_BITRATE_2MBPS;
    nrf_esb_config.event_handler            = nrf_esb_event_handler;
    nrf_esb_config.mode                     = NRF_ESB_MODE_PTX;
    nrf_esb_config.selective_auto_ack       = false;

    err_code = nrf_esb_init(&nrf_esb_config);

    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_0(base_addr_0);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_1(base_addr_1);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
    VERIFY_SUCCESS(err_code);
	
	(void) nrf_esb_flush_tx();
	(void) nrf_esb_start_rx();
//	uint8_t temp = 0;
//	ESB_SendBuff(&temp,1);
	
    return err_code;
}


