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

rt_uint8_t key = 0;
rt_uint8_t menu_index = 0;
rt_uint8_t cnt = 0;
void (*menu)();
char buf[20];
rt_base_t key_pins[] = { GET_PIN(C, 14), GET_PIN(C, 13), GET_PIN(B, 9), GET_PIN(B, 8), };

//开机界面
void Menu_index()
{
    //ssd1306_Fill(Black);
    ssd1306_SetCursor(9, 28);
    ssd1306_WriteString("GetonAgain", Font_11x18, White);
    ssd1306_UpdateScreenArea(9, 28, 110, 18);
}

//Mqtt状态
void Menu_MQTT()
{
    ssd1306_FillArea(119, 2, 7, 10, Black);
    ssd1306_SetCursor(119, 2);
    ssd1306_WriteString("Y", Font_7x10, White);
    ssd1306_UpdateScreenArea(119, 2, 7, 10);
}

//Mqtt状态
void Menu_NO_MQTT()
{
    ssd1306_FillArea(119, 2, 7, 10, Black);
    ssd1306_SetCursor(119, 2);
    ssd1306_WriteString("N", Font_7x10, White);
    ssd1306_UpdateScreenArea(119, 2, 7, 10);
}

//LTE状态
void Menu_LTE()
{
    ssd1306_FillArea(93, 2, 7, 10, Black);
    ssd1306_SetCursor(93, 2);
    ssd1306_WriteString("Y", Font_7x10, White);
    ssd1306_UpdateScreenArea(93, 2, 7, 10);
}

//LTE状态
void Menu_NO_LTE()
{
    ssd1306_FillArea(93, 2, 7, 10, Black);
    ssd1306_SetCursor(93, 2);
    ssd1306_WriteString("N", Font_7x10, White);
    ssd1306_UpdateScreenArea(93, 2, 7, 10);
}

//状态栏显示
void Menu_one()
{
//    ssd1306_Fill(Black);
    ssd1306_FillArea(79, 2, 49, 10, Black);
    ssd1306_SetCursor(79, 2);
    ssd1306_WriteString("G:", Font_7x10, White);

    ssd1306_SetCursor(105, 2);
    ssd1306_WriteString("M:", Font_7x10, White);
    ssd1306_UpdateScreenArea(79, 2, 49, 10);

//    Menu_LTE();
//    Menu_NO_MQTT();

    ssd1306_FillArea(0, 18, 128, 46, Black);

    //ssd1306_Line(1, 12, SSD1306_WIDTH - 1, 12, White);
    ssd1306_SetCursor(2, 20);
    ssd1306_WriteString("GOATS", Font_11x18, White);

//    ssd1306_UpdateScreen();
    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}

//二极菜单
void Menu2_IN()
{
    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(48, 25);
    ssd1306_WriteString("IN", Font_16x26, White);
    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//二极菜单
void Menu2_OUT()
{
    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(40, 25);
    ssd1306_WriteString("OUT", Font_16x26, White);
    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//二极菜单
void Menu2_485()
{

    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(40, 25);
    ssd1306_WriteString("485", Font_16x26, White);

    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//二极菜单
void Menu2_232()
{

    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(40, 25);
    ssd1306_WriteString("232", Font_16x26, White);

    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//二极菜单
void Menu2_LTE()
{
    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(40, 25);
    ssd1306_WriteString("LTE", Font_16x26, White);
    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//三极菜单
void Menu3_IN()
{

    rt_tick_t tick_start, tick_end;

//     tick_start = rt_tick_get();

    while (1)
    {
        tick_end = rt_tick_get();
        if ((tick_end - tick_start) > 1000)
        {
            ssd1306_FillArea(0, 18, 128, 46, Black);
            //ssd1306_SetCursor(2, 20);
            //ssd1306_WriteString("000 000 000 000", Font_7x10, White);
            ssd1306_SetCursor(2, 28);
            rt_sprintf(buf, "%4d", get_IO_count(0));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(36, 28);
            rt_sprintf(buf, "%4d", get_IO_count(1));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(70, 28);
            rt_sprintf(buf, "%4d", get_IO_count(2));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(104, 28);
            rt_sprintf(buf, "%4d", get_IO_count(3));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(2, 48);
            rt_sprintf(buf, "%4d", get_IO_count(4));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(36, 48);
            rt_sprintf(buf, "%4d", get_IO_count(5));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(70, 48);
            rt_sprintf(buf, "%4d", get_IO_count(6));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(104, 48);
            rt_sprintf(buf, "%4d", get_IO_count(7));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_UpdateScreenArea(0, 18, 128, 46);
            tick_start = tick_end;
        }

        rt_kprintf("3 in\n");
        if (!rt_pin_read(key_pins[2]))
        {
            break;
        }
    }

}
//三极菜单
void Menu3_OUT()
{
    rt_tick_t tick_start, tick_end;

//     tick_start = rt_tick_get();

    while (1)
    {
        tick_end = rt_tick_get();
        if ((tick_end - tick_start) > 1000)
        {
            ssd1306_FillArea(0, 18, 128, 46, Black);

            ssd1306_SetCursor(2, 28);
            rt_sprintf(buf, "%4d", get_IO_count(0));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(36, 28);
            rt_sprintf(buf, "%4d", get_IO_count(1));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(70, 28);
            rt_sprintf(buf, "%4d", get_IO_count(2));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(104, 28);
            rt_sprintf(buf, "%4d", get_IO_count(3));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(2, 48);
            rt_sprintf(buf, "%4d", get_IO_count(4));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(36, 48);
            rt_sprintf(buf, "%4d", get_IO_count(5));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(70, 48);
            rt_sprintf(buf, "%4d", get_IO_count(6));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_SetCursor(104, 48);
            rt_sprintf(buf, "%4d", get_IO_count(7));
            ssd1306_WriteString(buf, Font_6x8, White);

            ssd1306_UpdateScreenArea(0, 18, 128, 46);
            tick_start = tick_end;
        }

        rt_kprintf("3 in\n");
        if (!rt_pin_read(key_pins[2]))
        {
            break;
        }
    }
}
//三极菜单
void Menu3_485()
{
//    char buf[4];
    ssd1306_FillArea(0, 18, 128, 46, Black);
//    ssd1306_SetCursor(2, 20);
//    ssd1306_WriteString("addr:", Font_7x10, White);
//    ssd1306_SetCursor(55, 20);
//    rt_sprintf(buf, "0x%2d", get_Modbus_Addr());
//    ssd1306_WriteString(buf, Font_6x8, White);
//
//    ssd1306_SetCursor(2, 32);
//    ssd1306_WriteString("reg:", Font_7x10, White);
//    ssd1306_SetCursor(55, 32);
//    rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Addr());
//    ssd1306_WriteString(buf, Font_6x8, White);
//
//    ssd1306_SetCursor(2, 44);
//    ssd1306_WriteString("r_num:", Font_7x10, White);
//    ssd1306_SetCursor(55, 44);
//    rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Num());
//    ssd1306_WriteString(buf, Font_6x8, White);

    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//三极菜单
void Menu3_232()
{

    ssd1306_FillArea(0, 18, 128, 46, Black);
    ssd1306_SetCursor(2, 25);
    ssd1306_WriteString("addr:", Font_7x10, White);

    ssd1306_SetCursor(2, 40);
    ssd1306_WriteString("reg:", Font_7x10, White);

    ssd1306_SetCursor(64, 25);
    ssd1306_WriteString("num:", Font_7x10, White);
    ssd1306_UpdateScreenArea(0, 18, 128, 46);
}
//三极菜单
void Menu3_LTE()
{

    ssd1306_FillArea(0, 18, 128, 46, Black);

    ssd1306_SetCursor(2, 20);
    ssd1306_WriteString("m_ip:", Font_7x10, White);
    ssd1306_SetCursor(40, 20);
    rt_sprintf(buf, "%s", get_MQTT_ADDR());
    ssd1306_WriteString(buf, Font_6x8, White);

    ssd1306_SetCursor(2, 32);
    ssd1306_WriteString("mid:", Font_7x10, White);
    ssd1306_SetCursor(30, 32);
    rt_sprintf(buf, "%s", get_MQTT_CLI_ID());
    ssd1306_WriteString(buf, Font_6x8, White);

    ssd1306_SetCursor(2, 44);
    ssd1306_WriteString("port:", Font_7x10, White);
    ssd1306_SetCursor(30, 44);
    rt_sprintf(buf, "%s", get_MQTT_PORT());
    ssd1306_WriteString(buf, Font_6x8, White);

    ssd1306_UpdateScreenArea(0, 18, 128, 46);

}

//结构体数组 每个元素代表一个菜单项
key_table menu_table[] = {
//菜单界面函数 -- 一级界面
        { 0, 5, 1, 0, 0, Menu_one }, //
        //功能界面函数 -- 二级界面
        { 1, 0, 2, 0, 6, Menu2_IN }, //显示IO
        { 2, 1, 3, 0, 7, Menu2_OUT }, //显示IO
        { 3, 2, 4, 0, 8, Menu2_LTE }, //485
        { 4, 3, 5, 0, 9, Menu2_485 }, //232
        { 5, 4, 0, 0, 10, Menu2_232 }, //LTE
        //功能查看界面函数 -- 三级界面
        { 6, 6, 6, 1, 1, Menu3_IN }, //
        { 7, 7, 7, 2, 2, Menu3_OUT }, //
        { 8, 8, 8, 3, 3, Menu3_LTE }, //
        { 9, 9, 9, 4, 4, Menu3_485 }, //
        { 10, 10, 10, 5, 5, Menu3_232 }, //
        };

//获取按键值
rt_uint8_t key_Value(void)
{

    for (rt_uint8_t var = 0; var < 4; ++var)
    {
        if (rt_pin_read(key_pins[var]) == PIN_LOW)
        {
         //   rt_kprintf("%d\n", var);
            return var + 1;
        }
    }
    return 0;
}

//菜单页面管理
void oled_menu()
{
    if (menu_index == 0)
    {
        if (!cnt)
        {
            menu = menu_table[menu_index].menu;
            (*menu)();
            cnt = 1;
        }
    }

    if (get_LTE_re())
    {
        Menu_NO_LTE();
    }
    else
    {
        Menu_LTE();
    }

    if (get_L_MQTT_re())
    {
        Menu_NO_MQTT();
    }
    else
    {
        Menu_MQTT();
    }

    key = key_Value();

    switch (key)
    {
    case 1:
        menu_index = menu_table[menu_index].up;   // 向上翻
        break;
    case 2:
        menu_index = menu_table[menu_index].down; // 向下翻
        break;
    case 3:
        menu_index = menu_table[menu_index].back; // 返回
        break;
    case 4:
        menu_index = menu_table[menu_index].enter; // 确认
        break;
    default:
        return;
    }

    while (!rt_pin_read(key_pins[key - 1]))
    {
    }

    menu = menu_table[menu_index].menu;
    //rt_kprintf("index %d\n", menu_index);
    (*menu)();
}

//初始化
void oled_init()
{
    for (rt_uint8_t var = 0; var < 4; var++)
    {
        rt_pin_mode(key_pins[var], PIN_MODE_INPUT_PULLUP);
    }

    ssd1306_Init();
    //ssd1306_TestFonts();
    Menu_index();
    rt_thread_mdelay(1000);
}

