fl2440——Platform_Button实例

/*********************************************************************************
 *      Copyright:  (C) 2017 TangBin<tangbinmvp@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  button.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/04/2017)
 *         Author:  TangBin <tangbinmvp@gmail.com>
 *      ChangeLog:  1, Release initial version on "04/04/2017 11:51:41 AM"
 *                 
 ********************************************************************************/

#include"plat_ioctl.h"
#include<stdio.h>
#include<stdlib.h> 
#include<fcntl.h>
#include<unistd.h>
#include<sys/ioctl.h> 
#include<sys/time.h>
#include<errno.h>

#define PLATDRV_MAGIC             0x60
#define LED_OFF                   _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON                    _IO (PLATDRV_MAGIC, 0x19)

#define KEY1 0x1
#define KEY2 0x2
#define KEY3 0x4
#define KEY4 0x8


/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int fd_button;
    int fd_led;

    int status;
    int retval;
    fd_set rfds;

    fd_button =open("/dev/button",O_RDWR);
    if(fd_button <0)
        goto err1;
    fd_led =open("/dev/led",O_RDWR);
    if(fd_led <0)
        goto err2;

    FD_ZERO(&rfds);
    FD_SET(fd_button,&rfds);

    for(;;)
    {
        retval =select(fd_button+1,&rfds,NULL,NULL,NULL);

        if(retval <0)
        {
            printf("error!%s\n",strerror(errno));
            break;
        }
        else if(retval ==0)
            continue;
        else
        {
            if(FD_ISSET(fd_button,&rfds) >0)
            {
                read(fd_button, &status, sizeof(status));
            }
        }

        if(status &KEY1)
        {
            ioctl(fd_led, LED_ON, 0);
            printf("led0 on\n");
            sleep(2);
            ioctl(fd_led, LED_OFF, 0);
        }

        if(status &KEY2)
        {
            ioctl(fd_led, LED_ON, 1);
            printf("led1 on\n");
            sleep(2);
            ioctl(fd_led, LED_OFF, 1);
        }

        if(status &KEY3)
        {
            ioctl(fd_led, LED_ON, 2);
            printf("led2 on\n");
            sleep(2);
            ioctl(fd_led, LED_OFF, 2);
        }
        if(status &KEY4)
        {
            ioctl(fd_led, LED_ON, 3);
            printf("led3 on\n");
            sleep(2);
            ioctl(fd_led, LED_OFF, 3);
        }

    }
    close(fd_button);
    close(fd_led);
err1:
    perror("open device buttons error!");
    close(fd_button);
err2:
    perror("open device leds error!");
    close(fd_led); 

    return 0;
} /* ----- End of main() ----- */

./a.out 之后,按下对应的按键,LED灯就会亮2s,再灭掉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值