/*
 * logic_diagnose.c
 *
 *  Created on: 2025骞�4鏈�18鏃�
 *      Author: 22332
 */


#include "bsp_register_map.h"
#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include <stdio.h>
#include "../fml/fml_pwm.h"
#include "../fml/fml_gpio.h"
#include "../fml/fml_adc.h"
extern uint16_t V_battery_ad;
extern uint16_t adc_temp;


static task_t loop_diagnose_task;
extern void logWrite(const char *fmt, ...);


ST_DIAGNOSE1_DATA Err_status1 = {};
ST_DIAGNOSE2_DATA Err_status2 = {};
uint8_t	result1,result2;
uint8_t convertBoolStruc1ToUint8(ST_DIAGNOSE1_DATA bs) {
    uint8_t result = 0;

    // 璁剧疆姣忎釜浣�
    if (bs.LOW_VOL_FAIL) result 			|= (1 << 0);
    if (bs.HIGH_VOL_FAIL) result 			|= (1 << 1);
    if (bs.LCD_FAIL) result 				|= (1 << 2);
    if (bs.BACKLIGHT_FAIL) result 			|= (1 << 3);
    if (bs.LCD_TEMPERATURE_FAIL) result 	|= (1 << 4);
    if (bs.LCD_NTC_FAIL) result 			|= (1 << 5);
    if (bs.RESERVER1) result 				|= (1 << 6);
    if (bs.RESERVER2) result 				|= (1 << 7);

    return result;
}
uint8_t convertBoolStruct2ToUint8(ST_DIAGNOSE2_DATA bs) {
    uint8_t result = 0;

    // 璁剧疆姣忎釜浣�
    if (bs.RESERVER0) result 			|= (1 << 0);
    if (bs.RESERVER1) result 			|= (1 << 1);
    if (bs.LOCK_FAIL) result 			|= (1 << 2);
    if (bs.RESERVER3) result 			|= (1 << 3);
    if (bs.RESERVER4) result 			|= (1 << 4);
    if (bs.RESERVER5) result 			|= (1 << 5);
    if (bs.RESERVER6) result 			|= (1 << 6);
    if (bs.RESERVER7) result 			|= (1 << 7);

    return result;
}
void diagnose_polling(void)
{
	static uint8_t low_vol_cnt = 0;
	static uint8_t high_vol_cnt = 0;
	static uint8_t high_temp_cnt = 0;
	// vol_diagnose
	if((Err_status1.LOW_VOL_FAIL||Err_status1.HIGH_VOL_FAIL) == 0)
	{
			if(V_battery_ad<=1172)//灏忎簬8.5V
			{
				low_vol_cnt++;
				if(low_vol_cnt>=10)//妫�娴�100ms
				{
					Err_status1.LOW_VOL_FAIL = 1;
					logWrite("Err_status1.LOW_VOL_FAIL = %d",Err_status1.LOW_VOL_FAIL);

				}
			}
			else if(V_battery_ad>=2273)//澶т簬16.5V
			{
				high_vol_cnt++;
				if(high_vol_cnt>=10)//妫�娴�100ms
				{
					Err_status1.HIGH_VOL_FAIL = 1;
					logWrite("Err_status1.HIGH_VOL_FAIL = %d",Err_status1.HIGH_VOL_FAIL);
				}
			}
			else
			{
				low_vol_cnt = 0;
				high_vol_cnt = 0;
			}
	}
	else
	{
		if((V_battery_ad>=1239)&&(V_battery_ad<=2204))//鐢靛帇鍦�9V~16V涔嬮棿
		{
			Err_status1.LOW_VOL_FAIL = 0;
			Err_status1.HIGH_VOL_FAIL = 0;
		}
	}
	//high_temperature_diagnose
	if(Err_status1.LCD_TEMPERATURE_FAIL == 0)
	{
		if(adc_temp<2164)//947
		{
			high_temp_cnt++;
			if(high_temp_cnt>=100)//妫�娴�1000ms
			{
			Err_status1.LCD_TEMPERATURE_FAIL = 1;
			}
		}
		else
		{
			high_temp_cnt = 0;
		}
	}
	else if(Err_status1.LCD_TEMPERATURE_FAIL == 1)
	{
		if(adc_temp>2303)//1190
		{
			high_temp_cnt++;
			if(high_temp_cnt>=100)//妫�娴�1000ms
			Err_status1.LCD_TEMPERATURE_FAIL = 0;
		}
		else
		{
			high_temp_cnt = 0;
		}
	}
	if(adc_temp == 0 || adc_temp == 4095)
		Err_status1.LCD_NTC_FAIL = 1;

	Err_status1.LCD_FAIL = fml_gpio_read(IN_TFT_ERR);
	Err_status1.BACKLIGHT_FAIL = fml_gpio_read(IN_BL_ERR);
	Err_status2.LOCK_FAIL = fml_gpio_read(IN_LVDS_UNLOCK);

	result1 = convertBoolStruc1ToUint8(Err_status1);
	result2 = convertBoolStruct2ToUint8(Err_status2);
}
void logic_diagnose_init(void)
{
	cola_timer_create(&loop_diagnose_task, diagnose_polling, NULL);
	cola_timer_start(&loop_diagnose_task, EN_COLA_TIMER_ALWAYS, 10);
}
