LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / gpsdisplay / gpsdisplaygadget.cpp
blob9763b7d0c0b6f55352fad5ee4bb44a6a1bce509e
1 /**
2 ******************************************************************************
4 * @file gpsdisplaygadget.cpp
5 * @author Edouard Lafargue Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup GPSGadgetPlugin GPS Gadget Plugin
9 * @{
10 * @brief A gadget that displays GPS status and enables basic configuration
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "gpsdisplaygadget.h"
29 #include "gpsdisplaywidget.h"
30 #include "gpsdisplaygadgetconfiguration.h"
32 GpsDisplayGadget::GpsDisplayGadget(QString classId, GpsDisplayWidget *widget, QWidget *parent) :
33 IUAVGadget(classId, parent),
34 m_widget(widget),
35 connected(false)
37 connect(m_widget->connectButton, SIGNAL(clicked(bool)), this, SLOT(onConnect()));
38 connect(m_widget->disconnectButton, SIGNAL(clicked(bool)), this, SLOT(onDisconnect()));
41 GpsDisplayGadget::~GpsDisplayGadget()
43 delete m_widget;
47 This is called when a configuration is loaded, and updates the plugin's settings.
48 Careful: the plugin is already drawn before the loadConfiguration method is called the
49 first time, so you have to be careful not to assume all the plugin values are initialized
50 the first time you use them
52 void GpsDisplayGadget::loadConfiguration(IUAVGadgetConfiguration *config)
54 // Delete the (old)port, this also closes it.
55 if (port) {
56 delete port;
59 // Delete the (old)parser, this also disconnects all signals.
60 if (parser) {
61 delete parser;
64 GpsDisplayGadgetConfiguration *gpsDisplayConfig = qobject_cast< GpsDisplayGadgetConfiguration *>(config);
66 if (gpsDisplayConfig->connectionMode() == "Serial") {
67 m_portsettings.BaudRate = gpsDisplayConfig->speed();
68 m_portsettings.DataBits = gpsDisplayConfig->dataBits();
69 m_portsettings.FlowControl = gpsDisplayConfig->flow();
70 m_portsettings.Parity = gpsDisplayConfig->parity();
71 m_portsettings.StopBits = gpsDisplayConfig->stopBits();
72 m_portsettings.Timeout_Millisec = gpsDisplayConfig->timeOut();
74 // In case we find no port, buttons disabled
75 m_widget->connectButton->setEnabled(false);
76 m_widget->disconnectButton->setEnabled(false);
78 QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
79 foreach(QSerialPortInfo nport, ports) {
80 if (nport.portName() == gpsDisplayConfig->port()) {
81 qDebug() << "Using Serial parser";
82 parser = new NMEAParser();
83 port = new QSerialPort(nport);
84 m_widget->connectButton->setEnabled(true);
85 m_widget->disconnectButton->setEnabled(false);
86 m_widget->connectButton->setHidden(false);
87 m_widget->disconnectButton->setHidden(false);
89 connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
92 m_widget->dataStreamGroupBox->setHidden(false);
93 } else if (gpsDisplayConfig->connectionMode() == "Telemetry") {
94 qDebug() << "Using Telemetry parser";
95 parser = new TelemetryParser();
96 m_widget->disconnectButton->setHidden(true);
97 m_widget->connectButton->setHidden(true);
98 m_widget->dataStreamGroupBox->setHidden(true);
99 } else if (gpsDisplayConfig->connectionMode() == "Network") {
100 // Not implemented for now...
101 m_widget->connectButton->setEnabled(false);
102 m_widget->disconnectButton->setEnabled(false);
103 m_widget->dataStreamGroupBox->setHidden(false);
106 connect(parser, SIGNAL(sv(int)), m_widget, SLOT(setSVs(int)));
107 connect(parser, SIGNAL(position(double, double, double)), m_widget, SLOT(setPosition(double, double, double)));
108 connect(parser, SIGNAL(speedheading(double, double)), m_widget, SLOT(setSpeedHeading(double, double)));
109 connect(parser, SIGNAL(datetime(double, double)), m_widget, SLOT(setDateTime(double, double)));
110 connect(parser, SIGNAL(packet(QString)), m_widget, SLOT(dumpPacket(QString)));
111 connect(parser, SIGNAL(satellite(int, int, int, int, int)), m_widget->gpsSky, SLOT(updateSat(int, int, int, int, int)));
112 connect(parser, SIGNAL(satellite(int, int, int, int, int)), m_widget->gpsSnrWidget, SLOT(updateSat(int, int, int, int, int)));
113 connect(parser, SIGNAL(fixtype(QString)), m_widget, SLOT(setFixType(QString)));
114 connect(parser, SIGNAL(dop(double, double, double)), m_widget, SLOT(setDOP(double, double, double)));
117 void GpsDisplayGadget::onConnect()
119 m_widget->textBrowser->append(QString("Connecting to GPS ...\n"));
120 // TODO: Somehow mark that we're running, and disable connect button while so?
122 if (port) {
123 qDebug() << "Opening: " << port->portName() << ".";
124 bool isOpen = port->open(QIODevice::ReadWrite);
125 qDebug() << "Open: " << isOpen;
126 if (isOpen) {
127 if (port->setBaudRate(m_portsettings.BaudRate)
128 && port->setDataBits(m_portsettings.DataBits)
129 && port->setParity(m_portsettings.Parity)
130 && port->setStopBits(m_portsettings.StopBits)
131 && port->setFlowControl(m_portsettings.FlowControl)) {
132 m_widget->connectButton->setEnabled(false);
133 m_widget->disconnectButton->setEnabled(true);
136 } else {
137 qDebug() << "Port undefined or invalid.";
141 void GpsDisplayGadget::onDisconnect()
143 if (port) {
144 qDebug() << "Closing: " << port->portName() << ".";
145 port->close();
146 m_widget->connectButton->setEnabled(true);
147 m_widget->disconnectButton->setEnabled(false);
148 } else {
149 qDebug() << "Port undefined or invalid.";
153 void GpsDisplayGadget::onDataAvailable()
155 int avail = port->bytesAvailable();
157 if (avail > 0) {
158 QByteArray serialData;
159 serialData.resize(avail);
160 int bytesRead = port->read(serialData.data(), serialData.size());
161 if (bytesRead > 0) {
162 processNewSerialData(serialData);
167 void GpsDisplayGadget::processNewSerialData(QByteArray serialData)
169 int dataLength = serialData.size();
170 const char *data = serialData.constData();
172 for (int pos = 0; pos < dataLength; pos++) {
173 parser->processInputStream(data[pos]);