#include "demm_app.h"
#include "system_voltage_manage.h"
#include "Dem_Types.h"
#include "DEM_Cfg.h"
#include "key_adc_mid.h"
#include "key_digital_driver_cfg.h"
#include "key_digital_driver.h"
#include "Demm.h"
#include "WheelHeat.h"
#define NUM_OF_DTC_KEY  15


const uint8_t dtc_key_id_tab[NUM_OF_DTC_KEY] = {
    DTC_PREV_BUTTON_STUCK_ID,
    DTC_PHONE_BUTTON_STUCK_ID,
    DTC_VOICE_BUTTON_STUCK_ID,
    DTC_VEDIO_BUTTON_STUCK_ID,
    DTC_NEXT_BUTTON_STUCK_ID,
    DTC_MUTE_BUTTON_STUCK_ID,
    DTC_CUSTOM_BUTTON_STUCK_ID,
    DTC_MENU2_CTRL_BUTTON_STUCK_ID,
    DTC_SPDP_BUTTON_STUCK_ID,
    DTC_SPDD_BUTTON_STUCK_ID,
    DTC_DSPD_BUTTON_STUCK_ID,
    DTC_DSPP_BUTTON_STUCK_ID,
    DTC_LEFT_MENU_STUCK_ID,
    DTC_LEFT_PICK_STUCK_ID,
    DTC_RIGHT_PICK_STUCK_ID
};


void demm_app_task(void)
{
    uint8_t system_voltage_mode;
    uint8_t key_state[NUM_OF_DTC_KEY];
    uint8_t i;
    uint8_t ntc_state;
    uint8_t heat_overcurrent;
    ntc_state = WheelHeat_GetNtcState();
    heat_overcurrent = WheelHeat_GetHeatOverCurrent();
    system_voltage_mode = system_voltage_manage_mode_get(FUNC_SYSTEM_VOLTAGE_MANAGE_ID);

    if(system_voltage_mode == SYSTEM_VOLTAGE_MODE_LOW)
    {
        dem_set_test_result(DTC_POWER_LOW_ID, DTC_TEST_STATE_FAILED);
        dem_set_test_result(DTC_POWER_HIGH_ID, DTC_TEST_STATE_PASSED);
    }
    else if(system_voltage_mode == SYSTEM_VOLTAGE_MODE_HIGH)
    {
        dem_set_test_result(DTC_POWER_HIGH_ID, DTC_TEST_STATE_FAILED);
        dem_set_test_result(DTC_POWER_LOW_ID, DTC_TEST_STATE_PASSED);
    }
    else
    {
        dem_set_test_result(DTC_POWER_LOW_ID, DTC_TEST_STATE_PASSED);
        dem_set_test_result(DTC_POWER_HIGH_ID, DTC_TEST_STATE_PASSED);
    }
    #if 0
    if(ntc_state == WHEEL_HEAT_NTC_SHORT )
    {
        dem_set_test_result(DTC_HEAT_NTC_SHORT_ID, DTC_TEST_STATE_FAILED);
    }
    else if(ntc_state == WHEEL_HEAT_NTC_OPEN )
    {
        dem_set_test_result(DTC_HEAT_NTC_OPEN_ID, DTC_TEST_STATE_FAILED);
    }
    else
    {
        dem_set_test_result(DTC_HEAT_NTC_SHORT_ID, DTC_TEST_STATE_PASSED);
        dem_set_test_result(DTC_HEAT_NTC_OPEN_ID, DTC_TEST_STATE_PASSED);
    }

    
    if(heat_overcurrent == WHEEL_HEAT_OVERCURRENT )
    {
        dem_set_test_result(DTC_HEAT_OVERCURRENT_ID, DTC_TEST_STATE_FAILED);
    }
    else
    {
        dem_set_test_result(DTC_HEAT_OVERCURRENT_ID, DTC_TEST_STATE_PASSED);
        
    }
    #endif
    key_state[0] = key_digital_driver_get_state(KEY_DIGI_ID_PREV);
    key_state[1] = key_digital_driver_get_state(KEY_DIGI_ID_PHONE);
    key_state[2] = key_digital_driver_get_state(KEY_DIGI_ID_VOICE);
    key_state[3] = key_adc_mid_get_result(KEY_ADC_ID_VEDIO);
    key_state[4] = key_digital_driver_get_state(KEY_DIGI_ID_NEXT);
    key_state[5] = key_digital_driver_get_state(KEY_DIGI_ID_MUTE);
    key_state[6] = key_adc_mid_get_result(KEY_ADC_ID_CUSTOM);
    key_state[7] = key_digital_driver_get_state(KEY_DIGI_ID_RMENU);
    key_state[8] = key_adc_mid_get_result(KEY_ADC_ID_RES_SPD_ADD);
    key_state[9] = key_adc_mid_get_result(KEY_ADC_ID_SET_SPD_SUB);
    key_state[10] = key_adc_mid_get_result(KEY_ADC_ID_DSP_SUB);
    key_state[11] = key_adc_mid_get_result(KEY_ADC_ID_DSP_PLUS);
    key_state[12] = key_adc_mid_get_result(KEY_ADC_ID_LMENU);
    key_state[13] = key_adc_mid_get_result(KEY_ADC_ID_LPICK);
    key_state[14] = key_digital_driver_get_state(KEY_DIGI_ID_RPICK);;

    for(i = 0; i < NUM_OF_DTC_KEY; i ++)
    {
        if(i == 0 || i == 1 || i == 2 || i == 4 || i == 5 || i == 7 || i == 14)
        {
            if(key_state[i] == KEY_DIGITAL_DRIVER_STATE_STICK)
            {
                dem_set_test_result(dtc_key_id_tab[i], DTC_TEST_STATE_FAILED);
            }
            else
            {
                dem_set_test_result(dtc_key_id_tab[i], DTC_TEST_STATE_PASSED);
            }
        }
        else
        {
            if(key_state[i] == KEY_ADC_RESULT_STICK)
            {
                dem_set_test_result(dtc_key_id_tab[i], DTC_TEST_STATE_FAILED);
            }
            else
            {
                dem_set_test_result(dtc_key_id_tab[i], DTC_TEST_STATE_PASSED);
            }
        }
    }
}