34 #include <sys/ioctl.h> 36 #define DEFAULT_VALUE 0 39 :
TorcQThread(QStringLiteral(
"GPIOInput%1").arg(Pin)),
43 m_file(QStringLiteral(
"/sys/class/gpio/gpio%1/value").arg(wpiPinToGpio(Pin)))
54 QFile unexport(QStringLiteral(
"/sys/class/gpio/unexport"));
55 if (unexport.open(QIODevice::WriteOnly))
57 QByteArray pin = QByteArray::number(wpiPinToGpio(m_pin));
59 if (unexport.write(pin) > -1)
60 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Unexported pin %1").arg(m_pin));
68 int value = digitalRead(m_pin);
70 LOG(VB_GENERAL, LOG_INFO, QString::number(value));
76 pullUpDnControl(m_pin, PUD_OFF);
78 int bcmpin = wpiPinToGpio(m_pin);
80 QFile export1(QStringLiteral(
"/sys/class/gpio/export"));
81 if (!export1.open(QIODevice::WriteOnly))
83 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to open '%1' for writing").arg(export1.fileName()));
87 QByteArray pin = QByteArray::number(bcmpin);
89 if (export1.write(pin) < 0)
91 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to write to '%1'").arg(export1.fileName()));
96 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Exported pin %1").arg(m_pin));
98 QFile direction(QStringLiteral(
"/sys/class/gpio/gpio%1/direction").arg(bcmpin));
99 if (!direction.open(QIODevice::WriteOnly))
101 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to open '%1' for writing").arg(direction.fileName()));
105 QByteArray dir(
"in\n");
106 if (direction.write(dir) < 0)
108 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to write to '%1'").arg(direction.fileName()));
113 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Pin %1 set as input").arg(m_pin));
115 QFile edge(QStringLiteral(
"/sys/class/gpio/gpio%1/edge").arg(bcmpin));
116 if (!edge.open(QIODevice::WriteOnly))
118 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to open '%1' for writing").arg(edge.fileName()));
122 QByteArray both(
"both\n");
123 if (edge.write(both) < 0)
125 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to write to '%1'").arg(edge.fileName()));
132 if (!m_file.open(QIODevice::ReadWrite))
134 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to open '%1' to monitor GPIO input (err: %2)")
135 .arg(m_file.fileName()).arg(strerror(errno)));
139 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Pin %1 input setup complete").arg(m_pin));
153 ioctl(m_file.handle(), FIONREAD, &count);
154 for (
int i = 0; i < count; i++)
155 read(m_file.handle(), &c, 1);
156 lseek(m_file.handle(), 0, SEEK_SET);
164 polls.fd = m_file.handle();
165 polls.events = POLLPRI;
168 int res = poll(&polls, 1, 10);
176 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Poll failed for '%1' (err: %2)")
177 .arg(m_file.fileName()).arg(strerror(errno)));
183 (void)read(m_file.handle(), &c, 1);
184 lseek(m_file.handle(), 0, SEEK_SET);
213 m_inputThread->
Stop();
214 m_inputThread->quit();
215 m_inputThread->wait();
216 delete m_inputThread;
222 m_inputThread->start();
230 return QStringList() << tr(
"Pin %1 Switch").arg(m_pin);
void Initialise(void)
Performs Torc specific thread initialisation.
#define LOG(_MASK_, _LEVEL_, _STRING_)
void Deinitialise(void)
Performs Torc specific thread cleanup.
A Torc specific wrapper around QThread.