Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120023
  • 博文数量: 24
  • 博客积分: 616
  • 博客等级: 中士
  • 技术积分: 375
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 10:54
文章分类
文章存档

2012年(24)

我的朋友

分类: LINUX

2012-06-27 10:48:59

共有四个按键,对应四个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) |
给主人留下些什么吧!~~