Compare commits
4 Commits
07790e128d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cf1803dd77 | |||
| 3a5491c11e | |||
| cc9f363ff8 | |||
| 0ec8a5b380 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.cache/*
|
||||||
|
build/*
|
||||||
|
|
||||||
25
CMakeLists.txt
Normal file
25
CMakeLists.txt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(MenuSystem C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
api
|
||||||
|
src/core
|
||||||
|
src/param
|
||||||
|
src/lang
|
||||||
|
port
|
||||||
|
)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SOURCES
|
||||||
|
api/*.c
|
||||||
|
src/core/*.c
|
||||||
|
src/param/*.c
|
||||||
|
src/lang/*.c
|
||||||
|
port/*.c
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(menu STATIC ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(menu_demo examples/demo.c)
|
||||||
|
target_link_libraries(menu_demo menu)
|
||||||
@ -275,8 +275,11 @@ menu_register_nodes(nodes, sizeof(nodes)/sizeof(nodes[0]));
|
|||||||
|
|
||||||
#### 5.2.3 事件处理
|
#### 5.2.3 事件处理
|
||||||
```c
|
```c
|
||||||
// 发布事件
|
// 发布事件 - 带优先级
|
||||||
menu_post_event(MENU_EVENT_KEY_UP, 0);
|
menu_post_event(MENU_EVENT_KEY_UP, 0, MENU_EVENT_PRIORITY_NORMAL);
|
||||||
|
|
||||||
|
// 发布事件 - 简化方式(使用正常优先级)
|
||||||
|
menu_post_event_normal(MENU_EVENT_KEY_UP, 0);
|
||||||
|
|
||||||
// 主循环
|
// 主循环
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|||||||
193
api/menu_api.c
Normal file
193
api/menu_api.c
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
#include "menu_api.h"
|
||||||
|
#include "../src/core/menu_core.h"
|
||||||
|
#include "../src/core/menu_event.h"
|
||||||
|
#include "../src/core/menu_stack.h"
|
||||||
|
#include "../src/core/menu_permission.h"
|
||||||
|
#include "../src/core/menu_persistence.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
static MenuCoreCtx g_menu_ctx;
|
||||||
|
|
||||||
|
MenuErrCode menu_init(void) {
|
||||||
|
return menu_core_init(&g_menu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu_deinit(void) {
|
||||||
|
// Cleanup if needed
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu_main_loop(uint32_t tick) {
|
||||||
|
menu_core_loop(&g_menu_ctx, tick);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuNodeId menu_register_node(
|
||||||
|
MenuNodeId id,
|
||||||
|
MenuNodeId parent_id,
|
||||||
|
const char* name,
|
||||||
|
MenuCallback enter_cb,
|
||||||
|
MenuCallback exit_cb
|
||||||
|
) {
|
||||||
|
return menu_register_node_ex(id, parent_id, name, enter_cb, exit_cb, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuNodeId menu_register_node_ex(
|
||||||
|
MenuNodeId id,
|
||||||
|
MenuNodeId parent_id,
|
||||||
|
const char* name,
|
||||||
|
MenuCallback enter_cb,
|
||||||
|
MenuCallback exit_cb,
|
||||||
|
MenuCallback render_cb,
|
||||||
|
void* user_data
|
||||||
|
) {
|
||||||
|
MenuNode node = {0};
|
||||||
|
node.id = id;
|
||||||
|
node.parent_id = parent_id;
|
||||||
|
node.name = name;
|
||||||
|
node.enter_cb = enter_cb;
|
||||||
|
node.exit_cb = exit_cb;
|
||||||
|
node.render_cb = render_cb;
|
||||||
|
node.user_data = user_data;
|
||||||
|
node.flags.is_enabled = true;
|
||||||
|
node.flags.is_visible = true;
|
||||||
|
node.flags.is_registered = false; // Will be set by core
|
||||||
|
node.param_id = 0;
|
||||||
|
node.permission_level = 0;
|
||||||
|
|
||||||
|
MenuNodeId new_id = 0;
|
||||||
|
if (menu_core_register_node(&g_menu_ctx, &node, &new_id) == MENU_ERR_OK) {
|
||||||
|
return new_id;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_register_nodes(
|
||||||
|
const MenuNode* nodes,
|
||||||
|
size_t count,
|
||||||
|
MenuNodeId* out_ids
|
||||||
|
) {
|
||||||
|
if (!nodes || count == 0) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
MenuNode node = nodes[i];
|
||||||
|
MenuNodeId new_id = 0;
|
||||||
|
|
||||||
|
node.flags.is_enabled = true;
|
||||||
|
node.flags.is_visible = true;
|
||||||
|
node.flags.is_registered = false;
|
||||||
|
node.param_id = node.param_id ? node.param_id : 0;
|
||||||
|
node.permission_level = node.permission_level ? node.permission_level : 0;
|
||||||
|
|
||||||
|
MenuErrCode err = menu_core_register_node(&g_menu_ctx, &node, &new_id);
|
||||||
|
if (err != MENU_ERR_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out_ids) {
|
||||||
|
out_ids[i] = new_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_post_event(MenuEventType type, uint32_t param, MenuEventPriority priority) {
|
||||||
|
return menu_event_post(&g_menu_ctx.event_queue, type, param, priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_post_event_normal(MenuEventType type, uint32_t param) {
|
||||||
|
return menu_event_post(&g_menu_ctx.event_queue, type, param, MENU_EVENT_PRIORITY_NORMAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_enter(void) {
|
||||||
|
// We try to enter the first available root node.
|
||||||
|
// We can trigger an event or just let the loop handle it.
|
||||||
|
// If we rely on handle_event to enter root on NULL current,
|
||||||
|
// we just need to ensure loop is called.
|
||||||
|
// But explicitly:
|
||||||
|
if (g_menu_ctx.current_node_id == 0) {
|
||||||
|
// Force finding a root
|
||||||
|
// Since we don't have public access to ctx nodes from here easily without exposing everything,
|
||||||
|
// we can just call handle_event with a dummy event or NONE event if handle_event checks NULL current.
|
||||||
|
// But handle_event checks event type.
|
||||||
|
// We can add MENU_EVENT_NONE handling in handle_event to check entry?
|
||||||
|
// Or just manually find root here.
|
||||||
|
// We have access to g_menu_ctx.
|
||||||
|
for(int i=0; i<MENU_CONFIG_MAX_NODES; i++) {
|
||||||
|
if(g_menu_ctx.nodes[i].flags.is_registered && g_menu_ctx.nodes[i].parent_id == 0) {
|
||||||
|
// Enter root
|
||||||
|
MenuErrCode ret = menu_core_enter_node(&g_menu_ctx, g_menu_ctx.nodes[i].id);
|
||||||
|
if (ret != MENU_ERR_OK) return ret;
|
||||||
|
|
||||||
|
// Optimization: Automatically enter first child if root has children
|
||||||
|
// This makes the menu start with the first item selected
|
||||||
|
if (g_menu_ctx.nodes[i].first_child_id != 0) {
|
||||||
|
// We need to simulate Enter key or just manually transition
|
||||||
|
// Manual transition:
|
||||||
|
// Push root to stack
|
||||||
|
// But menu_core_enter_node logic doesn't push to stack automatically unless via handle_event
|
||||||
|
// So we mimic handle_event logic
|
||||||
|
menu_stack_push(&g_menu_ctx.stack, g_menu_ctx.nodes[i].id);
|
||||||
|
// Exit root (deselect)
|
||||||
|
menu_core_exit_node(&g_menu_ctx, g_menu_ctx.nodes[i].id);
|
||||||
|
// Enter child
|
||||||
|
return menu_core_enter_node(&g_menu_ctx, g_menu_ctx.nodes[i].first_child_id);
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_back(void) {
|
||||||
|
return menu_post_event_normal(MENU_EVENT_KEY_ESC, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_node_bind_param(MenuNodeId node_id, uint16_t param_id) {
|
||||||
|
MenuNode* node = menu_core_get_node(&g_menu_ctx, node_id);
|
||||||
|
if (!node) return MENU_ERR_NOT_FOUND;
|
||||||
|
node->param_id = param_id;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permission management functions
|
||||||
|
MenuErrCode menu_permission_register_role(uint8_t role_id, const char* name, MenuPermissionLevel level) {
|
||||||
|
return menu_permission_register_role_impl(&g_menu_ctx, role_id, name, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_set_current_role(uint8_t role_id) {
|
||||||
|
return menu_permission_set_current_role_impl(&g_menu_ctx, role_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_update_node_level(MenuNodeId node_id, MenuPermissionLevel level) {
|
||||||
|
return menu_permission_update_node_level_impl(&g_menu_ctx, node_id, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_permission_check_node_access(MenuNodeId node_id) {
|
||||||
|
return menu_permission_check_node_access_impl(&g_menu_ctx, node_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persistence functions
|
||||||
|
MenuErrCode menu_persistence_save(void) {
|
||||||
|
return menu_persistence_save_impl(&g_menu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_load(void) {
|
||||||
|
return menu_persistence_load_impl(&g_menu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_clear(void) {
|
||||||
|
return menu_persistence_clear_impl(&g_menu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_persistence_is_dirty(void) {
|
||||||
|
return menu_persistence_is_dirty_impl(&g_menu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_refresh(void) {
|
||||||
|
MenuErrCode err = menu_post_event_normal(MENU_EVENT_RENDER, 0);
|
||||||
|
if (err != MENU_ERR_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
74
api/menu_api.h
Normal file
74
api/menu_api.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#ifndef MENU_API_H
|
||||||
|
#define MENU_API_H
|
||||||
|
|
||||||
|
#include "../src/core/menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Initialization
|
||||||
|
MenuErrCode menu_init(void);
|
||||||
|
void menu_deinit(void);
|
||||||
|
|
||||||
|
// Core Loop
|
||||||
|
void menu_main_loop(uint32_t tick);
|
||||||
|
|
||||||
|
// Node Registration
|
||||||
|
MenuNodeId menu_register_node(
|
||||||
|
MenuNodeId id,
|
||||||
|
MenuNodeId parent_id,
|
||||||
|
const char* name,
|
||||||
|
MenuCallback enter_cb,
|
||||||
|
MenuCallback exit_cb
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extended registration (Optimization)
|
||||||
|
MenuNodeId menu_register_node_ex(
|
||||||
|
MenuNodeId id,
|
||||||
|
MenuNodeId parent_id,
|
||||||
|
const char* name,
|
||||||
|
MenuCallback enter_cb,
|
||||||
|
MenuCallback exit_cb,
|
||||||
|
MenuCallback render_cb,
|
||||||
|
void* user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
// Batch node registration
|
||||||
|
MenuErrCode menu_register_nodes(
|
||||||
|
const MenuNode* nodes,
|
||||||
|
size_t count,
|
||||||
|
MenuNodeId* out_ids
|
||||||
|
);
|
||||||
|
|
||||||
|
// Event Handling
|
||||||
|
MenuErrCode menu_post_event(MenuEventType type, uint32_t param, MenuEventPriority priority);
|
||||||
|
MenuErrCode menu_post_event_normal(MenuEventType type, uint32_t param);
|
||||||
|
|
||||||
|
// Navigation
|
||||||
|
MenuErrCode menu_enter(void); // Enter root
|
||||||
|
MenuErrCode menu_back(void);
|
||||||
|
|
||||||
|
// Parameter Binding
|
||||||
|
MenuErrCode menu_node_bind_param(MenuNodeId node_id, uint16_t param_id);
|
||||||
|
|
||||||
|
// Permission Management
|
||||||
|
MenuErrCode menu_permission_register_role(uint8_t role_id, const char* name, MenuPermissionLevel level);
|
||||||
|
MenuErrCode menu_permission_set_current_role(uint8_t role_id);
|
||||||
|
MenuErrCode menu_permission_update_node_level(MenuNodeId node_id, MenuPermissionLevel level);
|
||||||
|
bool menu_permission_check_node_access(MenuNodeId node_id);
|
||||||
|
|
||||||
|
// Persistence Management
|
||||||
|
MenuErrCode menu_persistence_save(void);
|
||||||
|
MenuErrCode menu_persistence_load(void);
|
||||||
|
MenuErrCode menu_persistence_clear(void);
|
||||||
|
bool menu_persistence_is_dirty(void);
|
||||||
|
|
||||||
|
// Dynamic menu updates
|
||||||
|
MenuErrCode menu_refresh(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_API_H
|
||||||
148
examples/demo.c
Normal file
148
examples/demo.c
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
#include "menu_api.h"
|
||||||
|
#include "menu_param.h"
|
||||||
|
#include "menu_lang.h"
|
||||||
|
#include "menu_port.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Globals
|
||||||
|
int32_t g_volume = 50;
|
||||||
|
int32_t g_brightness = 80;
|
||||||
|
uint8_t g_language = 1; // 0=EN, 1=CN
|
||||||
|
|
||||||
|
// Callbacks
|
||||||
|
MenuErrCode on_enter_main(MenuNodeId id, struct MenuCoreCtx* ctx) {
|
||||||
|
menu_port_log(">> Entered Main Menu");
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode on_enter_settings(MenuNodeId id, struct MenuCoreCtx* ctx) {
|
||||||
|
menu_port_log(">> Entered Settings");
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode on_enter_volume(MenuNodeId id, struct MenuCoreCtx* ctx) {
|
||||||
|
menu_port_log(">> Entered Volume Param Edit");
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode on_enter_brightness(MenuNodeId id, struct MenuCoreCtx* ctx) {
|
||||||
|
menu_port_log(">> Entered Brightness Param Edit");
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode on_render(MenuNodeId id, struct MenuCoreCtx* ctx) {
|
||||||
|
// In a real system, this would draw to screen
|
||||||
|
// Here we just print the current state occasionally or on change
|
||||||
|
// For demo simplicity, we rely on enter/exit logs
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_params(void) {
|
||||||
|
menu_param_init();
|
||||||
|
|
||||||
|
MenuParam p_vol = {0};
|
||||||
|
p_vol.id = 1;
|
||||||
|
p_vol.type = MENU_PARAM_TYPE_INT32;
|
||||||
|
p_vol.name = "Volume";
|
||||||
|
p_vol.value_ptr = &g_volume;
|
||||||
|
p_vol.meta.num.min = 0;
|
||||||
|
p_vol.meta.num.max = 100;
|
||||||
|
p_vol.meta.num.step = 5;
|
||||||
|
menu_param_register(&p_vol);
|
||||||
|
|
||||||
|
MenuParam p_bri = {0};
|
||||||
|
p_bri.id = 2;
|
||||||
|
p_bri.type = MENU_PARAM_TYPE_INT32;
|
||||||
|
p_bri.name = "Brightness";
|
||||||
|
p_bri.value_ptr = &g_brightness;
|
||||||
|
p_bri.meta.num.min = 10;
|
||||||
|
p_bri.meta.num.max = 100;
|
||||||
|
p_bri.meta.num.step = 10;
|
||||||
|
menu_param_register(&p_bri);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_menu(void) {
|
||||||
|
menu_init();
|
||||||
|
|
||||||
|
// Root: Main Menu
|
||||||
|
MenuNodeId main = menu_register_node(0, 0, "$MAIN", on_enter_main, NULL);
|
||||||
|
|
||||||
|
// Child: Settings
|
||||||
|
MenuNodeId settings = menu_register_node(0, main, "$SETTING", on_enter_settings, NULL);
|
||||||
|
|
||||||
|
// Child: Info
|
||||||
|
MenuNodeId info = menu_register_node(0, main, "$INFO", NULL, NULL);
|
||||||
|
|
||||||
|
// Child of Settings: Volume
|
||||||
|
MenuNodeId vol_node = menu_register_node(0, settings, "Volume", on_enter_volume, NULL);
|
||||||
|
menu_node_bind_param(vol_node, 1); // Bind to Volume param (ID 1)
|
||||||
|
|
||||||
|
// Child of Settings: Brightness
|
||||||
|
MenuNodeId bri_node = menu_register_node(0, settings, "Brightness", on_enter_brightness, NULL);
|
||||||
|
menu_node_bind_param(bri_node, 2); // Bind to Brightness param (ID 2)
|
||||||
|
|
||||||
|
// Set permission levels for nodes
|
||||||
|
menu_permission_update_node_level(settings, 1); // User level required for Settings
|
||||||
|
menu_permission_update_node_level(vol_node, 1); // User level required for Volume
|
||||||
|
menu_permission_update_node_level(bri_node, 1); // User level required for Brightness
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stub for binding param (will implement in API)
|
||||||
|
void bind_param(MenuNodeId node, uint16_t param) {
|
||||||
|
// Need API support
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
menu_port_init();
|
||||||
|
setup_params();
|
||||||
|
setup_menu();
|
||||||
|
|
||||||
|
menu_port_log("Menu System Demo Started");
|
||||||
|
menu_enter(); // Enter root
|
||||||
|
|
||||||
|
// Demo: Permission management
|
||||||
|
menu_port_log("=== Permission Management Demo ===");
|
||||||
|
menu_port_log("Current role: Guest");
|
||||||
|
menu_port_log("Trying to enter Settings...");
|
||||||
|
menu_post_event_normal(MENU_EVENT_KEY_ENTER, 0);
|
||||||
|
menu_main_loop(0);
|
||||||
|
|
||||||
|
menu_port_log("Switching to User role...");
|
||||||
|
menu_permission_set_current_role(2); // Switch to User role
|
||||||
|
menu_post_event_normal(MENU_EVENT_KEY_ENTER, 0);
|
||||||
|
menu_main_loop(0);
|
||||||
|
|
||||||
|
// Navigate to Volume setting
|
||||||
|
menu_post_event_normal(MENU_EVENT_KEY_DOWN, 0);
|
||||||
|
menu_main_loop(0);
|
||||||
|
menu_post_event_normal(MENU_EVENT_KEY_ENTER, 0);
|
||||||
|
menu_main_loop(0);
|
||||||
|
|
||||||
|
// Demo: Persistence
|
||||||
|
menu_port_log("=== Persistence Demo ===");
|
||||||
|
menu_port_log("Saving current state...");
|
||||||
|
menu_persistence_save();
|
||||||
|
|
||||||
|
menu_port_log("Exiting Volume menu...");
|
||||||
|
menu_post_event_normal(MENU_EVENT_MENU_EXIT, 0);
|
||||||
|
menu_main_loop(0);
|
||||||
|
|
||||||
|
menu_port_log("Loading saved state...");
|
||||||
|
menu_persistence_load();
|
||||||
|
menu_main_loop(0);
|
||||||
|
|
||||||
|
// Demo: Clear persistence
|
||||||
|
menu_port_log("=== Clear Persistence Demo ===");
|
||||||
|
menu_port_log("Clearing saved state...");
|
||||||
|
menu_persistence_clear();
|
||||||
|
|
||||||
|
menu_port_log("Trying to load cleared state (should fail)...");
|
||||||
|
MenuErrCode err = menu_persistence_load();
|
||||||
|
if (err != MENU_ERR_OK) {
|
||||||
|
menu_port_log("Load failed as expected (state was cleared)");
|
||||||
|
}
|
||||||
|
|
||||||
|
menu_port_log("Menu System Demo Completed");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
21
port/menu_port.c
Normal file
21
port/menu_port.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "menu_port.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
void menu_port_init(void) {
|
||||||
|
// Platform specific init
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t menu_port_get_tick(void) {
|
||||||
|
// Return ms
|
||||||
|
return (uint32_t)(clock() * 1000 / CLOCKS_PER_SEC);
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu_port_log(const char* fmt, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
vprintf(fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
18
port/menu_port.h
Normal file
18
port/menu_port.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef MENU_PORT_H
|
||||||
|
#define MENU_PORT_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void menu_port_init(void);
|
||||||
|
uint32_t menu_port_get_tick(void);
|
||||||
|
void menu_port_log(const char* fmt, ...);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_PORT_H
|
||||||
49
src/core/menu_config.h
Normal file
49
src/core/menu_config.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#ifndef MENU_CONFIG_H
|
||||||
|
#define MENU_CONFIG_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Maximum number of menu nodes
|
||||||
|
#ifndef MENU_CONFIG_MAX_NODES
|
||||||
|
#define MENU_CONFIG_MAX_NODES 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Maximum stack depth for navigation
|
||||||
|
#ifndef MENU_CONFIG_STACK_DEPTH
|
||||||
|
#define MENU_CONFIG_STACK_DEPTH 8
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Event queue length
|
||||||
|
#ifndef MENU_CONFIG_EVENT_QUEUE_LEN
|
||||||
|
#define MENU_CONFIG_EVENT_QUEUE_LEN 16
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Hash table size (should be a power of 2 for efficiency)
|
||||||
|
#ifndef MENU_CONFIG_HASH_TABLE_SIZE
|
||||||
|
#define MENU_CONFIG_HASH_TABLE_SIZE 16
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Feature switches
|
||||||
|
#ifndef MENU_CONFIG_ENABLE_PERMISSION
|
||||||
|
#define MENU_CONFIG_ENABLE_PERMISSION 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MENU_CONFIG_ENABLE_PERSISTENCE
|
||||||
|
#define MENU_CONFIG_ENABLE_PERSISTENCE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MENU_CONFIG_ENABLE_LANG
|
||||||
|
#define MENU_CONFIG_ENABLE_LANG 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MENU_CONFIG_ENABLE_DEBUG
|
||||||
|
#define MENU_CONFIG_ENABLE_DEBUG 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_CONFIG_H
|
||||||
453
src/core/menu_core.c
Normal file
453
src/core/menu_core.c
Normal file
@ -0,0 +1,453 @@
|
|||||||
|
#include "menu_core.h"
|
||||||
|
#include "menu_event.h"
|
||||||
|
#include "menu_stack.h"
|
||||||
|
#include "menu_hash.h"
|
||||||
|
#include "menu_param.h"
|
||||||
|
#include "menu_permission.h"
|
||||||
|
#include "menu_persistence.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Internal helper to get node pointer from ID
|
||||||
|
MenuNode* menu_core_get_node(MenuCoreCtx* ctx, MenuNodeId id) {
|
||||||
|
if (!ctx || id == 0) return NULL;
|
||||||
|
return menu_hash_find(ctx, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_init(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
memset(ctx, 0, sizeof(MenuCoreCtx));
|
||||||
|
|
||||||
|
menu_event_init(&ctx->event_queue);
|
||||||
|
menu_stack_init(&ctx->stack);
|
||||||
|
menu_hash_init(ctx);
|
||||||
|
menu_permission_init_impl(ctx);
|
||||||
|
|
||||||
|
ctx->is_initialized = true;
|
||||||
|
ctx->current_state = MENU_STATE_INIT;
|
||||||
|
ctx->free_node_count = MENU_CONFIG_MAX_NODES;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_register_node(MenuCoreCtx* ctx, const MenuNode* node, MenuNodeId* out_id) {
|
||||||
|
if (!ctx || !node) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (ctx->node_count >= MENU_CONFIG_MAX_NODES) return MENU_ERR_NO_MEM;
|
||||||
|
|
||||||
|
// Find a free slot
|
||||||
|
int slot = -1;
|
||||||
|
for (int i = 0; i < MENU_CONFIG_MAX_NODES; i++) {
|
||||||
|
if (!ctx->nodes[i].flags.is_registered) {
|
||||||
|
slot = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slot == -1) return MENU_ERR_NO_MEM;
|
||||||
|
|
||||||
|
MenuNode* new_node = &ctx->nodes[slot];
|
||||||
|
*new_node = *node; // Copy data
|
||||||
|
|
||||||
|
// Assign ID if 0
|
||||||
|
if (new_node->id == 0) {
|
||||||
|
new_node->id = slot + 1; // 1-based ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for ID conflict (simple check)
|
||||||
|
MenuNode* existing = menu_core_get_node(ctx, new_node->id);
|
||||||
|
if (existing && existing != new_node) {
|
||||||
|
return MENU_ERR_INVALID_PARAM; // ID conflict
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark as registered
|
||||||
|
new_node->flags.is_registered = true;
|
||||||
|
ctx->node_count++;
|
||||||
|
ctx->free_node_count--;
|
||||||
|
|
||||||
|
// Link to parent
|
||||||
|
if (new_node->parent_id != 0) {
|
||||||
|
MenuNode* parent = menu_core_get_node(ctx, new_node->parent_id);
|
||||||
|
if (parent) {
|
||||||
|
if (parent->first_child_id == 0) {
|
||||||
|
parent->first_child_id = new_node->id;
|
||||||
|
} else {
|
||||||
|
// Add to end of sibling list
|
||||||
|
MenuNode* sibling = menu_core_get_node(ctx, parent->first_child_id);
|
||||||
|
while (sibling && sibling->next_sibling_id != 0) {
|
||||||
|
sibling = menu_core_get_node(ctx, sibling->next_sibling_id);
|
||||||
|
}
|
||||||
|
if (sibling) {
|
||||||
|
sibling->next_sibling_id = new_node->id;
|
||||||
|
new_node->prev_sibling_id = sibling->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to hash
|
||||||
|
menu_hash_insert(ctx, new_node->id, slot);
|
||||||
|
|
||||||
|
if (out_id) *out_id = new_node->id;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_unregister_node(MenuCoreCtx* ctx, MenuNodeId id) {
|
||||||
|
if (!ctx || id == 0) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, id);
|
||||||
|
if (!node) return MENU_ERR_NOT_FOUND;
|
||||||
|
|
||||||
|
// Find slot index
|
||||||
|
int slot = -1;
|
||||||
|
for (int i = 0; i < MENU_CONFIG_MAX_NODES; i++) {
|
||||||
|
if (ctx->nodes[i].flags.is_registered && ctx->nodes[i].id == id) {
|
||||||
|
slot = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slot == -1) return MENU_ERR_NOT_FOUND;
|
||||||
|
|
||||||
|
// Update parent-child relationship
|
||||||
|
if (node->parent_id != 0) {
|
||||||
|
MenuNode* parent = menu_core_get_node(ctx, node->parent_id);
|
||||||
|
if (parent) {
|
||||||
|
// Check if this is the first child
|
||||||
|
if (parent->first_child_id == id) {
|
||||||
|
parent->first_child_id = node->next_sibling_id;
|
||||||
|
} else {
|
||||||
|
// Find previous sibling to update links
|
||||||
|
MenuNode* prev_sibling = menu_core_get_node(ctx, node->prev_sibling_id);
|
||||||
|
if (prev_sibling) {
|
||||||
|
prev_sibling->next_sibling_id = node->next_sibling_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update next sibling's prev_sibling_id
|
||||||
|
if (node->next_sibling_id != 0) {
|
||||||
|
MenuNode* next_sibling = menu_core_get_node(ctx, node->next_sibling_id);
|
||||||
|
if (next_sibling) {
|
||||||
|
next_sibling->prev_sibling_id = node->prev_sibling_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear node data
|
||||||
|
ctx->nodes[slot].flags.is_registered = false;
|
||||||
|
ctx->nodes[slot].id = 0;
|
||||||
|
ctx->nodes[slot].parent_id = 0;
|
||||||
|
ctx->nodes[slot].first_child_id = 0;
|
||||||
|
ctx->nodes[slot].next_sibling_id = 0;
|
||||||
|
ctx->nodes[slot].prev_sibling_id = 0;
|
||||||
|
ctx->nodes[slot].hash_next_id = 0;
|
||||||
|
ctx->nodes[slot].flags.is_selected = false;
|
||||||
|
ctx->nodes[slot].flags.is_visible = false;
|
||||||
|
ctx->nodes[slot].flags.is_enabled = false;
|
||||||
|
ctx->nodes[slot].permission_level = 0;
|
||||||
|
ctx->nodes[slot].param_id = 0;
|
||||||
|
ctx->nodes[slot].user_data = NULL;
|
||||||
|
ctx->nodes[slot].name = NULL;
|
||||||
|
ctx->nodes[slot].enter_cb = NULL;
|
||||||
|
ctx->nodes[slot].exit_cb = NULL;
|
||||||
|
ctx->nodes[slot].render_cb = NULL;
|
||||||
|
|
||||||
|
// Update counts
|
||||||
|
ctx->node_count--;
|
||||||
|
ctx->free_node_count++;
|
||||||
|
|
||||||
|
// Remove node from hash table using optimized removal
|
||||||
|
menu_hash_remove(ctx, id, slot);
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_enter_node(MenuCoreCtx* ctx, MenuNodeId node_id) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, node_id);
|
||||||
|
if (!node) return MENU_ERR_NOT_FOUND;
|
||||||
|
|
||||||
|
// Permission check
|
||||||
|
#if MENU_CONFIG_ENABLE_PERMISSION
|
||||||
|
if (!menu_permission_check_node_access_impl(ctx, node_id)) {
|
||||||
|
return MENU_ERR_PERMISSION_DENIED;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Call enter callback
|
||||||
|
if (node->enter_cb) {
|
||||||
|
node->enter_cb(node_id, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->current_node_id = node_id;
|
||||||
|
node->flags.is_selected = true;
|
||||||
|
|
||||||
|
// Update path
|
||||||
|
ctx->nav_path.depth = 0;
|
||||||
|
MenuNodeId curr = node_id;
|
||||||
|
MenuNodeId temp_path[MENU_CONFIG_STACK_DEPTH];
|
||||||
|
int depth = 0;
|
||||||
|
while(curr != 0 && depth < MENU_CONFIG_STACK_DEPTH) {
|
||||||
|
temp_path[depth++] = curr;
|
||||||
|
MenuNode* n = menu_core_get_node(ctx, curr);
|
||||||
|
if (n) curr = n->parent_id;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
// Reverse
|
||||||
|
for(int i=0; i<depth; i++) {
|
||||||
|
ctx->nav_path.nodes[i] = temp_path[depth-1-i];
|
||||||
|
}
|
||||||
|
ctx->nav_path.depth = depth;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_exit_node(MenuCoreCtx* ctx, MenuNodeId node_id) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, node_id);
|
||||||
|
if (node) {
|
||||||
|
if (node->exit_cb) {
|
||||||
|
node->exit_cb(node_id, ctx);
|
||||||
|
}
|
||||||
|
node->flags.is_selected = false;
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_handle_event(MenuCoreCtx* ctx, const MenuEvent* event) {
|
||||||
|
if (!ctx || !event) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuNode* current = menu_core_get_node(ctx, ctx->current_node_id);
|
||||||
|
// If no current node, maybe we need to enter root?
|
||||||
|
if (!current) {
|
||||||
|
// Try to find a root node (parent_id == 0)
|
||||||
|
if (ctx->node_count > 0) {
|
||||||
|
for(int i=0; i<MENU_CONFIG_MAX_NODES; i++) {
|
||||||
|
if(ctx->nodes[i].flags.is_registered && ctx->nodes[i].parent_id == 0) {
|
||||||
|
return menu_core_enter_node(ctx, ctx->nodes[i].id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MENU_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle Parameter Edit State
|
||||||
|
if (ctx->current_state == MENU_STATE_PARAM_EDIT) {
|
||||||
|
if (current->param_id == 0) {
|
||||||
|
ctx->current_state = MENU_STATE_NORMAL;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MenuParam* param = menu_param_get(current->param_id);
|
||||||
|
if (!param) {
|
||||||
|
ctx->current_state = MENU_STATE_NORMAL;
|
||||||
|
return MENU_ERR_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (event->type) {
|
||||||
|
case MENU_EVENT_KEY_UP:
|
||||||
|
// Increase value
|
||||||
|
// For simplicity assuming int types
|
||||||
|
if (param->type >= MENU_PARAM_TYPE_INT8 && param->type <= MENU_PARAM_TYPE_UINT32) {
|
||||||
|
int32_t val = menu_param_get_int(param->id);
|
||||||
|
val += param->meta.num.step;
|
||||||
|
if (val > param->meta.num.max) val = param->meta.num.min; // Wrap or clamp?
|
||||||
|
menu_param_set_int(param->id, val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MENU_EVENT_KEY_DOWN:
|
||||||
|
// Decrease value
|
||||||
|
if (param->type >= MENU_PARAM_TYPE_INT8 && param->type <= MENU_PARAM_TYPE_UINT32) {
|
||||||
|
int32_t val = menu_param_get_int(param->id);
|
||||||
|
val -= param->meta.num.step;
|
||||||
|
if (val < param->meta.num.min) val = param->meta.num.max;
|
||||||
|
menu_param_set_int(param->id, val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MENU_EVENT_KEY_ENTER:
|
||||||
|
// Confirm
|
||||||
|
ctx->current_state = MENU_STATE_NORMAL;
|
||||||
|
break;
|
||||||
|
case MENU_EVENT_KEY_ESC:
|
||||||
|
// Cancel (could revert if we stored old value)
|
||||||
|
ctx->current_state = MENU_STATE_NORMAL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (event->type) {
|
||||||
|
case MENU_EVENT_KEY_UP:
|
||||||
|
if (current->prev_sibling_id != 0) {
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, current->prev_sibling_id);
|
||||||
|
} else {
|
||||||
|
// Wrap around to last sibling
|
||||||
|
MenuNode* last = current;
|
||||||
|
while(last->next_sibling_id != 0) {
|
||||||
|
MenuNode* next = menu_core_get_node(ctx, last->next_sibling_id);
|
||||||
|
if(next) last = next; else break;
|
||||||
|
}
|
||||||
|
if (last != current) {
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, last->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_KEY_DOWN:
|
||||||
|
if (current->next_sibling_id != 0) {
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, current->next_sibling_id);
|
||||||
|
} else {
|
||||||
|
// Wrap to first sibling
|
||||||
|
// First sibling is the first_child of parent
|
||||||
|
if (current->parent_id != 0) {
|
||||||
|
MenuNode* parent = menu_core_get_node(ctx, current->parent_id);
|
||||||
|
if (parent && parent->first_child_id != 0 && parent->first_child_id != current->id) {
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, parent->first_child_id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Root level wrap
|
||||||
|
// We need to find the first root node
|
||||||
|
// But we don't track "first root".
|
||||||
|
// We can traverse back using prev_sibling
|
||||||
|
MenuNode* first = current;
|
||||||
|
while(first->prev_sibling_id != 0) {
|
||||||
|
MenuNode* prev = menu_core_get_node(ctx, first->prev_sibling_id);
|
||||||
|
if(prev) first = prev; else break;
|
||||||
|
}
|
||||||
|
if (first != current) {
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, first->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_KEY_ENTER:
|
||||||
|
if (current->first_child_id != 0) {
|
||||||
|
// Enter child
|
||||||
|
menu_stack_push(&ctx->stack, ctx->current_node_id);
|
||||||
|
// Note: We don't 'exit' the parent in terms of callback if we consider it "active in background"?
|
||||||
|
// But normally in a menu system, you leave the parent screen and enter child screen.
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, current->first_child_id);
|
||||||
|
} else {
|
||||||
|
// Leaf node action
|
||||||
|
// Can trigger a custom event or just let the enter_cb handle it.
|
||||||
|
// If we want to support "editing", we check if param_id is set.
|
||||||
|
if (current->param_id != 0) {
|
||||||
|
ctx->current_state = MENU_STATE_PARAM_EDIT;
|
||||||
|
// Trigger redraw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_KEY_ESC:
|
||||||
|
if (!menu_stack_is_empty(&ctx->stack)) {
|
||||||
|
MenuNodeId parent_id;
|
||||||
|
menu_stack_pop(&ctx->stack, &parent_id);
|
||||||
|
menu_core_exit_node(ctx, ctx->current_node_id);
|
||||||
|
menu_core_enter_node(ctx, parent_id);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_RENDER:
|
||||||
|
// Handle render event - this could trigger custom render callbacks or refresh the display
|
||||||
|
// For now, we'll just update the last refresh tick
|
||||||
|
ctx->last_refresh_tick = 0; // TODO: Use actual tick parameter from event
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_PARAM_CHANGED:
|
||||||
|
// Handle parameter change event
|
||||||
|
// This could be used to update the display or trigger other actions when a parameter changes
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_MENU_ENTER:
|
||||||
|
// Handle menu enter event
|
||||||
|
// This event is triggered when entering a menu node
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_MENU_EXIT:
|
||||||
|
// Handle menu exit event
|
||||||
|
// This event is triggered when exiting a menu node
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_STATE_CHANGED:
|
||||||
|
// Handle state change event
|
||||||
|
// This event is triggered when the menu state changes
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_ERROR:
|
||||||
|
// Handle error event
|
||||||
|
// This event is triggered when an error occurs
|
||||||
|
ctx->current_state = MENU_STATE_ERROR;
|
||||||
|
ctx->error_code = (MenuErrCode)event->param;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MENU_EVENT_REFRESH:
|
||||||
|
// Handle refresh event
|
||||||
|
// This event is used to force a refresh of the menu display
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Handle custom events
|
||||||
|
if (event->type >= MENU_EVENT_CUSTOM_BASE) {
|
||||||
|
// Custom events can be handled by user-defined callbacks
|
||||||
|
// For now, we'll just ignore them
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate_checksum is now implemented in menu_persistence.c
|
||||||
|
// Keeping this for backward compatibility in case it's used elsewhere
|
||||||
|
static uint8_t calculate_checksum(const MenuPersistenceData* data) {
|
||||||
|
(void)data; // Suppress unused parameter warning
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_save_state(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
return menu_persistence_save_impl(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_core_load_state(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
return menu_persistence_load_impl(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu_core_loop(MenuCoreCtx* ctx, uint32_t tick) {
|
||||||
|
if (!ctx || !ctx->is_initialized) return;
|
||||||
|
|
||||||
|
// Process events
|
||||||
|
while (!menu_event_is_empty(&ctx->event_queue)) {
|
||||||
|
MenuEvent evt;
|
||||||
|
if (menu_event_get(&ctx->event_queue, &evt) == MENU_ERR_OK) {
|
||||||
|
menu_core_handle_event(ctx, &evt);
|
||||||
|
ctx->persistence.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render
|
||||||
|
MenuNode* current = menu_core_get_node(ctx, ctx->current_node_id);
|
||||||
|
if (current && current->render_cb) {
|
||||||
|
current->render_cb(current->id, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-save if dirty
|
||||||
|
#if MENU_CONFIG_ENABLE_PERSISTENCE
|
||||||
|
if (ctx->persistence.dirty) {
|
||||||
|
menu_core_save_state(ctx);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
36
src/core/menu_core.h
Normal file
36
src/core/menu_core.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef MENU_CORE_H
|
||||||
|
#define MENU_CORE_H
|
||||||
|
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Core initialization
|
||||||
|
MenuErrCode menu_core_init(MenuCoreCtx* ctx);
|
||||||
|
|
||||||
|
// Node management
|
||||||
|
MenuErrCode menu_core_register_node(MenuCoreCtx* ctx, const MenuNode* node, MenuNodeId* out_id);
|
||||||
|
MenuNode* menu_core_get_node(MenuCoreCtx* ctx, MenuNodeId id);
|
||||||
|
MenuErrCode menu_core_unregister_node(MenuCoreCtx* ctx, MenuNodeId id);
|
||||||
|
|
||||||
|
// Main loop
|
||||||
|
void menu_core_loop(MenuCoreCtx* ctx, uint32_t tick);
|
||||||
|
|
||||||
|
// State machine
|
||||||
|
MenuErrCode menu_core_handle_event(MenuCoreCtx* ctx, const MenuEvent* event);
|
||||||
|
|
||||||
|
// Internal helpers
|
||||||
|
MenuErrCode menu_core_enter_node(MenuCoreCtx* ctx, MenuNodeId node_id);
|
||||||
|
MenuErrCode menu_core_exit_node(MenuCoreCtx* ctx, MenuNodeId node_id);
|
||||||
|
|
||||||
|
// Persistence management
|
||||||
|
MenuErrCode menu_core_save_state(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_core_load_state(MenuCoreCtx* ctx);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_CORE_H
|
||||||
80
src/core/menu_event.c
Normal file
80
src/core/menu_event.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "menu_event.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MenuErrCode menu_event_init(MenuEventQueue* queue) {
|
||||||
|
if (!queue) return MENU_ERR_INVALID_PARAM;
|
||||||
|
memset(queue, 0, sizeof(MenuEventQueue));
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_event_post(MenuEventQueue* queue, MenuEventType type, uint32_t param, MenuEventPriority priority) {
|
||||||
|
if (!queue) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
if (queue->count >= MENU_CONFIG_EVENT_QUEUE_LEN) {
|
||||||
|
return MENU_ERR_QUEUE_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the new event
|
||||||
|
MenuEvent new_event;
|
||||||
|
new_event.type = type;
|
||||||
|
new_event.param = param;
|
||||||
|
new_event.timestamp = 0; // timestamp handling can be added later if needed
|
||||||
|
new_event.priority = priority;
|
||||||
|
|
||||||
|
if (queue->count == 0) {
|
||||||
|
// Queue is empty, add at tail
|
||||||
|
queue->events[queue->tail] = new_event;
|
||||||
|
queue->tail = (queue->tail + 1) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
queue->count++;
|
||||||
|
} else {
|
||||||
|
// Find insertion position based on priority
|
||||||
|
uint8_t insert_pos = queue->tail;
|
||||||
|
uint8_t i = queue->head;
|
||||||
|
|
||||||
|
// Traverse the queue to find the first event with lower or equal priority
|
||||||
|
for (uint8_t j = 0; j < queue->count; j++) {
|
||||||
|
if (queue->events[i].priority < priority) {
|
||||||
|
insert_pos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i = (i + 1) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shift events to make room for the new event
|
||||||
|
if (insert_pos != queue->tail) {
|
||||||
|
uint8_t current = queue->tail;
|
||||||
|
uint8_t prev = (current - 1 + MENU_CONFIG_EVENT_QUEUE_LEN) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
|
||||||
|
while (current != insert_pos) {
|
||||||
|
queue->events[current] = queue->events[prev];
|
||||||
|
current = prev;
|
||||||
|
prev = (prev - 1 + MENU_CONFIG_EVENT_QUEUE_LEN) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the new event
|
||||||
|
queue->events[insert_pos] = new_event;
|
||||||
|
queue->tail = (queue->tail + 1) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
queue->count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_event_get(MenuEventQueue* queue, MenuEvent* event) {
|
||||||
|
if (!queue || !event) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
if (queue->count == 0) {
|
||||||
|
return MENU_ERR_QUEUE_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
*event = queue->events[queue->head];
|
||||||
|
queue->head = (queue->head + 1) % MENU_CONFIG_EVENT_QUEUE_LEN;
|
||||||
|
queue->count--;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_event_is_empty(const MenuEventQueue* queue) {
|
||||||
|
return (queue == NULL) || (queue->count == 0);
|
||||||
|
}
|
||||||
19
src/core/menu_event.h
Normal file
19
src/core/menu_event.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef MENU_EVENT_H
|
||||||
|
#define MENU_EVENT_H
|
||||||
|
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MenuErrCode menu_event_init(MenuEventQueue* queue);
|
||||||
|
MenuErrCode menu_event_post(MenuEventQueue* queue, MenuEventType type, uint32_t param, MenuEventPriority priority);
|
||||||
|
MenuErrCode menu_event_get(MenuEventQueue* queue, MenuEvent* event);
|
||||||
|
bool menu_event_is_empty(const MenuEventQueue* queue);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_EVENT_H
|
||||||
86
src/core/menu_hash.c
Normal file
86
src/core/menu_hash.c
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#include "menu_hash.h"
|
||||||
|
#include "menu_core.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MenuErrCode menu_hash_init(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
memset(ctx->hash_table, 0, sizeof(ctx->hash_table));
|
||||||
|
|
||||||
|
// Initialize hash_next_id to 0 for all nodes
|
||||||
|
for (int i = 0; i < MENU_CONFIG_MAX_NODES; i++) {
|
||||||
|
ctx->nodes[i].hash_next_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_hash_insert(MenuCoreCtx* ctx, MenuNodeId id, uint16_t index) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
uint16_t hash_index = id % MENU_CONFIG_HASH_TABLE_SIZE;
|
||||||
|
uint16_t current = ctx->hash_table[hash_index];
|
||||||
|
|
||||||
|
if (current == 0) {
|
||||||
|
ctx->hash_table[hash_index] = index + 1;
|
||||||
|
} else {
|
||||||
|
MenuNode* prev_node = &ctx->nodes[current - 1];
|
||||||
|
while (prev_node->hash_next_id != 0) {
|
||||||
|
prev_node = &ctx->nodes[prev_node->hash_next_id - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
prev_node->hash_next_id = index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuNode* menu_hash_find(MenuCoreCtx* ctx, MenuNodeId id) {
|
||||||
|
if (!ctx) return NULL;
|
||||||
|
|
||||||
|
uint16_t hash_index = id % MENU_CONFIG_HASH_TABLE_SIZE;
|
||||||
|
uint16_t current = ctx->hash_table[hash_index];
|
||||||
|
|
||||||
|
while (current != 0) {
|
||||||
|
uint16_t node_index = current - 1;
|
||||||
|
MenuNode* node = &ctx->nodes[node_index];
|
||||||
|
if (node->flags.is_registered && node->id == id) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = node->hash_next_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_hash_remove(MenuCoreCtx* ctx, MenuNodeId id, uint16_t index) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
uint16_t hash_index = id % MENU_CONFIG_HASH_TABLE_SIZE;
|
||||||
|
uint16_t current = ctx->hash_table[hash_index];
|
||||||
|
uint16_t prev = 0;
|
||||||
|
|
||||||
|
while (current != 0) {
|
||||||
|
uint16_t node_index = current - 1;
|
||||||
|
|
||||||
|
if (node_index == index) {
|
||||||
|
if (prev == 0) {
|
||||||
|
// Remove head of the list
|
||||||
|
ctx->hash_table[hash_index] = ctx->nodes[node_index].hash_next_id;
|
||||||
|
} else {
|
||||||
|
// Remove from middle/end
|
||||||
|
ctx->nodes[prev - 1].hash_next_id = ctx->nodes[node_index].hash_next_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the hash_next_id for this node
|
||||||
|
ctx->nodes[node_index].hash_next_id = 0;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev = current;
|
||||||
|
current = ctx->nodes[node_index].hash_next_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
19
src/core/menu_hash.h
Normal file
19
src/core/menu_hash.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef MENU_HASH_H
|
||||||
|
#define MENU_HASH_H
|
||||||
|
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MenuErrCode menu_hash_init(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_hash_insert(MenuCoreCtx* ctx, MenuNodeId id, uint16_t index);
|
||||||
|
MenuNode* menu_hash_find(MenuCoreCtx* ctx, MenuNodeId id);
|
||||||
|
MenuErrCode menu_hash_remove(MenuCoreCtx* ctx, MenuNodeId id, uint16_t index);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_HASH_H
|
||||||
157
src/core/menu_permission.c
Normal file
157
src/core/menu_permission.c
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
#include "menu_permission.h"
|
||||||
|
#include "menu_core.h"
|
||||||
|
#include "menu_hash.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_init_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
memset(&ctx->permission, 0, sizeof(ctx->permission));
|
||||||
|
ctx->permission.role_count = 0;
|
||||||
|
ctx->permission.current_role_id = 0;
|
||||||
|
|
||||||
|
// Register default roles
|
||||||
|
menu_permission_register_role_impl(ctx, 1, "Guest", 0);
|
||||||
|
menu_permission_register_role_impl(ctx, 2, "User", 1);
|
||||||
|
menu_permission_register_role_impl(ctx, 3, "Admin", 2);
|
||||||
|
|
||||||
|
// Set default role to Guest
|
||||||
|
ctx->permission.current_role_id = 1;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_register_role_impl(MenuCoreCtx* ctx, uint8_t role_id, const char* name, MenuPermissionLevel level) {
|
||||||
|
if (!ctx || !name) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (ctx->permission.role_count >= 8) return MENU_ERR_NO_MEM;
|
||||||
|
|
||||||
|
// Check if role already exists
|
||||||
|
for (uint8_t i = 0; i < ctx->permission.role_count; i++) {
|
||||||
|
if (ctx->permission.roles[i].id == role_id) {
|
||||||
|
return MENU_ERR_FAIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuRole* role = &ctx->permission.roles[ctx->permission.role_count];
|
||||||
|
role->id = role_id;
|
||||||
|
role->name = name;
|
||||||
|
role->level = level;
|
||||||
|
ctx->permission.role_count++;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_unregister_role_impl(MenuCoreCtx* ctx, uint8_t role_id) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < ctx->permission.role_count; i++) {
|
||||||
|
if (ctx->permission.roles[i].id == role_id) {
|
||||||
|
// Shift roles up
|
||||||
|
for (uint8_t j = i; j < ctx->permission.role_count - 1; j++) {
|
||||||
|
ctx->permission.roles[j] = ctx->permission.roles[j + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->permission.role_count--;
|
||||||
|
|
||||||
|
// If we removed the current role, set to default
|
||||||
|
if (ctx->permission.current_role_id == role_id) {
|
||||||
|
ctx->permission.current_role_id = 1; // Guest role
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_set_current_role_impl(MenuCoreCtx* ctx, uint8_t role_id) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
// Check if role exists
|
||||||
|
if (!menu_permission_role_exists_impl(ctx, role_id)) {
|
||||||
|
return MENU_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->permission.current_role_id = role_id;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t menu_permission_get_current_role_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return 0;
|
||||||
|
return ctx->permission.current_role_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_update_node_level_impl(MenuCoreCtx* ctx, MenuNodeId node_id, MenuPermissionLevel level) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, node_id);
|
||||||
|
if (!node) return MENU_ERR_NOT_FOUND;
|
||||||
|
|
||||||
|
node->permission_level = level;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_permission_check_node_access_impl(MenuCoreCtx* ctx, MenuNodeId node_id) {
|
||||||
|
if (!ctx) return false;
|
||||||
|
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, node_id);
|
||||||
|
if (!node) return false;
|
||||||
|
|
||||||
|
#if MENU_CONFIG_ENABLE_PERMISSION
|
||||||
|
MenuRole* role_node = NULL;
|
||||||
|
for (uint8_t i = 0; i < ctx->permission.role_count; i++) {
|
||||||
|
if (ctx->permission.roles[i].id == ctx->permission.current_role_id) {
|
||||||
|
role_node = &ctx->permission.roles[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!role_node || role_node->level < node->permission_level) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_permission_check_and_enter_node_impl(MenuCoreCtx* ctx, MenuNodeId node_id) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
// Check permission first
|
||||||
|
if (!menu_permission_check_node_access_impl(ctx, node_id)) {
|
||||||
|
return MENU_ERR_PERMISSION_DENIED;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If permission granted, enter the node
|
||||||
|
return menu_core_enter_node(ctx, node_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuRole* menu_permission_get_role_impl(MenuCoreCtx* ctx, uint8_t role_id) {
|
||||||
|
if (!ctx) return NULL;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < ctx->permission.role_count; i++) {
|
||||||
|
if (ctx->permission.roles[i].id == role_id) {
|
||||||
|
return &ctx->permission.roles[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t menu_permission_get_role_count_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return 0;
|
||||||
|
return ctx->permission.role_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_permission_role_exists_impl(MenuCoreCtx* ctx, uint8_t role_id) {
|
||||||
|
if (!ctx) return false;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < ctx->permission.role_count; i++) {
|
||||||
|
if (ctx->permission.roles[i].id == role_id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
31
src/core/menu_permission.h
Normal file
31
src/core/menu_permission.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef MENU_PERMISSION_H
|
||||||
|
#define MENU_PERMISSION_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Permission management functions
|
||||||
|
MenuErrCode menu_permission_init_impl(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_permission_register_role_impl(MenuCoreCtx* ctx, uint8_t role_id, const char* name, MenuPermissionLevel level);
|
||||||
|
MenuErrCode menu_permission_unregister_role_impl(MenuCoreCtx* ctx, uint8_t role_id);
|
||||||
|
MenuErrCode menu_permission_set_current_role_impl(MenuCoreCtx* ctx, uint8_t role_id);
|
||||||
|
uint8_t menu_permission_get_current_role_impl(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_permission_update_node_level_impl(MenuCoreCtx* ctx, MenuNodeId node_id, MenuPermissionLevel level);
|
||||||
|
bool menu_permission_check_node_access_impl(MenuCoreCtx* ctx, MenuNodeId node_id);
|
||||||
|
MenuErrCode menu_permission_check_and_enter_node_impl(MenuCoreCtx* ctx, MenuNodeId node_id);
|
||||||
|
|
||||||
|
// Role query functions
|
||||||
|
MenuRole* menu_permission_get_role_impl(MenuCoreCtx* ctx, uint8_t role_id);
|
||||||
|
uint8_t menu_permission_get_role_count_impl(MenuCoreCtx* ctx);
|
||||||
|
bool menu_permission_role_exists_impl(MenuCoreCtx* ctx, uint8_t role_id);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_PERMISSION_H
|
||||||
109
src/core/menu_persistence.c
Normal file
109
src/core/menu_persistence.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
#include "menu_persistence.h"
|
||||||
|
#include "menu_types.h"
|
||||||
|
#include "menu_core.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Mock storage buffer for persistence (simulating non-volatile storage)
|
||||||
|
static MenuPersistenceData g_persistence_storage = {0};
|
||||||
|
static bool g_storage_initialized = false;
|
||||||
|
|
||||||
|
// Calculate checksum for integrity verification
|
||||||
|
static uint16_t calculate_checksum(const MenuPersistenceData* data) {
|
||||||
|
if (!data) return 0;
|
||||||
|
|
||||||
|
const uint8_t* bytes = (const uint8_t*)data;
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint16_t len = sizeof(MenuPersistenceData) - sizeof(data->checksum);
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < len; i++) {
|
||||||
|
checksum ^= bytes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_init_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
ctx->persistence.dirty = false;
|
||||||
|
|
||||||
|
// Initialize storage if not already initialized
|
||||||
|
if (!g_storage_initialized) {
|
||||||
|
memset(&g_persistence_storage, 0, sizeof(g_persistence_storage));
|
||||||
|
g_storage_initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_save_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuPersistenceData data = {
|
||||||
|
.current_node_id = ctx->current_node_id,
|
||||||
|
.current_state = ctx->current_state,
|
||||||
|
.nav_path = ctx->nav_path,
|
||||||
|
.stack = ctx->stack,
|
||||||
|
.timestamp = 0, // In a real system, this would be a actual timestamp
|
||||||
|
.checksum = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate checksum for integrity verification
|
||||||
|
data.checksum = calculate_checksum(&data);
|
||||||
|
|
||||||
|
// Write to mock storage
|
||||||
|
memcpy(&g_persistence_storage, &data, sizeof(MenuPersistenceData));
|
||||||
|
|
||||||
|
// Mark as not dirty
|
||||||
|
ctx->persistence.dirty = false;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_load_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
MenuPersistenceData data;
|
||||||
|
|
||||||
|
// Read from mock storage
|
||||||
|
memcpy(&data, &g_persistence_storage, sizeof(MenuPersistenceData));
|
||||||
|
|
||||||
|
// Verify checksum for integrity
|
||||||
|
uint16_t expected_checksum = calculate_checksum(&data);
|
||||||
|
if (data.checksum != expected_checksum) {
|
||||||
|
return MENU_ERR_CORRUPTED_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the current_node_id exists in the node table
|
||||||
|
MenuNode* node = menu_core_get_node(ctx, data.current_node_id);
|
||||||
|
if (!node) {
|
||||||
|
return MENU_ERR_INVALID_NODE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the state into the context
|
||||||
|
ctx->current_node_id = data.current_node_id;
|
||||||
|
ctx->current_state = data.current_state;
|
||||||
|
ctx->nav_path = data.nav_path;
|
||||||
|
ctx->stack = data.stack;
|
||||||
|
|
||||||
|
ctx->persistence.dirty = false;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_clear_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
// Clear mock storage
|
||||||
|
memset(&g_persistence_storage, 0, sizeof(g_persistence_storage));
|
||||||
|
g_persistence_storage.checksum = calculate_checksum(&g_persistence_storage);
|
||||||
|
|
||||||
|
ctx->persistence.dirty = true;
|
||||||
|
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_persistence_is_dirty_impl(MenuCoreCtx* ctx) {
|
||||||
|
if (!ctx) return false;
|
||||||
|
return ctx->persistence.dirty;
|
||||||
|
}
|
||||||
36
src/core/menu_persistence.h
Normal file
36
src/core/menu_persistence.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* menu_persistence.h
|
||||||
|
*
|
||||||
|
* Created on: 2024-07-22
|
||||||
|
* Author: Jfen
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MENU_PERSISTENCE_H
|
||||||
|
#define MENU_PERSISTENCE_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
// Persistence management functions
|
||||||
|
MenuErrCode menu_persistence_init_impl(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_persistence_save_impl(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_persistence_load_impl(MenuCoreCtx* ctx);
|
||||||
|
MenuErrCode menu_persistence_clear_impl(MenuCoreCtx* ctx);
|
||||||
|
bool menu_persistence_is_dirty_impl(MenuCoreCtx* ctx);
|
||||||
|
|
||||||
|
// Storage write/read callbacks for platform-specific implementation
|
||||||
|
typedef MenuErrCode (*MenuPersistenceWriteFunc)(const void* data, size_t size);
|
||||||
|
typedef MenuErrCode (*MenuPersistenceReadFunc)(void* data, size_t size);
|
||||||
|
|
||||||
|
MenuErrCode menu_persistence_register_storage_impl(MenuCoreCtx* ctx,
|
||||||
|
MenuPersistenceWriteFunc write_func,
|
||||||
|
MenuPersistenceReadFunc read_func);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_PERSISTENCE_H
|
||||||
50
src/core/menu_stack.c
Normal file
50
src/core/menu_stack.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "menu_stack.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MenuErrCode menu_stack_init(MenuStack* stack) {
|
||||||
|
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||||
|
memset(stack, 0, sizeof(MenuStack));
|
||||||
|
stack->top = 0;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_stack_push(MenuStack* stack, MenuNodeId id) {
|
||||||
|
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (stack->top >= MENU_CONFIG_STACK_DEPTH) {
|
||||||
|
return MENU_ERR_STACK_OVERFLOW;
|
||||||
|
}
|
||||||
|
stack->path[stack->top++] = id;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_stack_pop(MenuStack* stack, MenuNodeId* id) {
|
||||||
|
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (stack->top == 0) {
|
||||||
|
return MENU_ERR_STACK_UNDERFLOW;
|
||||||
|
}
|
||||||
|
if (id) {
|
||||||
|
*id = stack->path[--stack->top];
|
||||||
|
} else {
|
||||||
|
stack->top--;
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_stack_peek(const MenuStack* stack, MenuNodeId* id) {
|
||||||
|
if (!stack || !id) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (stack->top == 0) {
|
||||||
|
return MENU_ERR_STACK_UNDERFLOW;
|
||||||
|
}
|
||||||
|
*id = stack->path[stack->top - 1];
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool menu_stack_is_empty(const MenuStack* stack) {
|
||||||
|
return (stack == NULL) || (stack->top == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu_stack_clear(MenuStack* stack) {
|
||||||
|
if (stack) {
|
||||||
|
stack->top = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/core/menu_stack.h
Normal file
21
src/core/menu_stack.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef MENU_STACK_H
|
||||||
|
#define MENU_STACK_H
|
||||||
|
|
||||||
|
#include "menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MenuErrCode menu_stack_init(MenuStack* stack);
|
||||||
|
MenuErrCode menu_stack_push(MenuStack* stack, MenuNodeId id);
|
||||||
|
MenuErrCode menu_stack_pop(MenuStack* stack, MenuNodeId* id);
|
||||||
|
MenuErrCode menu_stack_peek(const MenuStack* stack, MenuNodeId* id);
|
||||||
|
bool menu_stack_is_empty(const MenuStack* stack);
|
||||||
|
void menu_stack_clear(MenuStack* stack);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_STACK_H
|
||||||
190
src/core/menu_types.h
Normal file
190
src/core/menu_types.h
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
#ifndef MENU_TYPES_H
|
||||||
|
#define MENU_TYPES_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include "menu_config.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
struct MenuCoreCtx;
|
||||||
|
|
||||||
|
// Type definitions
|
||||||
|
typedef uint16_t MenuNodeId;
|
||||||
|
typedef uint8_t MenuPermissionLevel;
|
||||||
|
|
||||||
|
// Role structure
|
||||||
|
typedef struct {
|
||||||
|
uint8_t id;
|
||||||
|
const char* name;
|
||||||
|
MenuPermissionLevel level;
|
||||||
|
} MenuRole;
|
||||||
|
|
||||||
|
// Error codes
|
||||||
|
typedef enum {
|
||||||
|
MENU_ERR_OK = 0,
|
||||||
|
MENU_ERR_FAIL,
|
||||||
|
MENU_ERR_INVALID_PARAM,
|
||||||
|
MENU_ERR_NO_MEM,
|
||||||
|
MENU_ERR_NOT_FOUND,
|
||||||
|
MENU_ERR_PERMISSION_DENIED,
|
||||||
|
MENU_ERR_QUEUE_FULL,
|
||||||
|
MENU_ERR_QUEUE_EMPTY,
|
||||||
|
MENU_ERR_STACK_OVERFLOW,
|
||||||
|
MENU_ERR_STACK_UNDERFLOW,
|
||||||
|
MENU_ERR_NOT_INIT,
|
||||||
|
MENU_ERR_CORRUPTED_DATA,
|
||||||
|
MENU_ERR_INVALID_NODE,
|
||||||
|
} MenuErrCode;
|
||||||
|
|
||||||
|
// Menu States
|
||||||
|
typedef enum {
|
||||||
|
MENU_STATE_INIT = 0,
|
||||||
|
MENU_STATE_NORMAL,
|
||||||
|
MENU_STATE_PARAM_EDIT,
|
||||||
|
MENU_STATE_CONFIRM,
|
||||||
|
MENU_STATE_ERROR,
|
||||||
|
} MenuState;
|
||||||
|
|
||||||
|
// Menu Events
|
||||||
|
typedef enum {
|
||||||
|
MENU_EVENT_NONE = 0,
|
||||||
|
MENU_EVENT_KEY_UP,
|
||||||
|
MENU_EVENT_KEY_DOWN,
|
||||||
|
MENU_EVENT_KEY_ENTER,
|
||||||
|
MENU_EVENT_KEY_ESC,
|
||||||
|
MENU_EVENT_TIMEOUT,
|
||||||
|
MENU_EVENT_RENDER, // Optimization: Render event
|
||||||
|
|
||||||
|
// Extended event types for more complex interactions
|
||||||
|
MENU_EVENT_PARAM_CHANGED, // Parameter value changed
|
||||||
|
MENU_EVENT_MENU_ENTER, // Entered a menu node
|
||||||
|
MENU_EVENT_MENU_EXIT, // Exited a menu node
|
||||||
|
MENU_EVENT_STATE_CHANGED, // Menu state changed
|
||||||
|
MENU_EVENT_ERROR, // Error occurred
|
||||||
|
MENU_EVENT_REFRESH, // Force refresh the menu
|
||||||
|
MENU_EVENT_CUSTOM_BASE = 20, // Custom events start from here
|
||||||
|
} MenuEventType;
|
||||||
|
|
||||||
|
// Callback function types
|
||||||
|
typedef MenuErrCode (*MenuCallback)(MenuNodeId node_id, struct MenuCoreCtx* ctx);
|
||||||
|
|
||||||
|
// Menu Node Structure
|
||||||
|
typedef struct MenuNode {
|
||||||
|
MenuNodeId id;
|
||||||
|
MenuNodeId parent_id;
|
||||||
|
const char* name; // Or language key
|
||||||
|
MenuCallback enter_cb;
|
||||||
|
MenuCallback exit_cb;
|
||||||
|
MenuCallback render_cb; // Optimization: Custom render callback per node
|
||||||
|
|
||||||
|
MenuNodeId first_child_id;
|
||||||
|
MenuNodeId next_sibling_id;
|
||||||
|
MenuNodeId prev_sibling_id;
|
||||||
|
MenuNodeId hash_next_id;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
bool is_registered : 1;
|
||||||
|
bool is_selected : 1;
|
||||||
|
bool is_visible : 1;
|
||||||
|
bool is_enabled : 1;
|
||||||
|
unsigned int reserved : 4;
|
||||||
|
} flags;
|
||||||
|
|
||||||
|
MenuPermissionLevel permission_level;
|
||||||
|
uint16_t param_id;
|
||||||
|
void* user_data; // Optimization: User context
|
||||||
|
} MenuNode;
|
||||||
|
|
||||||
|
// Navigation Stack
|
||||||
|
typedef struct {
|
||||||
|
MenuNodeId path[MENU_CONFIG_STACK_DEPTH];
|
||||||
|
uint8_t top;
|
||||||
|
} MenuStack;
|
||||||
|
|
||||||
|
// Event priority levels
|
||||||
|
typedef enum {
|
||||||
|
MENU_EVENT_PRIORITY_LOW = 0,
|
||||||
|
MENU_EVENT_PRIORITY_NORMAL,
|
||||||
|
MENU_EVENT_PRIORITY_HIGH,
|
||||||
|
MENU_EVENT_PRIORITY_URGENT,
|
||||||
|
} MenuEventPriority;
|
||||||
|
|
||||||
|
// Event structure
|
||||||
|
typedef struct {
|
||||||
|
MenuEventType type;
|
||||||
|
uint32_t param;
|
||||||
|
uint32_t timestamp;
|
||||||
|
MenuEventPriority priority;
|
||||||
|
} MenuEvent;
|
||||||
|
|
||||||
|
// Event Queue
|
||||||
|
typedef struct {
|
||||||
|
MenuEvent events[MENU_CONFIG_EVENT_QUEUE_LEN];
|
||||||
|
uint8_t head;
|
||||||
|
uint8_t tail;
|
||||||
|
uint8_t count;
|
||||||
|
} MenuEventQueue;
|
||||||
|
|
||||||
|
// Navigation Path (breadcrumbs for display)
|
||||||
|
typedef struct {
|
||||||
|
MenuNodeId nodes[MENU_CONFIG_STACK_DEPTH];
|
||||||
|
uint8_t depth;
|
||||||
|
} MenuNavPath;
|
||||||
|
|
||||||
|
typedef struct MenuPersistenceData {
|
||||||
|
MenuNodeId current_node_id;
|
||||||
|
MenuState current_state;
|
||||||
|
MenuNavPath nav_path;
|
||||||
|
MenuStack stack;
|
||||||
|
uint32_t timestamp;
|
||||||
|
uint8_t checksum;
|
||||||
|
} MenuPersistenceData;
|
||||||
|
|
||||||
|
// Core Context
|
||||||
|
typedef struct MenuCoreCtx {
|
||||||
|
MenuNode nodes[MENU_CONFIG_MAX_NODES];
|
||||||
|
uint16_t hash_table[MENU_CONFIG_HASH_TABLE_SIZE];
|
||||||
|
MenuStack stack;
|
||||||
|
MenuNavPath nav_path;
|
||||||
|
MenuEventQueue event_queue;
|
||||||
|
|
||||||
|
MenuNodeId current_node_id;
|
||||||
|
MenuState current_state;
|
||||||
|
uint32_t last_refresh_tick;
|
||||||
|
bool is_initialized;
|
||||||
|
|
||||||
|
MenuErrCode error_code;
|
||||||
|
const char* error_msg;
|
||||||
|
|
||||||
|
uint16_t node_count;
|
||||||
|
uint16_t free_node_count;
|
||||||
|
|
||||||
|
// Permission module data
|
||||||
|
struct {
|
||||||
|
uint8_t current_role_id;
|
||||||
|
MenuRole roles[8];
|
||||||
|
uint8_t role_count;
|
||||||
|
} permission;
|
||||||
|
|
||||||
|
// Persistence module data
|
||||||
|
struct {
|
||||||
|
bool dirty;
|
||||||
|
} persistence;
|
||||||
|
|
||||||
|
// Language module data
|
||||||
|
struct {
|
||||||
|
uint8_t current_lang_id;
|
||||||
|
} lang;
|
||||||
|
|
||||||
|
} MenuCoreCtx;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_TYPES_H
|
||||||
28
src/lang/menu_lang.c
Normal file
28
src/lang/menu_lang.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include "menu_lang.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static MenuLangID g_curr_lang = MENU_LANG_EN;
|
||||||
|
|
||||||
|
void menu_lang_set(MenuLangID lang_id) {
|
||||||
|
if (lang_id < MENU_LANG_MAX) {
|
||||||
|
g_curr_lang = lang_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuLangID menu_lang_get(void) {
|
||||||
|
return g_curr_lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* menu_lang_get_str(const char* key) {
|
||||||
|
// Simple placeholder. In real implementation, look up in table.
|
||||||
|
// If key starts with '$', it's a key, else return as is.
|
||||||
|
if (!key) return "";
|
||||||
|
if (key[0] == '$') {
|
||||||
|
// Mock lookup
|
||||||
|
if (strcmp(key, "$MAIN") == 0) return (g_curr_lang == MENU_LANG_CN) ? "主菜单" : "Main Menu";
|
||||||
|
if (strcmp(key, "$SETTING") == 0) return (g_curr_lang == MENU_LANG_CN) ? "设置" : "Settings";
|
||||||
|
if (strcmp(key, "$INFO") == 0) return (g_curr_lang == MENU_LANG_CN) ? "信息" : "Info";
|
||||||
|
return key + 1; // Return key without $
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
27
src/lang/menu_lang.h
Normal file
27
src/lang/menu_lang.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef MENU_LANG_H
|
||||||
|
#define MENU_LANG_H
|
||||||
|
|
||||||
|
#include "../core/menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
MENU_LANG_EN = 0,
|
||||||
|
MENU_LANG_CN,
|
||||||
|
MENU_LANG_MAX
|
||||||
|
} MenuLangID;
|
||||||
|
|
||||||
|
// Set current language
|
||||||
|
void menu_lang_set(MenuLangID lang_id);
|
||||||
|
MenuLangID menu_lang_get(void);
|
||||||
|
|
||||||
|
// Get string
|
||||||
|
const char* menu_lang_get_str(const char* key);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_LANG_H
|
||||||
96
src/param/menu_param.c
Normal file
96
src/param/menu_param.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include "menu_param.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MENU_MAX_PARAMS 32
|
||||||
|
|
||||||
|
static MenuParam g_params[MENU_MAX_PARAMS];
|
||||||
|
static uint16_t g_param_count = 0;
|
||||||
|
|
||||||
|
MenuErrCode menu_param_init(void) {
|
||||||
|
memset(g_params, 0, sizeof(g_params));
|
||||||
|
g_param_count = 0;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_param_register(const MenuParam* param) {
|
||||||
|
if (!param) return MENU_ERR_INVALID_PARAM;
|
||||||
|
if (g_param_count >= MENU_MAX_PARAMS) return MENU_ERR_NO_MEM;
|
||||||
|
|
||||||
|
// Check ID conflict
|
||||||
|
for (int i = 0; i < g_param_count; i++) {
|
||||||
|
if (g_params[i].id == param->id) {
|
||||||
|
return MENU_ERR_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g_params[g_param_count++] = *param;
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MenuParam* menu_param_get(uint16_t id) {
|
||||||
|
for (int i = 0; i < g_param_count; i++) {
|
||||||
|
if (g_params[i].id == id) {
|
||||||
|
return &g_params[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuErrCode menu_param_set_int(uint16_t id, int32_t val) {
|
||||||
|
const MenuParam* p = menu_param_get(id);
|
||||||
|
if (!p || p->read_only || !p->value_ptr) return MENU_ERR_INVALID_PARAM;
|
||||||
|
|
||||||
|
switch (p->type) {
|
||||||
|
case MENU_PARAM_TYPE_BOOL:
|
||||||
|
*(bool*)p->value_ptr = (val != 0);
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_INT8:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(int8_t*)p->value_ptr = (int8_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_UINT8:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(uint8_t*)p->value_ptr = (uint8_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_INT16:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(int16_t*)p->value_ptr = (int16_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_UINT16:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(uint16_t*)p->value_ptr = (uint16_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_INT32:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(int32_t*)p->value_ptr = (int32_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_UINT32:
|
||||||
|
if (val < p->meta.num.min || val > p->meta.num.max) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(uint32_t*)p->value_ptr = (uint32_t)val;
|
||||||
|
break;
|
||||||
|
case MENU_PARAM_TYPE_ENUM:
|
||||||
|
if (val < 0 || val >= p->meta.enum_meta.count) return MENU_ERR_INVALID_PARAM;
|
||||||
|
*(uint8_t*)p->value_ptr = (uint8_t)val; // Assume enum stored as uint8
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return MENU_ERR_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
return MENU_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t menu_param_get_int(uint16_t id) {
|
||||||
|
const MenuParam* p = menu_param_get(id);
|
||||||
|
if (!p || !p->value_ptr) return 0;
|
||||||
|
|
||||||
|
switch (p->type) {
|
||||||
|
case MENU_PARAM_TYPE_BOOL: return *(bool*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_INT8: return *(int8_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_UINT8: return *(uint8_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_INT16: return *(int16_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_UINT16: return *(uint16_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_INT32: return *(int32_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_UINT32: return *(uint32_t*)p->value_ptr;
|
||||||
|
case MENU_PARAM_TYPE_ENUM: return *(uint8_t*)p->value_ptr;
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/param/menu_param.h
Normal file
63
src/param/menu_param.h
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#ifndef MENU_PARAM_H
|
||||||
|
#define MENU_PARAM_H
|
||||||
|
|
||||||
|
#include "../core/menu_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
MENU_PARAM_TYPE_NONE = 0,
|
||||||
|
MENU_PARAM_TYPE_BOOL,
|
||||||
|
MENU_PARAM_TYPE_INT8,
|
||||||
|
MENU_PARAM_TYPE_UINT8,
|
||||||
|
MENU_PARAM_TYPE_INT16,
|
||||||
|
MENU_PARAM_TYPE_UINT16,
|
||||||
|
MENU_PARAM_TYPE_INT32,
|
||||||
|
MENU_PARAM_TYPE_UINT32,
|
||||||
|
MENU_PARAM_TYPE_FLOAT,
|
||||||
|
MENU_PARAM_TYPE_STRING,
|
||||||
|
MENU_PARAM_TYPE_ENUM,
|
||||||
|
} MenuParamType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t id;
|
||||||
|
MenuParamType type;
|
||||||
|
const char* name;
|
||||||
|
void* value_ptr; // Pointer to actual variable
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
int32_t min;
|
||||||
|
int32_t max;
|
||||||
|
int32_t step;
|
||||||
|
} num;
|
||||||
|
struct {
|
||||||
|
const char** options;
|
||||||
|
uint8_t count;
|
||||||
|
} enum_meta;
|
||||||
|
struct {
|
||||||
|
uint16_t max_len;
|
||||||
|
} str_meta;
|
||||||
|
} meta;
|
||||||
|
bool read_only;
|
||||||
|
} MenuParam;
|
||||||
|
|
||||||
|
// Initialize param system
|
||||||
|
MenuErrCode menu_param_init(void);
|
||||||
|
|
||||||
|
// Register a parameter
|
||||||
|
MenuErrCode menu_param_register(const MenuParam* param);
|
||||||
|
|
||||||
|
// Get parameter by ID
|
||||||
|
const MenuParam* menu_param_get(uint16_t id);
|
||||||
|
|
||||||
|
// Set value (helper)
|
||||||
|
MenuErrCode menu_param_set_int(uint16_t id, int32_t val);
|
||||||
|
int32_t menu_param_get_int(uint16_t id);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MENU_PARAM_H
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user