2023-07-17 03:29:37 +00:00
|
|
|
|
#include "Sensor.h"
|
|
|
|
|
#include "DevUtil.h"
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdio.h>
|
2023-11-18 03:48:39 +00:00
|
|
|
|
#include "StopWatch.h"
|
|
|
|
|
|
2023-07-17 03:29:37 +00:00
|
|
|
|
|
|
|
|
|
//FileTools ftt("/home/linaro/scanpin.log");
|
|
|
|
|
|
|
|
|
|
Sensor::Sensor(BlockingQueue<ScanEvent> &sysEvents)
|
|
|
|
|
:events(sysEvents),
|
|
|
|
|
pwm2(2),
|
|
|
|
|
coverPin(PIN_PORT_7010::OPEN_COVER_SENSOR),
|
|
|
|
|
paperPin(PIN_PORT_7010::HAVE_OR_NO_PAPER),
|
|
|
|
|
scanPin (PIN_PORT_7010::SCAN_SENSOR),
|
|
|
|
|
doubleEnablePin(PIN_PORT_7010::ULTRASONIC_SENSORS_ON),
|
2023-09-01 01:51:35 +00:00
|
|
|
|
double_out0_Pin(PIN_PORT_7010::ULTRASONIC_SENSORS_OUT1),
|
|
|
|
|
double_out1_Pin(PIN_PORT_7010::ULTRASONIC_SENSORS_OUT0),
|
2023-07-17 03:29:37 +00:00
|
|
|
|
sensor_power(PIN_PORT_7010::SENSOR_POWER)
|
|
|
|
|
|
|
|
|
|
{
|
2023-08-31 08:37:05 +00:00
|
|
|
|
|
2023-07-17 03:29:37 +00:00
|
|
|
|
pwm2.setFreq(2583);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
|
|
|
|
|
pwm2.enable(Gpio::High);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
sensor_power.setValue(Gpio::High); //默认打开电源
|
2023-08-31 08:37:05 +00:00
|
|
|
|
doubleEnablePin.setValue(Gpio::High);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
|
2023-08-31 08:37:05 +00:00
|
|
|
|
///////输入输出模式//////
|
2023-07-17 03:29:37 +00:00
|
|
|
|
coverPin.setDirection(Gpio::in);
|
|
|
|
|
paperPin.setDirection(Gpio::in);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
scanPin.setDirection(Gpio::in);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
double_out0_Pin.setDirection(Gpio::in);
|
|
|
|
|
double_out1_Pin.setDirection(Gpio::in);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
sensor_power.setDirection(Gpio::in);
|
|
|
|
|
|
2023-07-17 03:29:37 +00:00
|
|
|
|
doubleEnablePin.setDirection(Gpio::out);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////中断模式///////
|
|
|
|
|
double_out0_Pin.setEdge(Gpio::both);
|
|
|
|
|
double_out1_Pin.setEdge(Gpio::both);
|
|
|
|
|
doubleEnablePin.setEdge(Gpio::both);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
coverPin.setEdge(Gpio::both);
|
|
|
|
|
paperPin.setEdge(Gpio::both);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
scanPin.setEdge(Gpio::both);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
|
|
|
|
|
//std::cout<<"scanPin "<< scanPin.getValue()<<std::endl;
|
|
|
|
|
thread_monitor = std::thread(&Sensor::monitor, this);
|
|
|
|
|
thread_monitor2 = std::thread(&Sensor::monitor2, this);
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
enableDoubleSensor(false);
|
|
|
|
|
LOG(" init sensor done \n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sensor::~Sensor()
|
|
|
|
|
{
|
|
|
|
|
bMonitor = false;
|
|
|
|
|
bMonitor2 = false;
|
|
|
|
|
if(thread_monitor.joinable())
|
|
|
|
|
thread_monitor.join();
|
|
|
|
|
if(thread_monitor2.joinable())
|
|
|
|
|
thread_monitor2.join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::isCoverClosed()
|
|
|
|
|
{
|
2023-09-01 01:51:35 +00:00
|
|
|
|
//std::cout<<"isCoverClosed "<< coverPin.getValue()<<std::endl;
|
2023-07-17 03:29:37 +00:00
|
|
|
|
return coverPin.getValue() == Gpio::Low;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Sensor::resetPaperPin()
|
|
|
|
|
{
|
|
|
|
|
scanPin.setDirection("in");
|
|
|
|
|
}
|
|
|
|
|
bool Sensor::isPaperStandby()
|
|
|
|
|
{
|
2023-09-01 01:51:35 +00:00
|
|
|
|
//std::cout<<"isPaperStandby "<< paperPin.getValue()<<std::endl;
|
2023-07-17 03:29:37 +00:00
|
|
|
|
return paperPin.getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::isPaperAtScan()
|
|
|
|
|
{
|
|
|
|
|
//std::cout<<"scanPin "<< scanPin.getValue()<<std::endl;
|
|
|
|
|
return scanPin.getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::isDoublePaper()
|
|
|
|
|
{
|
|
|
|
|
return doubleEnablePin.getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::waitPaperIn(int timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> lck(cv_m);
|
|
|
|
|
return cv_scan_at.wait_for(lck, std::chrono::milliseconds(timeout_ms)) == std::cv_status::no_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::waitPaperOut(int timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> lck(cv_m);
|
|
|
|
|
cv_scan_not_at.wait_for(lck, std::chrono::milliseconds(timeout_ms)) == std::cv_status::no_timeout;
|
2023-11-18 03:48:39 +00:00
|
|
|
|
return isPaperIn();
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sensor::waitPaperStandBy(int timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
if(!paperPin.getValue()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if(timeout_ms <= 0) {
|
|
|
|
|
return isPaperStandby();
|
|
|
|
|
}
|
|
|
|
|
std::unique_lock<std::mutex> lck(cv_m);
|
|
|
|
|
return cv_paper_on.wait_for(lck, std::chrono::milliseconds(timeout_ms)) == std::cv_status::no_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Sensor::cancelWaitPaper()
|
|
|
|
|
{
|
|
|
|
|
cv_paper_on.notify_all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Sensor::monitor()
|
|
|
|
|
{
|
|
|
|
|
std::condition_variable *edges[] = {&cv_scan_not_at, &cv_scan_at};
|
|
|
|
|
pollfd pfd;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
pfd.fd = -1;
|
|
|
|
|
char buf[8];
|
|
|
|
|
int num;
|
|
|
|
|
pfd.fd = open(scanPin.getValuePath().c_str(), O_RDONLY);
|
|
|
|
|
if (pfd.fd < 0)
|
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pfd.events = POLLPRI;
|
|
|
|
|
StopWatch sw;
|
2023-09-04 03:28:08 +00:00
|
|
|
|
StopWatch sw1;
|
|
|
|
|
StopWatch sw2;
|
2023-07-17 03:29:37 +00:00
|
|
|
|
num = read(pfd.fd, buf, 8); // This is to clear the avaible read
|
|
|
|
|
std::cout<<"read time "<< sw.elapsed_ms()<<std::endl;
|
2023-11-14 10:02:27 +00:00
|
|
|
|
int indx = 0;
|
2023-11-18 03:48:39 +00:00
|
|
|
|
bool b = false;
|
2023-07-17 03:29:37 +00:00
|
|
|
|
while (bMonitor)
|
|
|
|
|
{
|
|
|
|
|
ret = poll(&pfd, 1, 1000);
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (ret <= 0)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (pfd.revents & POLLPRI)
|
|
|
|
|
{
|
2023-11-04 01:38:17 +00:00
|
|
|
|
if(sw.elapsed_ms() < 10)
|
|
|
|
|
{
|
2023-11-14 10:02:27 +00:00
|
|
|
|
indx++;
|
|
|
|
|
if (indx % 10 == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("扫描传感器消抖数:%d\r\n",indx);
|
2023-11-16 06:49:01 +00:00
|
|
|
|
}
|
2023-11-04 01:38:17 +00:00
|
|
|
|
sw.reset();
|
|
|
|
|
ret = poll(&pfd, 1, 1);
|
|
|
|
|
if (ret > 0)
|
|
|
|
|
{
|
|
|
|
|
lseek(pfd.fd, 0, SEEK_SET);
|
|
|
|
|
num = read(pfd.fd, buf, 8);
|
|
|
|
|
buf[num - 1] = '\0';
|
|
|
|
|
ret = atoi(buf);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-19 02:40:47 +00:00
|
|
|
|
|
|
|
|
|
lseek(pfd.fd, 0, SEEK_SET);
|
|
|
|
|
num = read(pfd.fd, buf, 8);
|
|
|
|
|
buf[num - 1] = '\0';
|
|
|
|
|
ret = atoi(buf);
|
2023-11-18 03:48:39 +00:00
|
|
|
|
|
2023-11-14 10:02:27 +00:00
|
|
|
|
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (ret)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-11-04 01:38:17 +00:00
|
|
|
|
b = true;
|
2023-09-19 02:40:47 +00:00
|
|
|
|
printf("检测纸张进入 time:%f\r\n",sw2.elapsed_ms());
|
|
|
|
|
sw1.reset();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("检测纸张出去 time:%f\r\n",sw1.elapsed_ms());
|
|
|
|
|
sw2.reset();
|
2023-11-04 01:38:17 +00:00
|
|
|
|
if (!b) //理论上来说
|
2023-11-14 10:02:27 +00:00
|
|
|
|
{
|
|
|
|
|
printf("没有检测到纸张进入\r\n");
|
2023-11-04 01:38:17 +00:00
|
|
|
|
continue;
|
2023-11-18 03:48:39 +00:00
|
|
|
|
}
|
|
|
|
|
b = false;
|
2023-09-19 02:40:47 +00:00
|
|
|
|
}
|
2023-11-18 03:48:39 +00:00
|
|
|
|
|
2023-09-19 02:40:47 +00:00
|
|
|
|
edges[ret]->notify_all();
|
2023-09-04 03:28:08 +00:00
|
|
|
|
|
2023-11-04 01:38:17 +00:00
|
|
|
|
sw.reset();
|
2023-09-19 02:40:47 +00:00
|
|
|
|
}
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(pfd.fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Sensor::monitor2()
|
|
|
|
|
{
|
|
|
|
|
ScanEvent evts[] = {S_EVT_COVER_CLOSED, S_EVT_COVER_OPENED , S_EVT_PAPER_STANDBY, S_EVT_PAPER_NOT_STANDBY,
|
|
|
|
|
S_EVT_NOT_DOUBLEPAPER, S_EVT_DOUBLEPAPER, S_EVT_JAM_OUT, S_EVT_NORES};
|
|
|
|
|
std::vector<Gpio *> gpios;
|
|
|
|
|
gpios.push_back(&coverPin);
|
|
|
|
|
gpios.push_back(&paperPin);
|
2023-08-31 08:37:05 +00:00
|
|
|
|
gpios.push_back(&double_out0_Pin);
|
|
|
|
|
gpios.push_back(&double_out1_Pin);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
std::vector<pollfd> pollfds;
|
|
|
|
|
pollfd pfd;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
int fd = -1;
|
|
|
|
|
char buf[8];
|
|
|
|
|
int num;
|
|
|
|
|
for (size_t i = 0; i < gpios.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
fd = open(gpios[i]->getValuePath().c_str(), O_RDONLY);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pfd.fd = fd;
|
|
|
|
|
pfd.events = POLLPRI;
|
|
|
|
|
num = read(fd, buf, 8); // This is to clear the avaible read
|
|
|
|
|
pollfds.push_back(pfd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
{
|
|
|
|
|
bMonitor2 = true;
|
|
|
|
|
while (bMonitor2)
|
|
|
|
|
{
|
|
|
|
|
ret = poll(&pollfds[0], pollfds.size(), 1000);
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < pollfds.size(); ++i)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (pollfds[i].revents)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
ret = readfile(pollfds[i].fd,num,buf);
|
|
|
|
|
|
|
|
|
|
if (i == 2)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
double_1 = ret;
|
2023-11-07 06:34:33 +00:00
|
|
|
|
LOG("2 double_1 :%d double_2:%d\r\n",double_1,double_2);
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (double_2 && double_1 && enbale_double_)
|
2023-07-17 03:29:37 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
events.Put(ScanEvent(S_EVT_DOUBLEPAPER));
|
|
|
|
|
enableDoubleSensor(false);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
2023-09-19 02:40:47 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (i == 3)
|
|
|
|
|
{
|
|
|
|
|
double_2 = ret;
|
2023-11-07 06:34:33 +00:00
|
|
|
|
LOG("3 double_1 :%d double_2:%d\r\n",double_1,double_2);
|
2023-09-19 02:40:47 +00:00
|
|
|
|
if (double_2 && double_1 && enbale_double_)
|
2023-08-31 08:37:05 +00:00
|
|
|
|
{
|
2023-09-19 02:40:47 +00:00
|
|
|
|
events.Put(ScanEvent(S_EVT_DOUBLEPAPER));
|
|
|
|
|
enableDoubleSensor(false);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
2023-08-31 08:37:05 +00:00
|
|
|
|
|
2023-09-19 02:40:47 +00:00
|
|
|
|
}
|
|
|
|
|
if (i != 2 && i != 3)
|
|
|
|
|
{
|
2023-11-14 10:02:27 +00:00
|
|
|
|
int val = i * 2 + ret;
|
|
|
|
|
if (is_open_cover && (val == 1 || val == 2 ||val == 3)) //这个地方主要作用是在开盖以防其他状态消息发送
|
|
|
|
|
break;
|
|
|
|
|
if(val == 1)
|
2023-09-19 02:40:47 +00:00
|
|
|
|
{
|
2023-11-14 10:02:27 +00:00
|
|
|
|
is_open_cover = true;
|
2023-09-19 02:40:47 +00:00
|
|
|
|
cv_paper_on.notify_all();
|
|
|
|
|
}
|
2023-11-14 10:02:27 +00:00
|
|
|
|
if(val == 0)
|
|
|
|
|
is_open_cover = false;
|
|
|
|
|
|
|
|
|
|
events.Put(evts[val]);
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-19 02:40:47 +00:00
|
|
|
|
}
|
2023-07-17 03:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pollfds.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
close(pollfds[i].fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-31 08:37:05 +00:00
|
|
|
|
|
|
|
|
|
int Sensor::readfile(int fd,int num , char* buf){
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
num = read(fd, buf, 8);
|
|
|
|
|
buf[num - 1] = '\0';
|
|
|
|
|
return atoi(buf);
|
|
|
|
|
}
|