/*
 * 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;
uint8_t color_black_flag = 0;
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 ver_Display_A(uint8_t u8ina1, uint8_t u8ina2, uint8_t u8ina3, uint8_t u8ina4, uint8_t u8ina5, uint8_t u8ina6,uint8_t u8ina7,uint8_t u8ina8,uint8_t u8ina9,uint8_t u8ina10,uint8_t u8ina11)
{
    uint8_t u8x = 0;
    if(u8ina1==45)
    {
        u8ina1 = 37;
    }
    else if(u8ina1>47&&u8ina1<58)
    {
        u8ina1 = u8ina1 - 48;

    }
    else if(u8ina1>58&&u8ina1<91)
    {
        u8ina1 = u8ina1 - 55;

    }
    else if(u8ina1>96&&u8ina1<123)
    {
        u8ina1 = u8ina1 - 87;

    }         
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina1]); // 十进制十位

    u8x += 18;    
    if(u8ina2==45)
    {
        u8ina2 = 37;
    }
    else if(u8ina2>47&&u8ina2<58)
    {
        u8ina2 = u8ina2 - 48;

    }
    else if(u8ina2>58&&u8ina2<91)
    {
        u8ina2 = u8ina2 - 55;

    }
    else if(u8ina2>96&&u8ina2<123)
    {
        u8ina2 = u8ina2 - 87;

    } 
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina2]);       

    u8x += 18;
    if(u8ina3==45)
    {
        u8ina3 = 37;
    }
    else if(u8ina3>47&&u8ina3<58)
    {
        u8ina3 = u8ina3 - 48;

    }
    else if(u8ina3>58&&u8ina3<91)
    {
        u8ina3 = u8ina3 - 55;

    }
    else if(u8ina1>96&&u8ina1<123)
    {
        u8ina3 = u8ina3 - 87;

    }          
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina3]);

    u8x += 18;
    if(u8ina4==45)
    {
        u8ina4 = 37;
    }
    else if(u8ina4>47&&u8ina4<58)
    {
        u8ina4 = u8ina4 - 48;

    }
    else if(u8ina4>58&&u8ina4<91)
    {
        u8ina4 = u8ina4 - 55;

    }
    else if(u8ina1>96&&u8ina1<123)
    {
        u8ina4 = u8ina4 - 87;
    }         
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina4]); 


    u8x += 18;
    if(u8ina5==45)
    {
        u8ina5 = 37;
    }
    else if(u8ina5>47&&u8ina5<58)
    {
        u8ina5 = u8ina5 - 48;

    }
    else if(u8ina5>58&&u8ina5<91)
    {
        u8ina5 = u8ina5 - 55;

    }
    else if(u8ina5>96&&u8ina5<123)
    {
        u8ina5 = u8ina5 - 87;

    }            
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina5]); 

    u8x += 18;
    if(u8ina6==45)
    {
        u8ina6 = 37;
    }
    else if(u8ina6>47&&u8ina6<58)
    {
        u8ina6 = u8ina6 - 48;

    }
    else if(u8ina6>58&&u8ina6<91)
    {
        u8ina6 = u8ina6 - 55;

    }
    else if(u8ina6>96&&u8ina6<123)
    {
        u8ina6 = u8ina6 - 87;

    }           
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina6]); 

    u8x += 18;
    if(u8ina7==45)
    {
        u8ina7 = 37;
    }
    else if(u8ina7>47&&u8ina7<58)
    {
        u8ina7 = u8ina7 - 48;

    }
    else if(u8ina7>58&&u8ina7<91)
    {
        u8ina7 = u8ina7 - 55;

    }
    else if(u8ina7>96&&u8ina7<123)
    {
        u8ina7 = u8ina7 - 87;

    }            
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina7]); 

    u8x += 18;
    if(u8ina8==45)
    {
        u8ina8 = 37;
    }
    else if(u8ina8>47&&u8ina8<58)
    {
        u8ina8 = u8ina8 - 48;

    }
    else if(u8ina8>58&&u8ina8<91)
    {
        u8ina8 = u8ina8 - 55;

    }
    else if(u8ina8>96&&u8ina8<123)
    {
        u8ina8 = u8ina8 - 87;

    }             
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina8]); 

    u8x += 18;
    if(u8ina9==45)
    {
        u8ina9 = 37;
    }
    else if(u8ina9>47&&u8ina9<58)
    {
        u8ina9 = u8ina9 - 48;

    }
    else if(u8ina9>58&&u8ina9<91)
    {
        u8ina9 = u8ina9 - 55;

    }
    else if(u8ina9>96&&u8ina9<123)
    {
        u8ina9 = u8ina9 - 87;

    }     
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina9]); 

    u8x += 18;
     if(u8ina10==45)
    {
        u8ina10 = 37;
    }
    else if(u8ina10>47&&u8ina10<58)
    {
        u8ina10 = u8ina10 - 48;

    }
    else if(u8ina10>58&&u8ina10<91)
    {
        u8ina10 = u8ina10 - 55;

    }
    else if(u8ina10>96&&u8ina10<123)
    {
        u8ina10 = u8ina10 - 87;

    }        
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina10]); 

    u8x += 18;
     if(u8ina11==45)
    {
        u8ina11 = 37;
    }
    else if(u8ina11>47&&u8ina11<58)
    {
        u8ina11 = u8ina11 - 48;

    }
    else if(u8ina11>58&&u8ina11<91)
    {
        u8ina11 = u8ina11 - 55;

    }
    else if(u8ina11>96&&u8ina11<123)
    {
        u8ina11 = u8ina11 - 87;

    }            
    TFT_LCD_Draw_Bmp(u8x, 208, (uint8_t *)D[u8ina11]); 

}
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); // 版本号显示，前三个为
    ver_Display_A(0x00,0x00,0x00,0x00,0x00,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);
            ver_Display_A(0x00,0x00,0x00,0x00,0x00,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;
    }
    // 自动切换颜色

    TimeStamp_Update(&color_time);
    if (TimeStamp_GetCntTime(&color_time) >= 5000)
    {
        TimeStamp_Reset(&color_time);
        if (auto_color_flag == 1)
        {
            if(color_flag>1)
            {
                 color_black_flag = !color_black_flag;
            }

            if(color_flag == 1)
            {
                color_black_flag = 0;
            }
            
            if(color_black_flag==1)
            {
                switch_951_Color(1);
            }
            else
            {
                if (color_flag < 5)
                {
                    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]);
        }
        else if(touch_buff[0] == 0x4CU)
        {
            ver_Display_A(touch_buff[2],
            touch_buff[3],
            touch_buff[4],
            touch_buff[5],
            touch_buff[6],
            touch_buff[7],
            touch_buff[8],
            touch_buff[9],
            touch_buff[10],
            touch_buff[11],
            touch_buff[12]);
        }
        Clear_TOUCH_IRQHandler_flag();
    }
}

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