/*
 * Rs485.c
 *
 *  Created on: 2024年5月18日
 *      Author: 34509
 */
#include <thread.h>
#include "uart_dev.h"
#include "thread_communication.h"
#include <string.h>


/* 定义线程控制块指针 */
/* 接收线程控制块指针 */
static rt_thread_t Recv_thread = RT_NULL;
/* 发送线程控制块指针 */
static rt_thread_t Send_thread = RT_NULL;

/* 函数声明 */
/* 接收线程函数 */
static void Recv_thread_entry(void* parameter);
/* 发送线程函数 */
static void Send_thread_entry(void* parameter);



/**
 * @brief   创建接收线程
 * @param
 * @retval
 */
void Recv_thread_Init(void)
{
    Recv_thread =                   /* 线程控制块指针 */
        rt_thread_create( "Recv",   /* 线程名字 */
                Recv_thread_entry,  /* 线程入口函数 */
                RT_NULL,            /* 线程入口函数参数 */
                512,                /* 线程栈大小 */
                2,                  /* 线程的优先级 */
                20);                /* 线程时间片 */

    /* 启动线程，开启调度 */
    if (Recv_thread != RT_NULL) {
        rt_thread_startup(Recv_thread);
    }
    else
        return;
}

/**
 * @brief   创建发送线程
 * @param
 * @retval
 */
void Send_thread_Init(void)
{
    Send_thread =                   /* 线程控制块指针 */
        rt_thread_create( "send",   /* 线程名字 */
                Send_thread_entry,  /* 线程入口函数 */
                RT_NULL,            /* 线程入口函数参数 */
                512,                /* 线程栈大小 */
                3,                  /* 线程的优先级 */
                20);                /* 线程时间片 */

    /* 启动线程，开启调度 */
    if (Send_thread != RT_NULL) {
        rt_thread_startup(Send_thread);
    }
    else
        return;
}

void Recv_thread_entry(void* parameter)
{
    while (1) {
        read_and_process_sensorUart_data(g_J1RS485_UART6_handle);
        read_and_process_sensorUart_data(g_J2RS485_UART7_handle);
        read_and_process_sensorUart_data(g_J3RS485_USART2_handle);
        read_and_process_sensorUart_data(g_J4RS485_UART8_handle);
        read_and_process_sensorUart_data(g_J50RS485_USART3_handle);
        read_and_process_sensorUart_data(g_LORA_UART4_handle);
        read_and_process_intelligentModuleUart_data(g_Upward_UART5_handle);
        rt_thread_mdelay(5);
    }
}

static uint8_t buff[100];
void Send_thread_entry(void* parameter)
{
    while (1) {
        memset(buff, 0, sizeof(buff));
        rt_mq_recv(mqSend, buff, sizeof(buff), RT_WAITING_FOREVER);
//        rt_kprintf("\n Send buff \n");
        send_uart_data(buff);

//        uart_dev_write(g_Upward_UART5_handle, "hello world\n", sizeof("hello world\n"));
//        rt_thread_mdelay(1000);
    }
}
