/*
 * MV1_Touch_test.c
 *
 *  Created on: 2023年3月27日
 */

#include "r_device.h"
// #include "Analog_Signals.h"
#include "TFT_LCD.h"
#include "../BSP/BSP.h"
// #include "../RH850_OTA/RH850_OTA.h"
#include "../Display_Info/Display_Info.h"

#include "../TimeStamp/TimeStamp.h"

#define KEY_TIME 100
uint8_t Touch_press_Flag = 0;
uint8_t Touch_release_Flag = 0;
uint8_t tft_switch_switch = 0;
uint8_t display_color_switch = 5;
uint8_t pwm_flag = 0;
uint8_t version_flag = 0;
uint32_t key1_flag = 0;
uint32_t key2_flag = 0;
uint32_t key3_flag = 0;
uint32_t key4_flag = 0;
uint32_t key5_flag = 0;
uint32_t pwm_data;
uint32_t pwm_load_data;
uint8_t lock_flag = 0;

uint32_t size;
uint32_t count;

STimeStampInfo lock_time;          // lock后等待配置时间
STimeStampInfo color_time;         // 自动颜色切换时间
STimeStampInfo warning_light_time; // 自动颜色切换时间
STimeStampInfo detection_time;     // 热插拔检测时间
STimeStampInfo version_time;       // 版本检测时间
uint8_t warning_light_flag = 0;
extern uint8_t color_flag;
extern uint8_t auto_color_flag;
// 接收数据全部为ASCII
void ver_Display(uint8_t u8in1, uint8_t u8in2, uint8_t u8in3, uint8_t u8in4, uint8_t u8in5, uint8_t u8in6)
{
    uint8_t u8x = 80;
    uint32_t u32buff;
    u8in1 = u8in1 - 65;
    TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)gImage_letter[u8in1]); // 十进制十位

    // u32buff = (u8in1 % 100) / 10;
    // TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u32buff]); // 十进制十位

    // u8x += 18;
    // u32buff = u8in1 % 10;

    // TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u32buff]);

    u8x += 18;
    TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)gImage_D_GX9_Y9); // 点

    // u8x += 18;
    // u32buff = (u8in2 % 100) / 10;
    // TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u32buff]); // 十进制十位

    u8x += 18;
    u8in2 = u8in2 - 48;
    TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u8in2]);

    u8x += 18;
    TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)gImage_D_GX9_Y9); // 点

    // u8x += 18;
    // u32buff = (u8in3 % 100) / 10;
    // TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u32buff]); // 十进制十位

    u8x += 18;
    u8in3 = u8in3 - 48;
    TFT_LCD_Draw_Bmp(u8x, 144, (uint8_t *)D[u8in3]);

    u8x = 80;

    // u32buff = (u8in4 % 100) / 10;
    //  TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u32buff]); // 十进制十位

    // u8x += 18;
    // u32buff = u8in4 % 10;
    // TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u32buff]);
    u8in4 = u8in4 - 65;
    TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)gImage_letter[u8in4]); // 十进制十位
    u8x += 18;
    TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)gImage_D_GX9_Y9); // 点

    // u8x += 18;
    // u32buff = (u8in5 % 100) / 10;
    // TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u32buff]); // 十进制十位

    u8x += 18;
    u8in5 = u8in5 - 48;
    TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u8in5]);

    u8x += 18;
    TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)gImage_D_GX9_Y9); // 点

    // u8x += 18;
    // u32buff = (u8in6 % 100) / 10;
    // TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u32buff]); // 十进制十位

    u8x += 18;
    u8in6 = u8in6 - 48;
    TFT_LCD_Draw_Bmp(u8x, 176, (uint8_t *)D[u8in6]);
}

void Display_Init(void)
{
    TFT_LCD_Draw_Bmp(0, 0, (uint8_t *)&gImage_BG_test[0]);
    TFT_LCD_Draw_Bmp(0, 0, (uint8_t *)&gImage_BG_test_A[0]);
    TFT_LCD_Draw_Bmp(0, 144, (uint8_t *)&gImage_BG_HW_SW[0]);

    // TFT_LCD_Draw_Bmp( 1, 32, ( uint8_t * )gImage_point_right[tft_switch_switch]);//仪表选择显示
    // TFT_LCD_Draw_Bmp( 0, 128, ( uint8_t * )gImage_point_up[display_color_switch]);//纯色界面选择
    ver_Display(0x00, 0x00, 0x00, 0x00, 0x00, 0x00); // 版本号显示，前三个为

    TFT_LCD_Draw_Bmp(0, 240, (uint8_t *)gImage_lcd_error[0]);
    TFT_LCD_Draw_Bmp(118, 240, (uint8_t *)gImage_touch_error[0]);
    TFT_LCD_Draw_Bmp(0, 272, (uint8_t *)gImage_bl_error[0]);
    TFT_LCD_Draw_Bmp(118, 272, (uint8_t *)gImage_power_error[0]);
    // TFT_LCD_Draw_Bmp(90, 80, (uint8_t *)gImage_letter[0]);
    // TFT_LCD_Draw_Bmp(90, 120, (uint8_t *)gImage_letter[1]);
    // TFT_LCD_Draw_Bmp(90, 160, (uint8_t *)gImage_letter[3]);
    TimeStamp_Reset(&color_time);
}

uint8_t CMD_80[] = {0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82};
uint8_t CMD_81[] = {0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82};
// 亮度等级
uint8_t CMD_83[10][7] =
    {
        {0x83, 0x01, 0x19, 0x19, 0x01, 0x00, 0xB8}, {0x83, 0x01, 0x32, 0x32, 0x01, 0x00, 0xEA}, {0x83, 0x01, 0x4B, 0x4B, 0x01, 0x00, 0x1C}, {0x83, 0x01, 0x64, 0x64, 0x01, 0x00, 0x4E}, {0x83, 0x01, 0x7D, 0x7D, 0x01, 0x00, 0x80}, {0x83, 0x01, 0x96, 0x96, 0x01, 0x00, 0xB2}, {0x83, 0x01, 0xAF, 0xAF, 0x01, 0x00, 0xE4}, {0x83, 0x01, 0xC8, 0xC8, 0x01, 0x00, 0x16}, {0x83, 0x01, 0xE1, 0xE1, 0x01, 0x00, 0x48}, {0x83, 0x01, 0xFF, 0xFF, 0x01, 0x00, 0x84}};
// 七寸报警灯
uint8_t CMD_84[] = {0x84, 0x55, 0x55, 0x55, 0x55, 0x55, 0x15, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x54};
void fx12_test_server(void)
{
    uint8_t touch_buff[32];
    uint8_t send_buff[7] = {0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x82};
    uint8_t touchEx_buff[32];
    if (gpio_get_key1())
    {
        if (key1_flag < KEY_TIME)
        {
            key1_flag++;
        }
        else if (key1_flag == KEY_TIME)
        {
            key1_flag++;
            if (tft_switch_switch)
            {
                tft_switch_switch = 0;
            }
            else
            {
                tft_switch_switch = 1;
            }
            TFT_LCD_Draw_Bmp(1, 32, (uint8_t *)gImage_point_right[1]); // 仪表选择显示
            switch_951_init(1);
        }
    }
    else
    {
        key1_flag = 0;
    }

    if (gpio_get_key5())
    {
        if (key4_flag < KEY_TIME)
        {
            key4_flag++;
        }
        else if (key4_flag == KEY_TIME)
        {
            key4_flag++;
            pwm_data = pwm_data + 100;
            if (pwm_data > 1000)
            {
                pwm_data = 1000;
            }
        }
    }
    else
    {
        key4_flag = 0;
    }

    if (gpio_get_key6())
    {
        if (key5_flag < KEY_TIME)
        {
            key5_flag++;
        }
        else if (key5_flag == KEY_TIME)
        {
            key5_flag++;
            pwm_data = pwm_data - 100;

            if (pwm_data < 100)
            {
                pwm_data = 100;
            }
        }
    }
    else
    {
        key5_flag = 0;
    }

    if (pwm_load_data != pwm_data)
    {
        pwm_flag = 0;
        pwm_load_data = pwm_data;
        BSP_SetBL_Brightness_PWM(pwm_load_data);
    }

    if (gpio_get_key2())
    {
        if (key2_flag < KEY_TIME)
        {
            key2_flag++;
        }
        else if (key2_flag == KEY_TIME)
        {
            key2_flag++;
            if (display_color_switch < 5)
            {
                display_color_switch++;
            }
            else
            {
                display_color_switch = 0;
            }

            if (display_color_switch == 0)
            {
                auto_color_flag = 1;
            }
            else
            {
                auto_color_flag = 0;
                color_flag = (display_color_switch - 1);
                switch_951_Color(color_flag);
            }
            TFT_LCD_Draw_Bmp(0, 128, (uint8_t *)gImage_point_up[display_color_switch]); // 纯色界面选择
        }
    }
    else
    {
        key2_flag = 0;
    }

    if (gpio_get_key3())
    {
        if (key3_flag < KEY_TIME)
        {
            key3_flag++;
        }
        else if (key3_flag == KEY_TIME)
        {
            key3_flag++;
        }
    }
    else
    {
        key3_flag = 0;
    }

    // lock功能检测
    if (gpio_get_96751_lock() == 0)
    {
        // Clear_TOUCH_IRQHandler_flag();
        TimeStamp_Reset(&lock_time);
        if (lock_flag == 1)
        {
            ver_Display(0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
            TFT_LCD_Draw_Bmp(0, 240, (uint8_t *)gImage_lcd_error[0]);
            TFT_LCD_Draw_Bmp(118, 240, (uint8_t *)gImage_touch_error[0]);
            TFT_LCD_Draw_Bmp(0, 272, (uint8_t *)gImage_bl_error[0]);
            TFT_LCD_Draw_Bmp(118, 272, (uint8_t *)gImage_power_error[0]);
        }
        lock_flag = 0;
    }
    // else
    {
        // if (lock_flag == 0)
        // {
        //     Clear_TOUCH_IRQHandler_flag();
        //     TimeStamp_Update(&lock_time);
        //     if (TimeStamp_GetCntTime(&lock_time) >= 100)
        //     {
        //         lock_flag = 1;
        //     }
        // }
    }

    // 自动切换颜色
    TimeStamp_Update(&color_time);
    if (TimeStamp_GetCntTime(&color_time) >= 2000)
    {
        TimeStamp_Reset(&color_time);
        if (auto_color_flag == 1)
        {
            if (color_flag < 4)
            {
                color_flag++;
            }
            else
            {
                color_flag = 0;
            }
            switch_951_Color(color_flag);
        }
    }

    // 判断921和956是否断开
    TimeStamp_Update(&detection_time);
    if (TimeStamp_GetCntTime(&detection_time) >= 3000)
    {
        unsigned char recive_buf[1] = {1};
        TimeStamp_Reset(&detection_time);
        AIM951_i2c_sendreceive(0x07, recive_buf);
        if (recive_buf[0] != 0x19)
        {
            version_flag = 0;
            switch_951_init(1);
            TFT_LCD_Draw_Bmp(80, 144, (uint8_t *)D[0]);
            TFT_LCD_Draw_Bmp(116, 144, (uint8_t *)D[0]);
            TFT_LCD_Draw_Bmp(152, 144, (uint8_t *)D[0]);
            TFT_LCD_Draw_Bmp(80, 176, (uint8_t *)D[0]);
            TFT_LCD_Draw_Bmp(116, 176, (uint8_t *)D[0]);
            TFT_LCD_Draw_Bmp(152, 176, (uint8_t *)D[0]);
        }
        else
        {
            recive_buf[0] = 0;
        }
    }
    // 版本检测
    TimeStamp_Update(&version_time);
    if (TimeStamp_GetCntTime(&version_time) >= 1000)
    {
        TimeStamp_Reset(&version_time);
        if (version_flag == 0)
        {
            BSP_Delay_ms(1);
            RIIC0_Master_SendMultiByte_Interrupt(0x34U, send_buff, 7);
        }
    }

    // 通讯处理
    if (GET_TOUCH_IRQHandler_flag() == 0)
    {

        RIIC0_WaitForBusy_Block();
        RIIC0_Master_RecvMultiByte_Interrupt(0x34U, touch_buff, 23);
        RIIC0_WaitForBusy_Block();

        if (touch_buff[0] == 0x4BU)
        {
            version_flag = 1;
            ver_Display(touch_buff[4],
                        touch_buff[6],
                        touch_buff[8],
                        touch_buff[12],
                        touch_buff[14],
                        touch_buff[16]);
        }
        Clear_TOUCH_IRQHandler_flag();
    }
}

void BSP_SetBL_Brightness_PWM(uint32_t in)
{
    PWM_Channel_Set_Duty(7, in); // 设置屏幕背光占空比
}