/*
 * fml_TFT.c
 *
 *  Created on: 2025骞�4鏈�10鏃�
 *      Author: 22332
 */
#include <stdio.h>
#include "cola_init.h"
#include "cola_device.h"
#include "bsp_register_map.h"
#include "shell.h"

static cola_device_t *g_pDev_tft_iic = NULL;
static uint8_t transmitBuffer[7U][5U] = {{0x50,0x00,0x00,0x60,0x08},
                                         {0x50,0x00,0x21,0x88,0x08},
                                         {0x50,0x00,0x21,0x8C,0x08},
                                         {0x50,0x00,0x22,0x5C,0x08},
                                         {0x50,0x00,0x22,0x68,0x08},
                                         {0x50,0x00,0x22,0x64,0x08},
                                         {0x50,0x00,0x22,0x58,0x08},
                                        };

static uint8_t First_Buffer[6] = {0xC8,0x00,0x58,0xA8,0X18,0X23};
static uint8_t Sec_Buffer[3] = {0x00,0x00,0x07};
static uint8_t Third_Buffer[3] = {0xFD,0x00,0xAA};
static uint8_t Forth_Buffer[3] = {0x49,0x00,0x80};

void fml_tft_init(void)
{
    g_pDev_tft_iic = cola_device_find("TFT_IIC");
}

uint8_t fml_tft_read_page_reg(uint8_t page,uint8_t reg)
{
	uint8_t pStartCmdSet[] = {0xC8,0x00,0x58,0xA8,0x18,0x01};
	uint8_t pPageSel[] = {0x00,0x00,page};
	uint8_t pPassWord[] = {0xFD,0x00,0xAA};
	uint16_t u16RegAddress = (reg<<8);
	uint8_t readValue = 0x00;
	cola_device_write(g_pDev_tft_iic,pStartCmdSet[0],pStartCmdSet+1,sizeof(pStartCmdSet)-1);
	cola_device_write(g_pDev_tft_iic,pPageSel[0],pPageSel+1,sizeof(pPageSel)-1);
	cola_device_write(g_pDev_tft_iic,pPassWord[0],pPassWord+1,sizeof(pPassWord)-1);
	cola_device_read(g_pDev_tft_iic,u16RegAddress,&readValue,1);
	logWrite("readValue=0x%02X\r\n",readValue);
	return readValue;
}

void fml_tft_write_page_reg(uint8_t page,uint8_t reg)
{
}


void fml_tft_BIST_clear(void)
{
	cola_device_write(g_pDev_tft_iic,First_Buffer[0],First_Buffer+1,5);
	cola_device_write(g_pDev_tft_iic,Sec_Buffer[0],Sec_Buffer+1,2);
	cola_device_write(g_pDev_tft_iic,Third_Buffer[0],Third_Buffer+1,2);
	Forth_Buffer[0] = 0x49;
	Forth_Buffer[2] = 0x07;
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);
	Forth_Buffer[0] = 0x4A;
	Forth_Buffer[2] = 0x03;
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);
	Forth_Buffer[0] = 0x4B;
	Forth_Buffer[2] = 0x00;
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);
	Forth_Buffer[0] = 0x4C;
	Forth_Buffer[2] = 0x00;
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);
	Forth_Buffer[0] = 0x60;
	Forth_Buffer[2] = 0x07;
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);

}
void fml_tft_write_BIST_mode(uint8_t mode)
{
	switch(mode)
	{
		case PTGN_RED://red
			Forth_Buffer[0] = 0x49;
			Forth_Buffer[2] = 0x87;
			break;
		case PTGN_WHITE://white
			Forth_Buffer[0] = 0x49;
			Forth_Buffer[2] = 0x47;
			break;
		case PTGN_BLACK://black
			Forth_Buffer[0] = 0x49;
			Forth_Buffer[2] = 0x27;
			break;
		case PTGN_CBAR_A://color bar pattern with 8-level-gray
			Forth_Buffer[0] = 0x4A;
			Forth_Buffer[2] = 0x83;
			break;
		case PTGN_BLUE://blue
			Forth_Buffer[0] = 0x4A;
			Forth_Buffer[2] = 0x43;
			break;
		case PTGN_GREEN://green
			Forth_Buffer[0] = 0x4A;
			Forth_Buffer[2] = 0x23;
			break;
		case PTGN_FLICKER://flicker
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x80;
			break;
		case PTGN_CROSS_B://Enable crosstalk pattern with gray code 0 (inside) and 186 (outside)
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x40;
			break;
		case PTGN_CROSS_A://Enable crosstalk pattern with gray code 255 (inside) and 186 (outside)
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x20;
			break;
		case PTGN_CHESS_B://chess-board pattern with 32x32 pixels
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x10;
			break;
		case PTGN_CHESS_A://64-block chess-board pattern
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x08;
			break;
		case PTGN_H_GRAY_B://256-level horizontal gray pattern
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x04;
			break;
		case PTGN_H_GRAY_A://16-level horizontal gray pattern
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x02;
			break;
		case PTGN_CBAR_B://8-color bar and 8-gray scale pattern
			Forth_Buffer[0] = 0x4B;
			Forth_Buffer[2] = 0x01;
			break;
		case PTGN_SPONOFF://sub-pixel on/off pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x80;
			break;
		case PTGN_PONOFF://pixel on/off pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x40;
			break;
		case PTGN_WFRAME_B://black ground and white frame pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x20;
			break;
		case PTGN_WFRAME_A://black ground and white frame pattern with + symbo
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x10;
			break;
		case PTGN_GLEVEL://gray-level pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x08;
			break;
		case PTGN_RGBW://RGBW pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x04;
			break;
		case PTGN_V_GRAY_B://256-level vertical gray pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x02;
			break;
		case PTGN_V_GRAY_A://16-level vertical gray pattern
			Forth_Buffer[0] = 0x4C;
			Forth_Buffer[2] = 0x01;
			break;
		case PTGN_H_BW_B://16-level vertical gray pattern
		    Forth_Buffer[0] = 0x60;
			Forth_Buffer[2] = 0x87;
			break;
		case PTGN_H_BW_A://16-level vertical gray pattern
			Forth_Buffer[0] = 0x60;
			Forth_Buffer[2] = 0x47;
			break;

		default:
		    break;
	}
	cola_device_write(g_pDev_tft_iic,First_Buffer[0],First_Buffer+1,5);
	cola_device_write(g_pDev_tft_iic,Sec_Buffer[0],Sec_Buffer+1,2);
	cola_device_write(g_pDev_tft_iic,Third_Buffer[0],Third_Buffer+1,2);
	cola_device_write(g_pDev_tft_iic,Forth_Buffer[0],Forth_Buffer+1,2);
}
int*  fml_tft_check_one_round(int event)
{
  //  uint64_t troubleCode = 0;
    static uint8_t status ,err_status= 0;

    int buffer[32];
    int *result = buffer;
//    char *pTroubleCode = (char *)&troubleCode;
    if (g_pDev_tft_iic != NULL) {

            status = cola_device_write(g_pDev_tft_iic,0,transmitBuffer[event],0);
            if (status == 0x00)
            {
                err_status = cola_device_read(g_pDev_tft_iic,0,buffer,sizeof(buffer));
            }
    }
    return result;
}
uint8_t fml_tft_report_err_status(void)
{
    for (int i = 0; i < 7; i++)
    {
        int *result = fml_tft_check_one_round(i);
        switch (i)
        {
        case EVENT_TP:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case TP_WDT_ERROR:
                    /* code */
                    break;
                case TP_Configuration_CRC_Error:
                    /* code */
                    break;
                case TP_Sensor_open_short_test:
                    /* code */
                    break;
                case TP_Flash_load_CRC_Error:
                    /* code */
                    break;
                case TP_Flash_data_load_CRC_Error:
                    /* code */
                    break;
                default:
                    break;
                }
            }
            break;
        case EVENT_DCLK:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case DCLK_Detection_Port1:
                    /* code */
                    break;
                case DCLK_Detection_Port2:
                    /* code */
                    break;
                case DE_Detection:
                    /* code */
                    break;
                case Float_Detection_in_LVDS:

                	return 18;

                    /* code */
                    break;
                case SDNOFB_Feedback_STH_Detection:
                    /* code */
                    break;
                case GDNOFB_Feedback_STV_Detection:
                    /* code */
                    break;
                default:
                    break;
                }
            }
            break;
        case EVENT_HS:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case HS_Detection:
                    /* code */
                    break;
                case VS_Stop_Detection:
                    /* code */
                    break;
                case Data_CRC:
                    /* code */
                    break;
                default:
                    break;
                }
            }
            break;
        case EVENT_LOCK:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case LOCK_Detection_in_LVDS:
                    /* code */
                    break;
                case OEV_Stop_Detection:
                    /* code */
                    break;
                case POL_Stop_Detection:
                    /* code */
                    break;
                case GOA_ASIL_GOA_Detection_Feedback:
                    /* code */
                    break;
                default:
                    break;
                }
            }
            break;
        case EVENT_SD:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case EDT_PA_OVP_FLAG:
                    /* code */
                    break;
                case EDT_PA_UVP_FLAG:
                    /* code */
                    break;
                case EDT_NA_OVP_FLAG:
                    /* code */
                    break;
                case EDT_NA_UVP_FLAG:
                    /* code */
                    break;
                case EDT_VGH_OVP_FLAG:
                    /* code */
                    break;
                case EDT_VGH_UVP_FLAG:
                    /* code */
                    break;
                case EDT_VGL_OVP_FLAG:
                    /* code */
                    break;
                case EDT_VGL_UVP_FLAG:
                    /* code */
                    break;
                case EDT_VCOM_OVP_FLAG:
                    /* code */
                    break;
                case EDT_VCOM_UVP_FLAG:
                    /* code */
                    break;
                default:
                    break;
                }
            }
            break;
        case EVENT_POWER:
            for (int i = 0; i < 32; i++)
            {
                switch (*(result+i))
                {
                case STV_Detection:
                    /* code */
                    break;
                case CKV_Detection:
                    /* code */
                    break;
                default:
                    break;
                }
            }
                break;
        case EVENT_VDPA:
            for (int i = 0; i < 32; i++)
            {
                    switch (*(result+i))
                    {
                    case VDPA_VDNA_Fail_Ext:
                        /* code */
                        break;
                    case VDPA_VDNA_Fail_Int:
                        /* code */
                        break;
                    case EDT_VDNA_OCP_FLAG:
                        /* code */
                        break;
                    case EDT_VDPA_OCP_FLAG:
                        /* code */
                        break;
                    case Flash_Checksum_Detection:
                        /* code */
                        break;
                    case Register_Checksum_Detection:
                        /* code */
                        break;
                    case Thermal_Ultra_High_Detection:
                        /* code */
                        break;
                    case XON_Detection:
                        /* code */
                        break;
                    default:
                        break;
                    }
            }

                break;
            default:
                break;
        }
    }
    return 0;
}


