
#include "uart_control.h"


static void uart_event_handler(app_uart_evt_t* p_event);

void UARTControl(void)
{
  StateMachine();
}

void UARTConfig(void)
{
    const int  UART_RX_BUF_SIZE = 256;                //Buffers für FIFO
    const int  UART_TX_BUF_SIZE = 256;

      
    app_uart_comm_params_t const comm_params = 
    {
      .rx_pin_no    = RX_PIN_NUMBER,                        /**< RX pin number. */
      .tx_pin_no    = TX_PIN_NUMBER,                        /**< TX pin number. */
      .rts_pin_no   = RTS_PIN_NUMBER,                       /**< RTS pin number, only used if flow control is enabled. */
      .cts_pin_no   = CTS_PIN_NUMBER,                       /**< CTS pin number, only used if flow control is enabled. */
      .flow_control = APP_UART_FLOW_CONTROL_DISABLED,       /**< Flow control setting, if flow control is used, the system will use low power UART mode, based on CTS signal. */
      .use_parity   = false,                                /**< Even parity if TRUE, no parity if FALSE. */
      .baud_rate    = NRF_UART_BAUDRATE_115200              /**< Baud rate configuration. */
    };

    uint32_t err_code = 0;

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handler,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);

    APP_ERROR_CHECK(err_code);
}


void UARTDataWrite(uint8_t DataArray[])
{

  /*
  static bool init_flag = true;

  if(init_flag == true)
    char InitMessage[] = "UART Initialized\r\n";        //(\r carriage return \n new line)
    int size = sizeof(InitMessage)/sizeof(InitMessage[0]);
   
    for(int i = 0; i < sizeof(InitMessage)/sizeof(InitMessage[0]); i++)
    {
      while(app_uart_put(InitMessage[i]);
    }
    init_flag = false;
    */

    printf("UART INIT");
}


/*
  uint32_t errorMessage = 0;
 
   for(int i=0; i < sizeof(DataArray) / sizeof(DataArray[0]); i++)
   {
        APP_UART_TX_EMPTY
        errorMessage = app_uart_put(DataArray[i]);
   }
}
*/

static void uart_event_handler(app_uart_evt_t* p_event)
{

    UART_TX_EMPTY_FLAG = false;
    UART_DATA_READY_FLAG = false;

  switch(p_event->evt_type){
  
    case APP_UART_DATA_READY:

        UART_DATA_READY_FLAG = true;

    break;

    //case APP_UART_FIFO_ERROR:

     //APP_ERROR_HANDLER(p_event->data.error_code);

    break;

    case APP_UART_COMMUNICATION_ERROR:

      APP_ERROR_HANDLER(p_event->data.error_communication);

    break;

    case APP_UART_TX_EMPTY:

        UART_TX_EMPTY_FLAG   = true;

    break;

    default:
       //__LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Undifinded UART Handle state\n");
      break;
  }   
}
  

void StateMachine(void)
{ 
   enum states{IDLE, UART_DATA_READ};
   static enum states currState = IDLE;
  
   switch(currState){

      case IDLE:
      break;

      case UART_DATA_READ:
      {
        static uint8_t data_array[20];
        static uint8_t index = 0;

        bool buffer_flag = true;              //TODO anders Implementieren
        while(buffer_flag == true)
        {
          app_uart_get(&data_array[index]);
          index++;
          if(data_array[index-1] == '\n')
          {
            buffer_flag = false;
            currState = IDLE;
          }
        }

        
        
        break;
      }

      default:
        currState = IDLE;
        break;
   }
    //Events
    if(UART_DATA_READY_FLAG == 1)
    {
      currState = UART_DATA_READ;
    }
}