
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_timer.h"
#include "nrf.h"
#include "bsp.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#include "Common.h"

//#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

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

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

unsigned char	ReceivedBuffer[100];
uint16_t		ReceiveIndex = 0;

void uart_event_handle(app_uart_evt_t * p_event)
{

	switch (p_event->evt_type)
	{
		case APP_UART_DATA_READY:
			UNUSED_VARIABLE(app_uart_get(&ReceivedBuffer[ReceiveIndex]));
			if ( (ReceivedBuffer[ReceiveIndex] == 0x0d) || (ReceivedBuffer[ReceiveIndex] == 0x0a) )
			{
				printf("Command: %s\r\n", ReceivedBuffer);
				ReceiveIndex = 0;
			}
			else
				ReceiveIndex++;
			break;

		case APP_UART_COMMUNICATION_ERROR:
			APP_ERROR_HANDLER(p_event->data.error_communication);
			break;

		case APP_UART_FIFO_ERROR:
			APP_ERROR_HANDLER(p_event->data.error_code);
			break;

		default:
		    break;
	}
}

void UART_Init(void)
{
	uint32_t err_code;

	const app_uart_comm_params_t comm_params =
	{
		19,
		18,
		RTS_PIN_NUMBER,
		CTS_PIN_NUMBER,
		APP_UART_FLOW_CONTROL_DISABLED,
		false,
		UART_BAUDRATE_BAUDRATE_Baud115200
	};

	APP_UART_FIFO_INIT(&comm_params,
			UART_RX_BUF_SIZE,
			UART_TX_BUF_SIZE,
			uart_event_handle,
			APP_IRQ_PRIORITY_LOWEST,
			err_code);
	
	APP_ERROR_CHECK(err_code);
};

// ------- About Timer -----
APP_TIMER_DEF(Timer_10Ms_timer_id);
void Timer_10Ms(void *p)
{
}

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); 

	err_code = app_timer_create(&Timer_10Ms_timer_id, APP_TIMER_MODE_REPEATED, Timer_10Ms); 
	APP_ERROR_CHECK(err_code);
}
void timers_start(void)
{
    ret_code_t err_code;

    app_timer_start(Timer_10Ms_timer_id, APP_TIMER_TICKS(1000), (void *)0);
    APP_ERROR_CHECK(err_code);
}
// ---------------------------

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    //	uint32_t err_code;

    clocks_start();

	UART_Init();

    timers_init();

    timers_start();

	nrf_gpio_cfg_output(LED);
	nrf_gpio_cfg_output(MotorControl_A);
	nrf_gpio_cfg_output(MotorControl_B);
	nrf_gpio_cfg_output(MotorControl_C);
	nrf_gpio_cfg_output(MotorControl_D);
	nrf_gpio_pin_set(LED);

	printf(" Fan Start\r\n");
	while(1)
		;
}


/** @} */
