Open hhk7734 opened 4 years ago
epoll -> poll
#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;
}
}
}
* Real signal
┌┐┌┐┌┐┌────────────┐┌┐┌┐
│││││││ │││││
──┘└┘└┘└┘ └┘└┘└────
* Processed signal
┌──────────────────┐
│ │
──┘ └────────
* Real signal
┌┐┌┐┌┐
││││││
──┘└┘└┘└────
* Processed signal
┌────┐
│ │
──┘ └────
* Real signal
───┐┌┐┌┐ ┌┐┌───
│││││ │││
└┘└┘└────────────┘└┘
* Processed signal
───┐ ┌───
│ │
└──────────────────┘
epoll test code