STATIC TIMER_ID low_power_alert_timer = NULL;

STATIC VOID_T __low_power_alert_timer_cb(TIMER_ID timer_id, VOID_T *arg)
{
    OPERATE_RET rt = OPRT_OK;
#if defined(TUYA_AI_TOY_BATTERY_ENABLE) && (TUYA_AI_TOY_BATTERY_ENABLE == 1)
    /** Uninit battery */
    tuya_ai_toy_battery_uninit();
#endif
    /** Turn off speaker and LED */
    tkl_gpio_write(s_ai_toy->cfg.spk_en_pin, TUYA_GPIO_LEVEL_LOW);
    tkl_gpio_write(s_ai_toy->cfg.led_pin, TUYA_GPIO_LEVEL_LOW);

    /** Stop AI agent */
    __ai_toy_stop();
#if defined(ENABLE_TUYA_UI) && (ENABLE_TUYA_UI == 1)
    /** Close LCD backlight */
    tuya_disp_lcd_backlight_close();
#endif

    if (TY_AI_LOW_POWER_MODE == 1) {
        /* Deep sleep: set audio trigger pin as wakeup, then enter deep sleep. */
        __ai_toy_set_wakeup_source(s_ai_toy->cfg.audio_trigger_pin);
        __ai_toy_set_wakeup_source(s_ai_toy->cfg.touch_pin);
        tal_cpu_sleep_mode_set(1, TUYA_CPU_DEEP_SLEEP);
    } else {
        rt = tal_cpu_lp_enable();
        rt |= tal_wifi_lp_enable();
        s_ai_toy->lp_stat = TRUE;
        TAL_PR_DEBUG("tal_cpu_lp_enable");
    }

    tal_sw_timer_delete(timer_id);
    low_power_alert_timer = NULL;
}

/** Low-power timer: if not playing and low-power enabled, enter deep sleep or light sleep. */
STATIC VOID __on_ai_toy_lowpower_timer(TIMER_ID timer_id, VOID_T *arg)
{
#if defined(ENABLE_LOW_POWER) && (ENABLE_LOW_POWER == 1)
    TAL_PR_NOTICE("ai toy -> lowpower timer out, will check and enter idle status");
    if (wukong_audio_player_is_playing()) {
        TAL_PR_NOTICE("ai toy -> player is playing, lowpower timer reset");
        tal_sw_timer_start(s_ai_toy->lowpower_timer, TOY_LOWPOWER_TIMEOUT, TAL_TIMER_ONCE);
        return;
    }

    wukong_audio_player_alert(AI_TOY_ALERT_TYPE_SLEEP_MODE, FALSE);

    if (low_power_alert_timer == NULL)
    {
        tal_sw_timer_create(__low_power_alert_timer_cb, NULL, &low_power_alert_timer);
        tal_sw_timer_start(low_power_alert_timer, 4000, TAL_TIMER_ONCE);
    }
#endif
}