SerialConfigurationWindow.cc 9.68 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

7
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
8

9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    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.

14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    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
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25

======================================================================*/

/**
 * @file
lm's avatar
lm committed
26
 *   @brief Implementation of SerialConfigurationWindow
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QDebug>
#include <QDir>
33
#include <QSettings>
pixhawk's avatar
pixhawk committed
34 35
#include <QFileInfoList>

Bryant's avatar
Bryant committed
36 37 38 39
#include "SerialConfigurationWindow.h"
#include "SerialLinkInterface.h"
#include "SerialLink.h"

pixhawk's avatar
pixhawk committed
40
SerialConfigurationWindow::SerialConfigurationWindow(LinkInterface* link, QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags),
41
    userConfigured(false)
pixhawk's avatar
pixhawk committed
42 43 44
{
    SerialLinkInterface* serialLink = dynamic_cast<SerialLinkInterface*>(link);

45 46
    if(serialLink != 0)
    {
47
        serialLink->loadSettings();
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53 54 55
        this->link = serialLink;

        // Setup the user interface according to link type
        ui.setupUi(this);

        // Create action to open this menu
        // Create configuration action for this link
        // Connect the current UAS
56
        action = new QAction(QIcon(":/files/images/devices/network-wireless.svg"), "", this);
pixhawk's avatar
pixhawk committed
57
        setLinkName(link->getName());
58

59 60 61 62
        // Scan for serial ports. Let the user know if none were found for debugging purposes
        if (!setupPortList()) {
            qDebug() << "No serial ports found.";
        }
63

64 65
        // Set up baud rates
        ui.baudRate->clear();
66 67 68 69 70 71 72 73 74 75 76 77 78
		
		// Keep track of all desired baud rates by OS. These are iterated through
		// later and added to ui.baudRate.
		QList<int> supportedBaudRates;

		// Baud rates supported only by POSIX systems
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
		supportedBaudRates << 50;
		supportedBaudRates << 75;
		supportedBaudRates << 134;
		supportedBaudRates << 150;
		supportedBaudRates << 200;
		supportedBaudRates << 1800;
79
#endif
80 81 82 83 84 85 86

		// Baud rates supported only by Windows
#if defined(Q_OS_WIN)
		supportedBaudRates << 14400;
		supportedBaudRates << 56000;
		supportedBaudRates << 128000;
		supportedBaudRates << 256000;
87
#endif
88 89 90 91 92 93 94 95 96 97 98 99 100

		// Baud rates supported by everyone
		supportedBaudRates << 110;
		supportedBaudRates << 300;
		supportedBaudRates << 600;
		supportedBaudRates << 1200;
		supportedBaudRates << 2400;
		supportedBaudRates << 4800;
		supportedBaudRates << 9600;
		supportedBaudRates << 19200;
		supportedBaudRates << 38400;
		supportedBaudRates << 57600;
		supportedBaudRates << 115200;
101 102 103 104 105 106 107 108 109 110
        supportedBaudRates << 230400;
        supportedBaudRates << 460800;

#if defined(Q_OS_LINUX)
        // Baud rates supported only by Linux
        supportedBaudRates << 500000;
        supportedBaudRates << 576000;
#endif

        supportedBaudRates << 921600;
111 112 113 114 115 116
		
		// Now actually add all of our supported baud rates to the UI.
		qSort(supportedBaudRates.begin(), supportedBaudRates.end());
		for (int i = 0; i < supportedBaudRates.size(); ++i) {
			ui.baudRate->addItem(QString::number(supportedBaudRates.at(i)), supportedBaudRates.at(i));
		}
117

118 119 120
        // Load current link config
        ui.portName->setCurrentIndex(ui.baudRate->findText(QString("%1").arg(this->link->getPortName())));

pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127 128
        connect(action, SIGNAL(triggered()), this, SLOT(configureCommunication()));

        // Make sure that a change in the link name will be reflected in the UI
        connect(link, SIGNAL(nameChanged(QString)), this, SLOT(setLinkName(QString)));

        // Connect the individual user interface inputs
        connect(ui.portName, SIGNAL(editTextChanged(QString)), this, SLOT(setPortName(QString)));
        connect(ui.portName, SIGNAL(currentIndexChanged(QString)), this, SLOT(setPortName(QString)));
Bryant's avatar
Bryant committed
129
        connect(ui.baudRate, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated), this, &SerialConfigurationWindow::setBaudRate);
pixhawk's avatar
pixhawk committed
130
        connect(ui.flowControlCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableFlowControl(bool)));
lm's avatar
lm committed
131 132 133
        connect(ui.parNone, SIGNAL(toggled(bool)), this, SLOT(setParityNone(bool)));
        connect(ui.parOdd, SIGNAL(toggled(bool)), this, SLOT(setParityOdd(bool)));
        connect(ui.parEven, SIGNAL(toggled(bool)), this, SLOT(setParityEven(bool)));
Bryant's avatar
Bryant committed
134 135
        connect(ui.dataBitsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &SerialConfigurationWindow::setDataBits);
        connect(ui.stopBitsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &SerialConfigurationWindow::setStopBits);
136 137
        connect(ui.advCheckBox, SIGNAL(clicked(bool)), ui.advGroupBox, SLOT(setVisible(bool)));
        ui.advCheckBox->setCheckable(true);
138 139
        ui.advCheckBox->setChecked(false);
        ui.advGroupBox->setVisible(false);
pixhawk's avatar
pixhawk committed
140

141
        switch(this->link->getParityType()) {
pixhawk's avatar
pixhawk committed
142 143 144 145 146 147 148 149 150 151 152
        case 0:
            ui.parNone->setChecked(true);
            break;
        case 1:
            ui.parOdd->setChecked(true);
            break;
        case 2:
            ui.parEven->setChecked(true);
            break;
        default:
            // Enforce default: no parity in link
lm's avatar
lm committed
153
            setParityNone(true);
pixhawk's avatar
pixhawk committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            ui.parNone->setChecked(true);
            break;
        }

        switch(this->link->getFlowType()) {
        case 0:
            ui.flowControlCheckBox->setChecked(false);
            break;
        case 1:
            ui.flowControlCheckBox->setChecked(true);
            break;
        default:
            ui.flowControlCheckBox->setChecked(false);
            enableFlowControl(false);
        }

170
        ui.baudRate->setCurrentIndex(ui.baudRate->findText(QString("%1").arg(this->link->getBaudRate())));
pixhawk's avatar
pixhawk committed
171

172 173
        ui.dataBitsSpinBox->setValue(this->link->getDataBits());
        ui.stopBitsSpinBox->setValue(this->link->getStopBits());
pixhawk's avatar
pixhawk committed
174 175 176 177 178 179
        portCheckTimer = new QTimer(this);
        portCheckTimer->setInterval(1000);
        connect(portCheckTimer, SIGNAL(timeout()), this, SLOT(setupPortList()));

        // Display the widget
        this->window()->setWindowTitle(tr("Serial Communication Settings"));
180 181 182
    }
    else
    {
pixhawk's avatar
pixhawk committed
183 184 185 186
        qDebug() << "Link is NOT a serial link, can't open configuration window";
    }
}

187 188
SerialConfigurationWindow::~SerialConfigurationWindow()
{
pixhawk's avatar
pixhawk committed
189 190 191 192 193

}

void SerialConfigurationWindow::showEvent(QShowEvent* event)
{
194 195
    Q_UNUSED(event);
    portCheckTimer->start();
pixhawk's avatar
pixhawk committed
196 197 198 199
}

void SerialConfigurationWindow::hideEvent(QHideEvent* event)
{
200 201
    Q_UNUSED(event);
    portCheckTimer->stop();
pixhawk's avatar
pixhawk committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

QAction* SerialConfigurationWindow::getAction()
{
    return action;
}

void SerialConfigurationWindow::configureCommunication()
{
    QString selected = ui.portName->currentText();
    setupPortList();
    ui.portName->setEditText(selected);
    this->show();
}

217
bool SerialConfigurationWindow::setupPortList()
pixhawk's avatar
pixhawk committed
218
{
219
    if (!link) return false;
pixhawk's avatar
pixhawk committed
220 221

    // Get the ports available on this system
222
    QList<QString> ports = link->getCurrentPorts();
pixhawk's avatar
pixhawk committed
223

224 225 226
    QString storedName = this->link->getPortName();
    bool storedFound = false;

pixhawk's avatar
pixhawk committed
227
    // Add the ports in reverse order, because we prepend them to the list
228
    for (int i = ports.count() - 1; i >= 0; --i)
229
    {
pixhawk's avatar
pixhawk committed
230
        // Prepend newly found port to the list
231
        if (ui.portName->findText(ports[i]) == -1)
232
        {
233 234
            ui.portName->insertItem(0, ports[i]);
            if (!userConfigured) ui.portName->setEditText(ports[i]);
pixhawk's avatar
pixhawk committed
235
        }
236 237

        // Check if the stored link name is still present
238
        if (ports[i].contains(storedName) || storedName.contains(ports[i]))
239
            storedFound = true;
pixhawk's avatar
pixhawk committed
240 241
    }

242 243
    if (storedFound)
        ui.portName->setEditText(storedName);
244 245

    return (ports.count() > 0);
pixhawk's avatar
pixhawk committed
246 247 248 249
}

void SerialConfigurationWindow::enableFlowControl(bool flow)
{
250 251
    if(flow)
    {
pixhawk's avatar
pixhawk committed
252
        link->setFlowType(1);
253 254 255
    }
    else
    {
pixhawk's avatar
pixhawk committed
256 257 258 259
        link->setFlowType(0);
    }
}

lm's avatar
lm committed
260
void SerialConfigurationWindow::setParityNone(bool accept)
pixhawk's avatar
pixhawk committed
261
{
lm's avatar
lm committed
262
    if (accept) link->setParityType(0);
pixhawk's avatar
pixhawk committed
263 264
}

lm's avatar
lm committed
265
void SerialConfigurationWindow::setParityOdd(bool accept)
pixhawk's avatar
pixhawk committed
266
{
Bill Bonney's avatar
Bill Bonney committed
267
    if (accept) link->setParityType(1); // [TODO] This needs to be Fixed [BB]
pixhawk's avatar
pixhawk committed
268 269
}

lm's avatar
lm committed
270
void SerialConfigurationWindow::setParityEven(bool accept)
pixhawk's avatar
pixhawk committed
271
{
lm's avatar
lm committed
272
    if (accept) link->setParityType(2);
pixhawk's avatar
pixhawk committed
273 274 275 276
}

void SerialConfigurationWindow::setPortName(QString port)
{
277
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
278
    port = port.split("-").first();
279
#endif
pixhawk's avatar
pixhawk committed
280 281
    port = port.remove(" ");

282
    if (this->link->getPortName() != port) {
pixhawk's avatar
pixhawk committed
283 284 285 286 287
        link->setPortName(port);
    }
    userConfigured = true;
}

Bryant's avatar
Bryant committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
void SerialConfigurationWindow::setBaudRate(QString rate)
{
	bool ok;
	int intrate = rate.toInt(&ok);
	if (ok) {
		link->setBaudRate(intrate);
	}
	userConfigured = true;
}

void SerialConfigurationWindow::setDataBits(int dataBits)
{
	link->setDataBitsType(static_cast<QSerialPort::DataBits>(dataBits));
	userConfigured = true;
}

void SerialConfigurationWindow::setStopBits(int stopBits)
{
	link->setStopBitsType(static_cast<QSerialPort::DataBits>(stopBits));
	userConfigured = true;
}

pixhawk's avatar
pixhawk committed
310 311
void SerialConfigurationWindow::setLinkName(QString name)
{
lm's avatar
lm committed
312 313
    Q_UNUSED(name);
    // FIXME
pixhawk's avatar
pixhawk committed
314 315 316 317 318
    action->setText(tr("Configure ") + link->getName());
    action->setStatusTip(tr("Configure ") + link->getName());
    setWindowTitle(tr("Configuration of ") + link->getName());
}