linux中应用程序控制led
1sch.高电平点亮
image.png
2驱动设备节点
image.png
3控制代码
#include <stdio.h>
//文件操作函数头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LED_NUM 2
#define LED_CMD 2
/*
cmd:0--->led off 1--->led on
io: 00-->靠近蜂鸣器的led 1---》靠近按键的led
*/
int main(int argc ,char **argv)
{
int fd,led_num,led_cmd;
char *led_path="/dev/leds";
printf("\n argv1 is cmd,argv2 is io\n");
printf("\ncmd:0--->led off 1--->led on\no: 00-->靠近蜂鸣器的led 1---》靠近按键的led\n");
printf("app name:%s\n",argv[0]);
led_cmd = atoi(argv[1]);
led_num =atoi(argv[2]);
if(led_cmd>LED_CMD)
{
printf("para1 len cmd err \n");
exit(1);
}
if(led_num>LED_NUM)
{
printf("para2 led err \n");
exit(1);
}
if((fd = open(led_path,O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
printf("open path %s err \n",led_path);
exit(1);
}
if((ioctl(fd,led_cmd,led_num))<0)
{
printf("led ioctl err \n");
}else
{
printf("led ioctl success \n");
}
close(fd);
return 0;
}
4控制命令
image.png
网友评论