Skip to content

Commit

Permalink
feat: general submenus
Browse files Browse the repository at this point in the history
  • Loading branch information
Otrebor671 committed Sep 9, 2024
1 parent 0f3e292 commit 17c4d1c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion firmware/main/general/general_screens.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ void genera_screen_display_card_information(char* title, char* body) {

void genera_screen_display_notify_information(char* title, char* body) {
general_clear_screen();
general_screen_display_breadcrumb();
// general_screen_display_breadcrumb();
int page = ITEM_PAGE_OFFSET;
oled_screen_display_card_border();
oled_screen_display_text_center(title, page, OLED_DISPLAY_NORMAL);
page++;
if (strlen(body) > MAX_LINE_CHAR) {
Expand Down
80 changes: 80 additions & 0 deletions firmware/main/general/general_submenu/general_submenu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "general_submenu.h"

#include "bitmaps_general.h"
#include "menus_module.h"
#include "oled_screen.h"

#ifdef CONFIG_RESOLUTION_128X64
#define MAX_OPTIONS_NUM 8
#else // CONFIG_RESOLUTION_128X32
#define MAX_OPTIONS_NUM 4
#endif

static general_submenu_menu_t* general_radio_selection_ctx;

static void list_submenu_options() {
general_submenu_menu_t* ctx = general_radio_selection_ctx;
static uint8_t items_offset = 0;
items_offset = MAX(ctx->selected_option - MAX_OPTIONS_NUM + 2, items_offset);
items_offset =
MIN(MAX(ctx->options_count - MAX_OPTIONS_NUM + 2, 0), items_offset);
items_offset = MIN(ctx->selected_option, items_offset);
oled_screen_clear_buffer();
char* str = malloc(20);
for (uint8_t i = 0; i < (MIN(ctx->options_count, MAX_OPTIONS_NUM - 1)); i++) {
bool is_selected = i + items_offset == ctx->selected_option;
sprintf(str, "%s", ctx->options[i + items_offset]);
oled_screen_display_text(str, 0, i + 1, is_selected);
}
oled_screen_display_show();
free(str);
}

static void input_cb(uint8_t button_name, uint8_t button_event) {
if (button_event != BUTTON_PRESS_DOWN) {
return;
}
switch (button_name) {
case BUTTON_LEFT:
void (*exit_cb)() = general_radio_selection_ctx->exit_cb;
free(general_radio_selection_ctx);
if (exit_cb) {
exit_cb();
}
break;
case BUTTON_RIGHT:
void (*select_cb)() = general_radio_selection_ctx->select_cb;
if (select_cb) {
select_cb(general_radio_selection_ctx->selected_option);
}
break;
case BUTTON_UP:
general_radio_selection_ctx->selected_option =
general_radio_selection_ctx->selected_option == 0
? general_radio_selection_ctx->options_count - 1
: general_radio_selection_ctx->selected_option - 1;
list_submenu_options();
break;
case BUTTON_DOWN:
general_radio_selection_ctx->selected_option =
++general_radio_selection_ctx->selected_option <
general_radio_selection_ctx->options_count
? general_radio_selection_ctx->selected_option
: 0;
list_submenu_options();
break;
default:
break;
}
}

void general_submenu(general_submenu_menu_t radio_selection_menu) {
general_radio_selection_ctx = calloc(1, sizeof(general_submenu_menu_t));
general_radio_selection_ctx->options = radio_selection_menu.options;
general_radio_selection_ctx->options_count =
radio_selection_menu.options_count;
general_radio_selection_ctx->select_cb = radio_selection_menu.select_cb;
general_radio_selection_ctx->exit_cb = radio_selection_menu.exit_cb;
menus_module_set_app_state(true, input_cb);
list_submenu_options();
}
17 changes: 17 additions & 0 deletions firmware/main/general/general_submenu/general_submenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdbool.h>
#include <stdio.h>
#include "general_screens.h"

typedef void (*submenu_selection_handler_t)(uint8_t);

typedef struct {
uint8_t options_count;
char** options;
uint8_t selected_option;
submenu_selection_handler_t* select_cb;
void* exit_cb;
} general_submenu_menu_t;

void general_submenu(general_submenu_menu_t radio_selection_menu);
2 changes: 1 addition & 1 deletion firmware/main/modules/menus_module/menus_screens.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ void menus_screens_display_menus_f(menus_manager_t* ctx) {

#ifdef CONFIG_RESOLUTION_128X64
oled_screen_display_selected_item_box();
oled_screen_display_show();
#endif
oled_screen_display_show();
}

0 comments on commit 17c4d1c

Please sign in to comment.