#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "sdk_common.h"
#include "nrf.h"
#include "nrf_esb.h"
#include "nrf_error.h"
#include "nrf_esb_error_codes.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "nrf_delay.h"
#include "app_util.h"
#include "app_timer.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "GpioProcess.h"
#include "EsbProcess.h"

void PacketSend(uint8_t *data, uint32_t data_length)
{
    int							i;
    uint8_t						Sum = 0;
    uint8_t						Value;

	Value = *(data + 1);
	Value += Value;
	*(data + 1) = Value;
	for ( i = 1 ; i < data_length ; i++ )
		Sum = Sum + *(data + i);
	*data = Sum;
	esb_packet_send(data, data_length);
}

void esb_packet_received(uint8_t *data, uint32_t data_length)
{
	int				i;
	uint8_t			Sum = 0;

	for ( i = 1 ; i < data_length ; i++ )
		Sum = Sum + *(data + i);

	if ( Sum == *data )
	{
		nrf_gpio_pin_toggle(GpioLed0);
		PacketReceiveOKCnt++;
		PacketSend(data, data_length);
	}
	else
	{
		printf(" -NG(%d)\r\n", data_length);
		PacketReceiveNGCnt++;
	}
}

void Action_Key1(void)
{
	//	PacketSend();
}

long	RxLedFlashCnt = 0;

void clocks_start( void )
{
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;

    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
}

// ---- APP_TIMER code added here ----
APP_TIMER_DEF(Timer_1S_timer_id);


extern	uint8_t		KeyDebunseTick;
void Timer_1S(void *p)
{
	static uint32_t		sPacketSendOKCnt, sPacketSentNGCnt, sPacketReceiveOKCnt, sPacketReceiveNGCnt;
	printf(" TX: %d/%d, RX: %d/%d\r\n", (PacketSendOKCnt - sPacketSendOKCnt), (PacketSentNGCnt - sPacketSentNGCnt), (PacketReceiveOKCnt - sPacketReceiveOKCnt), (PacketReceiveNGCnt - sPacketReceiveNGCnt));
	sPacketSendOKCnt = PacketSendOKCnt;
	sPacketSentNGCnt = PacketSentNGCnt;
	sPacketReceiveOKCnt = PacketReceiveOKCnt;
	sPacketReceiveNGCnt = PacketReceiveNGCnt;
}

static void timers_init(void)
{
    ret_code_t err_code;

    // Start the 32.768 kHz low frequency clock. Without this the app_timer module won't work
    NRF_CLOCK->TASKS_LFCLKSTART = 1;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    // Create timers.'
	err_code = app_timer_create(&Timer_1S_timer_id, APP_TIMER_MODE_REPEATED, Timer_1S);
	APP_ERROR_CHECK(err_code);
}

static void timers_start(void)
{
    ret_code_t err_code;
#if 0
    err_code = app_timer_start(Timer_1S_timer_id, APP_TIMER_TICKS(5000/*100*/), (void *)0);
    APP_ERROR_CHECK(err_code);
#endif
}
// -----------------------------------

int main(void)
{
    uint32_t	err_code;
   	int8_t		rssi_reading;

    gpio_init();

    clocks_start();

    UART_Init();
    printf(" ... Start (RX) ...\r\n");
    timers_init();

    err_code = esb_init(Network_ID, Esb_Address_0, Esb_Address_1, esb_packet_received);
    APP_ERROR_CHECK(err_code);

	timers_start();

    while (true)
    {
    	// ----- Start -----. Chianglin Test. 07/30/2019
    	NRF_RADIO->TASKS_RSSISTART = 1;					// Start the RSSI sampling
    	// Wait for the RSSIEND event to occur, either by enabling an interrupt on this event or by looping over the event register:
    	while(NRF_RADIO->EVENTS_RSSIEND == 0);			// END event occured, proceed after this
    	rssi_reading = NRF_RADIO->RSSISAMPLE;
    	printf(" >> RSSI: %d\r\n", rssi_reading);
    	nrf_delay_ms(3000);
    	// -----------------
        // Nothing to do now that the app_timer runs everything, except flush the log and go to sleep
        if(NRF_LOG_PROCESS() == false)
        {
            __WFE();
        }
    }
}
