Added toggle switch widget.

This commit is contained in:
Dylan Smith
2026-01-16 16:28:00 -05:00
parent fd46ea65ca
commit 3ef5bd9240
7 changed files with 62 additions and 61 deletions

View File

@@ -29,7 +29,7 @@ void DrawBox(uint32_t topleft_x_loc, uint32_t topleft_y_loc, uint32_t width, uin
void draw_toggle_switch(volatile pixel_t *const framebuffer, const toggle_switch_t *const toggle_switch)
{
const uint16_t default_width = 50;
const uint16_t default_width = 40;
const uint16_t default_height = 20;
const pixel_t default_on_color = MAKE_PIXEL(0, 255, 0);
const pixel_t default_off_color = MAKE_PIXEL(160, 20, 20);

View File

@@ -20,7 +20,7 @@ void draw_menu_entry(volatile pixel_t *const framebuffer, const graphical_menu_t
// Draw entry background - use highlighted color if this is the selected entry
pixel_t bg_color = is_highlighted ? highlighted_bg_color : entry_bg_color;
DrawBox(0, y_pos, DISPLAY_WIDTH, entry_height, bg_color);
DrawBox(0, y_pos + 1, DISPLAY_WIDTH, entry_height - 1, bg_color);
// Draw entry text - use highlighted color if selected, otherwise use enabled/disabled color
pixel_t text_color;
@@ -35,10 +35,16 @@ void draw_menu_entry(volatile pixel_t *const framebuffer, const graphical_menu_t
// Calculate baseline from top position: baseline = top + (line_height - base_line)
const uint16_t text_baseline_y = y_pos + padding_y + (roboto_bold_font.line_height - roboto_bold_font.base_line);
draw_string(framebuffer, &runescape_font,
draw_string(framebuffer, &roboto_bold_font,
padding_x, text_baseline_y,
entry->title, text_color);
// draw a line between the menu entries
if (entry_idx != 0)
{
DrawBox(5, y_pos, DISPLAY_WIDTH - 10, 1, MAKE_PIXEL(80, 80, 80));
}
if (entry->extra_draw_function != NULL)
{
const menu_entry_size_t menu_entry_size = {
@@ -132,4 +138,24 @@ void increment_selected_menu_entry_idx(volatile pixel_t *const framebuffer, grap
}
set_selected_menu_entry_idx(framebuffer, menu, idx);
}
void add_toggle_switch_to_menu_entry(const menu_entry_size_t *const menu_entry_size, void *const toggle_switch_value)
{
const uint16_t width_padding = 10;
const uint16_t toggle_switch_width = 40;
const uint16_t toggle_switch_height = 20;
const uint16_t x = menu_entry_size->x + ( (menu_entry_size->width - toggle_switch_width)) - width_padding;
const uint16_t y = menu_entry_size->y + ( (menu_entry_size->height / 2) - (toggle_switch_height / 2));
const toggle_switch_t toggle_switch = {
.x = x,
.y = y,
.width = toggle_switch_width,
.height = toggle_switch_height,
.inner_padding_pixels = 3,
.value = *(bool *)toggle_switch_value,
};
draw_toggle_switch(next_framebuffer, &toggle_switch);
}

View File

@@ -65,7 +65,8 @@ SDRAM_HandleTypeDef hsdram1;
osThreadId defaultTaskHandle;
/* USER CODE BEGIN PV */
volatile bool led1_state = false;
volatile bool led2_state = false;
/* USER CODE END PV */
@@ -951,15 +952,13 @@ void StartDefaultTask(void const * argument)
{
ui_left_button_pressed();
}
if (RIGHT_BUTTON_PRESSED())
{
ui_right_button_pressed();
}
if (OK_BUTTON_PRESSED())
{
ui_ok_button_pressed();
}
ui_task();
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, led1_state ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, led2_state ? GPIO_PIN_SET : GPIO_PIN_RESET);
osDelay(100);
}

View File

@@ -7,8 +7,8 @@
void ui_enter_submenu(void *const args);
void ui_toggle_led1(void *const args);
void ui_toggle_led2(void *const args);
void ui_toggle_led1_switch(const menu_entry_size_t *const menu_entry_size, void *const args);
void ui_toggle_led2_switch(const menu_entry_size_t *const menu_entry_size, void *const args);
void ui_draw_toggle_led1_switch(const menu_entry_size_t *const menu_entry_size, void *const args);
void ui_draw_toggle_led2_switch(const menu_entry_size_t *const menu_entry_size, void *const args);
/******************* */
/* Menu declarations */
@@ -36,13 +36,15 @@ static graphical_menu_t main_menu = {
.disabled = true,
.selected_callback_function = ui_toggle_led1,
.selected_callback_function_args = 0,
.extra_draw_function = ui_toggle_led1_switch,
.extra_draw_function = add_toggle_switch_to_menu_entry,
.extra_draw_function_args = (void *)&led1_state,
},
{
.title = "Toggle LED2",
.selected_callback_function = ui_toggle_led2,
.selected_callback_function_args = 0,
.extra_draw_function = ui_toggle_led2_switch,
.extra_draw_function = add_toggle_switch_to_menu_entry,
.extra_draw_function_args = (void *)&led2_state,
}
}
};
@@ -87,40 +89,6 @@ void ui_toggle_led2(void *const args)
ui_request_redraw();
}
void ui_toggle_led1_switch(const menu_entry_size_t *const menu_entry_size, void *const args)
{
const uint16_t width_padding = 10;
const uint16_t height_padding = 10;
const uint16_t x = menu_entry_size->x + ( (menu_entry_size->width / 2) + width_padding);
const uint16_t y = menu_entry_size->y + height_padding;
const toggle_switch_t toggle_switch = {
.x = x,
.y = y,
.inner_padding_pixels = 3,
.value = HAL_GPIO_ReadPin(LD3_GPIO_Port, LD3_Pin) == GPIO_PIN_SET,
};
draw_toggle_switch(next_framebuffer, &toggle_switch);
}
void ui_toggle_led2_switch(const menu_entry_size_t *const menu_entry_size, void *const args)
{
const uint16_t width_padding = 10;
const uint16_t height_padding = 10;
const uint16_t x = menu_entry_size->x + ( (menu_entry_size->width / 2) + width_padding);
const uint16_t y = menu_entry_size->y + height_padding;
const toggle_switch_t toggle_switch = {
.x = x,
.y = y,
.inner_padding_pixels = 3,
.value = HAL_GPIO_ReadPin(LD4_GPIO_Port, LD4_Pin) == GPIO_PIN_SET,
};
draw_toggle_switch(next_framebuffer, &toggle_switch);
}
/************************* */
/* UI function definitions */
/************************* */
@@ -143,15 +111,9 @@ void ui_exit_submenu(void)
void ui_left_button_pressed(void)
{
// decrement_selected_menu_entry_idx(&main_menu);
ui_exit_submenu();
}
void ui_right_button_pressed(void)
{
// increment_selected_menu_entry_idx(&main_menu);
}
void ui_up_button_pressed(void)
{
decrement_selected_menu_entry_idx(next_framebuffer, current_menu);