1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
#!/bin/sh<br> base =led-player //定义一个变量方便引用led-player程序<br> # See how we were called. case "$1" in
//开关机时传递的参数,开机为start,关机为stop start) /usr/bin/$ base
& //开机运行该脚本时自动运行led-player于后台 ;; stop) //关机运行该脚本时终止led-player进程 pid=`/bin/pidof $ base ` //获取led-player进程号(反引号在shell中表示执行命令) if
[ -n "$pid"
]; then //若字符长度非0,则执行kill -9 pid(led-player进程号) kill -9 $pid fi ;; esac exit 0 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/select.h> #include <sys/time.h> #include <string.h> static
int led_fd; //静态变量,没有初始化则默认为0 static
int type = 1; static
void push_leds( void ) { static
unsigned step; unsigned led_bitmap; int
i; switch (type) { case
0: //当type变量值为0时,实现循环流水灯效果,即1-2-3-4-3-2-1如此循环 if
(step >= 6) { step = 0; } if
(step < 3) { led_bitmap = 1 << step; } else
{ led_bitmap = 1 << (6 - step); } break ; case
1: /*默认type变量值为1,实现累加器功能,即0000-0001-0010-0011-0100-0101...依次累加(1表示灯亮)*/ if
(step > 255) { step = 0; } led_bitmap = step; //此时led_bitmap为8位二进制数 break ; default : led_bitmap = 0; } step++; //每次调用执行本函数则step自加1 for
(i = 0; i < 4; i++) { ioctl(led_fd, led_bitmap & 1, i); //控制leds设备,分四次写入相应的值,估计i是写入驱动设备的位置参数 led_bitmap >>= 1; } } int
main( void ) { int
led_control_pipe; int
null_writer_fd; // for read endpoint not blocking when control process exit double
period = 0.5; led_fd = open( "/dev/leds0" , 0); //以只读方式打开设备leds0 if
(led_fd < 0) { led_fd = open( "/dev/leds" , 0); //mini2440开发板上的是这个设备 } if
(led_fd < 0) { perror( "open device leds" ); exit(1); } unlink( "/tmp/led-control" ); //删除指定文件 mkfifo( "/tmp/led-control" , 0666); //创建指定命名管道文件,权限为0666(首位0表示这个数为八进制数) led_control_pipe = open( "/tmp/led-control" , O_RDONLY | O_NONBLOCK); if
(led_control_pipe < 0) { perror( "open control pipe for read" ); exit(1); } null_writer_fd = open( "/tmp/led-control" , O_WRONLY | O_NONBLOCK); if
(null_writer_fd < 0) { perror( "open control pipe for write" ); exit(1); } for
(;;) { fd_set rds; struct
timeval step; int
ret; FD_ZERO(&rds); //将rds描述符集清零 FD_SET(led_control_pipe, &rds); //将rds描述符集中描述符led_control_pipe对应的位置1 step.tv_sec = period; step.tv_usec = (period - step.tv_sec) * 1000000L; ret = select (led_control_pipe + 1, &rds, NULL, NULL, &step); //只关心readfds描述符集,其他两个为NULL if
(ret < 0) { perror( "select" ); exit(1); } if
(ret == 0) { //指定等待时间已超过而指定的描述符还没准备好 push_leds(); } else
if (FD_ISSET(led_control_pipe, &rds)) { static
char buffer[200]; for
(;;) { char
c; int
len = strlen(buffer); if
(len >= sizeof
buffer - 1) { memset(buffer, 0, sizeof
buffer); //对buffer数组初始化 break ; } if
(read(led_control_pipe, &c, 1) != 1) { //从命名管道文件led-control中读取一个字节 break ; } if
(c == ‘\r‘ ) { //读出字符为回车符,则忽略后面的语句,重新循环 continue ; } if
(c == ‘\n‘ ) { //读出字符为换行符 int
tmp_type; double
tmp_period; if
(sscanf(buffer, "%d%lf" , &tmp_type, &tmp_period) == 2) { type = tmp_type; period = tmp_period; } fprintf(stderr, "type is %d, period is %lf\n" , type, period); //将参数type、period输出到终端显示 memset(buffer, 0, sizeof
buffer); //成功取完一次参数,将buffer数组清零 break ; } buffer[len] = c; //将读到的字符写入buffer数组(前面的程序已经实现舍弃回车符和换行符) } } } close(led_fd); //关掉leds设备 return
0; } |
mini2440应用例程学习(一)—— led-player,布布扣,bubuko.com
mini2440应用例程学习(一)—— led-player
原文:http://www.cnblogs.com/11hwu2/p/3714967.html