运行该命令来停止led闪动
/etc/rc.d/init.d/leds stop
led
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<time.h>
int main() {
int fd,i;
fd=open("/dev/leds",O_WRONLY);
if(fd<0) {
printf("Open leds error!\n");
exit(1);
}
for(i=0;i<4;i++) ioctl(fd,0,i);
for(;;) {
for(i=0;i<4;i++)
ioctl(fd,0,i);
for(i=0;i<4;i++) {
ioctl(fd,1,i);
sleep(1);
}
}
close(fd);
return 0;
}
button
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<fcntl.h>
int main() {
int fd,i;
char key[4]={'0'};
fd=open("/dev/buttons",O_RDONLY);
if(fd<0) {
printf("Open buttons error!\n");
exit(1);
}
for(;;) {
read(fd,key,sizeof(key));
for(i=0;i<4;i++) {
usleep(50000);
for(i=0;i<4;i++)
if(key[i]=='1') printf("key%d\n",i+1);
}
}
close(fd);
return 0;
}
pwm
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<time.h>
int main() {
int fd,frq=2000;
fd=open("/dev/pwm",O_RDONLY);
if(fd<0) {
printf("Open pwm error!\n");
exit(1);
}
ioctl(fd,1,frq);
sleep(3);
ioctl(fd,0);
close(fd);
return 0;
}
Ctrl + C 结束进程
Ctrl + Z 暂停进程
改良
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main(void) {
int fd;
char key[4]={'0'};
fd = open("/dev/buttons",O_RDONLY);
if (fd<0) {
printf("ERROR\n");
exit(1);
}
for (;;) {
char now[4];
int cnt=0;
int i;
read(fd,now,sizeof now);
for (i=0,cnt=0;i<4;i++) {
if (key[i]!=now[i]) {
key[i] = now[i];
printf("key%d is ",i+1);
if(key[i]=='1') printf("down");
else printf("up");
cnt++;
}
}
if (cnt) printf("\n");
}
close(fd);
return 0;
}