#include <>
/*H********************************************************************
*
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "driver/uart.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "soc/uart_struct.h"

//************************* DEFINES ************************************
#define BAUD      9600
#define BUF_SIZE ( 200 )

//************************* PROTOTYPES ************************************
void RxToTx(  void *pvParameters  );

//************************* VARIABLES ************************************
static const char *TAG = "uart_example";

uint8_t* RXdata[BUF_SIZE];
int len;
int RX = 0;
QueueHandle_t uart0_queue;
QueueHandle_t EventToRXTX;


/*F********************************************************************
*
**********************************************************************/
void 
RxToTx(  void *pvParameters  )
{
    uint8_t* data = ( uint8_t* ) malloc( BUF_SIZE ); // malloc(  ) = memory allocation
    uint32_t NewData;
    while( 1 )
    {
        if( xQueueReceive( EventToRXTX, &NewData, 60000 / portTICK_RATE_MS ) 
            != pdTRUE )
            printf( "\t\tfail to receive queued value" );
        else
        {  // MAX WAIT 60S
            ESP_LOGI( TAG, "About To Read" )
            int len = uart_read_bytes( UART_NUM_0, data, BUF_SIZE
                , 100 / portTICK_RATE_MS );
            ESP_LOGI( TAG, "Read Length 2 = %d", len );
            ESP_LOGI( TAG, "Bytes Read" )
            uart_write_bytes( UART_NUM_1, (const char*)data, len );
            ESP_LOGI( TAG, "Bytes Written" )
            //uart_write_bytes( UART_NUM_1, "Data Received\n", 15 );
        }
    }
    RX = 0;
}
/*F********************************************************************
*
**********************************************************************/
void 
patternDetect( void *pvParameters )
{
    int uart_num = ( int ) pvParameters;
    uint32_t TaskTrigger = 1;
    uart_event_t event;
    size_t buffered_size;

    // uint8_t* data = (uint8_t*)malloc( BUF_SIZE ); 
    for( ;; ) 
    {
        //Waiting for UART event.
        if( xQueueReceive( uart0_queue, ( void *  )&event, ( portTickType )portMAX_DELAY ) ) 
        {
            switch( event.type ) 
            {
                case UART_PATTERN_DET:   //UART_PATTERN_DET
                    ESP_LOGI( TAG, "Pattern Detected\n" );
                    xQueueSendToBack( EventToRXTX, &TaskTrigger
                        ,1000/portTICK_RATE_MS );
                    break;
                default:
                    //ESP_LOGI( TAG, "uart event type: %d\n", event.type );
                    break;
            }
        }
    }
    vTaskDelete( NULL );
}
/*F********************************************************************
*
**********************************************************************/
void 
app_main(  )
{
    int uart_num = UART_NUM_0;
    int uart_num1 = UART_NUM_1;
    uart_config_t uart_config = 
    { .baud_rate = 4800, .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .rx_flow_ctrl_thresh = 122, };
    // UART CONFIGURATION
    uart_param_config( uart_num, &uart_config );
    uart_param_config( uart_num1, &uart_config );
    esp_log_level_set( TAG, ESP_LOG_INFO );
    uart_driver_install( uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 10
        , &uart0_queue, 0 );
    uart_driver_install( uart_num1, BUF_SIZE * 2, BUF_SIZE * 2, 10, NULL, 0 );
    uart_set_pin( uart_num1, 4, 5, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE );
    uart_enable_pattern_det_intr( uart_num, 10, 1, 10000, 10, 10 );
    EventToRXTX = xQueueCreate( 10, sizeof( uint32_t ) );
    // Start Pattern Detect Trigger Task
    xTaskCreate( patternDetect, "patternDetect", 6000, (void*)uart_num
        , 12, NULL );
    // Start Pattern Detect Trigger Task
    xTaskCreate( RxToTx, "RxToTx", 6000, NULL, 13, NULL );
}