diff --git a/bspMCU/l_stm32.c b/bspMCU/l_stm32.c index 230cac7..06cc3ed 100644 --- a/bspMCU/l_stm32.c +++ b/bspMCU/l_stm32.c @@ -743,25 +743,20 @@ int luaopen_stm32(lua_State *L) #endif lua_setglobal(L, "st"); + lua_add_help_entry("st", "STM32 hardware module (Pin, LED, delay, etc.)"); - lua_pushcfunction(L, l_reboot); - lua_setglobal(L, "reboot"); // reboot = l_reboot + lua_register_help(L, "reboot", l_reboot, "Reset the system"); #ifdef USE_FREERTOS - lua_pushcfunction(L, l_ps); - lua_setglobal(L, "ps"); // ps = l_ps - - lua_pushcfunction(L, l_free); - lua_setglobal(L, "free"); // ps = l_ps + lua_register_help(L, "ps", l_ps, "Show task list"); + lua_register_help(L, "free", l_free, "Show heap size"); #endif #ifdef USE_LWIP - lua_pushcfunction(L, l_lwip); - lua_setglobal(L, "lwip"); // lwip = l_lwip + lua_register_help(L, "lwip", l_lwip, "Display lwIP stats"); #endif - lua_pushcfunction(L, l_sendto); - lua_setglobal(L, "sendto"); // sendto = l_sendto + lua_register_help(L, "sendto", l_sendto, "Send msg: sendto(dst,id,data)"); return 1; } diff --git a/lua/lua.c b/lua/lua.c index f74dfeb..04b3a43 100644 --- a/lua/lua.c +++ b/lua/lua.c @@ -17,6 +17,9 @@ static size_t len = 0; static size_t cursor_pos = 0; static int escape_mode = 0; // 0: 正常, 1: 收到 ESC, 2: 收到 ESC [ +static lua_help_entry_t help_entries[MAX_HELP_ENTRIES]; +static int help_entry_count = 0; + static void add_to_history(const char *cmd) { if (cmd == NULL || strlen(cmd) == 0) return; @@ -397,6 +400,39 @@ static char *readline(const char *_pcPrompt, char *_pcBuffer, int _iSize, int _i extern int luaopen_stm32(lua_State *L); +void lua_register_help(lua_State *L, const char *name, lua_CFunction func, const char *description) +{ + lua_pushcfunction(L, func); + lua_setglobal(L, name); + + if (help_entry_count < MAX_HELP_ENTRIES) + { + help_entries[help_entry_count].name = name; + help_entries[help_entry_count].description = description; + help_entry_count++; + } +} + +void lua_add_help_entry(const char *name, const char *description) +{ + if (help_entry_count < MAX_HELP_ENTRIES) + { + help_entries[help_entry_count].name = name; + help_entries[help_entry_count].description = description; + help_entry_count++; + } +} + +static int l_help(lua_State *L) +{ + lua_print("Available commands:\n"); + for (int i = 0; i < help_entry_count; i++) + { + lua_print(" %-16s %s\n", help_entries[i].name, help_entries[i].description); + } + return 0; +} + static int l_name(lua_State *L) { lua_print("%s\n", Rd_GetBuildTime()); @@ -405,8 +441,8 @@ static int l_name(lua_State *L) static int luaopen_default(lua_State *L) { - lua_pushcfunction(L, l_name); - lua_setglobal(L, "name"); // name = l_name + lua_register_help(L, "help", l_help, "Show this help message"); + lua_register_help(L, "name", l_name, "Show build time"); return 0; } @@ -415,6 +451,7 @@ int lua_init(lua_output _lua_print) if (NULL == _lua_print) return -1; lua_print = _lua_print; + help_entry_count = 0; buffer_len = 0; capacity = 256; len = 0; diff --git a/lua/lua_base.h b/lua/lua_base.h index c7a0770..9a85c49 100644 --- a/lua/lua_base.h +++ b/lua/lua_base.h @@ -79,6 +79,16 @@ extern "C"{ typedef void (*lua_output)(const char *fmt, ...); +/*==============================================* + * help system definitions * + *----------------------------------------------*/ +#define MAX_HELP_ENTRIES 32 + +typedef struct { + const char *name; + const char *description; +} lua_help_entry_t; + /*==============================================* * project-wide global variables * *----------------------------------------------*/ @@ -91,6 +101,8 @@ extern lua_output lua_print; int lua_init(lua_output _lua_print); int lua_repl(char *_pcBuffer, int _iSize, int _iEcho); void luaprint_stdout(const char *fmt, ...); +void lua_register_help(lua_State *L, const char *name, lua_CFunction func, const char *description); +void lua_add_help_entry(const char *name, const char *description); #ifdef __cplusplus #if __cplusplus