1 #include "adcinterface.h"
2 #include <linux/input.h>
3 #include <QSocketNotifier>
4 #include <QFile>
5 #include <QDebug>
6
ADCInterface(const QString & devName,QObject * parent)7 ADCInterface::ADCInterface(const QString &devName, QObject *parent) : QObject(parent),
8 m_devName(devName),
9 m_file(Q_NULLPTR),
10 m_notfier(Q_NULLPTR)
11 {
12 // /dev/input/event2 sunxi-gpadc0
13 // /dev/input/event3 sunxi-gpadc1
14 // /dev/input/event4 sunxi-gpadc2
15 // /dev/input/event5 sunxi-gpadc3
16 if(m_devName.contains("/dev/input/event"))
17 {
18 m_index = m_devName.right(1).toInt()-2;;
19 }
20 }
21
~ADCInterface()22 ADCInterface::~ADCInterface()
23 {
24 if(m_file && m_file->isOpen())
25 {
26 m_file->close();
27
28 }
29 }
30
open(bool bOpen)31 void ADCInterface::open(bool bOpen)
32 {
33 QString cmd=QString("echo gpadc%1,%2> /sys/class/gpadc/status")
34 .arg(adcIndex())
35 .arg(bOpen);
36
37 qDebug()<< cmd;
38 system(cmd.toLocal8Bit().constData());
39 }
40
open()41 bool ADCInterface::open()
42 {
43 bool bRet = false;
44 if(m_file != Q_NULLPTR)
45 {
46 return !bRet;
47 }
48 open(true);
49 m_file = new QFile(m_devName, this);
50 if(m_file->open(QFile::ReadWrite| QFile::Unbuffered))
51 {
52 m_notfier = new QSocketNotifier(m_file->handle(), QSocketNotifier::Read, this);
53 QSocketNotifier *notifier = new QSocketNotifier(m_file->handle(), QSocketNotifier::Read, this);
54 connect(notifier, &QSocketNotifier::activated, this, [=](){
55 union LED_EVENT{
56 input_event e;
57 char buf[200];
58 };
59 LED_EVENT data;
60 m_file->read(data.buf, sizeof(input_event));
61 if(data.e.type == EV_MSC)
62 {
63 emit readRead(data.e.value);
64 }
65 });
66 }
67 }
68
close()69 void ADCInterface::close()
70 {
71 open(false);
72 if(m_file)
73 {
74 m_file->close();
75 delete m_file;
76 delete m_notfier;
77 }
78 }
79
adcIndex()80 int ADCInterface::adcIndex()
81 {
82 return m_index;
83 }
84