使用epoll检测gpio引脚

背景

在应用层操作GPIO除了驱动以外主要是通过读取改变gpio子系统的文件内容来实现gpio的读写,但是当需要监测gpio的状态改变时循环去检测文件改变是会消耗大量cpu资源的事情,此时我们可以使用epoll来检测文件的改变

代码实现

#include <stdlib.h>  
#include <stdio.h>   
#include <stdbool.h>   
#include <string.h> 
#include <unistd.h> 
#include <fcntl.h>  
#include <poll.h> 
#include <sys/epoll.h> 
#include <sys/time.h>
#include <errno.h> 
#include <linux/input.h> 

#define MSG(args...) printf(args) 
#define MSG_I(args...) printf(args) 
#define MSG_W(args...) printf(args) 
#define MSG_E(args...) printf(args) 

static int g_epollFd = -1;

//函数声明
static int gpio_export(int pin);
static int gpio_unexport(int pin);
static int gpio_direction(int pin, int dir);
static int gpio_write(int pin, int value);
static int gpio_read(int pin);

static int gpio_export(int pin)  
{  
    char buffer[64];  
    int len;  
    int fd;  

    char gpio_pin[64];
    snprintf( gpio_pin, sizeof(gpio_pin), "/sys/class/gpio/gpio%d", pin );
    if( access( gpio_pin, F_OK ) == 0 ){    // 如果已经申请
        MSG_I("%s it already exists\n", gpio_pin );
        return 0;
    }

    fd = open("/sys/class/gpio/export", O_WRONLY);  
    if (fd < 0) {  
        MSG("Failed to open export for writing!\n");  
        return(-1);  
    }  

    len = snprintf(buffer, sizeof(buffer), "%d", pin);  
    if (write(fd, buffer, len) < 0) {  
        MSG("Failed to export gpio!\n");  
        return -1;  
    }  

    close(fd);  
    return 0;  
}  

static int gpio_unexport(int pin)  
{  
    char buffer[64];  
    int len;  
    int fd;  

    fd = open("/sys/class/gpio/unexport", O_WRONLY);  
    if (fd < 0) {  
        MSG("Failed to open unexport for writing!\n");  
        return -1;  
    }  

    len = snprintf(buffer, sizeof(buffer), "%d", pin);  
    if (write(fd, buffer, len) < 0) {  
        MSG("Failed to unexport gpio!");  
        return -1;  
    }  
    close(fd);  
    return 0;  
} 

//dir: 0-->IN, 1-->OUT
static int gpio_direction(int pin, int dir)  
{  
    static const char dir_str[] = "in\0out";  
    char path[64];  
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);  

    fd = open(path, O_WRONLY);  
    if (fd < 0) {  
        MSG("Failed to open gpio direction for writing!\n");  
        return -1;  
    }  

    if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) {  
        MSG("Failed to set direction!\n");  
        return -1;  
    }  

    close(fd);  
    return 0;  
}  

//value: 0-->LOW, 1-->HIGH
static int gpio_write(int pin, int value)  
{  
    static const char values_str[] = "01";  
    char path[64];  
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);  
    fd = open(path, O_WRONLY);  
    if (fd < 0) {  
        MSG("Failed to open gpio value for writing!\n");  
        return -1;  
    }  

    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {  
        MSG("Failed to write value!\n");  
        return -1;  
    }  

    close(fd);  
    return 0;  
}

static int gpio_read(int pin)  
{  
    char path[64];  
    char value_str[3];  
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);  
    fd = open(path, O_RDONLY);  
    if (fd < 0) {  
        MSG("Failed to open gpio value for reading!\n");  
        return -1;  
    }  

    if (read(fd, value_str, 3) < 0) {  
        MSG("Failed to read value!\n");  
        return -1;  
    }  

    close(fd);  

    return (atoi(value_str));
}  

// none表示引脚为输入,不是中断引脚
// rising表示引脚为中断输入,上升沿触发
// falling表示引脚为中断输入,下降沿触发
// both表示引脚为中断输入,边沿触发
// 0-->none, 1-->rising, 2-->falling, 3-->both
static int gpio_edge(int pin, int edge)
{
    const char dir_str[][9] = {"none\0","rising\0","falling\0","both"}; 
    char path[64];  
    int fd; 

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);  
    fd = open(path, O_WRONLY);  
    if (fd < 0) {  
        MSG("Failed to open gpio edge for writing!\n");  
        return -1;  
    }  

    if (write(fd, &dir_str[edge], strlen(dir_str[edge])) < 0) {  
        MSG("Failed to set edge!\n");  
        return -1;  
    }  

    close(fd);  
    return 0;  
}

static long int getNowTime()
{
    struct timeval p_time;
    gettimeofday( &p_time, NULL );
    return p_time.tv_sec*1000+p_time.tv_usec/1000;
}


int addFdToEpoll( int pollFd, int addFd )
{
    struct epoll_event event;
    event.events = EPOLLET;
    event.data.fd = addFd;
    return epoll_ctl( pollFd, EPOLL_CTL_ADD, addFd, &event );
}

#define CALL_KEY_NUM    ( 32 )
#define VOLDOWN_KEY_NUM ( 30 )
#define VOLUP_KEY_NUM   ( 98 )

typedef struct{
    int fd;
    bool press_key;     // 按下按键
    bool lift_key;
    long int press_time; // 按下按键的时间
}gpio_info;

int main()  
{  

    /* int gpio_fd1, gpio_fd2, gpio_fd3; */
    gpio_info gpioCall, gpioDown,gpioUp;
    int ret;
    struct pollfd fds[1];
    char buff[10];
    unsigned char cnt = 0;

    g_epollFd = epoll_create(4); //创建红黑树,返回给全局 g_efd, linux2.6之后size>0即可
    if(g_epollFd <= 0)
        MSG("create efd in %s err %s\n", __func__, strerror(errno));


    //按键引脚初始化
    gpio_export( VOLDOWN_KEY_NUM );
    gpio_direction( VOLDOWN_KEY_NUM, 0);
    gpio_edge( VOLDOWN_KEY_NUM,3);
    gpioDown.fd = open("/sys/class/gpio/gpio30/value",O_RDONLY);
    if( gpioDown.fd < 0){
        MSG("Failed to open 30 value!\n");  
        return -1;  
    }
    addFdToEpoll( g_epollFd, gpioDown.fd );

    gpio_export(CALL_KEY_NUM);
    gpio_direction(CALL_KEY_NUM, 0);
    gpio_edge(CALL_KEY_NUM,3);
    gpioCall.fd = open("/sys/class/gpio/gpio32/value",O_RDONLY);
    if( gpioCall.fd < 0){
        MSG("Failed to open 32 value!\n");  
        return -1;  
    }
    addFdToEpoll( g_epollFd, gpioCall.fd );

    gpio_export(VOLUP_KEY_NUM);
    gpio_direction(VOLUP_KEY_NUM, 0);
    gpio_edge(VOLUP_KEY_NUM,3);
    gpioUp.fd = open("/sys/class/gpio/gpio98/value",O_RDONLY);
    if(gpioUp.fd < 0){
        MSG("Failed to open 98 value!\n");  
        return -1;  
    }
    addFdToEpoll( g_epollFd, gpioUp.fd );

    struct epoll_event events;
    int value;
    gpio_info *gpioTmp;
    while(1){
        ret = epoll_wait( g_epollFd, &events, 1, 500 );
        if( ret < 0 ){
            MSG("epoll_wait %s err %s\n", __func__, strerror(errno));
            break;
        }
        else if( ret == 0 ){
            /* MSG("epoll_wait timeout\n"); */
            long int nowTime = getNowTime();
            if( ( gpioCall.press_key ) && 
                ( !gpioCall.lift_key ) &&
                ( (nowTime - gpioCall.press_time) > 1000 ) ){
                MSG("Press the CALL button continuously\n");
            }
            if( ( gpioUp.press_key ) && 
                ( !gpioUp.lift_key ) &&
                ( (nowTime - gpioUp.press_time) > 1000 ) ){
                MSG("Press the VOL UP button continuously\n");
            }
            if( ( gpioDown.press_key ) && 
                ( !gpioDown.lift_key ) &&
                ( (nowTime - gpioDown.press_time) > 1000 ) ){
                MSG("Press the VOL DOWN button continuously\n");
            }
        }
        else{
            lseek( events.data.fd, 0, SEEK_SET );
            memset( buff, 0, sizeof(buff) );
            ret = read( events.data.fd, buff, sizeof(buff) );
            if( ret <= 0 ){
                MSG_E("read error, %d, %s\n", errno, strerror(errno));
                continue;
            }

            /* MSG("ret = %d, buff : %s\n", ret, buff ); */
            /* MSG("ret = %d, buff : %s\n", ret, buff ); */
            value = atoi( buff );

            if( events.data.fd == gpioUp.fd){
                MSG( "push vol up, %s", buff );
                gpioTmp = &gpioUp;
            }
            else if( events.data.fd == gpioCall.fd){
                MSG( "push call, %s", buff );
                gpioTmp = &gpioCall;
            }
            else if( events.data.fd == gpioDown.fd){
                MSG( "push vol down, %s", buff );
                gpioTmp = &gpioDown;
            }

            if( value == 0 ){ // 按键按下
                gpioTmp->press_key = true;
                gpioTmp->press_time = getNowTime();
            }else if ( gpioTmp->press_key ){ // 已经按下了
                gpioTmp->lift_key = true;
            }
            else{
                gpioTmp->press_key = false;
                gpioTmp->lift_key = false;
            }

            // 当按下标志位和按键弹起标志位都为 1 时候,才执行回调
            if( gpioTmp->press_key & gpioTmp->lift_key ){
                gpioTmp->press_key = false;
                gpioTmp->lift_key = false;

                long int nowTime = getNowTime();
                if( ( nowTime - gpioTmp->press_time  ) > 1000 ){ // 长按
                    MSG("long pasaa!!!\n");
                }else if( ( nowTime - gpioTmp->press_time  ) < 10 ){ // 抖动
                    MSG("shake!!!\n");
                }else{
                    MSG("sort pasaa!!!\n");
                }
                MSG("nowTime: %ld, press_time: %ld, %ld\n", nowTime, gpioTmp->press_time, nowTime - gpioTmp->press_time);

            }
                
        }
    }
    return 0;
}  
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用epoll机制监测GPIO中断的C语言demo: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <poll.h> #include <errno.h> #include <string.h> #include <sys/epoll.h> #define GPIO_PIN_NUM 17 // 监测的GPIO引脚号 #define MAX_EVENTS 2 // 监测的最大事件数 int main() { int fd, epoll_fd, ret, count = 0; char buf[MAX_EVENTS]; struct epoll_event event, events[MAX_EVENTS]; struct pollfd pfd; // 打开GPIO文件 fd = open("/sys/class/gpio/gpio17/value", O_RDONLY); if(fd < 0) { printf("Failed to open GPIO file!\n"); return -1; } // 设置GPIO引脚为输入模式 fd = open("/sys/class/gpio/gpio17/direction", O_WRONLY); if(fd < 0) { printf("Failed to open GPIO direction file!\n"); return -1; } write(fd, "in", 2); close(fd); // 创建epoll实例 epoll_fd = epoll_create(1); if(epoll_fd < 0) { printf("Failed to create epoll instance!\n"); return -1; } // 添加GPIO文件描述符到epoll实例中 event.events = EPOLLIN | EPOLLET; event.data.fd = fd; ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event); if(ret < 0) { printf("Failed to add GPIO file descriptor to epoll instance!\n"); return -1; } // 等待GPIO中断事件发生 while(1) { ret = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if(ret < 0) { printf("Failed to wait for events!\n"); return -1; } else if(ret == 0) { printf("Timeout!\n"); } else { // 遍历所有就绪事件 for(int i = 0; i < ret; i++) { if(events[i].events & EPOLLIN) { // 读取GPIO文件内容 lseek(events[i].data.fd, 0, SEEK_SET); read(events[i].data.fd, buf, MAX_EVENTS); // 处理GPIO中断事件 if(buf[0] == '0') { printf("GPIO interrupt detected! Count: %d\n", ++count); } } } } } // 关闭文件描述符和epoll实例 close(fd); close(epoll_fd); return 0; } ``` 这个demo使用了poll()和epoll_wait()函数来监测GPIO中断事件。首先,程序打开GPIO文件并将GPIO引脚设置为输入模式。然后,创建epoll实例并将GPIO文件描述符添加到epoll实例中。最后,程序在一个无限循环中等待epoll事件的发生,当有事件发生时,程序读取GPIO文件内容并处理GPIO中断事件。需要注意的是,该程序只监测一个GPIO引脚的中断事件,如果需要监测多个GPIO引脚,需要在程序中添加相应的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值