【嵌入式】利用FinSH控制台控制LED灯

点灯程序

要求:在FinSH控制台输入led 1/2/3 on/off 命令来控制stm32f407板子上的三盏LED灯暗灭

#include <rtthread.h>
#include <dfs_posix.h>
#include <board.h>

#define LED1_PIN    GET_PIN(C, 3)
#define LED2_PIN    GET_PIN(A, 5)
#define LED3_PIN    GET_PIN(A, 6)

static void led(int argc, char**argv)
{
    if (argc < 3)
    {
        rt_kprintf("Please input'led <1 off|1 on |2 off|2 on|3 off|3 on>'\n");
        return;
    }
    
    if (!rt_strcmp(argv[1], "1"))
    {
        if (!rt_strcmp(argv[2], "off"))
            rt_pin_write(LED1_PIN, PIN_LOW);
        else if (!rt_strcmp(argv[2], "on"))
            rt_pin_write(LED1_PIN, PIN_HIGH);
        else
            rt_kprintf("Please input'led <1 off|1 on |2 off|2 on|3 off|3 on>'\n");
    }
    else if (!rt_strcmp(argv[1], "2"))
    {
        if (!rt_strcmp(argv[2], "off"))
            rt_pin_write(LED2_PIN, PIN_LOW);
        else if (!rt_strcmp(argv[2], "on"))
            rt_pin_write(LED2_PIN, PIN_HIGH);
        else
            rt_kprintf("Please input'led <1 off|1 on |2 off|2 on|3 off|3 on>'\n");
    }    
    else if (!rt_strcmp(argv[1], "3"))
    {
        if (!rt_strcmp(argv[2], "off"))
            rt_pin_write(LED3_PIN, PIN_LOW);
        else if (!rt_strcmp(argv[2], "on"))
            rt_pin_write(LED3_PIN, PIN_HIGH);
        else
            rt_kprintf("Please input'led <1 off|1 on |2 off|2 on|3 off|3 on>'\n");
    }
    else
    {
        rt_kprintf("Please input'led <1 off|1 on |2 off|2 on|3 off|3 on>'\n");
    }
}

MSH_CMD_EXPORT(led, led sample: led <1 off|1 on |2 off|2 on|3 off|3 on>);
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐