第29节:led 灯
1、编写运行编译例程:
root@ubuntu:/home/linuxsystemcode/led# cat led.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LED_NUM 2
#define LED_C 2
//cmd is zero(off),cmd is one(on)
//io is zero Near peak ringing device,io is one Close to independent keystroke lamp
int main(int argc,char *argv[]){
int fd,led_num,led_c;
led_c = LED_C;
printf("argv1 is cmd;argv2 is io\n");
if(atoi(argv[1])>led_c){
printf("argv1 is 0 or 1\n");
exit(1);
}
if(atoi(argv[1])>led_num){
printf("argv2 is 0 or 1\n");
exit(1);
}
if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s failed\n",leds);
}
else{
ioctl(fd,atoi(argv[1],atoi(argv[2])));
printf("iocti %s success\n",leds);
}
close(fd);
return 0;
}
2、进行文件编译
root@ubuntu:/home/linuxsystemcode/led# arm-none-linux-gnueabi-gcc -o led led.c -static
root@ubuntu:/home/linuxsystemcode/led# ls
led led.c
3、将文件拷贝到TF卡上,并传入开发板上运行结果
Please press Enter to activate this console.
[root@iTOP-4412]# mount /dev/mmcblk1p1 /mnt
[root@iTOP-4412]# cd /mnt
[root@iTOP-4412]# ls
System Volume Information creat.c read
argvc led read.c
argvc.c led.c write
creat open write.c
[root@iTOP-4412]# ./led
argv1 is cmd;argv2 is io
4、 0为灭
[root@iTOP-4412]# ./led 0 0
argv1 is cmd;argv[ 409.659981] LEDS_CTL DEBUG:Device Opened Success!
[ 409.664876] debug: leds_ioctl cmd is 0
[ 409.668605] LEDS_CTL DEBUG:Device Closed Success!
2 is io
1、编写运行编译例程:
root@ubuntu:/home/linuxsystemcode/led# cat led.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LED_NUM 2
#define LED_C 2
//cmd is zero(off),cmd is one(on)
//io is zero Near peak ringing device,io is one Close to independent keystroke lamp
int main(int argc,char *argv[]){
int fd,led_num,led_c;
char *leds ="/dev/leds";
led_num = LED_NUM;led_c = LED_C;
printf("argv1 is cmd;argv2 is io\n");
if(atoi(argv[1])>led_c){
printf("argv1 is 0 or 1\n");
exit(1);
}
if(atoi(argv[1])>led_num){
printf("argv2 is 0 or 1\n");
exit(1);
}
if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s failed\n",leds);
}
else{
ioctl(fd,atoi(argv[1],atoi(argv[2])));
printf("iocti %s success\n",leds);
}
close(fd);
return 0;
}
2、进行文件编译
root@ubuntu:/home/linuxsystemcode/led# arm-none-linux-gnueabi-gcc -o led led.c -static
root@ubuntu:/home/linuxsystemcode/led# ls
led led.c
3、将文件拷贝到TF卡上,并传入开发板上运行结果
Please press Enter to activate this console.
[root@iTOP-4412]# mount /dev/mmcblk1p1 /mnt
[root@iTOP-4412]# cd /mnt
[root@iTOP-4412]# ls
System Volume Information creat.c read
argvc led read.c
argvc.c led.c write
creat open write.c
[root@iTOP-4412]# ./led
argv1 is cmd;argv2 is io
4、 0为灭
[root@iTOP-4412]# ./led 0 0
argv1 is cmd;argv[ 409.659981] LEDS_CTL DEBUG:Device Opened Success!
[ 409.664876] debug: leds_ioctl cmd is 0
[ 409.668605] LEDS_CTL DEBUG:Device Closed Success!
2 is io
iocti /dev/leds success