kvm/internal/native/eez/src/ui/actions.c

140 lines
4.5 KiB
C

#include "actions.h"
#include "screens.h"
#include <stdio.h>
#include "ui.h"
#include "vars.h"
int handle_gesture_screen_switch(lv_event_t *e, lv_dir_t direction, int screenId) {
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code != LV_EVENT_GESTURE) {
return 0;
}
if (lv_indev_get_gesture_dir(lv_indev_get_act()) != direction) {
return 0;
}
lv_indev_wait_release(lv_indev_get_act());
loadScreen(screenId);
return 1;
}
void handle_gesture_main_screen_switch(lv_event_t *e, lv_dir_t direction) {
const char *main_screen = get_var_main_screen();
if (strcmp(main_screen, "home_screen") == 0) {
loadScreen(SCREEN_ID_HOME_SCREEN);
} else if (strcmp(main_screen, "no_network_screen") == 0) {
loadScreen(SCREEN_ID_NO_NETWORK_SCREEN);
}
}
void action_switch_to_menu(lv_event_t *e) {
loadScreen(SCREEN_ID_MENU_SCREEN);
}
void action_switch_to_advanced_menu(lv_event_t *e) {
loadScreen(SCREEN_ID_MENU_ADVANCED_SCREEN);
}
void action_switch_to_status(lv_event_t *e) {
loadScreen(SCREEN_ID_STATUS_SCREEN);
}
void action_switch_to_about(lv_event_t *e) {
loadScreen(SCREEN_ID_ABOUT_SCREEN);
}
void action_switch_to_reset_config(lv_event_t *e) {
loadScreen(SCREEN_ID_RESET_CONFIG_SCREEN);
}
void action_switch_to_reboot(lv_event_t *e) {
loadScreen(SCREEN_ID_REBOOT_SCREEN);
}
void action_menu_screen_gesture(lv_event_t * e) {
handle_gesture_main_screen_switch(e, LV_DIR_RIGHT);
}
void action_menu_advanced_screen_gesture(lv_event_t * e) {
handle_gesture_screen_switch(e, LV_DIR_RIGHT, SCREEN_ID_MENU_SCREEN);
}
void action_reset_config_screen_gesture(lv_event_t * e) {
handle_gesture_screen_switch(e, LV_DIR_RIGHT, SCREEN_ID_MENU_SCREEN);
}
void action_home_screen_gesture(lv_event_t * e) {
handle_gesture_screen_switch(e, LV_DIR_LEFT, SCREEN_ID_MENU_SCREEN);
}
void action_about_screen_gesture(lv_event_t * e) {
handle_gesture_screen_switch(e, LV_DIR_RIGHT, SCREEN_ID_MENU_SCREEN);
}
// user_data doesn't seem to be working, so we use a global variable here
static uint32_t t_reset_config;
static uint32_t t_reboot;
static bool b_reboot = false;
static bool b_reset_config = false;
const int RESET_CONFIG_HOLD_TIME = 10;
const int REBOOT_HOLD_TIME = 5;
void action_reset_config(lv_event_t * e) {
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t *obj = lv_event_get_target(e);
if (event_code == LV_EVENT_PRESSED) {
t_reset_config = lv_tick_get();
}
else if (event_code == LV_EVENT_PRESSING) {
int remaining_time = RESET_CONFIG_HOLD_TIME * 1000 - lv_tick_elaps(t_reset_config);
if (remaining_time <= 0) {
lv_obj_add_flag(objects.reset_config_button, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(objects.reset_config_spinner, LV_OBJ_FLAG_HIDDEN);
ui_call_rpc_handler("resetConfig", NULL);
b_reset_config = true;
} else {
b_reset_config = false;
char buf[100];
int remaining_time_seconds = remaining_time / 1000;
if (remaining_time_seconds <= 1) {
remaining_time_seconds = 1;
}
sprintf(buf, "Press and hold for %d seconds", remaining_time_seconds);
lv_label_set_text(objects.reset_config_label, buf);
}
} else if (event_code == LV_EVENT_RELEASED) {
if (!b_reset_config) {
lv_label_set_text(objects.reset_config_label, "Press and hold for 10 seconds");
}
}
}
void action_reboot(lv_event_t * e) {
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t *obj = lv_event_get_target(e);
if (event_code == LV_EVENT_PRESSED) {
t_reboot = lv_tick_get();
}
else if (event_code == LV_EVENT_PRESSING) {
int remaining_time = REBOOT_HOLD_TIME * 1000 - lv_tick_elaps(t_reboot);
if (remaining_time <= 0) {
ui_call_rpc_handler("reboot", NULL);
b_reboot = false;
} else {
b_reboot = false;
char buf[100];
int remaining_time_seconds = remaining_time / 1000;
if (remaining_time_seconds <= 1) {
remaining_time_seconds = 1;
}
sprintf(buf, "Press and hold for %d seconds", remaining_time_seconds);
lv_label_set_text(objects.reboot_label, buf);
}
} else if (event_code == LV_EVENT_RELEASED) {
if (!b_reboot) {
lv_label_set_text(objects.reboot_label, "Press and hold for 5 seconds");
}
}
}