#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define button_one 0x1
#define button_two 0x2
#define button_three 0x4
#define button_four 0x8
#define PLATDRV_MAGIC 0x60
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)
#define LED_BLINK _IO (PLATDRV_MAGIC, 0x1A)
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int i;
int j=1;
int button_fd;
int led_fd;
int ret=0;
int button_status;
fd_set rds;
button_fd=open("/dev/button",O_RDWR);
led_fd=open("/dev/led",O_RDWR);
if(button_fd<0)
{
printf("open device button false");
exit(1);
}
if(led_fd<0)
{
printf("open device led false");
exit(1);
}
while(1)
{
FD_ZERO(&rds);// 清空文件文件描述符 ,每次循环都要清空集合,否则不能检测描述符变化
FD_SET(button_fd,&rds);// 将该文件描述符,添加到文件描述符集中,
// 是系统调用select检查是否能从/dev/button 设备读取数据
ret=select(button_fd+1,&rds,NULL,NULL,NULL); //设置为阻塞模式
if(ret==-1)
{
printf("select error\n");
exit(-1);
}
if(ret==0) //经过了timeout等待后没有文件满足要求
{
printf("timeout!\n");
}
else if(FD_ISSET(button_fd,&rds)) //测试是否可读,文件描述符是否在文件描述符集中
{
read(button_fd,&button_status,sizeof(button_status));//读fd指定的文件,读取<span style="font-family: Arial, Helvetica, sans-serif;">sizeof(button_status)个字节,读到</span><span style="font-family: Arial, Helvetica, sans-serif;">button_status中</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span>
// if(ret<0)
// printf("read worr\n");
}
printf("mi");
if(button_one==button_status)
{
switch(j%3)
{
case 1:ioctl(led_fd,LED_ON,0);j++;break;
case 2:ioctl(led_fd,LED_OFF,0); j++;break;
case 0:ioctl(led_fd,LED_BLINK,0);j++;break;
default: break;
}
}
}
close (led_fd);
close(button_fd);
return 0;
} /* ----- End of main() ----- */
全局变量J的意思是按一次灯亮,两次灯灭,三次闪烁。大家可以验证一下