1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14:52:12 +01:00

Use event based IO instead of our own thread + polling, the manual says this is preferred/easier.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1457 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
cranphin 2010-08-29 21:20:32 +00:00 committed by cranphin
parent 78befcba73
commit 37e9dd52eb
7 changed files with 380 additions and 479 deletions

View File

@ -6,7 +6,6 @@ include(../../plugins/coreplugin/coreplugin.pri)
include(gpsdisplay_dependencies.pri)
include(../../libs/qwt/qwt.pri)
HEADERS += gpsdisplayplugin.h
HEADERS += gpsdisplaythread.h
HEADERS += buffer.h
HEADERS += nmeaparser.h
HEADERS += gpsdisplaygadget.h
@ -15,7 +14,6 @@ HEADERS += gpsdisplaygadgetfactory.h
HEADERS += gpsdisplaygadgetconfiguration.h
HEADERS += gpsdisplaygadgetoptionspage.h
SOURCES += gpsdisplayplugin.cpp
SOURCES += gpsdisplaythread.cpp
SOURCES += buffer.cpp
SOURCES += nmeaparser.cpp
SOURCES += gpsdisplaygadget.cpp

View File

@ -61,9 +61,9 @@ void GpsDisplayGadget::loadConfiguration(IUAVGadgetConfiguration* config)
if(nport.friendName == m->port())
{
#ifdef Q_OS_WIN
QextSerialPort *port=new QextSerialPort(nport.portName,portsettings,QextSerialPort::Polling);
QextSerialPort *port=new QextSerialPort(nport.portName,portsettings,QextSerialPort::EventDriven);
#else
QextSerialPort *port=new QextSerialPort(nport.physName,portsettings,QextSerialPort::Polling);
QextSerialPort *port=new QextSerialPort(nport.physName,portsettings,QextSerialPort::EventDriven);
#endif
//Creates new serial port with the user configuration and passes it to the widget
m_widget->setPort(port);

View File

@ -1,84 +0,0 @@
/**
******************************************************************************
*
* @file gpsdisplaythread.cpp
* @author Edouard Lafargue Copyright (C) 2010.
* @addtogroup GCSPlugins GCS Plugins
* @{
* @addtogroup GPSGadgetPlugin GPS Gadget Plugin
* @{
* @brief A gadget that displays GPS status and enables basic configuration
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gpsdisplaythread.h"
#include <QDebug>
GpsDisplayThread::GpsDisplayThread(QObject *parent)
: QThread(parent)
{
}
GpsDisplayThread::~GpsDisplayThread()
{
}
void GpsDisplayThread::setPort(QextSerialPort* port)
{
this->port=port;
}
void GpsDisplayThread::setParser(NMEAParser* parser)
{
this->parser=parser;
}
void GpsDisplayThread::run()
{
qDebug() << "Opening.";
if (port) {
qDebug() << port->portName();
bool isOpen = port->open(QIODevice::ReadWrite);
qDebug() << "Open: " << isOpen;
char buf[1024];
char c;
while(true) {
qDebug() << "Reading.";
/*qint64 bytesRead = port->readLine(buf, sizeof(buf));
qDebug() << "bytesRead: " << bytesRead;
if (bytesRead != -1) {
qDebug() << "Result: '" << buf << "'";
}*/
while(port->bytesAvailable()>0)
{
port->read(&c,1);
parser->processInputStream(c);
}
yieldCurrentThread();
}
} else {
qDebug() << "Port undefined or invalid.";
}
}

View File

@ -1,51 +0,0 @@
/**
******************************************************************************
*
* @file gpsdisplaythread.h
* @author Edouard Lafargue Copyright (C) 2010.
* @addtogroup GCSPlugins GCS Plugins
* @{
* @addtogroup GPSGadgetPlugin GPS Gadget Plugin
* @{
* @brief A gadget that displays GPS status and enables basic configuration
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef GPSDISPLAYTHREAD_H
#define GPSDISPLAYTHREAD_H
#include <QThread>
#include <qextserialport/src/qextserialport.h>
#include "nmeaparser.h"
class GpsDisplayThread : public QThread
{
Q_OBJECT
public:
GpsDisplayThread(QObject *parent = 0);
~GpsDisplayThread();
QextSerialPort *port;
NMEAParser *parser;
void setPort(QextSerialPort* port);
void setParser(NMEAParser* parser);
void run();
};
#endif // GPSDISPLAYTHREAD_H

View File

@ -73,6 +73,8 @@ GpsDisplayWidget::GpsDisplayWidget(QWidget *parent) : QWidget(parent)
connect(widget->connectButton, SIGNAL(clicked(bool)),
this,SLOT(connectButtonClicked()));
connect(widget->disconnectButton, SIGNAL(clicked(bool)),
this,SLOT(disconnectButtonClicked()));
parser=new NMEAParser();
connect(parser,SIGNAL(sv(int)),this,SLOT(setSVs(int)));
connect(parser,SIGNAL(position(double,double,double)),this,SLOT(setPosition(double,double,double)));
@ -160,9 +162,42 @@ void GpsDisplayWidget::setParser(NMEAParser* parser)
}
void GpsDisplayWidget::connectButtonClicked() {
GpsDisplayThread* gpsThread = new GpsDisplayThread();
widget->textBrowser->append(QString("Connecting to GPS ...\n"));
gpsThread->setPort(port);
gpsThread->setParser(parser);
gpsThread->start();
// TODO: Somehow mark that we're running, and disable connect button while so?
if (port) {
connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
qDebug() << "Opening: " << port->portName() << ".";
bool isOpen = port->open(QIODevice::ReadWrite);
qDebug() << "Open: " << isOpen;
} else {
qDebug() << "Port undefined or invalid.";
}
}
void GpsDisplayWidget::disconnectButtonClicked() {
}
void GpsDisplayWidget::onDataAvailable() {
int avail = port->bytesAvailable();
if( avail > 0 ) {
QByteArray serialData;
serialData.resize(avail);
int bytesRead = port->read(serialData.data(), serialData.size());
if( bytesRead > 0 ) {
processNewSerialData(serialData);
}
}
}
void GpsDisplayWidget::processNewSerialData(QByteArray serialData) {
int dataLength = serialData.size();
const char* data = serialData.constData();
for(int pos = 0; pos < dataLength; pos++) {
parser->processInputStream(data[pos]);
}
}

View File

@ -54,6 +54,8 @@ public:
private slots:
void connectButtonClicked();
void disconnectButtonClicked();
void onDataAvailable();
void setSVs(int);
void setPosition(double, double, double);
void setDateTime(double, double);
@ -61,6 +63,7 @@ private slots:
void dumpPacket(char*);
private:
void processNewSerialData(QByteArray serialData);
Ui_GpsDisplayWidget* widget;
QextSerialPort *port;
NMEAParser *parser;

View File

@ -285,7 +285,7 @@
<string>Connect</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="disconnectButton">
<property name="geometry">
<rect>
<x>450</x>