radio thread fixed

This commit is contained in:
Simon Stürz 2013-12-30 22:10:19 +01:00
parent afb3ef0a02
commit 992ccd3a04
6 changed files with 85 additions and 50 deletions

View File

@ -17,7 +17,25 @@ DeviceManager::DeviceManager(QObject *parent) :
{ {
m_radio433 = new Radio433(this); m_radio433 = new Radio433(this);
<<<<<<< HEAD
QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection);
=======
qDebug() << "loading plugins";
foreach (QObject *pluginObject, QPluginLoader::staticInstances()) {
DevicePlugin *pluginIface = qobject_cast<DevicePlugin*>(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<DeviceClass> DeviceManager::supportedDevices() QList<DeviceClass> DeviceManager::supportedDevices()

View File

@ -1,8 +1,9 @@
#include "gpio.h" #include "gpio.h"
#include <QDebug> #include <QDebug>
Gpio::Gpio(QObject *parent, int gpio) : Gpio::Gpio(QObject *parent, int gpio) :
QObject(parent),m_gpio(gpio) QThread(parent),m_gpio(gpio)
{ {
exportGpio(); exportGpio();
} }
@ -12,6 +13,46 @@ Gpio::~Gpio()
unexportGpio(); 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() bool Gpio::exportGpio()
{ {
char buf[64]; char buf[64];
@ -174,35 +215,9 @@ bool Gpio::setEdgeInterrupt(int edge)
return false; return false;
} }
void Gpio::enableInterrupt() void Gpio::stop()
{ {
struct pollfd fdset[2]; m_mutex.lock();
int gpio_fd = openGpio(); m_enabled = false;
int nfds = 2; m_mutex.unlock();
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();
}
}
} }

View File

@ -2,6 +2,10 @@
#define GPIO_H #define GPIO_H
#include <QObject> #include <QObject>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -46,13 +50,15 @@
********************************** **********************************
*/ */
class Gpio : public QObject class Gpio : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit Gpio(QObject *parent = 0, int gpio = 0); explicit Gpio(QObject *parent = 0, int gpio = 0);
~Gpio(); ~Gpio();
void run() override;
bool exportGpio(); bool exportGpio();
bool unexportGpio(); bool unexportGpio();
@ -65,18 +71,20 @@ public:
bool setEdgeInterrupt(int edge); bool setEdgeInterrupt(int edge);
void stop();
private: private:
int m_gpio; int m_gpio;
int m_dir; int m_dir;
QMutex m_mutex;
bool m_enabled;
signals: signals:
void pinInterrupt(); void pinInterrupt();
public slots: public slots:
void enableInterrupt();
}; };

View File

@ -1,6 +1,9 @@
TARGET = hive TARGET = hive
TEMPLATE = lib TEMPLATE = lib
target.path = /usr/lib
INSTALLS += target
SOURCES += device.cpp \ SOURCES += device.cpp \
deviceclass.cpp \ deviceclass.cpp \
devicemanager.cpp \ devicemanager.cpp \

View File

@ -7,34 +7,31 @@ Radio433::Radio433(QObject *parent) :
QObject(parent) QObject(parent)
{ {
// Set up receiver // Set up receiver
m_receiverThread = new QThread(this); m_receiver = new Gpio(this,27);
m_receiver = new Gpio(0,27);
m_receiver->setDirection(INPUT); m_receiver->setDirection(INPUT);
m_receiver->setEdgeInterrupt(EDGE_BOTH); m_receiver->setEdgeInterrupt(EDGE_BOTH);
m_receiver->moveToThread(m_receiverThread);
// Set up transmitter // Set up transmitter
m_transmitter = new Gpio(this,22); m_transmitter = new Gpio(this,22);
m_transmitter->setDirection(OUTPUT); m_transmitter->setDirection(OUTPUT);
m_transmitter->setValue(LOW); m_transmitter->setValue(LOW);
connect(m_receiverThread,SIGNAL(finished()),this,SLOT(deleteLater()));
connect(m_receiver,SIGNAL(pinInterrupt()),this,SLOT(handleInterrupt())); connect(m_receiver,SIGNAL(pinInterrupt()),this,SLOT(handleInterrupt()));
enableReceiver(); m_receiver->start();
} }
Radio433::~Radio433() Radio433::~Radio433()
{ {
m_receiverThread->quit(); m_receiver->quit();
m_receiverThread->wait(); m_receiver->wait();
} }
void Radio433::sendData(QList<int> rawData) void Radio433::sendData(QList<int> rawData)
{ {
//first we have to disable our receiver, to prevent reading the hive signal it self //first we have to disable our receiver, to prevent reading the hive signal it self
disableReceiver(); m_receiver->stop();
m_transmitter->setValue(LOW); m_transmitter->setValue(LOW);
delayMicroseconds(500); delayMicroseconds(500);
@ -47,7 +44,7 @@ void Radio433::sendData(QList<int> rawData)
} }
// reenable it // reenable it
enableReceiver(); m_receiver->start();
} }
@ -133,16 +130,11 @@ void Radio433::handleInterrupt()
void Radio433::enableReceiver() void Radio433::enableReceiver()
{ {
qDebug() << "starting receiver"; qDebug() << "starting receiver";
m_receiverThread->start(); m_receiver->start();
qDebug() << "fooo";
QMetaObject::invokeMethod(m_receiver, SLOT(enableInterrupt()), Qt::QueuedConnection);
// m_receiver->enableInterrupt();
qDebug() << "receiver enabeld.";
} }
void Radio433::disableReceiver() void Radio433::disableReceiver()
{ {
m_receiverThread->quit(); m_receiver->stop();
m_receiverThread->wait();
qDebug() << "receiver disabeld."; qDebug() << "receiver disabeld.";
} }

View File

@ -21,7 +21,6 @@ public:
private: private:
Gpio *m_receiver; Gpio *m_receiver;
Gpio *m_transmitter; Gpio *m_transmitter;
QThread *m_receiverThread;
unsigned int m_timings[RC_MAX_CHANGES]; unsigned int m_timings[RC_MAX_CHANGES];
unsigned int m_duration; unsigned int m_duration;