
#include "stdio.h"

#include "freerotsTask.h"

#include "uart_dev.h"
#include "FM_GPIO.h"
#include "FM_ADC.h"
#include "queueUart.h"
#include "upUartParse.h"


#define Common_TASK_PRIO     2
#define Common_STK_SIZE      512

#define Transmit_TASK_PRIO     4
#define Transmit_STK_SIZE      256

#define UpReceive_TASK_PRIO     3
#define UpReceive_STK_SIZE      256

#define DownReceive_TASK_PRIO     3
#define DownReceive_STK_SIZE      256

/* 任务句柄 */
static TaskHandle_t CommonTask_Handler;
static TaskHandle_t TransmitTask_Handler;
static TaskHandle_t UpReceive_Handler;
static TaskHandle_t DownReceive_Handler;


/**
  * @brief  喂狗等任务在其中进行
  * @param
  * @retval
  */
static void common_Task(void *pvParameters)
{
	// writePwrCtrlState(Android_PwrCtrl, PwrCtrlOpen);
	// proportionalInt();
    // while(1) {
    //     // printf("task1 entry\r\n");
	// 	printf_adc_data();
    //     USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
	//     uartInterruptSend(g_Upward_uart5_handle, data, 12);
    //     vTaskDelay(1000);
    // }

    while (1) {
        // uint8_t *Buff = (uint8_t *)pvPortMalloc(200);
        // if (Buff == NULL) {
        //     log_error("Memory allocation failed\n");
        //     return;
        // }

        // queueUartSendInfo *sendBuff = (queueUartSendInfo *)Buff;
        // sendBuff->length = sizeof("hello world\n");
        // sendBuff->data = Buff + sizeof(queueUartSendInfo);
        // strlcpy((char *)sendBuff->data, "hello world\n", sizeof("hello world\n"));

        // xQueueSend(upward_uart_Queue, &Buff, 10);

        // log_info("xPortGetFreeHeapSize : %d",xPortGetFreeHeapSize());
        

        
        // USARTx_SendStr_Len(USART3, "hello world\n", sizeof("hello world\n"));


        // uint8_t *Buff = (uint8_t *)pvPortMalloc(200);
        // if (Buff == NULL) {
        //     log_error("Memory allocation failed\n");
        //     return;
        // }
        // queueTimeShareSendInfo *sendBuff = (queueTimeShareSendInfo *)Buff;
        // sendBuff->length = sizeof("hello world\n");
        // sendBuff->data = Buff + sizeof(queueTimeShareSendInfo);
        // sendBuff->connectPort = connectJ0;
        // strlcpy((char *)sendBuff->data, "hello world\n", sizeof("hello world\n"));

        // xQueueSend(J5_0_485_Queue, &Buff, 10);

        vTaskDelay(2000);
    }
}

/**
  * @brief  将数据发送
  * @param
  * @retval
  */
static void transmit_Task(void *pvParameters)
{
    while (1) {
        uartQueueSend();
    }
}

/**
  * @brief  解析su806发送来的数据
  * @param
  * @retval
  */
static void UpReceive_Task(void *pvParameters)
{
    while (1) {
        upwardUartDataAnalysis(g_Upward_uart5_handle);
        vTaskDelay(20);
    }
}

/**
  * @brief  解析传感率发送来的数据
  * @param
  * @retval
  */
static void DownReceive_Task(void *pvParameters)
{
    while (1) {
        vTaskDelay(1000);
    }    
}

/**
  * @brief  启动
  * @param
  * @retval
  */
void startApp(void)
{
	Init_Upward_uart(115200);
    Init_J5_0_485(9600);
	FM_GPIO_Init();
	FM_ADC_Init();

    uartQueueInit();

    /* create task */
    xTaskCreate((TaskFunction_t )common_Task,
                    (const char*    )"commonTask",
                    (uint16_t       )Common_STK_SIZE,
                    (void*          )NULL,
                    (UBaseType_t    )Common_TASK_PRIO,
                    (TaskHandle_t*  )&CommonTask_Handler);

    xTaskCreate((TaskFunction_t )transmit_Task,
                    (const char*    )"transmitTask",
                    (uint16_t       )Transmit_STK_SIZE,
                    (void*          )NULL,
                    (UBaseType_t    )Transmit_TASK_PRIO,
                    (TaskHandle_t*  )&TransmitTask_Handler);

    xTaskCreate((TaskFunction_t )UpReceive_Task,
                    (const char*    )"UpReceiveTask",
                    (uint16_t       )UpReceive_STK_SIZE,
                    (void*          )NULL,
                    (UBaseType_t    )UpReceive_TASK_PRIO,
                    (TaskHandle_t*  )&UpReceive_Handler);

    xTaskCreate((TaskFunction_t )DownReceive_Task,
                    (const char*    )"DownReceiveTask",
                    (uint16_t       )DownReceive_STK_SIZE,
                    (void*          )NULL,
                    (UBaseType_t    )DownReceive_TASK_PRIO,
                    (TaskHandle_t*  )&DownReceive_Handler);

    vTaskStartScheduler();
}




