1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

GPS Viewer: Delete port/parser when configuration gets reloaded, and

make connect/disconnect work properly for serial.
This did include some refactoring, sorry if that interfers with someone
else's work! Should be too hard to merge tho (and I couldn't find   
Edouard online, and I'll be gone the rest of the weekend :) ).


git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1512 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
cranphin 2010-09-03 22:04:54 +00:00 committed by cranphin
parent 84d054c4ed
commit 6e9b745f18
5 changed files with 136 additions and 146 deletions

View File

@ -31,8 +31,11 @@
GpsDisplayGadget::GpsDisplayGadget(QString classId, GpsDisplayWidget *widget, QWidget *parent) :
IUAVGadget(classId, parent),
m_widget(widget)
m_widget(widget),
connected(FALSE)
{
connect(m_widget->connectButton, SIGNAL(clicked(bool)), this,SLOT(onConnect()));
connect(m_widget->disconnectButton, SIGNAL(clicked(bool)), this,SLOT(onDisconnect()));
}
GpsDisplayGadget::~GpsDisplayGadget()
@ -47,35 +50,110 @@ GpsDisplayGadget::~GpsDisplayGadget()
*/
void GpsDisplayGadget::loadConfiguration(IUAVGadgetConfiguration* config)
{
GpsDisplayGadgetConfiguration *m = qobject_cast< GpsDisplayGadgetConfiguration*>(config);
// Delete the (old)port, this also closes it.
if(port) {
delete port;
}
if (m->connectionMode() == "Serial") {
// Delete the (old)parser, this also disconnects all signals.
if(parser) {
delete parser;
}
GpsDisplayGadgetConfiguration *gpsDisplayConfig = qobject_cast< GpsDisplayGadgetConfiguration*>(config);
if (gpsDisplayConfig->connectionMode() == "Serial") {
PortSettings portsettings;
portsettings.BaudRate=m->speed();
portsettings.DataBits=m->dataBits();
portsettings.FlowControl=m->flow();
portsettings.Parity=m->parity();
portsettings.StopBits=m->stopBits();
portsettings.Timeout_Millisec=m->timeOut();
portsettings.BaudRate=gpsDisplayConfig->speed();
portsettings.DataBits=gpsDisplayConfig->dataBits();
portsettings.FlowControl=gpsDisplayConfig->flow();
portsettings.Parity=gpsDisplayConfig->parity();
portsettings.StopBits=gpsDisplayConfig->stopBits();
portsettings.Timeout_Millisec=gpsDisplayConfig->timeOut();
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
foreach( QextPortInfo nport, ports ) {
if(nport.friendName == m->port())
if(nport.friendName == gpsDisplayConfig->port())
{
qDebug() << "Using Serial parser";
parser = new NMEAParser();
#ifdef Q_OS_WIN
QextSerialPort *port=new QextSerialPort(nport.portName,portsettings,QextSerialPort::EventDriven);
port=new QextSerialPort(nport.portName,portsettings,QextSerialPort::EventDriven);
#else
QextSerialPort *port=new QextSerialPort(nport.physName,portsettings,QextSerialPort::EventDriven);
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->setParser(QString("Serial"));
m_widget->setPort(port);
m_widget->connectButton->setEnabled(true);
m_widget->disconnectButton->setEnabled(false);
connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
}
}
} else if (m->connectionMode() == "Telemetry") {
m_widget->setParser(QString("Telemetry"));
} else if (m->connectionMode() == "Network") {
} else if (gpsDisplayConfig->connectionMode() == "Telemetry") {
qDebug() << "Using Telemetry parser";
parser = new TelemetryParser();
m_widget->connectButton->setEnabled(false);
m_widget->disconnectButton->setEnabled(false);
} else if (gpsDisplayConfig->connectionMode() == "Network") {
// Not implemented for now...
m_widget->connectButton->setEnabled(false);
m_widget->disconnectButton->setEnabled(false);
}
connect(parser, SIGNAL(sv(int)), m_widget,SLOT(setSVs(int)));
connect(parser, SIGNAL(position(double,double,double)), m_widget,SLOT(setPosition(double,double,double)));
connect(parser, SIGNAL(speedheading(double,double)), m_widget,SLOT(setSpeedHeading(double,double)));
connect(parser, SIGNAL(datetime(double,double)), m_widget,SLOT(setDateTime(double,double)));
connect(parser, SIGNAL(packet(char*)), m_widget, SLOT(dumpPacket(char*)));
connect(parser, SIGNAL(satellite(int,int,int,int,int)), m_widget->gpsSky, SLOT(updateSat(int,int,int,int,int)));
}
void GpsDisplayGadget::onConnect() {
m_widget->textBrowser->append(QString("Connecting to GPS ...\n"));
// TODO: Somehow mark that we're running, and disable connect button while so?
if (port) {
qDebug() << "Opening: " << port->portName() << ".";
bool isOpen = port->open(QIODevice::ReadWrite);
qDebug() << "Open: " << isOpen;
if(isOpen) {
m_widget->connectButton->setEnabled(false);
m_widget->disconnectButton->setEnabled(true);
}
} else {
qDebug() << "Port undefined or invalid.";
}
}
void GpsDisplayGadget::onDisconnect() {
if (port) {
qDebug() << "Closing: " << port->portName() << ".";
port->close();
m_widget->connectButton->setEnabled(true);
m_widget->disconnectButton->setEnabled(false);
} else {
qDebug() << "Port undefined or invalid.";
}
}
void GpsDisplayGadget::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 GpsDisplayGadget::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

@ -32,6 +32,8 @@
#include <qextserialport/src/qextserialenumerator.h>
#include <coreplugin/iuavgadget.h>
#include "gpsdisplaywidget.h"
#include "nmeaparser.h"
#include "telemetryparser.h"
class IUAVGadget;
class QWidget;
@ -48,10 +50,23 @@ public:
~GpsDisplayGadget();
QWidget *widget() { return m_widget; }
// void setMode(QString mode); // Either UAVTalk or serial port
void loadConfiguration(IUAVGadgetConfiguration* config);
public slots:
void onConnect();
void onDisconnect();
private slots:
void onDataAvailable();
private:
GpsDisplayWidget *m_widget;
QPointer<GpsDisplayWidget> m_widget;
QPointer<QextSerialPort> port;
QPointer<GPSParser> parser;
bool connected;
void processNewSerialData(QByteArray serialData);
};

View File

@ -26,41 +26,29 @@
*/
#include "gpsdisplaywidget.h"
#include "ui_gpsdisplaywidget.h"
#include "extensionsystem/pluginmanager.h"
#include "uavobjects/uavobjectmanager.h"
#include <iostream>
#include <QtGui>
#include <QDebug>
#include "nmeaparser.h"
#include "telemetryparser.h"
/*
* Initialize the widget
*/
GpsDisplayWidget::GpsDisplayWidget(QWidget *parent) : QWidget(parent)
{
widget = new Ui_GpsDisplayWidget();
widget->setupUi(this);
setupUi(this);
//Not elegant, just load the image for now
QGraphicsScene *fescene = new QGraphicsScene(this);
QPixmap earthpix( ":/gpsgadget/images/flatEarth.png" );
fescene->addPixmap( earthpix );
widget->flatEarth->setScene(fescene);
connect(widget->connectButton, SIGNAL(clicked(bool)),
this,SLOT(connectButtonClicked()));
connect(widget->disconnectButton, SIGNAL(clicked(bool)),
this,SLOT(disconnectButtonClicked()));
flatEarth->setScene(fescene);
}
GpsDisplayWidget::~GpsDisplayWidget()
{
delete widget;
}
void GpsDisplayWidget::setSpeedHeading(double speed, double heading)
@ -69,10 +57,10 @@ void GpsDisplayWidget::setSpeedHeading(double speed, double heading)
temp.append(QString::number(speed,'g',10));
temp.append(" Heaging: ");
temp.append(QString::number(heading,'g',10));
widget->speed_value->setText(QString::number(speed,'g',10));
widget->speed_value->adjustSize();
widget->bear_value->setText(QString::number(heading,'g',10));
widget->bear_value->adjustSize();
speed_value->setText(QString::number(speed,'g',10));
speed_value->adjustSize();
bear_value->setText(QString::number(heading,'g',10));
bear_value->adjustSize();
// widget->textBrowser->append(temp);
}
@ -83,25 +71,25 @@ void GpsDisplayWidget::setDateTime(double date, double time)
temp.append(QString::number(date,'g',10));
temp.append(" Time: ");
temp.append(QString::number(time,'g',10));
widget->gdate_value->setText(QString::number(date,'g',10));
widget->gdate_value->adjustSize();
widget->gtime_value->setText(QString::number(time,'g',10));
widget->gdate_value->adjustSize();
gdate_value->setText(QString::number(date,'g',10));
gdate_value->adjustSize();
gtime_value->setText(QString::number(time,'g',10));
gdate_value->adjustSize();
//widget->textBrowser->append(temp);
//textBrowser->append(temp);
}
void GpsDisplayWidget::dumpPacket(char *packet)
{
widget->textBrowser->append(QString(packet));
textBrowser->append(QString(packet));
}
void GpsDisplayWidget::setSVs(int sv)
{
QString temp = "Fix: Sats: ";
temp.append(QString::number(sv));
widget->status_value->setText(temp);
widget->status_value->adjustSize();
status_value->setText(temp);
status_value->adjustSize();
}
void GpsDisplayWidget::setPosition(double lat, double lon, double alt)
@ -112,86 +100,12 @@ void GpsDisplayWidget::setPosition(double lat, double lon, double alt)
temp.append(QString::number(lon,'g',10));
temp.append(" ");
temp.append(QString::number(alt,'g',10));
widget->lat_value->setText(QString::number(lat,'g',10));
widget->lat_value->adjustSize();
widget->long_value->setText(QString::number(lon,'g',10));
widget->long_value->adjustSize();
//widget->alt_value->setText(QString::number(alt,'g',10));
//widget->alt_value->adjustSize();
lat_value->setText(QString::number(lat,'g',10));
lat_value->adjustSize();
long_value->setText(QString::number(lon,'g',10));
long_value->adjustSize();
//alt_value->setText(QString::number(alt,'g',10));
//alt_value->adjustSize();
//widget->textBrowser->append(temp);
}
void GpsDisplayWidget::setPort(QextSerialPort* port)
{
this->port=port;
}
void GpsDisplayWidget::setParser(QString connectionMode)
{
if (connectionMode == "Serial") {
qDebug() << "Using Serial parser";
parser = new NMEAParser();
widget->connectButton->setEnabled(true);
widget->disconnectButton->setEnabled(false);
} else if (connectionMode == "Telemetry") {
qDebug() << "Using Telemetry parser";
parser = new TelemetryParser();
widget->connectButton->setEnabled(false);
widget->disconnectButton->setEnabled(false);
} else {
qDebug() << "Using Default parser";
parser = new NMEAParser(); // for the time being...
}
connect(parser,SIGNAL(sv(int)),this,SLOT(setSVs(int)));
connect(parser,SIGNAL(position(double,double,double)),this,SLOT(setPosition(double,double,double)));
connect(parser,SIGNAL(speedheading(double,double)),this,SLOT(setSpeedHeading(double,double)));
connect(parser,SIGNAL(datetime(double,double)),this,SLOT(setDateTime(double,double)));
connect(parser,SIGNAL(packet(char*)), this, SLOT(dumpPacket(char*)));
connect(parser, SIGNAL(satellite(int,int,int,int,int)), widget->gpsSky, SLOT(updateSat(int,int,int,int,int)));
port = NULL;
}
void GpsDisplayWidget::connectButtonClicked() {
widget->textBrowser->append(QString("Connecting to GPS ...\n"));
// 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]);
}
//textBrowser->append(temp);
}

View File

@ -28,6 +28,7 @@
#ifndef GPSDISPLAYWIDGET_H_
#define GPSDISPLAYWIDGET_H_
#include "ui_gpsdisplaywidget.h"
#include "gpsdisplaygadgetconfiguration.h"
#include "gpsconstellationwidget.h"
#include "uavobjects/uavobject.h"
@ -35,13 +36,9 @@
#include <QtSvg/QSvgRenderer>
#include <QtSvg/QGraphicsSvgItem>
#include <QFile>
#include <QTimer>
#include "nmeaparser.h"
class Ui_GpsDisplayWidget;
class GpsDisplayWidget : public QWidget
class GpsDisplayWidget : public QWidget, public Ui_GpsDisplayWidget
{
Q_OBJECT
@ -49,14 +46,7 @@ public:
GpsDisplayWidget(QWidget *parent = 0);
~GpsDisplayWidget();
// void setMode(QString mode); // Either UAVTalk or serial port
void setPort(QextSerialPort* port);
void setParser(QString connectionMode);
private slots:
void connectButtonClicked();
void disconnectButtonClicked();
void onDataAvailable();
void setSVs(int);
void setPosition(double, double, double);
void setDateTime(double, double);
@ -64,12 +54,6 @@ private slots:
void dumpPacket(char*);
private:
void processNewSerialData(QByteArray serialData);
Ui_GpsDisplayWidget* widget;
GpsConstellationWidget * gpsConstellation;
QextSerialPort *port;
GPSParser *parser;
bool connected;
};
#endif /* GPSDISPLAYWIDGET_H_ */

View File

@ -65,7 +65,6 @@
*/
NMEAParser::NMEAParser(QObject *parent):GPSParser(parent)
{
qDebug() << "NMEAParser::NMEAParser(" << parent << ")";
bufferInit(&gpsRxBuffer, (unsigned char *)gpsRxData, 512);
gpsRxOverflow=0;
}