#include "uarte_link.h"

void uart_ir_handler(app_uart_evt_t *p) {
    static uint8_t data_array[DATA_BUFF_LEN];
    static uint8_t index = 0;

    switch(p->evt_type) {
	case APP_UART_COMMUNICATION_ERROR:
	    APP_ERROR_HANDLER(p->data.error_communication);
	    break;
	case APP_UART_FIFO_ERROR:
	    APP_ERROR_HANDLER(p->data.error_code);
	    break;
	case APP_UART_DATA_READY:
	    app_uart_get(&data_array[index]);
	    index++;
	    if (index >= DATA_BUFF_LEN) {
		//NRF_LOG_INFO("Data from uart sent: %x %x", data_array[0], data_array[1]);
		//process_evt(data_array);
		index = 0;
	    }
	    break;
	case APP_UART_TX_EMPTY:
	    break;
	default:
	    break;
    }
}

void uart_start(void) {
    uint32_t err_code;
    //UART data struct initialization
    app_uart_comm_params_t com_params;
    com_params.baud_rate = NRF_UARTE_BAUDRATE_115200;
    com_params.use_parity = false;
    com_params.flow_control = APP_UART_FLOW_CONTROL_DISABLED;
    com_params.rx_pin_no = RXD;
    com_params.tx_pin_no = TXD;
    com_params.rts_pin_no = RTSD; //only matters if flow control is enabled
    com_params.cts_pin_no = CTSD; //only matters if flow control is enabled

    APP_UART_FIFO_INIT(&com_params, RX_BUFF_SIZE, TX_BUFF_SIZE, uart_ir_handler, APP_IRQ_PRIORITY_LOWEST, err_code);
    APP_ERROR_CHECK(err_code);
    
}

