首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

按键控制led应用软件

按键控制led应用软件

/*******************************************************************************
硬件环境:  mini2440开发板
软件环境:  操作系统ubuntu8.04,内核版本linux2.6.29
按键驱动说明:
    一个好的,非常精简的测试驱动应用程序
    此程序是按键控制led应用程序
   
作者:   ljh
   
时间:   2012.06.20
   
********************************************************************************/

#i nclude<stdio.h>
#i nclude<stdlib.h>
#i nclude<errno.h>
int main(int argc,char **argv)
{
    int fd;
    int key_status[6];
    //以阻塞方式打开设备文件,非阻塞时flags=O_NONBLOCK
    fd = open("/dev/buttons-ctl-leds", 0);
    if(fd < 0)
    {
        printf("Open Buttons Device Faild!\n");
        exit(1);
    }
    while(1)
    {
        int i;
        int ret;
        fd_set rds;
        
        FD_ZERO(&rds);
        FD_SET(fd,&rds);
        
        //应用程序进行轮询,查询是否可对设备进行访问
        ret = select(fd+ 1, &rds, NULL,NULL, NULL);
        
        if(ret< 0)
        {
            printf("Read Buttons Device Faild!\n");
            exit(1);
        }
        
        if(ret== 0)
        {
            printf("Read Buttons Device Timeout!\n");
        }
        else if(FD_ISSET(fd,&rds))
        {
            //读设备
            ret = read(fd, key_status,sizeof(key_status));
            //读取错误
            if(ret!= sizeof(key_status))
            {
                if(errno!= EAGAIN)
                {
                    printf("Read Button Device Faild!\n");
                }
                continue;
            }
            else
            {
             //读取正确,打印按键值
                for(i= 0; i < 6; i++)
                {
                    //对应驱动中按键的状态,为0即按键被按下
                    if(key_status[i]== 0)
                    {
                     
                        //系统调用ioctl函数命令cmd, 参数arg
                        ioctl(fd, 0, i);     //fd文件描述符 on控制命令1或0 led_no哪个led
                       
                        printf("Key%d DOWN\n", i+1);
                        
                        if(i == 4)       //如果按键key5 则所有led灯光闪烁20s
                        {
                         ioctl(fd, 55, i);  
                        }
                    }
                }
               
            }
        }
    }
    close(fd);
    return 0;
}
返回列表