/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-04-23     lijian       the first version
 */
#include <thread.h>

/*　使用静态线程时，线程的栈需要设置字节对齐　*/
rt_align(RT_ALIGN_SIZE)

static rt_uint8_t thread_io_key_stack[THREAD_STACK_SIZE_512];
static rt_uint8_t thread_io_needle_stack[THREAD_STACK_SIZE_512];
static rt_uint8_t thread_io_adc_stack[THREAD_STACK_SIZE_512];
static rt_uint8_t thread_wireless_stack[THREAD_STACK_SIZE_512];
static rt_uint8_t thread_485_stack[THREAD_STACK_SIZE_1024];
static rt_uint8_t thread_lcd_stack[THREAD_STACK_SIZE_1024];
static rt_uint8_t thread_yields_stack[THREAD_STACK_SIZE_256];
static rt_uint8_t thread_check_save_stack[THREAD_STACK_SIZE_256];

/*
 *                                           优先级（从5开始，因4被定时器占据）
 *  电平检测                               5
 *  速度                                      6
 * adc              6
 *  无线与MQTT          7
 * 485              8
 * OLED             7
 *  产量计算                               8
 *  掉电检测与历史数据保存     5
 */

//static rt_uint8_t thread_lcd_stack[THREAD_STACK_SIZE_512];
static struct rt_thread tid_IO_Key;
static struct rt_thread tid_IO_Needle;
static struct rt_thread tid_IO_ADC;
static struct rt_thread tid_WL;
static struct rt_thread tid_485;
static struct rt_thread tid_LCD;
static struct rt_thread tid_Yields;
static struct rt_thread tid_Check_Save;

/**
 * 定时器回调函数，30秒一次的心跳
 * @param parameter
 */
//static void timer_callback(void* parameter)
//{
////    if (get_WIFI_STATUS() && get_MQTT_STATUS())
////    {
////        if (get_MQTT_UPDATE_FLAG())
////        {
////            MQTT_Keep_Alive();
////        }
////    }
//}

/* 线程的入口函数 */
static void Task_IO_Key(void *parameter)
{
    gpio_input_init();
    gpio_output_init();
    while (1)
    {
        updateKeyValue();
        rt_thread_mdelay(10);
    }
}

static void Task_IO_Needle(void *parameter)
{
    set_IO_Needle_io(7);
    while (1)
    {
        updateNeedleCount();
        rt_thread_mdelay(1);
    }
}

static void Task_IO_ADC(void *parameter)
{
    adc_init();
    while (1)
    {
        adc_read();
        rt_thread_mdelay(500);
    }
}

static void Task_Wireless(void *parameter)
{

    AT_device_init();
    LTE_init();
    MQTT_init();

//    rt_uint8_t var, Keep_Alive_OK,
    rt_tick_t tick_start, tick_end;

    tick_start = rt_tick_get();
    while (1)
    {
        tick_end = rt_tick_get();
        //两秒交替一次检测状态
        if ((tick_end - tick_start) > 2000)
        {
            check_LTE_MQTT();
            tick_start = tick_end;
        }

        //send MQTT

        rt_thread_mdelay(3000);

    }

//    rt_timer_t timer = rt_timer_create("timer",          // 定时器名称
//            timer_callback,   // 定时器到期时调用的回调函数
//            RT_NULL,          // 传递给回调函数的参数
//            rt_tick_from_millisecond(30000), // 定时器超时时间，单位为tick
//            RT_TIMER_FLAG_PERIODIC); // 定时器模式，周期性执行
//
//    // 启动定时器
//    if (timer != RT_NULL)
//    {
//        rt_timer_start(timer);
//    }
}

static void Task_485(void *parameter)
{
    while (1)
    {
        //Modbus_init();
        datu_init();
        rt_thread_mdelay(100);
    }
}

static void Task_Lcd(void *parameter)
{
    oled_init();

    while (1)
    {
        oled_menu();

        rt_thread_mdelay(50);

    }
}

static void Task_Yields(void *parameter)
{
    // rt_uint32_t count = 0;

    while (1)
    {

        //rt_kprintf("thread Yields count: %d\n", count++);
        rt_thread_mdelay(500);
    }
}

static void Task_Check_Save(void *parameter)
{
    rt_uint8_t toggle= 0;
   rt_uint8_t  no_led = 0;
    while (1)
    {
        if (get_DATU_toggle_led()) {
            //rt_kprintf("LED: %d", toggle);
            rt_pin_write(OUT0, toggle);
            toggle = ~toggle;
            no_led =1;
            rt_thread_mdelay(1000);
        } else {
            if (no_led) {
                rt_pin_write(OUT0, 1);
                no_led = 0;
            }
        }
        rt_thread_mdelay(100);
    }
}

/* 线程 */
rt_uint8_t thread_IO_Key(void)
{
    /* 静态创建线程 */

    /* 初始化IO线程 ，名称是 Thread_IO，入口是 THREAD_IO*/
    rt_thread_init(&tid_IO_Key,               //指向线程控制块的指针，是用户定义的一个静态或全局变量，线程控制块通常包含了线程的状态信息、栈指针、优先级
            "Thread_IO_Key",         //线程的名称
            Task_IO_Key,           //线程的入口函数，即线程开始执行时调用的函数
            RT_NULL,             //传递给线程入口函数的参数
            thread_io_key_stack,        //线程栈的起始地址，通常是一个指向预分配栈空间的指针，线程在运行中会在这个栈上存储局部变量、函数参数等
            THREAD_STACK_SIZE_512,   //线程栈的大小，单位通常是字节
            THREAD_PRIORITY_5,     //线程的优先级，数值越小表示优先级越高
            THREAD_TIMESLICE_5);   //时间片，用于调度同优先级的线程。当有多个相同优先级的线程时，它们会按照时间片轮转的方式共享CPU时间
    /* 启动线程 */
    rt_thread_startup(&tid_IO_Key);

    return 0;
}

rt_uint8_t thread_IO_Needle(void)
{
    /* 初始化速度线程  */
    rt_thread_init(&tid_IO_Needle, "Thread_IO_Speed", Task_IO_Needle,
    RT_NULL, thread_io_needle_stack,
    THREAD_STACK_SIZE_512,
    THREAD_PRIORITY_6,
    THREAD_TIMESLICE_5);

    /* 启动线程  */
    rt_thread_startup(&tid_IO_Needle);

    return 0;
}

rt_uint8_t thread_IO_ADC(void)
{
    /* 初始化针数线程  */
    rt_thread_init(&tid_IO_ADC, "Thread_IO_ADC", Task_IO_ADC,
    RT_NULL, thread_io_adc_stack,
    THREAD_STACK_SIZE_512,
    THREAD_PRIORITY_6,
    THREAD_TIMESLICE_3);

    /* 启动线程  */
    rt_thread_startup(&tid_IO_ADC);

    return 0;
}

rt_uint8_t thread_Wireless(void)
{
    /* 初始化网络线程  */
    rt_thread_init(&tid_WL, "Thread_wireless", Task_Wireless,
    RT_NULL, thread_wireless_stack,
    THREAD_STACK_SIZE_512,
    THREAD_PRIORITY_7,
    THREAD_TIMESLICE_5);

    /* 启动线程  */
    rt_thread_startup(&tid_WL);

    return 0;
}

rt_uint8_t thread_485(void)
{
    /* 初始化485线程 */
    rt_thread_init(&tid_485, "Thread_485", Task_485,
    RT_NULL, thread_485_stack,
    THREAD_STACK_SIZE_1024,
    THREAD_PRIORITY_8,
    THREAD_TIMESLICE_3);

    /* 启动线程  */
    rt_thread_startup(&tid_485);

    return 0;
}

rt_uint8_t thread_LCD(void)
{
    /* 初始化LCD线程 */
    rt_thread_init(&tid_LCD, "Thread_lcd", Task_Lcd,
    RT_NULL, thread_lcd_stack,
    THREAD_STACK_SIZE_1024,
    THREAD_PRIORITY_7,
    THREAD_TIMESLICE_6);

    /* 启动线程  */
    rt_thread_startup(&tid_LCD);

    return 0;
}

rt_uint8_t thread_Yields(void)
{
    /* 初始化产量线程 */
    rt_thread_init(&tid_Yields, "Thread_yields", Task_Yields,
    RT_NULL, thread_yields_stack,
    THREAD_STACK_SIZE_256,
    THREAD_PRIORITY_8,
    THREAD_TIMESLICE_5);

    /* 启动线程  */
    rt_thread_startup(&tid_Yields);

    return 0;
}

rt_uint8_t thread_Check_Save(void)
{
    /* 初始化掉电保存线程 */
    rt_thread_init(&tid_Check_Save, "Thread_check_save", Task_Check_Save,
    RT_NULL, thread_check_save_stack,
    THREAD_STACK_SIZE_256,
    THREAD_PRIORITY_5,
    THREAD_TIMESLICE_3);

    /* 启动线程  */
    rt_thread_startup(&tid_Check_Save);

    return 0;
}

/* 动态线程示例 */
//int thread(void)
//{
//    /* 动态线程 ，名称是 thread1，入口是 thread1_entry*/
//    tid1 = rt_thread_create("thread1",
//                            thread1_entry, RT_NULL,
//                            THREAD_STACK_SIZE,
//                            THREAD_PRIORITY, THREAD_TIMESLICE);
//    if (tid1 != RT_NULL)
//        rt_thread_startup(tid1);
//
//}
//static void thread1_entry(void *parameter)
//{
//    rt_uint32_t count = 0;
//
//    while (1)
//    {
//        /* 线程 1 采用低优先级运行，一直打印计数值 */
//        rt_kprintf("thread1 count: %d\n", count ++);
//        rt_thread_mdelay(500);
//    }
//}
