fix GPIO class

This commit is contained in:
Simon Stürz 2016-12-05 20:36:47 +01:00 committed by Michael Zanetti
parent e189f69df2
commit 78306cf751
3 changed files with 70 additions and 53 deletions

View File

@ -97,23 +97,33 @@ Gpio::~Gpio()
unexportGpio(); unexportGpio();
} }
/*! Returns the directory \tt {/sys/class/gpio/gpio<number>} of this Gpio. */
QString Gpio::gpioDirectory() const
{
return m_gpioDirectory.path();
}
/*! Returns true if the directory \tt {/sys/class/gpio} does exist. */ /*! Returns true if the directory \tt {/sys/class/gpio} does exist. */
bool Gpio::isAvailable() bool Gpio::isAvailable()
{ {
return QDir("/sys/class/gpio").exists(); return QDir("/sys/class/gpio").exists();
} }
/*! Returns true if this \l{Gpio} could be exported in the system file \tt {/sys/class/gpio/export}. */ /*! Returns true if this \l{Gpio} could be exported in the system file \tt {/sys/class/gpio/export}. If this Gpio is already exported, this function will return true. */
bool Gpio::exportGpio() bool Gpio::exportGpio()
{ {
// Check if already exported
if (m_gpioDirectory.exists())
return true;
QFile exportFile("/sys/class/gpio/export"); QFile exportFile("/sys/class/gpio/export");
if (!exportFile.open(QIODevice::WriteOnly)) { if (!exportFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not export GPIO" << m_gpio; qCWarning(dcHardware()) << "Could not export GPIO" << m_gpio;
return false; return false;
} }
QTextStream out(&exportFile); QTextStream out(&exportFile);
out << m_gpio; out << m_gpio << "\n";
exportFile.close(); exportFile.close();
return true; return true;
} }
@ -122,13 +132,13 @@ bool Gpio::exportGpio()
bool Gpio::unexportGpio() bool Gpio::unexportGpio()
{ {
QFile unexportFile("/sys/class/gpio/unexport"); QFile unexportFile("/sys/class/gpio/unexport");
if (!unexportFile.open(QIODevice::WriteOnly)) { if (!unexportFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not unexport GPIO" << m_gpio; qCWarning(dcHardware()) << "Could not unexport GPIO" << m_gpio;
return false; return false;
} }
QTextStream out(&unexportFile); QTextStream out(&unexportFile);
out << m_gpio; out << m_gpio << "\n";
unexportFile.close(); unexportFile.close();
return true; return true;
} }
@ -137,7 +147,7 @@ bool Gpio::unexportGpio()
bool Gpio::setDirection(Gpio::Direction direction) bool Gpio::setDirection(Gpio::Direction direction)
{ {
QFile directionFile(m_gpioDirectory.path() + "/direction"); QFile directionFile(m_gpioDirectory.path() + "/direction");
if (!directionFile.open(QIODevice::WriteOnly)) { if (!directionFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "direction file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "direction file.";
return false; return false;
} }
@ -145,10 +155,10 @@ bool Gpio::setDirection(Gpio::Direction direction)
QTextStream out(&directionFile); QTextStream out(&directionFile);
switch (direction) { switch (direction) {
case DirectionInput: case DirectionInput:
out << "in"; out << "in" << "\n";
break; break;
case DirectionOutput: case DirectionOutput:
out << "out"; out << "out" << "\n";
break; break;
default: default:
directionFile.close(); directionFile.close();
@ -163,7 +173,7 @@ bool Gpio::setDirection(Gpio::Direction direction)
Gpio::Direction Gpio::direction() Gpio::Direction Gpio::direction()
{ {
QFile directionFile(m_gpioDirectory.path() + "/direction"); QFile directionFile(m_gpioDirectory.path() + "/direction");
if (!directionFile.open(QIODevice::ReadOnly)) { if (!directionFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "direction file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "direction file.";
return Gpio::DirectionInvalid; return Gpio::DirectionInvalid;
} }
@ -191,7 +201,7 @@ bool Gpio::setValue(Gpio::Value value)
return false; return false;
QFile valueFile(m_gpioDirectory.path() + "/value"); QFile valueFile(m_gpioDirectory.path() + "/value");
if (!valueFile.open(QIODevice::WriteOnly)) { if (!valueFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "value file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "value file.";
return false; return false;
} }
@ -199,10 +209,10 @@ bool Gpio::setValue(Gpio::Value value)
QTextStream out(&valueFile); QTextStream out(&valueFile);
switch (value) { switch (value) {
case ValueLow: case ValueLow:
out << "0"; out << "0" << "\n";
break; break;
case ValueHigh: case ValueHigh:
out << "1"; out << "1" << "\n";
break; break;
default: default:
valueFile.close(); valueFile.close();
@ -217,7 +227,7 @@ bool Gpio::setValue(Gpio::Value value)
Gpio::Value Gpio::value() Gpio::Value Gpio::value()
{ {
QFile valueFile(m_gpioDirectory.path() + "/value"); QFile valueFile(m_gpioDirectory.path() + "/value");
if (!valueFile.open(QIODevice::ReadOnly)) { if (!valueFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "value file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "value file.";
return Gpio::ValueInvalid; return Gpio::ValueInvalid;
} }
@ -240,16 +250,16 @@ Gpio::Value Gpio::value()
bool Gpio::setActiveLow(bool activeLow) bool Gpio::setActiveLow(bool activeLow)
{ {
QFile activeLowFile(m_gpioDirectory.path() + "/active_low"); QFile activeLowFile(m_gpioDirectory.path() + "/active_low");
if (!activeLowFile.open(QIODevice::WriteOnly)) { if (!activeLowFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "active_low file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "active_low file.";
return false; return false;
} }
QTextStream out(&activeLowFile); QTextStream out(&activeLowFile);
if (activeLow) { if (activeLow) {
out << "0"; out << "0" << "\n";
} else { } else {
out << "1"; out << "1" << "\n";
} }
activeLowFile.close(); activeLowFile.close();
@ -260,7 +270,7 @@ bool Gpio::setActiveLow(bool activeLow)
bool Gpio::activeLow() bool Gpio::activeLow()
{ {
QFile activeLowFile(m_gpioDirectory.path() + "/active_low"); QFile activeLowFile(m_gpioDirectory.path() + "/active_low");
if (!activeLowFile.open(QIODevice::ReadOnly)) { if (!activeLowFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "active_low file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "active_low file.";
return false; return false;
} }
@ -286,7 +296,7 @@ bool Gpio::setEdgeInterrupt(Gpio::Edge edge)
} }
QFile edgeFile(m_gpioDirectory.path() + "/edge"); QFile edgeFile(m_gpioDirectory.path() + "/edge");
if (!edgeFile.open(QIODevice::WriteOnly)) { if (!edgeFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "edge file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "edge file.";
return false; return false;
} }
@ -294,16 +304,16 @@ bool Gpio::setEdgeInterrupt(Gpio::Edge edge)
QTextStream out(&edgeFile); QTextStream out(&edgeFile);
switch (edge) { switch (edge) {
case EdgeFalling: case EdgeFalling:
out << "falling"; out << "falling" << "\n";
break; break;
case EdgeRising: case EdgeRising:
out << "rising"; out << "rising" << "\n";
break; break;
case EdgeBoth: case EdgeBoth:
out << "both"; out << "both" << "\n";
break; break;
case EdgeNone: case EdgeNone:
out << "none"; out << "none" << "\n";
break; break;
default: default:
return false; return false;
@ -317,7 +327,7 @@ bool Gpio::setEdgeInterrupt(Gpio::Edge edge)
Gpio::Edge Gpio::edgeInterrupt() Gpio::Edge Gpio::edgeInterrupt()
{ {
QFile edgeFile(m_gpioDirectory.path() + "/edge"); QFile edgeFile(m_gpioDirectory.path() + "/edge");
if (!edgeFile.open(QIODevice::ReadOnly)) { if (!edgeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "edge file."; qCWarning(dcHardware()) << "Could not open GPIO" << m_gpio << "edge file.";
return Gpio::EdgeNone; return Gpio::EdgeNone;
} }
@ -347,35 +357,40 @@ int Gpio::gpioNumber() const
} }
QDebug operator<<(QDebug d, Gpio *gpio) QDebug operator<<(QDebug debug, Gpio *gpio)
{ {
d << "-----------------------------------"; debug.nospace() << "Gpio(" << gpio->gpioNumber() << ", ";
d << "\n--> gpio" << gpio->gpioNumber() << ":";
d << "\n------------------";
if (gpio->direction() == Gpio::DirectionInput) { if (gpio->direction() == Gpio::DirectionInput) {
d << "\n direction: input"; debug.nospace() << "Input, ";
switch (gpio->edgeInterrupt()) {
case Gpio::EdgeFalling:
debug.nospace() << "Falling, ";
break;
case Gpio::EdgeRising:
debug.nospace() <<"Rising, ";
break;
case Gpio::EdgeBoth:
debug.nospace() << "Both, ";
break;
case Gpio::EdgeNone:
debug.nospace() << "None, ";
break;
default:
break;
}
} else if (gpio->direction() == Gpio::DirectionOutput) { } else if (gpio->direction() == Gpio::DirectionOutput) {
d << "\n direction: output"; debug.nospace() << "Output, ";
} else {
debug.nospace() << "Invalid, ";
} }
d << "\n value:" << gpio->value();
d << "\n active low:" << gpio->activeLow();
switch (gpio->edgeInterrupt()) { if (gpio->value() == Gpio::ValueHigh) {
case Gpio::EdgeFalling: debug.nospace() << "Value: 1)";
d << "\n edge interrupt:" << "falling"; } else if (gpio->value() == Gpio::ValueLow) {
break; debug.nospace() << "Value: 0)";
case Gpio::EdgeRising: } else {
d << "\n edge interrupt:" << "rising"; debug.nospace() << "Value: Invalid)";
break;
case Gpio::EdgeBoth:
d << "\n edge interrupt:" << "both";
break;
case Gpio::EdgeNone:
d << "\n edge interrupt:" << "none";
break;
default:
break;
} }
d << "\n-----------------------------------\n";
return d; return debug;
} }

View File

@ -55,6 +55,8 @@ public:
explicit Gpio(int gpio = 0, QObject *parent = 0); explicit Gpio(int gpio = 0, QObject *parent = 0);
~Gpio(); ~Gpio();
QString gpioDirectory() const;
static bool isAvailable(); static bool isAvailable();
bool exportGpio(); bool exportGpio();
@ -81,6 +83,6 @@ private:
}; };
QDebug operator<< (QDebug d, Gpio *gpio); QDebug operator<< (QDebug debug, Gpio *gpio);
#endif // GPIO_H #endif // GPIO_H

View File

@ -92,12 +92,12 @@ bool GpioMonitor::enable(bool activeLow, Gpio::Edge edgeInterrupt)
!m_gpio->setDirection(Gpio::DirectionInput) || !m_gpio->setDirection(Gpio::DirectionInput) ||
!m_gpio->setActiveLow(activeLow) || !m_gpio->setActiveLow(activeLow) ||
!m_gpio->setEdgeInterrupt(edgeInterrupt)) { !m_gpio->setEdgeInterrupt(edgeInterrupt)) {
qDebug() << "ERROR: while initializing GPIO" << m_gpio->gpioNumber(); qCWarning(dcHardware()) << "GpioMonitor: Error while initializing GPIO" << m_gpio->gpioNumber();
return false; return false;
} }
if (!m_valueFile.open(QFile::ReadOnly)) { if (!m_valueFile.open(QFile::ReadOnly)) {
qWarning() << "ERROR: could not open value file for gpio monitor" << m_gpio->gpioNumber(); qWarning(dcHardware()) << "GpioMonitor: Could not open value file for gpio monitor" << m_gpio->gpioNumber();
return false; return false;
} }
@ -123,9 +123,9 @@ void GpioMonitor::disable()
/*! Returns true if this \l{GpioMonitor} is running. */ /*! Returns true if this \l{GpioMonitor} is running. */
bool GpioMonitor::isRunning() const bool GpioMonitor::isRunning() const
{ {
if (!m_notifier) { if (!m_notifier)
return false; return false;
}
return m_notifier->isEnabled(); return m_notifier->isEnabled();
} }