1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +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

@ -1,336 +1,336 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GpsDisplayWidget</class>
<widget class="QWidget" name="GpsDisplayWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>570</width>
<height>302</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QGraphicsView" name="gpsWorld">
<property name="geometry">
<rect>
<x>190</x>
<y>10</y>
<width>171</width>
<height>171</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>171</width>
<height>171</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>171</width>
<height>171</height>
</size>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="toolTip">
<string extracomment="Shows locations of satellites"/>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sceneRect">
<rectf>
<x>0.000000000000000</x>
<y>0.000000000000000</y>
<width>169.000000000000000</width>
<height>169.000000000000000</height>
</rectf>
</property>
</widget>
<widget class="QGraphicsView" name="gpsGraph">
<property name="geometry">
<rect>
<x>370</x>
<y>114</y>
<width>191</width>
<height>67</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string extracomment="Individual satellite information"/>
</property>
</widget>
<widget class="QGroupBox" name="statusGroupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>4</y>
<width>171</width>
<height>177</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>GPS Status</string>
</property>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<width>171</width>
<height>151</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="horizontalSpacing">
<number>12</number>
</property>
<property name="verticalSpacing">
<number>1</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="status_label">
<property name="text">
<string>Status:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="status_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lat_label">
<property name="text">
<string>Latitude:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="lat_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="long_label">
<property name="text">
<string>Longitude:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="bear_label">
<property name="text">
<string>Bearing:</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="speed_label">
<property name="text">
<string>Speed:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="fix_label">
<property name="text">
<string>Fix Type:</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="gtime_label">
<property name="text">
<string>GPS Time:</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="gdate_label">
<property name="text">
<string>GPS Date:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="long_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="bear_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="speed_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="fix_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="gtime_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="gdate_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QGroupBox" name="dataStreamGroupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>180</y>
<width>551</width>
<height>111</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>GPS Data Stream</string>
</property>
<widget class="QTextBrowser" name="textBrowser">
<property name="geometry">
<rect>
<x>0</x>
<y>40</y>
<width>551</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<family>Courier New</family>
<pointsize>8</pointsize>
</font>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::WidgetWidth</enum>
</property>
</widget>
<widget class="QPushButton" name="connectButton">
<property name="geometry">
<rect>
<x>360</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Connect</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>450</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Disconnect</string>
</property>
</widget>
</widget>
<widget class="QGraphicsView" name="flatEarth">
<property name="geometry">
<rect>
<x>370</x>
<y>10</y>
<width>191</width>
<height>95</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="toolTip">
<string extracomment="Location of GCS on the Earth"/>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="optimizationFlags">
<set>QGraphicsView::DontAdjustForAntialiasing</set>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GpsDisplayWidget</class>
<widget class="QWidget" name="GpsDisplayWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>570</width>
<height>302</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QGraphicsView" name="gpsWorld">
<property name="geometry">
<rect>
<x>190</x>
<y>10</y>
<width>171</width>
<height>171</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>171</width>
<height>171</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>171</width>
<height>171</height>
</size>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="toolTip">
<string extracomment="Shows locations of satellites"/>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sceneRect">
<rectf>
<x>0.000000000000000</x>
<y>0.000000000000000</y>
<width>169.000000000000000</width>
<height>169.000000000000000</height>
</rectf>
</property>
</widget>
<widget class="QGraphicsView" name="gpsGraph">
<property name="geometry">
<rect>
<x>370</x>
<y>114</y>
<width>191</width>
<height>67</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string extracomment="Individual satellite information"/>
</property>
</widget>
<widget class="QGroupBox" name="statusGroupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>4</y>
<width>171</width>
<height>177</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>GPS Status</string>
</property>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<width>171</width>
<height>151</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="horizontalSpacing">
<number>12</number>
</property>
<property name="verticalSpacing">
<number>1</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="status_label">
<property name="text">
<string>Status:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="status_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lat_label">
<property name="text">
<string>Latitude:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="lat_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="long_label">
<property name="text">
<string>Longitude:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="bear_label">
<property name="text">
<string>Bearing:</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="speed_label">
<property name="text">
<string>Speed:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="fix_label">
<property name="text">
<string>Fix Type:</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="gtime_label">
<property name="text">
<string>GPS Time:</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="gdate_label">
<property name="text">
<string>GPS Date:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="long_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="bear_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="speed_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="fix_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="gtime_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="gdate_value">
<property name="text">
<string>Unknown</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QGroupBox" name="dataStreamGroupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>180</y>
<width>551</width>
<height>111</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>GPS Data Stream</string>
</property>
<widget class="QTextBrowser" name="textBrowser">
<property name="geometry">
<rect>
<x>0</x>
<y>40</y>
<width>551</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<family>Courier New</family>
<pointsize>8</pointsize>
</font>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::WidgetWidth</enum>
</property>
</widget>
<widget class="QPushButton" name="connectButton">
<property name="geometry">
<rect>
<x>360</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Connect</string>
</property>
</widget>
<widget class="QPushButton" name="disconnectButton">
<property name="geometry">
<rect>
<x>450</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Disconnect</string>
</property>
</widget>
</widget>
<widget class="QGraphicsView" name="flatEarth">
<property name="geometry">
<rect>
<x>370</x>
<y>10</y>
<width>191</width>
<height>95</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="toolTip">
<string extracomment="Location of GCS on the Earth"/>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="optimizationFlags">
<set>QGraphicsView::DontAdjustForAntialiasing</set>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>