#include "system_voltage_app.h"
#include "system_voltage_manage.h"
#include "app_encoder.h"
#include "key_encoder_driver.h"
#include "key_app.h"
#include "led_app.h"
#include "pwm_manage.h"
#include "Com.h"
#include "Demm.h"
#include "Dcm.h"
#include "Dcm_Types.h"
#include "CanNm.h"
#include "can_app.h"
void system_voltage_app_task(void)
{
    Nm_StateType can_nm_state;
    Nm_ModeType  can_nm_mode;
    uint8_t system_voltage_mode;
    uint8_t CAN_ControlBy;

    system_voltage_mode = system_voltage_manage_mode_get(FUNC_SYSTEM_VOLTAGE_MANAGE_ID);

    if(system_voltage_mode != SYSTEM_VOLTAGE_MODE_NORMAL)
    {
        (void)app_encoder_init();
        (void)key_encoder_driver_init();
        (void)key_app_init();
        (void)led_app_init();
        (void)pwm_set_duty(BACKGROUND_LIGHT_PWM_ID, BACKGROUND_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(PICK_BACKGROUND_LIGHT_PWM_ID, L_PICK_BACKGROUND_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(PICK_BACKGROUND_LIGHT_PWM_ID, R_PICK_BACKGROUND_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(L_PICK_HIGH_LIGHT_PWM_ID, L_PICK_HIGH_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(L_PICK_LOW_LIGHT_PWM_ID, L_PICK_LOW_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(R_PICK_HIGH_LIGHT_PWM_ID, R_PICK_HIGH_LIGHT_PWM_CH, 0);
        (void)pwm_set_duty(R_PICK_LOW_LIGHT_PWM_ID, R_PICK_LOW_LIGHT_PWM_CH, 0);
    }
    else
    {

    }

    system_voltage_mode = system_voltage_manage_mode_get(COMM_SYSTEM_VOLTAGE_MANAGE_ID);
    CanNm_GetState(0 ,(Nm_StateType *)(&can_nm_state),(Nm_ModeType *)(&can_nm_mode));
    CAN_ControlBy = CAN_GetControlBy();
    if(CAN_ControlBy == CAN_CONTROL_BY_APP)
    {
        if((system_voltage_mode != SYSTEM_VOLTAGE_MODE_NORMAL) && (system_voltage_mode != SYSTEM_VOLTAGE_MODE_UNKNOWN))
        {
            if(can_nm_state > NM_STATE_READY_SLEEP)
            {
                (void)CanNm_DisableCommunication(0);
                
            }
            if(can_nm_state > NM_STATE_PREPARE_BUS_SLEEP)
            {
                (void)Com_TxStop();
            }
            
            
        }
        else
        {
            if(can_nm_state > NM_STATE_READY_SLEEP)
            {
                (void)CanNm_EnableCommunication(0);
            }
            if(can_nm_state > NM_STATE_PREPARE_BUS_SLEEP)
            {
                (void)Com_TxStart();
            }
            
        }

    }


    system_voltage_mode = system_voltage_manage_mode_get(DTC_SYSTEM_VOLTAGE_MANAGE_ID);
    if((system_voltage_mode != SYSTEM_VOLTAGE_MODE_NORMAL) && (system_voltage_mode != SYSTEM_VOLTAGE_MODE_UNKNOWN))
    {
        (void)dem_disable_dtc_record_update();
    }
    else
    {
        (void)dem_enable_dtc_record_update();
    }
}