#include "adcinterface.h" #include #include #include #include ADCInterface::ADCInterface(const QString &devName, QObject *parent) : QObject(parent), m_devName(devName), m_file(Q_NULLPTR), m_notfier(Q_NULLPTR) { // /dev/input/event2 sunxi-gpadc0 // /dev/input/event3 sunxi-gpadc1 // /dev/input/event4 sunxi-gpadc2 // /dev/input/event5 sunxi-gpadc3 if(m_devName.contains("/dev/input/event")) { m_index = m_devName.right(1).toInt()-2;; } } ADCInterface::~ADCInterface() { if(m_file && m_file->isOpen()) { m_file->close(); } } void ADCInterface::open(bool bOpen) { QString cmd=QString("echo gpadc%1,%2> /sys/class/gpadc/status") .arg(adcIndex()) .arg(bOpen); qDebug()<< cmd; system(cmd.toLocal8Bit().constData()); } bool ADCInterface::open() { bool bRet = false; if(m_file != Q_NULLPTR) { return !bRet; } open(true); m_file = new QFile(m_devName, this); if(m_file->open(QFile::ReadWrite| QFile::Unbuffered)) { m_notfier = new QSocketNotifier(m_file->handle(), QSocketNotifier::Read, this); QSocketNotifier *notifier = new QSocketNotifier(m_file->handle(), QSocketNotifier::Read, this); connect(notifier, &QSocketNotifier::activated, this, [=](){ union LED_EVENT{ input_event e; char buf[200]; }; LED_EVENT data; m_file->read(data.buf, sizeof(input_event)); if(data.e.type == EV_MSC) { emit readRead(data.e.value); } }); } } void ADCInterface::close() { open(false); if(m_file) { m_file->close(); delete m_file; delete m_notfier; } } int ADCInterface::adcIndex() { return m_index; }