From 992ccd3a047b7241d47e4613fa14239f2d2c9c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 30 Dec 2013 22:10:19 +0100 Subject: [PATCH] radio thread fixed --- libhive/devicemanager.cpp | 18 +++++++++ libhive/gpio.cpp | 77 +++++++++++++++++++++++---------------- libhive/gpio.h | 12 +++++- libhive/libhive.pro | 3 ++ libhive/radio433.cpp | 24 ++++-------- libhive/radio433.h | 1 - 6 files changed, 85 insertions(+), 50 deletions(-) diff --git a/libhive/devicemanager.cpp b/libhive/devicemanager.cpp index 96ada1c7..c296effc 100644 --- a/libhive/devicemanager.cpp +++ b/libhive/devicemanager.cpp @@ -17,7 +17,25 @@ DeviceManager::DeviceManager(QObject *parent) : { m_radio433 = new Radio433(this); +<<<<<<< HEAD QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection); +======= + qDebug() << "loading plugins"; + foreach (QObject *pluginObject, QPluginLoader::staticInstances()) { + DevicePlugin *pluginIface = qobject_cast(pluginObject); + qDebug() << "got plugin instance"; + if (pluginIface) { + qDebug() << "got device plugin" << pluginIface->pluginName(); + } + } + + // TODO: load dynamically +// RfSwitch *rfSwitch = new RfSwitch(this); +// m_supportedDevices.append(rfSwitch->supportedDevices()); +// m_devicePlugins.append(rfSwitch); + + +>>>>>>> radio thread fixed } QList DeviceManager::supportedDevices() diff --git a/libhive/gpio.cpp b/libhive/gpio.cpp index a7511365..135d492e 100644 --- a/libhive/gpio.cpp +++ b/libhive/gpio.cpp @@ -1,8 +1,9 @@ #include "gpio.h" #include + Gpio::Gpio(QObject *parent, int gpio) : - QObject(parent),m_gpio(gpio) + QThread(parent),m_gpio(gpio) { exportGpio(); } @@ -12,6 +13,46 @@ Gpio::~Gpio() unexportGpio(); } +void Gpio::run() +{ + struct pollfd fdset[2]; + int gpio_fd = openGpio(); + int nfds = 2; + int rc; + int timeout = 3000; + char buf[64]; + + + bool enabled = true; + + while(enabled){ + memset((void*)fdset, 0, sizeof(fdset)); + fdset[0].fd = STDIN_FILENO; + fdset[0].events = POLLIN; + + fdset[1].fd = gpio_fd; + fdset[1].events = POLLPRI; + + rc = poll(fdset, nfds, timeout); + + if (rc < 0) { + qDebug() << "ERROR: poll failed"; + return; + } + if(rc == 0){ + //timeout + } + if (fdset[1].revents & POLLPRI) { + read(fdset[1].fd, buf, 64); + emit pinInterrupt(); + } + + m_mutex.lock(); + enabled = m_enabled; + m_mutex.unlock(); + } +} + bool Gpio::exportGpio() { char buf[64]; @@ -174,35 +215,9 @@ bool Gpio::setEdgeInterrupt(int edge) return false; } -void Gpio::enableInterrupt() +void Gpio::stop() { - struct pollfd fdset[2]; - int gpio_fd = openGpio(); - int nfds = 2; - int rc; - int timeout = 3000; - char buf[64]; - - while(1){ - memset((void*)fdset, 0, sizeof(fdset)); - fdset[0].fd = STDIN_FILENO; - fdset[0].events = POLLIN; - - fdset[1].fd = gpio_fd; - fdset[1].events = POLLPRI; - - rc = poll(fdset, nfds, timeout); - - if (rc < 0) { - qDebug() << "ERROR: poll failed"; - return; - } - if(rc == 0){ - //timeout - } - if (fdset[1].revents & POLLPRI) { - read(fdset[1].fd, buf, 64); - emit pinInterrupt(); - } - } + m_mutex.lock(); + m_enabled = false; + m_mutex.unlock(); } diff --git a/libhive/gpio.h b/libhive/gpio.h index 2836f52b..8574af47 100644 --- a/libhive/gpio.h +++ b/libhive/gpio.h @@ -2,6 +2,10 @@ #define GPIO_H #include +#include +#include +#include + #include #include #include @@ -46,13 +50,15 @@ ********************************** */ -class Gpio : public QObject +class Gpio : public QThread { Q_OBJECT public: explicit Gpio(QObject *parent = 0, int gpio = 0); ~Gpio(); + void run() override; + bool exportGpio(); bool unexportGpio(); @@ -65,18 +71,20 @@ public: bool setEdgeInterrupt(int edge); + void stop(); private: int m_gpio; int m_dir; + QMutex m_mutex; + bool m_enabled; signals: void pinInterrupt(); public slots: - void enableInterrupt(); }; diff --git a/libhive/libhive.pro b/libhive/libhive.pro index 48b85bb2..dca77bf0 100644 --- a/libhive/libhive.pro +++ b/libhive/libhive.pro @@ -1,6 +1,9 @@ TARGET = hive TEMPLATE = lib +target.path = /usr/lib +INSTALLS += target + SOURCES += device.cpp \ deviceclass.cpp \ devicemanager.cpp \ diff --git a/libhive/radio433.cpp b/libhive/radio433.cpp index dde9e8e4..f2d4ca57 100644 --- a/libhive/radio433.cpp +++ b/libhive/radio433.cpp @@ -7,34 +7,31 @@ Radio433::Radio433(QObject *parent) : QObject(parent) { // Set up receiver - m_receiverThread = new QThread(this); - m_receiver = new Gpio(0,27); + m_receiver = new Gpio(this,27); m_receiver->setDirection(INPUT); m_receiver->setEdgeInterrupt(EDGE_BOTH); - m_receiver->moveToThread(m_receiverThread); // Set up transmitter m_transmitter = new Gpio(this,22); m_transmitter->setDirection(OUTPUT); m_transmitter->setValue(LOW); - connect(m_receiverThread,SIGNAL(finished()),this,SLOT(deleteLater())); connect(m_receiver,SIGNAL(pinInterrupt()),this,SLOT(handleInterrupt())); - enableReceiver(); + m_receiver->start(); } Radio433::~Radio433() { - m_receiverThread->quit(); - m_receiverThread->wait(); + m_receiver->quit(); + m_receiver->wait(); } void Radio433::sendData(QList rawData) { //first we have to disable our receiver, to prevent reading the hive signal it self - disableReceiver(); + m_receiver->stop(); m_transmitter->setValue(LOW); delayMicroseconds(500); @@ -47,7 +44,7 @@ void Radio433::sendData(QList rawData) } // reenable it - enableReceiver(); + m_receiver->start(); } @@ -133,16 +130,11 @@ void Radio433::handleInterrupt() void Radio433::enableReceiver() { qDebug() << "starting receiver"; - m_receiverThread->start(); - qDebug() << "fooo"; - QMetaObject::invokeMethod(m_receiver, SLOT(enableInterrupt()), Qt::QueuedConnection); -// m_receiver->enableInterrupt(); - qDebug() << "receiver enabeld."; + m_receiver->start(); } void Radio433::disableReceiver() { - m_receiverThread->quit(); - m_receiverThread->wait(); + m_receiver->stop(); qDebug() << "receiver disabeld."; } diff --git a/libhive/radio433.h b/libhive/radio433.h index 6324866b..af5ee1e0 100644 --- a/libhive/radio433.h +++ b/libhive/radio433.h @@ -21,7 +21,6 @@ public: private: Gpio *m_receiver; Gpio *m_transmitter; - QThread *m_receiverThread; unsigned int m_timings[RC_MAX_CHANGES]; unsigned int m_duration;