Skip to content

Commit

Permalink
Merge pull request hd-zero#339 from Master92/state-handling
Browse files Browse the repository at this point in the history
Let pages handle roller interaction
  • Loading branch information
ligenxxxx authored Oct 17, 2023
2 parents f2c5216 + e82043e commit ce5d9bd
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 172 deletions.
8 changes: 0 additions & 8 deletions src/core/app_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ typedef enum {
APP_STATE_WIFI = 13,

APP_STATE_USER_INPUT_DISABLED = 20,

// TODO pages should set the on_roller callback and handle the input themselves
// instead of creating an app state. (use APP_STATE_SUBMENU_ITEM_FOCUSED)
PAGE_FAN_SLIDE = 100,
PAGE_ANGLE_SLIDE = 101,
PAGE_POWER_SLIDE_CELL_COUNT = 102,
PAGE_POWER_SLIDE_WARNING_CELL_VOLTAGE = 103,
PAGE_POWER_SLIDE_CALIBRATION_OFFSET = 104,
} app_state_t;

extern app_state_t g_app_state;
Expand Down
27 changes: 1 addition & 26 deletions src/core/input_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,7 @@ static void btn_click(void) // short press enter key
} else if (g_app_state == APP_STATE_SUBMENU ||
g_app_state == APP_STATE_PLAYBACK ||
g_app_state == APP_STATE_SUBMENU_ITEM_FOCUSED ||
g_app_state == APP_STATE_WIFI ||
g_app_state == PAGE_FAN_SLIDE ||
g_app_state == PAGE_ANGLE_SLIDE ||
g_app_state == PAGE_POWER_SLIDE_CELL_COUNT ||
g_app_state == PAGE_POWER_SLIDE_WARNING_CELL_VOLTAGE ||
g_app_state == PAGE_POWER_SLIDE_CALIBRATION_OFFSET) {
g_app_state == APP_STATE_WIFI) {
submenu_click();
}
pthread_mutex_unlock(&lvgl_mutex);
Expand Down Expand Up @@ -318,16 +313,6 @@ 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 == PAGE_FAN_SLIDE) {
fans_speed_dec();
} else if (g_app_state == PAGE_ANGLE_SLIDE) {
ht_angle_dec();
} else if (g_app_state == PAGE_POWER_SLIDE_CELL_COUNT) {
power_cell_count_dec();
} else if (g_app_state == PAGE_POWER_SLIDE_WARNING_CELL_VOLTAGE) {
power_warning_voltage_dec();
} else if (g_app_state == PAGE_POWER_SLIDE_CALIBRATION_OFFSET) {
power_calibration_offset_dec();
}
pthread_mutex_unlock(&lvgl_mutex);
}
Expand Down Expand Up @@ -361,16 +346,6 @@ 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 == PAGE_FAN_SLIDE) {
fans_speed_inc();
} else if (g_app_state == PAGE_ANGLE_SLIDE) {
ht_angle_inc();
} else if (g_app_state == PAGE_POWER_SLIDE_CELL_COUNT) {
power_cell_count_inc();
} else if (g_app_state == PAGE_POWER_SLIDE_WARNING_CELL_VOLTAGE) {
power_warning_voltage_inc();
} else if (g_app_state == PAGE_POWER_SLIDE_CALIBRATION_OFFSET) {
power_calibration_offset_inc();
}

pthread_mutex_unlock(&lvgl_mutex);
Expand Down
158 changes: 104 additions & 54 deletions src/ui/page_fans.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
#include "ui/ui_style.h"

typedef enum {
FANS_MODE_TOP = 0,
FANS_MODE_NO_FAN = 0,
FANS_MODE_TOP,
FANS_MODE_SIDE,
} fans_mode_t;

static lv_coord_t col_dsc[] = {160, 160, 160, 160, 140, 160, LV_GRID_TEMPLATE_LAST};
static lv_coord_t row_dsc[] = {60, 60, 60, 60, 60, 60, 60, 60, 60, 60, LV_GRID_TEMPLATE_LAST};

fans_mode_t fans_mode = FANS_MODE_TOP;
static fans_mode_t fans_mode = FANS_MODE_NO_FAN;

static btn_group_t btn_group_fans;

Expand Down Expand Up @@ -76,72 +77,122 @@ static lv_obj_t *page_fans_create(lv_obj_t *parent, panel_arr_t *arr) {
return page;
}

void fans_speed_inc(void) {
int32_t value = 0;
static void fans_top_speed_inc() {
char buf[5];
int32_t value = lv_slider_get_value(slider_group[0].slider);

if (fans_mode == FANS_MODE_TOP) {
value = lv_slider_get_value(slider_group[0].slider);
if (value < MAX_FAN_TOP)
value += 1;
if (value < MAX_FAN_TOP)
value += 1;

lv_slider_set_value(slider_group[0].slider, value, LV_ANIM_OFF);
lv_slider_set_value(slider_group[0].slider, value, LV_ANIM_OFF);

sprintf(buf, "%d", value);
lv_label_set_text(slider_group[0].label, buf);
sprintf(buf, "%d", value);
lv_label_set_text(slider_group[0].label, buf);

fans_top_setspeed(value);
fans_top_setspeed(value);

g_setting.fans.top_speed = value;
ini_putl("fans", "top_speed", value, SETTING_INI);
} else if (fans_mode == FANS_MODE_SIDE) {
value = lv_slider_get_value(slider_group[1].slider);
if (value < MAX_FAN_SIDE)
value += 1;
g_setting.fans.top_speed = value;
ini_putl("fans", "top_speed", value, SETTING_INI);
}

lv_slider_set_value(slider_group[1].slider, value, LV_ANIM_OFF);
static void fans_top_speed_dec() {
char buf[5];
int32_t value = lv_slider_get_value(slider_group[0].slider);

sprintf(buf, "%d", value);
lv_label_set_text(slider_group[1].label, buf);
if (value > MIN_FAN_TOP)
value -= 1;

g_setting.fans.left_speed = value;
ini_putl("fans", "left_speed", value, SETTING_INI);
g_setting.fans.right_speed = value;
ini_putl("fans", "right_speed", value, SETTING_INI);
}
lv_slider_set_value(slider_group[0].slider, value, LV_ANIM_OFF);
sprintf(buf, "%d", value);
lv_label_set_text(slider_group[0].label, buf);

fans_top_setspeed(value);

g_setting.fans.top_speed = (uint8_t)value;
ini_putl("fans", "top_speed", value, SETTING_INI);
}
void fans_speed_dec(void) {
int32_t value = 0;

static void fans_side_speed_inc() {
char buf[5];
int32_t value = lv_slider_get_value(slider_group[1].slider);

if (fans_mode == FANS_MODE_TOP) {
value = lv_slider_get_value(slider_group[0].slider);
if (value < MAX_FAN_SIDE)
value += 1;

lv_slider_set_value(slider_group[1].slider, value, LV_ANIM_OFF);

sprintf(buf, "%d", value);
lv_label_set_text(slider_group[1].label, buf);

g_setting.fans.left_speed = value;
ini_putl("fans", "left_speed", value, SETTING_INI);
g_setting.fans.right_speed = value;
ini_putl("fans", "right_speed", value, SETTING_INI);
}

static void fans_side_speed_dec() {
char buf[5];
int32_t value = lv_slider_get_value(slider_group[1].slider);

if (value > MIN_FAN_SIDE)
value -= 1;

lv_slider_set_value(slider_group[1].slider, value, LV_ANIM_OFF);
sprintf(buf, "%d", value);
lv_label_set_text(slider_group[1].label, buf);

if (value > MIN_FAN_TOP)
value -= 1;
g_setting.fans.left_speed = value;
ini_putl("fans", "left_speed", value, SETTING_INI);
g_setting.fans.right_speed = value;
ini_putl("fans", "right_speed", value, SETTING_INI);
}

lv_slider_set_value(slider_group[0].slider, value, LV_ANIM_OFF);
sprintf(buf, "%d", value);
lv_label_set_text(slider_group[0].label, buf);
static void fans_speed_inc(void) {
if (fans_mode == FANS_MODE_TOP) {
fans_top_speed_inc();
} else if (fans_mode == FANS_MODE_SIDE) {
fans_side_speed_inc();
}
}

fans_top_setspeed(value);
static void fans_speed_dec(void) {
if (fans_mode == FANS_MODE_TOP) {
fans_top_speed_dec();
} else if (fans_mode == FANS_MODE_SIDE) {
fans_side_speed_dec();
}
}

g_setting.fans.top_speed = (uint8_t)value;
ini_putl("fans", "top_speed", value, SETTING_INI);
static void page_fans_exit_slider() {
lv_obj_t *slider;
if (fans_mode == FANS_MODE_TOP) {
slider = slider_group[0].slider;
} else if (fans_mode == FANS_MODE_SIDE) {
value = lv_slider_get_value(slider_group[1].slider);
slider = slider_group[1].slider;
} else {
return;
}

app_state_push(APP_STATE_SUBMENU);
lv_obj_add_style(slider, &style_silder_main, LV_PART_MAIN);
fans_mode = FANS_MODE_NO_FAN;
}

if (value > MIN_FAN_SIDE)
value -= 1;
static void page_fans_mode_exit() {
if (fans_mode != FANS_MODE_NO_FAN) {
page_fans_exit_slider();
}
}

lv_slider_set_value(slider_group[1].slider, value, LV_ANIM_OFF);
sprintf(buf, "%d", value);
lv_label_set_text(slider_group[1].label, buf);
static void page_fans_mode_on_roller(uint8_t key) {
if (g_app_state != APP_STATE_SUBMENU_ITEM_FOCUSED) {
return;
}

g_setting.fans.left_speed = value;
ini_putl("fans", "left_speed", value, SETTING_INI);
g_setting.fans.right_speed = value;
ini_putl("fans", "right_speed", value, SETTING_INI);
if (key == DIAL_KEY_UP) {
fans_speed_dec();
} else if (key == DIAL_KEY_DOWN) {
fans_speed_inc();
}
}

Expand All @@ -163,11 +214,10 @@ static void page_fans_mode_on_click(uint8_t key, int sel) {
return;
}

if (g_app_state == PAGE_FAN_SLIDE) {
app_state_push(APP_STATE_SUBMENU);
lv_obj_add_style(slider, &style_silder_main, LV_PART_MAIN);
if (g_app_state == APP_STATE_SUBMENU_ITEM_FOCUSED) {
page_fans_exit_slider();
} else {
app_state_push(PAGE_FAN_SLIDE);
app_state_push(APP_STATE_SUBMENU_ITEM_FOCUSED);
lv_obj_add_style(slider, &style_silder_select, LV_PART_MAIN);
}
}
Expand Down Expand Up @@ -343,8 +393,8 @@ page_pack_t pp_fans = {
.name = "Fans",
.create = page_fans_create,
.enter = NULL,
.exit = NULL,
.on_roller = NULL,
.exit = page_fans_mode_exit,
.on_roller = page_fans_mode_on_roller,
.on_click = page_fans_mode_on_click,
.on_right_button = NULL,
};
2 changes: 0 additions & 2 deletions src/ui/page_fans.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

extern page_pack_t pp_fans;

void fans_speed_dec(void);
void fans_speed_inc(void);
void step_topfan();
void fans_auto_ctrl();

Expand Down
Loading

0 comments on commit ce5d9bd

Please sign in to comment.