
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "UartProcess.h"

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[16];
    static uint8_t index = 0;
    //	uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;
            if ((data_array[index - 1] == '\n') || (index >= (1)))
            {
            	//	printf("data_array[] = %s\r\n", data_array);
            #if 0
                err_code = ble_nus_string_send(&m_nus, data_array, index);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
            #endif
                index = 0;
            }
            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 =
	{
		UART_RX,
		UART_TX,
		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);
};
