rk3399_arm_lvds/deviceio/PinMonitor.cpp

76 lines
1.4 KiB
C++
Raw Permalink Normal View History

2024-03-05 03:46:18 +00:00
#include "PinMonitor.h"
#include "DevUtil.h"
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <thread>
#include <iostream>
#include "StopWatch.h"
PinMonitor::PinMonitor(unsigned int pinNum, std::function<void(int)> call_back)
: pin(pinNum)
{
pin.setDirection(Gpio::in);
pin.setEdge(Gpio::falling);
this->call_back = call_back;
thread_monitor = std::thread(&PinMonitor::monitor, this);
// printf("PinMonitor threadid = %d \n",thread_monitor.get_id());
}
PinMonitor::~PinMonitor()
{
bMonitor = false;
if (thread_monitor.joinable())
thread_monitor.join();
}
void PinMonitor::monitor()
{
pollfd pfd;
int ret = 0;
pfd.fd = -1;
char buf[8];
int num;
StopWatch sw;
pfd.fd = open(pin.getValuePath().c_str(), O_RDONLY);
if (pfd.fd < 0)
ret = -1;
pfd.events = POLLPRI;
num = read(pfd.fd, buf, 8); // This is to clear the avaible read
while (bMonitor)
{
ret = poll(&pfd, 1, 1000);
if (ret > 0)
{
if (pfd.revents & POLLPRI)
{
lseek(pfd.fd, 0, SEEK_SET);
num = read(pfd.fd, buf, 8);
buf[num - 1] = '\0';
ret = atoi(buf);
if (call_back)
call_back(pin.getPort());
sw.reset();
while (sw.elapsed_ms() < 30)
{
ret = poll(&pfd, 1, 1);
if (ret > 0)
{
num = read(pfd.fd, buf, 8);
buf[num - 1] = '\0';
ret = atoi(buf);
// printf("pMonitor nread = %d ret val = %d \n",num,ret);
}
}
}
}
}
close(pfd.fd);
}