Toggle switches work

This commit is contained in:
Dylan Smith
2026-01-16 15:45:32 -05:00
parent ed31755a3a
commit fd46ea65ca
3 changed files with 38 additions and 14 deletions

View File

@@ -29,26 +29,33 @@ 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_height = 20;
const pixel_t default_on_color = MAKE_PIXEL(0, 255, 0);
const pixel_t default_off_color = MAKE_PIXEL(160, 20, 20);
const uint16_t default_inner_padding_pixels = 0;
const uint16_t x_loc = toggle_switch->x;
const uint16_t y_loc = toggle_switch->y;
const uint16_t width = toggle_switch->width ? toggle_switch->width : 50;
const uint16_t height = toggle_switch->height ? toggle_switch->height : 20;
const pixel_t on_color = toggle_switch->on_color ? toggle_switch->on_color : MAKE_PIXEL(0, 255, 0);
const pixel_t off_color = toggle_switch->off_color ? toggle_switch->off_color : MAKE_PIXEL(160, 20, 20);
const uint16_t width = toggle_switch->width ? toggle_switch->width : default_width;
const uint16_t height = toggle_switch->height ? toggle_switch->height : default_height;
const pixel_t on_color = toggle_switch->on_color ? toggle_switch->on_color : default_on_color;
const pixel_t off_color = toggle_switch->off_color ? toggle_switch->off_color : default_off_color;
const uint16_t inner_padding_pixels = toggle_switch->inner_padding_pixels ? toggle_switch->inner_padding_pixels : default_inner_padding_pixels;
const bool value = toggle_switch->value;
DrawBox(x_loc, y_loc, width, height, value ? on_color : off_color);
if (value)
{
const uint16_t inner_switchbox_width = height - (toggle_switch->inner_padding_pixels * 2);
const uint16_t inner_switchbox_x_loc = x_loc + width - toggle_switch->inner_padding_pixels - inner_switchbox_width;
const uint16_t inner_switchbox_y_loc = y_loc + toggle_switch->inner_padding_pixels;
const uint16_t inner_switchbox_width = height - (inner_padding_pixels * 2);
const uint16_t inner_switchbox_x_loc = x_loc + width - inner_padding_pixels - inner_switchbox_width;
const uint16_t inner_switchbox_y_loc = y_loc + inner_padding_pixels;
DrawBox(inner_switchbox_x_loc, inner_switchbox_y_loc, inner_switchbox_width, inner_switchbox_width, MAKE_PIXEL(255, 255, 255));
} else {
const uint16_t inner_switchbox_width = height - (toggle_switch->inner_padding_pixels * 2);
const uint16_t inner_switchbox_x_loc = x_loc + toggle_switch->inner_padding_pixels;
const uint16_t inner_switchbox_y_loc = y_loc + toggle_switch->inner_padding_pixels;
const uint16_t inner_switchbox_width = height - (inner_padding_pixels * 2);
const uint16_t inner_switchbox_x_loc = x_loc + inner_padding_pixels;
const uint16_t inner_switchbox_y_loc = y_loc + inner_padding_pixels;
DrawBox(inner_switchbox_x_loc, inner_switchbox_y_loc, inner_switchbox_width, inner_switchbox_width, MAKE_PIXEL(255, 255, 255));
}
}

View File

@@ -7,7 +7,7 @@ static const uint16_t entry_height = 40;
static const uint16_t padding_x = 10;
static const uint16_t padding_y = 0;
static const pixel_t enabled_text_color = MAKE_PIXEL(255, 255, 255);
static const pixel_t disabled_text_color = MAKE_PIXEL(128, 128, 128);
static const pixel_t disabled_text_color = MAKE_PIXEL(80, 80, 80);
static const pixel_t entry_bg_color = MAKE_PIXEL(0, 0, 0);
static const pixel_t highlighted_bg_color = MAKE_PIXEL(50, 50, 50);
static const pixel_t highlighted_text_color = MAKE_PIXEL(255, 255, 0);
@@ -38,6 +38,18 @@ void draw_menu_entry(volatile pixel_t *const framebuffer, const graphical_menu_t
draw_string(framebuffer, &runescape_font,
padding_x, text_baseline_y,
entry->title, text_color);
if (entry->extra_draw_function != NULL)
{
const menu_entry_size_t menu_entry_size = {
.x = 0,
.y = y_pos,
.width = DISPLAY_WIDTH,
.height = entry_height,
};
entry->extra_draw_function(&menu_entry_size, entry->extra_draw_function_args);
}
}
void draw_menu(volatile pixel_t *const framebuffer, const graphical_menu_t *const menu)
@@ -54,7 +66,6 @@ void partial_redraw_menu(volatile pixel_t *const framebuffer, graphical_menu_t *
{
draw_menu_entry(framebuffer, menu, old_highlighted_entry_idx);
draw_menu_entry(framebuffer, menu, new_highlighted_entry_idx);
}
void select_menu_entry(graphical_menu_t *const menu)

View File

@@ -78,22 +78,26 @@ static graphical_menu_t *current_menu = &main_menu;
void ui_toggle_led1(void *const args)
{
LED1_TOGGLE();
ui_request_redraw();
}
void ui_toggle_led2(void *const args)
{
LED2_TOGGLE();
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 + width_padding);
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,
};
@@ -104,11 +108,13 @@ void ui_toggle_led2_switch(const menu_entry_size_t *const menu_entry_size, void
{
const uint16_t width_padding = 10;
const uint16_t height_padding = 10;
const uint16_t x = menu_entry_size->x + (menu_entry_size->width + width_padding);
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,
};