/*
 * logic_public.c
 *
 *  Created on: 2025骞�4鏈�14鏃�
 *      Author: 22332
 */
#include "bsp_register_map.h"
#include "version.h"

#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include "cola_fifo.h"
#include <stdio.h>
#include "../fml/fml_pwm.h"
#include "../fml/fml_gpio.h"
#include "../fml/fml_adc.h"
#include "../fml/fml_ds90ub948.h"
#include "../fml/fml_flash.h"

extern ST_VENDOR_DATA gst_vendor_data;
extern uint8_t	result1,result2;
extern void logWrite(const char *fmt, ...);
extern void fml_ds90ub948_read_commod(uint8_t *pBuf);
extern ST_DIAGNOSE1_DATA Err_status1;

#define MAX_RX_SIZE	100
uint8_t pub_slaveRxBuffer[2];
uint8_t pub_slaveTxBuffer[1];
int Rx_length = sizeof(pub_slaveRxBuffer);
int Tx_length = sizeof(pub_slaveTxBuffer);
void (*p_read_commod)(uint8_t*) = fml_ds90ub948_read_commod;
static task_t s_948_task;
static cola_fifo_t g_rx_fifo;
static uint8_t g_rx_buf[MAX_RX_SIZE];
volatile unsigned int time_tag1 = 0;
volatile unsigned int time_tag2 = 0;
static int receive_iic = 0;
bool bist_mode = false;
void logic_read_948_task(void *arg, uint32_t event)
{
	(void) arg;

	if(jiffies-time_tag1 > 12000)
	{
		bist_mode = true;
	}
	if((jiffies-time_tag2 > 3000) && (receive_iic == 1))
	{
			bist_mode = true;
	}
	if(event == 1)
	{
#if 0
		//Here print cause data wrong display
		for(int i=0; i<sizeof(pub_slaveRxBuffer); i++)
			logWrite("0x%02X ",pub_slaveRxBuffer[i]);
		logWrite("\r\n");
#endif
		fml_ds90ub948_read_commod(pub_slaveRxBuffer);
		receive_iic = 1;
		time_tag1 = jiffies;
		time_tag2 = jiffies;
		bist_mode = false;
	}
#if 0
	else
	{
		//Here print better,but wrong display sometimes
		uint8_t buffer[2];
		if(cola_fifo_read(&g_rx_fifo, (uint8_t *)buffer, sizeof(buffer)))
		{
			logWrite("%02X %02X\r\n",buffer[0],buffer[1]);
		}
	}
#endif
}

void logic_init(void)
{
	cola_fifo_init(&g_rx_fifo, g_rx_buf, MAX_RX_SIZE);
	fml_ds90ub948_init(pub_slaveRxBuffer,Rx_length,pub_slaveTxBuffer,Tx_length,p_read_commod,&s_948_task);
	cola_task_create(&s_948_task,logic_read_948_task,NULL);
}

void fml_ds90ub948_read_commod(uint8_t *pBuf)
{
		cola_fifo_write(&g_rx_fifo, pBuf, 2);
        switch (pBuf[0])
        {
        case LCD_BACKLIGHT:
        	if(Err_status1.LCD_TEMPERATURE_FAIL == 1)
        	{
            	fml_pwm_change_duty(pBuf[1]/2);
        	}
        	else
        	{
        		fml_pwm_change_duty(pBuf[1]);
        	}
            /* code */
            break;
        case LCD_BACKLIGHT_SWITCH:
        	if(pBuf[1] == 1)
        	{
    			fml_gpio_write(OUT_BL_EN,true);
    			 fml_Write_ISET(0x02);
        	}
        	else
        	{
    			fml_gpio_write(OUT_BL_EN,false);
        	}
            /* code */
            break;
        case DAY_NIGHT_MODE:
            /* code */
            break;
        case TOUCH_RESET:
        	fml_gpio_write(OUT_TP_RST,true);
            /* code */
            break;
        case UPDATE_WRITE:
            /* code */
            break;
        case TIME_STAMP:
            /* code */
            break;

        /* code */
        case LCD_BACKLIGHT_FB:
        	pub_slaveTxBuffer[0] =  pwm_pal_1_configs.pwmChannels[0].duty;
            /* code */
            break;
        case DIAGNOSTIC_STATUS1:
        	pub_slaveTxBuffer[0] =  result1;
            /* code */
            break;
        case DIAGNOSTIC_STATUS2:
        	pub_slaveTxBuffer[0] =  result2;
            /* code */
            break;
        case LCD_BACKLIGHT_SWITCH_STATE:
        	//iic閫氫俊寮�鍚紝鑳屽厜浣胯兘涔熶竴瀹氭槸鎵撳紑鐨�
        	pub_slaveTxBuffer[0] = 0x01;
            /* code */
            break;
        case LCD_TEMPURATURE:
        	pub_slaveTxBuffer[0] = fml_adc_read_once_temp();
            /* code */
            break;
        case UPDATE_READ:
            /* code */
            break;
        case LCD_BACKLIGHT_AMBIENT_LIGHT_SENSOR:
        	pub_slaveTxBuffer[0] = 0xFF;
            /* code */
            break;
        case I2C_CLASSIFY_VERSION0_READ:
        	pub_slaveTxBuffer[0] = APP_CAR_MODLE;
            /* code */
            break;
        case I2C_CLASSIFY_VERSION1_READ:
        	pub_slaveTxBuffer[0] = APP_MAJOR_VERSION;
            /* code */
            break;
        case I2C_CLASSIFY_VERSION2_READ:
        	pub_slaveTxBuffer[0] = APP_MINOR_VERSION;
            /* code */
            break;
        case I2C_CLASSIFY_VERSION3_READ:
        	pub_slaveTxBuffer[0] = APP_SUB_VERSION;
            /* code */
            break;
        case I2C_CLASSIFY_FBL_VERSION0_READ:
        	pub_slaveTxBuffer[0] = FBL_MAJOR_VERSION;
            /* code */
            break;
        case I2C_CLASSIFY_FBL_VERSION1_READ:
        	pub_slaveTxBuffer[0] = FBL_MINOR_VERSION;
            /* code */
            break;
        case I2C_CLASSIFY_FBL_VERSION2_READ:
        	pub_slaveTxBuffer[0] = FBL_SUB_VERSION;
            /* code */
            break;
        case LCD_TYPE:
        	pub_slaveTxBuffer[0] = WENZHOUCHANGJIANG_LCD;
            /* code */
            break;
        case TP_type:
        	pub_slaveTxBuffer[0] = WENZHOUCHANGJIANG_TP;
            /* code */
            break;
        case PN_VERSION0:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION0;
            /* code */
            break;
        case PN_VERSION1:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION1;
            /* code */
            break;
        case PN_VERSION2:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION2;
            /* code */
            break;
        case PN_VERSION3:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION3;
            /* code */
            break;
        case PN_VERSION4:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION4;
            /* code */
            break;
        case PN_VERSION5:
        	pub_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION5;
            /* code */
            break;
        case HARDWARE_VERSION0:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION0;
            /* code */
            break;
        case HARDWARE_VERSION1:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION1;
            /* code */
            break;
        case HARDWARE_VERSION2:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION2;
            /* code */
            break;
        case HARDWARE_VERSION3:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION3;
            /* code */
            break;
        case HARDWARE_VERSION4:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION4;
            /* code */
            break;
        case HARDWARE_VERSION5:
        	pub_slaveTxBuffer[0] =  gst_vendor_data.HARDWARE_VERSION5;
            /* code */
            break;
        case SOFTWARE_VERSION0:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION0;
            /* code */
            break;
        case SOFTWARE_VERSION1:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION1;
            /* code */
            break;
        case SOFTWARE_VERSION2:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION2;
            /* code */
            break;
        case SOFTWARE_VERSION3:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION3;
            /* code */
            break;
        case SOFTWARE_VERSION4:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION4;
            /* code */
            break;
        case SOFTWARE_VERSION5:
        	pub_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION5;
            /* code */
            break;
        case BOOTLOADER_VERSION:
        	pub_slaveTxBuffer[0] = BOOTLOADER_VERSION_V;

            /* code */
            break;
        case I2C_CLASSIFY_FLASH_VERSION_READ:
        	pub_slaveTxBuffer[0] = FALSH_VERSION;
            /* code */
            break;
        case DISPLAY_SELF_CHECK_INFO:
            /* code */
            break;
        default:
        	break;
        }
}
