This article refers to "RT-Thread Programming Manual" https://www.bookstack.cn/read/rtthread-manual-doc/10.6.md
Article directory
How to add commands
When using FinSH, the system provides some very useful commands, such as:
In fact, we can also add some commands to msh ourselves, as follows:
MSH_CMD_EXPORT(command, desc);
command is the command (function name) to be added, and desc is the description of the command.
simple test
Let's use a simple my_hello
command to test, and add the following code to the project:
void my_hello(void)
{
rt_kprintf("hello world\n");
}
MSH_CMD_EXPORT(my_hello, msh cmd test);
Enter the help command on the msh command line to see that our my_hello
command has been added to the command list:
Enter the my_hello
command and the my_hello()
function runs successfully:
variadic command
Functions can generally take parameters, and the msh command can also take parameters, and msh also supports commands that add variable parameters. The defined format is:
void my_cmd(int argc, char **argv)
Similarly, let's write a simple test example: (remember to open MicroLIB)
#include <string.h>
#include <stdio.h>
void led_ctrl(int argc, char **argv)
{
if(argc != 3)
{
rt_kprintf("正确格式:led_ctrl LEDx ON/OFF\n");
return;
}
if(!strcmp(argv[1] , "LED0"))
{
if(!strcmp(argv[2] , "ON"))
{
rt_kprintf("led0 on!\n");
}
else if(!strcmp(argv[2] , "OFF"))
{
rt_kprintf("led0 off!\n");
}
}
else if(!strcmp(argv[1] , "LED1"))
{
if(!strcmp(argv[2] , "ON"))
{
rt_kprintf("led1 on!\n");
}
else if(!strcmp(argv[2] , "OFF"))
{
rt_kprintf("led1 off!\n");
}
}
}
MSH_CMD_EXPORT(led_ctrl, "LEDx ON/OFF"); // 不加双引号,ON/OFF 会显示为1/0
Test results: (different parameters, different running content)