zoukankan      html  css  js  c++  java
  • [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 

    
    

  • 相关阅读:
    布隆过滤器(Bloom Filter) 未完待续
    [面试]future模式
    R语言入门(2)-数据对象
    R语言入门(1)-初识R语言
    [面试] Java GC (未整理完)
    [面试]StringBuilder StringBuffer源码粘贴(并非源码分析, 请绕道)
    [面试]synchronized
    [面试]volatile类型修饰符/内存屏障/处理器缓存
    [面试]死锁-最简单的死锁demo
    [面试]Actor模型
  • 原文地址:https://www.cnblogs.com/lyyyuna/p/4123912.html
Copyright © 2011-2022 走看看