Browse Source

改为带帮助信息的注册

master
Lizongdi 3 days ago
parent
commit
b8154fcf1a
  1. 17
      bspMCU/l_stm32.c
  2. 41
      lua/lua.c
  3. 12
      lua/lua_base.h

17
bspMCU/l_stm32.c

@ -743,25 +743,20 @@ int luaopen_stm32(lua_State *L)
#endif #endif
lua_setglobal(L, "st"); lua_setglobal(L, "st");
lua_add_help_entry("st", "STM32 hardware module (Pin, LED, delay, etc.)");
lua_pushcfunction(L, l_reboot); lua_register_help(L, "reboot", l_reboot, "Reset the system");
lua_setglobal(L, "reboot"); // reboot = l_reboot
#ifdef USE_FREERTOS #ifdef USE_FREERTOS
lua_pushcfunction(L, l_ps); lua_register_help(L, "ps", l_ps, "Show task list");
lua_setglobal(L, "ps"); // ps = l_ps lua_register_help(L, "free", l_free, "Show heap size");
lua_pushcfunction(L, l_free);
lua_setglobal(L, "free"); // ps = l_ps
#endif #endif
#ifdef USE_LWIP #ifdef USE_LWIP
lua_pushcfunction(L, l_lwip); lua_register_help(L, "lwip", l_lwip, "Display lwIP stats");
lua_setglobal(L, "lwip"); // lwip = l_lwip
#endif #endif
lua_pushcfunction(L, l_sendto); lua_register_help(L, "sendto", l_sendto, "Send msg: sendto(dst,id,data)");
lua_setglobal(L, "sendto"); // sendto = l_sendto
return 1; return 1;
} }

41
lua/lua.c

@ -17,6 +17,9 @@ static size_t len = 0;
static size_t cursor_pos = 0; static size_t cursor_pos = 0;
static int escape_mode = 0; // 0: 正常, 1: 收到 ESC, 2: 收到 ESC [ 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) static void add_to_history(const char *cmd)
{ {
if (cmd == NULL || strlen(cmd) == 0) return; 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); 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) static int l_name(lua_State *L)
{ {
lua_print("%s\n", Rd_GetBuildTime()); lua_print("%s\n", Rd_GetBuildTime());
@ -405,8 +441,8 @@ static int l_name(lua_State *L)
static int luaopen_default(lua_State *L) static int luaopen_default(lua_State *L)
{ {
lua_pushcfunction(L, l_name); lua_register_help(L, "help", l_help, "Show this help message");
lua_setglobal(L, "name"); // name = l_name lua_register_help(L, "name", l_name, "Show build time");
return 0; return 0;
} }
@ -415,6 +451,7 @@ int lua_init(lua_output _lua_print)
if (NULL == _lua_print) return -1; if (NULL == _lua_print) return -1;
lua_print = _lua_print; lua_print = _lua_print;
help_entry_count = 0;
buffer_len = 0; buffer_len = 0;
capacity = 256; capacity = 256;
len = 0; len = 0;

12
lua/lua_base.h

@ -79,6 +79,16 @@ extern "C"{
typedef void (*lua_output)(const char *fmt, ...); 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 * * project-wide global variables *
*----------------------------------------------*/ *----------------------------------------------*/
@ -91,6 +101,8 @@ extern lua_output lua_print;
int lua_init(lua_output _lua_print); int lua_init(lua_output _lua_print);
int lua_repl(char *_pcBuffer, int _iSize, int _iEcho); int lua_repl(char *_pcBuffer, int _iSize, int _iEcho);
void luaprint_stdout(const char *fmt, ...); 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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus

Loading…
Cancel
Save