sanmaoljh的笔记 https://passport2.21ic.com/?560548 [收藏] [复制] [RSS]

日志

按键控制led应用软件

已有 511 次阅读2012-6-30 07:06 |系统分类:ARM| 按键控制led应用软件


/*******************************************************************************
硬件环境:  mini2440开发板


软件环境:  操作系统ubuntu8.04,内核版本linux2.6.29


按键驱动说明:
    一个好的,非常精简的测试驱动应用程序
    此程序是按键控制led应用程序
    
作者:   ljh
    
时间:   2012.06.20
    
********************************************************************************/



#include<stdio.h>
#include<stdlib.h>
#include<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== 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;
}


 


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)