[RTT例程练习] 6.2 在 Finsh 中运行自定义函数

可以直接在Finsh中运行自己定义的函数。官方有三种方式,我觉得第二种比较方便,其他就不介绍了。

首先需要在application.c 中包含 finsh.h 头文件,然后在函数下方添加宏

 
 
FINSH_FUNCTION_EXPORT ( fun_with_arg , function with a argument );
程序

#include <rtthread.h>

#include <finsh.h>

static rt_thread_t tid = RT_NULL;

static void thread_entry(void *parameter)
{
    while (1)
    {
        rt_thread_delay(1000);
    }
}

void fun_with_arg(int a)
{
    rt_kprintf("fun's arg is: %d.\n", a);
}
FINSH_FUNCTION_EXPORT(fun_with_arg, function with a argument);

int rt_application_init()
{

    
    tid = rt_thread_create("tid",
        thread_entry, RT_NULL,
        1024,
        8, 50);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    
    return 0;
}

可以看到这是一个带参的函数。

在Finsh中

finsh>>list() 
--Function List: 
fun_with_arg -- funciton with a argument 
list_mem -- list memory usage information 
hello -- say hello world 
version -- show RT-Thread version information 
list_thread -- list thread 
list_sem -- list semaphone in system 
list_event -- list event in system 
list_mutex -- list mutex in system 
list_mailbox -- list mail box in system 
list_msgqueue -- list message queue in system 
list_mempool -- list memory pool in system 
list_timer -- list timer in system 
list_device -- list device in system 
list -- list all symbol in system 
[l] fun 
--Variable List: 
dummy -- dummy variable for finsh 
0, 0x00000000 

可以看到,我们的
fun_with_arg
已经被添加其中。

然后试着在Finsh中运行

fun_with_arg(7)
就能得到

finsh>>fun_with_arg(7) 
fun's arg is: 7 
0, 0x00000000 

 
 

转载于:https://www.cnblogs.com/lyyyuna/archive/2013/04/07/4123912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值