#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "./SYSTEM/sys/sys.h"

#include "./hmi/hmi_cmd.h"
#include "./hmi/hmi_rx_parse.h"
#include "./hmi/hmi_hex_parse.h"

#include "./hmi_btn_example.h"

#include "./BSP/LED0_PWM/led0_pwm.h"
#include "./BSP/USART2/usart2.h"

static char tx_buf[128];

static const char *prints_cmd_tab[] =
{
    "page1 led0 on",
    "page1 led0 off",
};


void hmi_btn_example_str_cmd(char *str_cmd)
{
    uint16_t tab_cnt = sizeof(prints_cmd_tab) / sizeof(prints_cmd_tab[0]);
    uint16_t i;

    for (i = 0; i < tab_cnt; i++)
    {
        if (strstr(str_cmd,prints_cmd_tab[i]))
        {
            break;
        }
    }

    switch (i)
    {
    case 0:
        /*  led on  */
        led0_set_duty(100);
        break;

    case 1:
        /*  led off  */
        led0_set_duty(0);
        break;
    }
}

void hmi_btn_example_hex_cmd(struct hex_cmd_fmt *fram)
{
    uint8_t wid_id = fram->func;
    cmd_res_t *back;
    bool res = false; 

    if (wid_id == 0)
    {
        res = API_GET(tn2.val);
        if (!res)
        {
            printf("get err!");
        }
        back = hmi_cmd_back_res();
        int val = back->val.value;
        if (val >= 3660)
        {
            val = 3660;
            sprintf(tx_buf, "tn3.val = %d;", val);
            hmi_cmd_send(tx_buf);
        }
        sprintf(tx_buf, "tn3.val = %d;", val); //发送整数1024  对应硬件屏显示10.24
        hmi_cmd_send(tx_buf);
    }
}


void hmi_btn_example_timer_update_50ms(uint16_t page_id)
{
    if (page_id != 1) 
        return;
    
    static int32_t test_value;                         /*  文本测试值     */
    
    API_CMD_SEND(t0.txt.str = "机房10";);
    sprintf(tx_buf,"tn0.val =%d;",test_value%1000);
    hmi_cmd_send(tx_buf);
    sprintf(tx_buf,"tn1.val =%d;",test_value%1000);
    hmi_cmd_send(tx_buf);
    test_value++;
    if(test_value >= 1000)                                                       
    {                                                                            
      test_value = 0;                                                            
    }  
    if(test_value>0&&test_value<500)                                             //大于0小于500文本显示红色
    { 
      API_CMD_SEND(tn0.bg.color =0xF800;);                                       //设置文本背景色
    }                                                                            
    else if(test_value>=500)                                                     //大于500文本显蓝色
    {    
      API_CMD_SEND(tn0.bg.color =0x001F;);           
    } 
}


