hhk7734 / lot-SBC

API: GPIO development library for lot-SBC
MIT License
1 stars 0 forks source link

Add PCINT(pin, edge, debounce_delay_ms, ISR, args) #24

Open hhk7734 opened 4 years ago

hhk7734 commented 4 years ago

epoll test code

#include <sys/epoll.h>    // epoll
#include <fcntl.h>        // open
#include <sys/ioctl.h>
#include <stdio.h>    // fopen
#include <string>
#include <unistd.h>
#include <map>
#include <iostream>
#include <errno.h>
#include <string.h>

std::map<int, int> pin_fd;
std::map<int, int> fd_pin;

void set( int epfd, int pin )
{
    FILE *file = fopen( "/sys/class/gpio/export", "w" );
    fprintf( file, "%d\n", pin );
    fclose( file );

    std::string path( "/sys/class/gpio/gpio" );
    path += std::to_string( pin );
    int index = path.size();
    path += "/direction";
    while( ( file = fopen( path.c_str(), "w" ) ) == NULL )
    {
        sleep( 1 );
    }
    fprintf( file, "in\n" );
    fclose( file );

    path.replace( index + 1, 9, "edge" );
    while( ( file = fopen( path.c_str(), "w" ) ) == NULL )
    {
        sleep( 1 );
    }
    fprintf( file, "both\n" );
    fclose( file );

    path.replace( index + 1, 4, "value" );
    pin_fd[pin]         = open( path.c_str(), O_RDWR );
    fd_pin[pin_fd[pin]] = pin;

    int count;
    ioctl( pin_fd.at( pin ), FIONREAD, &count );
    for( int i = 0; i < count; ++i )
    {
        char c;
        read( pin_fd.at( pin ), &c, 1 );
    }

    struct epoll_event ev;

    ev.events  = EPOLLET;
    ev.data.fd = pin_fd.at( pin );

    epoll_ctl( epfd, EPOLL_CTL_ADD, pin_fd.at( pin ), &ev );
}

int main( void )
{
    int epfd = epoll_create( 10 );

    set( epfd, 479 );
    set( epfd, 480 );

    struct epoll_event events[10];

    while( 1 )
    {
        int nfds, n;
        nfds = epoll_wait( epfd, events, 10, -1 );

        for( n = 0; n < nfds; ++n )
        {
            int  fd = events[n].data.fd;
            char c;
            lseek( fd, 0, SEEK_SET );
            read( fd, &c, 1 );
            if( c == '1' )
            {
                std::cout << std::to_string( fd_pin[fd] ) << "rising"
                          << std::endl;
            }
            else
            {
                std::cout << std::to_string( fd_pin[fd] ) << "falling"
                          << std::endl;
            }
        }
    }
}
hhk7734 commented 4 years ago

epoll -> poll

hhk7734 commented 4 years ago
#include <sys/epoll.h>    // epoll
#include <fcntl.h>        // open
#include <sys/ioctl.h>
#include <stdio.h>    // fopen
#include <string>
#include <unistd.h>
#include <map>
#include <iostream>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <poll.h>
#include <lot/lot.h>

std::map<int, int> pin_fd;
std::map<int, int> fd_pin;

void set( int epfd, int pin )
{
    FILE *file = fopen( "/sys/class/gpio/export", "w" );
    fprintf( file, "%d\n", pin );
    fclose( file );

    std::string path( "/sys/class/gpio/gpio" );
    path += std::to_string( pin );
    int index = path.size();
    path += "/direction";
    while( ( file = fopen( path.c_str(), "w" ) ) == NULL )
    {
        sleep( 1 );
    }
    fprintf( file, "in\n" );
    fclose( file );

    path.replace( index + 1, 9, "edge" );
    while( ( file = fopen( path.c_str(), "w" ) ) == NULL )
    {
        sleep( 1 );
    }
    fprintf( file, "both\n" );
    fclose( file );

    path.replace( index + 1, 4, "value" );
    pin_fd[pin]         = open( path.c_str(), O_RDWR );
    fd_pin[pin_fd[pin]] = pin;

    int count;
    ioctl( pin_fd.at( pin ), FIONREAD, &count );
    for( int i = 0; i < count; ++i )
    {
        char c;
        read( pin_fd.at( pin ), &c, 1 );
    }
}

int main( void )
{
    lot::gpio::init();
    lot::init_time();

    set( 1, 479 );

    struct pollfd polls;
    polls.fd     = pin_fd.at( 479 );
    polls.events = POLLPRI;

    int  fd = pin_fd.at( 479 );
    char c;

    while( 1 )
    {
        int x = poll( &polls, 1, -1 );

        lseek( fd, 0, SEEK_SET );
        read( fd, &c, 1 );

        int status = lot::gpio::digital( 11 );
        for( int i = 0; i < 9; ++i )
        {
            status += lot::gpio::digital( 11 );
        }

        std::cout << status << std::endl;

        while( 1 )
        {
            x = poll( &polls, 1, 10 );
            if( x != 0 )
            {
                lseek( fd, 0, SEEK_SET );
                read( fd, &c, 1 );
                continue;
            }
            if( x == 0 )
                break;
        }
    }
}
hhk7734 commented 4 years ago
* Real signal
  ┌┐┌┐┌┐┌────────────┐┌┐┌┐
  │││││││            │││││
──┘└┘└┘└┘            └┘└┘└────
* Processed signal
  ┌──────────────────┐
  │                  │
──┘                  └────────
* Real signal
  ┌┐┌┐┌┐
  ││││││
──┘└┘└┘└────
* Processed signal
  ┌────┐
  │    │
──┘    └────
hhk7734 commented 4 years ago
* Real signal
───┐┌┐┌┐            ┌┐┌───
   │││││            │││
   └┘└┘└────────────┘└┘
* Processed signal
───┐                  ┌───
   │                  │
   └──────────────────┘