/*
 * logic_test_mode.c
 *
 *  Created on: 2025骞�4鏈�23鏃�
 *      Author: 22332
 */
#include <stdio.h>
#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include "sdk_project_config.h"
#include "../fml/fml_ds90ub948.h"
#include "../fml/fml_gpio.h"
#include "../fml/fml_pwm.h"
#include "../fml/fml_adc.h"
#include "../fml/fml_TFT.h"
#include "../fml/fml_flash.h"
#include "bsp_register_map.h"
#include "version.h"

#define READ_COMD	0x22
#define WRITE_COMD	0x2E


void fml_test_mode(uint8_t *pBuf);

uint8_t test_slaveRxBuffer[8];
uint8_t test_slaveTxBuffer[6];
bool test_mode_flag = true;

extern ST_VENDOR_DATA gst_vendor_data;

void (*p_test_commod)(uint8_t*) = fml_test_mode;
static task_t s_test_task;
static task_t s_vendor_task;

extern void fml_ds90ub948_read_commod(uint8_t *pBuf);
extern uint8_t pub_slaveRxBuffer[2];
extern uint8_t pub_slaveTxBuffer[1];
extern uint8_t	result1,result2;


void (*p_turn_read)(uint8_t*) = fml_ds90ub948_read_commod;
//extern static task_t s_test_task;

void logic_vendor_task(void *arg, uint32_t event)
{
	(void) arg;

	if(event == 1)
	{
		printf("vendor_data_load");
		vendor_data_load();
	}
	else if(event == 2)
	{
		printf("vendor_data_save");
		vendor_data_save();
	}

}


void check_one_second(void)
{
	if(test_slaveRxBuffer[0] == 0 && jiffies>100)
	{
		test_mode_flag = false;
		int sRx_length = sizeof(test_slaveRxBuffer);
		int sTx_length = sizeof(test_slaveTxBuffer);
		fml_ds90ub948_close();
		i2c_pal_lpi2c0_SlaveConfig0.slaveAddress = 0x6B;
		fml_ds90ub948_open();
		fml_ds90ub948_init(pub_slaveRxBuffer,&sRx_length,pub_slaveTxBuffer,&sTx_length,p_turn_read,NULL);
		cola_timer_stop(&s_test_task);
	}
}
void test_mode_open(void)
{
	int test_Rx_length = sizeof(test_slaveRxBuffer);
	int test_Tx_length = sizeof(test_slaveTxBuffer);
	fml_ds90ub948_init(test_slaveRxBuffer,test_Rx_length,test_slaveTxBuffer,test_Tx_length,p_test_commod,NULL);
	cola_timer_create(&s_test_task, check_one_second, NULL);
	cola_timer_start(&s_test_task, EN_COLA_TIMER_ALWAYS, 10);
	cola_task_create(&s_vendor_task, logic_vendor_task, NULL);
	//vendor_factory_set();

}

void fml_test_mode(uint8_t *pBuf)
{

	switch (pBuf[1])
	        {
	        case APP_VERSION:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		cola_set_event(&s_vendor_task,  1);

	        		test_slaveTxBuffer[0] = gst_vendor_data.SOFTWARE_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_data.SOFTWARE_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_data.SOFTWARE_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_data.SOFTWARE_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_data.SOFTWARE_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_data.SOFTWARE_VERSION5;

	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_data.SOFTWARE_VERSION0 = pBuf[2];
	        		gst_vendor_data.SOFTWARE_VERSION1 = pBuf[3];
	        		gst_vendor_data.SOFTWARE_VERSION2 = pBuf[4];
	        		gst_vendor_data.SOFTWARE_VERSION3 = pBuf[5];
	        		gst_vendor_data.SOFTWARE_VERSION4 = pBuf[6];
	        		gst_vendor_data.SOFTWARE_VERSION5 = pBuf[7];
	        		cola_set_event(&s_vendor_task,  2);


	        	}
	            /* code */
	            break;
	        case PN_VERSION:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		cola_set_event(&s_vendor_task,  1);

	        		test_slaveTxBuffer[0] = gst_vendor_data.PN_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_data.PN_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_data.PN_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_data.PN_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_data.PN_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_data.PN_VERSION5;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_data.PN_VERSION0 = pBuf[2];
	        		gst_vendor_data.PN_VERSION1 = pBuf[3];
	        		gst_vendor_data.PN_VERSION2 = pBuf[4];
	        		gst_vendor_data.PN_VERSION3 = pBuf[5];
	        		gst_vendor_data.PN_VERSION4 = pBuf[6];
	        		gst_vendor_data.PN_VERSION5 = pBuf[7];
	        		cola_set_event(&s_vendor_task,  2);

	        	}
	       	    /* code */
	       	    break;
	        case HARDWARE_VERSION:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		cola_set_event(&s_vendor_task,  1);

	        		test_slaveTxBuffer[0] = gst_vendor_data.HARDWARE_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_data.HARDWARE_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_data.HARDWARE_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_data.HARDWARE_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_data.HARDWARE_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_data.HARDWARE_VERSION5;

	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_data.HARDWARE_VERSION0 = pBuf[2];
	        		gst_vendor_data.HARDWARE_VERSION1 = pBuf[3];
	        		gst_vendor_data.HARDWARE_VERSION2 = pBuf[4];
	        		gst_vendor_data.HARDWARE_VERSION3 = pBuf[5];
	        		gst_vendor_data.HARDWARE_VERSION4 = pBuf[6];
	        		gst_vendor_data.HARDWARE_VERSION5 = pBuf[7];
	        		cola_set_event(&s_vendor_task,  2);
	        	}
	       	    /* code */
	       	    break;
	        case PANNL_HARDWARE:

	       	     /* code */
	       	     break;
	        case PANNL_SOFTWAER:
	       	     /* code */
	       	     break;
	        case BLACKLIGHT_EN:
	        	if (pBuf[0] == WRITE_COMD)
	        	{
	    			fml_gpio_write(OUT_BL_EN,pBuf[2]);
	        	}
	       	     /* code */
	       	     break;
	        case BLACKLIGHT_DUTY:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] =  pwm_pal_1_configs.pwmChannels[0].duty;

	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	            	fml_pwm_change_duty(pBuf[2]);
	        	}
	       	     /* code */
	       	     break;
	        case DIAGNOSTIC_1:
	        	test_slaveTxBuffer[0] =  result1;
	       	     /* code */
	       	     break;
	        case DIAGNOSTIC_2:
	        	test_slaveTxBuffer[0] =  result2;
	       	     /* code */
	       	     break;
	        case TEMP_AD_VALUE:
	        	test_slaveTxBuffer[0] = fml_adc_read_once_temp();
	       	     /* code */
	       	     break;
	        case TP_POSITION:
	        	 /* code */
	        	 break;
	        case TP_TYPE:
	        	 /* code */
	        	 break;
	        case DISPLAY_COLAL:
	    		fml_gpio_write(OUT_TFT_BIST,true);
	    		switch(pBuf[2])
	    		{
	    		case 0x01:
	    			fml_tft_write_BIST_mode(PTGN_RED);
	    			break;
	    		case 0x02:
	    			fml_tft_write_BIST_mode(PTGN_GREEN);
	    			break;
	    		case 0x03:
	    			fml_tft_write_BIST_mode(PTGN_BLUE);
	    			break;
	    		case 0x04:
	    			fml_tft_write_BIST_mode(PTGN_WHITE);
	    			break;
	    		case 0x05:
	    			fml_tft_write_BIST_mode(PTGN_BLACK);
	    			break;
	    		case 0x06:
	    			fml_tft_write_BIST_mode(PTGN_CHESS_B);
	    			break;
	    		default:
	    			break;

	    		}
	        	 /* code */
	        	 break;
	        default:
	             break;
	        }
}

