diff --git a/src/core/app_state.c b/src/core/app_state.c index d3c41cc8..0b523a48 100644 --- a/src/core/app_state.c +++ b/src/core/app_state.c @@ -55,6 +55,24 @@ void app_switch_to_menu() { system_script(REC_STOP_LIVE); } +void app_exit_menu() { + switch (g_source_info.source) { + case SOURCE_HDZERO: + progress_bar.start = 1; + app_switch_to_hdzero(true); + break; + case SOURCE_HDMI_IN: + app_switch_to_hdmi_in(); + break; + case SOURCE_AV_IN: + app_switch_to_analog(0); + break; + case SOURCE_EXPANSION: + app_switch_to_analog(1); + break; + } +} + void app_switch_to_analog(bool is_bay) { Source_AV(is_bay); diff --git a/src/core/app_state.h b/src/core/app_state.h index 53609438..c606ef32 100644 --- a/src/core/app_state.h +++ b/src/core/app_state.h @@ -23,6 +23,8 @@ typedef enum { APP_STATE_WIFI = 13, APP_STATE_USER_INPUT_DISABLED = 20, + + APP_STATE_SLEEP = 30, } app_state_t; extern app_state_t g_app_state; @@ -30,6 +32,7 @@ extern app_state_t g_app_state; void app_state_push(app_state_t state); void app_switch_to_menu(); +void app_exit_menu(); void app_switch_to_analog(bool is_bay); void app_switch_to_hdmi_in(); void app_switch_to_hdzero(bool is_default); \ No newline at end of file diff --git a/src/core/ht.c b/src/core/ht.c index 8b25533d..1d5689ed 100644 --- a/src/core/ht.c +++ b/src/core/ht.c @@ -16,6 +16,7 @@ #include "bmi270/accel_gyro.h" #include "core/settings.h" +#include "core/sleep_mode.h" #include "driver/beep.h" #include "driver/dm6302.h" #include "driver/hardware.h" @@ -146,7 +147,7 @@ static void detect_motion(bool is_moving) { } void ht_detect_motion() { - if (has_motion_data) { + if (!isSleeping && has_motion_data) { detect_motion(is_moving); has_motion_data = false; } diff --git a/src/core/input_device.c b/src/core/input_device.c index f1029955..182d3051 100644 --- a/src/core/input_device.c +++ b/src/core/input_device.c @@ -28,6 +28,7 @@ #include "core/dvr.h" #include "core/elrs.h" #include "core/settings.h" +#include "core/sleep_mode.h" #include "driver/dm6302.h" #include "driver/hardware.h" #include "driver/i2c.h" @@ -170,21 +171,7 @@ static void btn_press(void) // long press left key g_autoscan_exit = true; if (g_app_state == APP_STATE_MAINMENU) // Main menu -> Video { - switch (g_source_info.source) { - case SOURCE_HDZERO: - progress_bar.start = 1; - app_switch_to_hdzero(true); - break; - case SOURCE_HDMI_IN: - app_switch_to_hdmi_in(); - break; - case SOURCE_AV_IN: - app_switch_to_analog(0); - break; - case SOURCE_EXPANSION: - app_switch_to_analog(1); - break; - } + app_exit_menu(); app_state_push(APP_STATE_VIDEO); } else if ((g_app_state == APP_STATE_VIDEO) || (g_app_state == APP_STATE_IMS)) { // video -> Main menu if (tune_timer && g_source_info.source == SOURCE_HDZERO) @@ -194,9 +181,11 @@ static void btn_press(void) // long press left key } else if (g_app_state == APP_STATE_OSD_ELEMENT_PREV) { ui_osd_element_pos_cancel_and_hide(); app_switch_to_menu(); - } else if (g_app_state == APP_STATE_PLAYBACK) + } else if (g_app_state == APP_STATE_PLAYBACK) { pb_key(DIAL_KEY_PRESS); - else { // Sub-menu -> Main menu + } else if (g_app_state == APP_STATE_SLEEP) { + wake_up(); + } else { // Sub-menu -> Main menu submenu_exit(); app_state_push(APP_STATE_MAINMENU); main_menu_show(true); @@ -250,6 +239,8 @@ static void btn_click(void) // short press enter key g_app_state == APP_STATE_SUBMENU_ITEM_FOCUSED || g_app_state == APP_STATE_WIFI) { submenu_click(); + } else if (g_app_state == APP_STATE_SLEEP) { + wake_up(); } pthread_mutex_unlock(&lvgl_mutex); } @@ -279,9 +270,16 @@ void rbtn_click(right_button_t click_type) { step_topfan(); pthread_mutex_unlock(&lvgl_mutex); } else if (click_type == RIGHT_DOUBLE_CLICK) { - ht_set_center_position(); + if (g_setting.ht.enable == true) { + ht_set_center_position(); + } else { + go_sleep(); + } } break; + case APP_STATE_SLEEP: + wake_up(); + break; } } @@ -315,6 +313,8 @@ static void roller_up(void) { ims_key(DIAL_KEY_UP); } else if (g_app_state == APP_STATE_OSD_ELEMENT_PREV) { ui_osd_element_pos_handle_input(DIAL_KEY_UP); + } else if (g_app_state == APP_STATE_SLEEP) { + wake_up(); } pthread_mutex_unlock(&lvgl_mutex); } @@ -348,8 +348,9 @@ static void roller_down(void) { ims_key(DIAL_KEY_DOWN); } else if (g_app_state == APP_STATE_OSD_ELEMENT_PREV) { ui_osd_element_pos_handle_input(DIAL_KEY_DOWN); + } else if (g_app_state == APP_STATE_SLEEP) { + wake_up(); } - pthread_mutex_unlock(&lvgl_mutex); } diff --git a/src/core/main.c b/src/core/main.c index 2fe09f96..cf1b8d55 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -20,6 +20,7 @@ #include "core/osd.h" #include "core/self_test.h" #include "core/settings.h" +#include "core/sleep_mode.h" #include "core/thread.h" #include "driver/TP2825.h" #include "driver/dm5680.h" @@ -195,6 +196,7 @@ int main(int argc, char *argv[]) { g_init_done = 1; for (;;) { pthread_mutex_lock(&lvgl_mutex); + sleep_reminder(); statubar_update(); osd_hdzero_update(); ims_update(); diff --git a/src/core/sleep_mode.c b/src/core/sleep_mode.c new file mode 100644 index 00000000..b0503a73 --- /dev/null +++ b/src/core/sleep_mode.c @@ -0,0 +1,97 @@ +#include "sleep_mode.h" + +#include "core/app_state.h" +#include "core/common.hh" +#include "core/dvr.h" +#include "core/settings.h" + +#include "driver/beep.h" +#include "driver/dm5680.h" +#include "driver/fans.h" +#include "driver/hardware.h" + +#include "log/log.h" + +#include "ui/page_fans.h" + +static app_state_t previousState; +static int fans_auto_mode_save; +static fan_speed_t fan_speed_save; + +static uint16_t beepCnt = 0; + +bool isSleeping = false; + +void go_sleep() { + LOGI("Entering sleep mode"); + previousState = g_app_state; + app_state_push(APP_STATE_SLEEP); + + // Stop DVR + dvr_cmd(DVR_STOP); + dvr_update_vi_conf(VR_1080P30); + + // Turn off OLED + OLED_ON(0); + + // Turn off HDZero Receiver + HDZero_Close(); + + // Turn off Analog Receiver -- Batch 2 goggles only + if (getHwRevision() >= HW_REV_2) { + DM5680_Power_AnalogModule(1); + } + + // Minimum fan + fans_auto_mode_save = g_setting.fans.auto_mode; + fan_speed_save.top = fan_speed.top; + fan_speed_save.left = fan_speed.left; + fan_speed_save.right = fan_speed.right; + g_setting.fans.top_speed = MIN_FAN_TOP; + g_setting.fans.left_speed = MIN_FAN_SIDE; + g_setting.fans.right_speed = MIN_FAN_SIDE; + g_setting.fans.auto_mode = 0; + fans_top_setspeed(MIN_FAN_TOP); + fans_left_setspeed(MIN_FAN_SIDE); + fans_right_setspeed(MIN_FAN_SIDE); + isSleeping = true; + beepCnt = 0; +} + +void wake_up() { + LOGI("Exiting sleep mode"); + isSleeping = false; + + OLED_ON(1); // Turn on OLED + Analog_Module_Power(1); + + g_setting.fans.top_speed = fan_speed_save.top; + g_setting.fans.left_speed = fan_speed_save.left; + g_setting.fans.right_speed = fan_speed_save.right; + g_setting.fans.auto_mode = fans_auto_mode_save; + fans_top_setspeed(fan_speed_save.top); + fans_left_setspeed(fan_speed_save.left); + fans_right_setspeed(fan_speed_save.right); + + app_state_push(previousState); + if (previousState == APP_STATE_SUBMENU) { + submenu_exit(); + } else if (previousState == APP_STATE_VIDEO) { + app_switch_to_menu(); // Necessary to display the progress bar + app_state_push(previousState); // Because app_switch_to_menu() pushes main menu state + app_exit_menu(); + } +} + +void sleep_reminder() +{ + if (isSleeping == false) { + return; + } + +#define BEEP_INTERVAL 4000 + if (++beepCnt == BEEP_INTERVAL) { + beep_dur(BEEP_VERY_SHORT); + beepCnt = 0; + } +} diff --git a/src/core/sleep_mode.h b/src/core/sleep_mode.h new file mode 100644 index 00000000..51a936a1 --- /dev/null +++ b/src/core/sleep_mode.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +extern bool isSleeping; + +void go_sleep(); +void wake_up(); +void sleep_reminder(); diff --git a/src/driver/beep.h b/src/driver/beep.h index 8e0c0160..821d1acd 100644 --- a/src/driver/beep.h +++ b/src/driver/beep.h @@ -1,5 +1,6 @@ #pragma once +#define BEEP_VERY_SHORT 1 #define BEEP_SHORT 50 #define BEEP_LONG 200 diff --git a/src/ui/page_sleep.c b/src/ui/page_sleep.c index 5fe3cfa5..3996674b 100644 --- a/src/ui/page_sleep.c +++ b/src/ui/page_sleep.c @@ -1,14 +1,9 @@ #include "page_sleep.h" -#include "core/app_state.h" -#include "core/common.hh" -#include "core/settings.h" -#include "driver/dm5680.h" #include "driver/fans.h" -#include "driver/hardware.h" #include "page_fans.h" +#include "sleep_mode.h" #include -#include #include lv_obj_t *page_sleep_create(lv_obj_t *parent, panel_arr_t *arr) { @@ -26,7 +21,10 @@ lv_obj_t *page_sleep_create(lv_obj_t *parent, panel_arr_t *arr) { lv_obj_t *cont = lv_menu_cont_create(section); lv_obj_t *desc_label = lv_label_create(cont); - lv_label_set_text(desc_label, "Click the Enter Button to go sleep.\nClick any button to exit sleep mode."); + lv_label_set_text(desc_label, "Click the Enter Button to go sleep.\n" + "Click any button to exit sleep mode.\n\n" + "Double-click the right button to go sleep from video mode.\n" + "(Only when Head Tracker is disabled!)"); lv_obj_set_style_text_font(desc_label, &lv_font_montserrat_26, 0); lv_obj_set_style_text_color(desc_label, lv_color_make(255, 255, 255), 0); lv_obj_set_style_pad_top(desc_label, 12, 0); @@ -41,63 +39,15 @@ static fan_speed_t fan_speed_save; static void page_sleep_enter() { LOGI("page_sleep_enter"); - // Turn off OLED - OLED_ON(0); - - // Turn off HDZero Receiver - HDZero_Close(); - - // Turn off Analog Receiver -- Batch 2 goggles only - if (getHwRevision() >= HW_REV_2) { - DM5680_Power_AnalogModule(1); - } - - // Minimum fan - fans_auto_mode_save = g_setting.fans.auto_mode; - fan_speed_save.top = fan_speed.top; - fan_speed_save.left = fan_speed.left; - fan_speed_save.right = fan_speed.right; - g_setting.fans.top_speed = MIN_FAN_TOP; - g_setting.fans.left_speed = MIN_FAN_SIDE; - g_setting.fans.right_speed = MIN_FAN_SIDE; - g_setting.fans.auto_mode = 0; - fans_top_setspeed(MIN_FAN_TOP); - fans_left_setspeed(MIN_FAN_SIDE); - fans_right_setspeed(MIN_FAN_SIDE); -} - -static void page_sleep_exit() { - LOGI("page_sleep_exit"); - OLED_ON(1); // Turn on OLED - Analog_Module_Power(1); - - g_setting.fans.top_speed = fan_speed_save.top; - g_setting.fans.left_speed = fan_speed_save.left; - g_setting.fans.right_speed = fan_speed_save.right; - g_setting.fans.auto_mode = fans_auto_mode_save; - fans_top_setspeed(fan_speed_save.top); - fans_left_setspeed(fan_speed_save.left); - fans_right_setspeed(fan_speed_save.right); -} - -static void page_sleep_click(uint8_t key, int sel) { - submenu_exit(); -} - -static void page_sleep_roller(uint8_t key) { - submenu_exit(); -}; - -static void page_sleep_right_button(bool is_short) { - submenu_exit(); + go_sleep(); } page_pack_t pp_sleep = { .create = page_sleep_create, .enter = page_sleep_enter, - .exit = page_sleep_exit, - .on_roller = page_sleep_roller, - .on_click = page_sleep_click, - .on_right_button = page_sleep_right_button, + .exit = NULL, + .on_roller = NULL, + .on_click = NULL, + .on_right_button = NULL, .name = "Go Sleep!", };