共有四个按键,对应四个LED,每按一个按键相应LED灯亮,其他LED灭
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
int buttons_fd;
int leds_fd;
char buttons[4] = {'0', '0', '0', '0',};
buttons_fd = open("/dev/keys_eint", 0);
leds_fd=open("/dev/LED_CONTROL",0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
if(leds_fd<0)
{
perror("open device leds");
}
while(1) {
char current_buttons[4];
int count_of_changed_key;
int i;
if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
perror("read buttons:");
exit(1);
}
for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
if (buttons[i] != current_buttons[i]) {
buttons[i] = current_buttons[i];
printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");
switch(i)
{
case 0:
ioctl(leds_fd,0);
break;
case 1:
ioctl(leds_fd,1);
break;
case 2:
ioctl(leds_fd,2);
break;
case 3:
ioctl(leds_fd,3);
break;
default:
break;
}
count_of_changed_key++;
}
}
if (count_of_changed_key) {
printf("\n");
}
}
close(buttons_fd);
close(leds_fd);
return 0;
}
下一步准备着手学习IIC的驱动和应用程序的编写
阅读(1628) | 评论(0) | 转发(0) |