Skip to content
SerialConfigurationWindow.cc 8.18 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================

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

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

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

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

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
    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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed

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

/**
 * @file
lm's avatar
lm committed
 *   @brief Implementation of SerialConfigurationWindow
pixhawk's avatar
pixhawk committed
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QDebug>

#include <SerialConfigurationWindow.h>
#include <SerialLinkInterface.h>
#include <QDir>
#include <QSettings>
pixhawk's avatar
pixhawk committed
#include <QFileInfoList>

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

pixhawk's avatar
pixhawk committed
        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
        action = new QAction(QIcon(":/images/devices/network-wireless.svg"), "", link);
        setLinkName(link->getName());
        // Set up baud rates
        ui.baudRate->clear();
        ui.baudRate->addItem("50", 50);
        ui.baudRate->addItem("70", 70);
        ui.baudRate->addItem("110", 110);
        ui.baudRate->addItem("134", 134);
        ui.baudRate->addItem("150", 150);
        ui.baudRate->addItem("200", 200);
        ui.baudRate->addItem("300", 300);
        ui.baudRate->addItem("600", 600);
        ui.baudRate->addItem("1200", 1200);
        ui.baudRate->addItem("1800", 1800);
        ui.baudRate->addItem("2400", 2400);
        ui.baudRate->addItem("4800", 4800);
        ui.baudRate->addItem("9600", 9600);
#ifdef Q_OS_WIN
        ui.baudRate->addItem("14400", 14400);
#endif
        ui.baudRate->addItem("19200", 19200);
LM's avatar
LM committed
        ui.baudRate->addItem("38400", 38400);
#ifdef Q_OS_WIN
        ui.baudRate->addItem("56000", 56000);
#endif
        ui.baudRate->addItem("57600", 57600);
#ifdef Q_OS_WIN
        ui.baudRate->addItem("76800", 76800);
#endif
        ui.baudRate->addItem("115200", 115200);
#ifdef Q_OS_WIN
        ui.baudRate->addItem("128000", 128000);
        ui.baudRate->addItem("230400", 230400);
        ui.baudRate->addItem("256000", 256000);
        ui.baudRate->addItem("460800", 460800);
#endif
        ui.baudRate->addItem("921600", 921600);

pixhawk's avatar
pixhawk committed
        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)));
        connect(ui.baudRate, SIGNAL(activated(QString)), this->link, SLOT(setBaudRateString(QString)));
pixhawk's avatar
pixhawk committed
        connect(ui.flowControlCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableFlowControl(bool)));
lm's avatar
lm committed
        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)));
        connect(ui.dataBitsSpinBox, SIGNAL(valueChanged(int)), this->link, SLOT(setDataBits(int)));
        connect(ui.stopBitsSpinBox, SIGNAL(valueChanged(int)), this->link, SLOT(setStopBits(int)));
pixhawk's avatar
pixhawk committed

        //connect(this->link, SIGNAL(connected(bool)), this, SLOT());
        ui.portName->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);
        ui.baudRate->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);

        switch(this->link->getParityType()) {
pixhawk's avatar
pixhawk committed
        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
            setParityNone(true);
pixhawk's avatar
pixhawk committed
            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);
        }

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

        ui.dataBitsSpinBox->setValue(this->link->getDataBits());
        ui.stopBitsSpinBox->setValue(this->link->getStopBits());
pixhawk's avatar
pixhawk committed

        portCheckTimer = new QTimer(this);
        portCheckTimer->setInterval(1000);
        connect(portCheckTimer, SIGNAL(timeout()), this, SLOT(setupPortList()));

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

SerialConfigurationWindow::~SerialConfigurationWindow()
{
pixhawk's avatar
pixhawk committed

}

void SerialConfigurationWindow::showEvent(QShowEvent* event)
{
    Q_UNUSED(event);
    portCheckTimer->start();
pixhawk's avatar
pixhawk committed
}

void SerialConfigurationWindow::hideEvent(QHideEvent* event)
{
    Q_UNUSED(event);
    portCheckTimer->stop();
pixhawk's avatar
pixhawk committed
}

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

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

void SerialConfigurationWindow::setupPortList()
{
pixhawk's avatar
pixhawk committed

    if (!userConfigured)
    {
        ui.portName->clear();
        ui.portName->clearEditText();
pixhawk's avatar
pixhawk committed
    }
    // Get the ports available on this system
    QVector<QString>* ports = link->getCurrentPorts();
pixhawk's avatar
pixhawk committed

    // Add the ports in reverse order, because we prepend them to the list
    for (int i = ports->size() - 1; i >= 0; --i)
    {
pixhawk's avatar
pixhawk committed
        // Prepend newly found port to the list
        if (ui.portName->findText(ports->at(i)) == -1)
        {
            ui.portName->insertItem(0, ports->at(i));
            if (!userConfigured) ui.portName->setEditText(ports->at(i));
    ui.portName->setEditText(this->link->getPortName());
pixhawk's avatar
pixhawk committed
}

void SerialConfigurationWindow::enableFlowControl(bool flow)
{
pixhawk's avatar
pixhawk committed
        link->setFlowType(1);
pixhawk's avatar
pixhawk committed
        link->setFlowType(0);
    }
}

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

lm's avatar
lm committed
void SerialConfigurationWindow::setParityOdd(bool accept)
pixhawk's avatar
pixhawk committed
{
lm's avatar
lm committed
    if (accept) link->setParityType(1);
pixhawk's avatar
pixhawk committed
}

lm's avatar
lm committed
void SerialConfigurationWindow::setParityEven(bool accept)
pixhawk's avatar
pixhawk committed
{
lm's avatar
lm committed
    if (accept) link->setParityType(2);
pixhawk's avatar
pixhawk committed
}

void SerialConfigurationWindow::setPortName(QString port)
{
pixhawk's avatar
pixhawk committed
    port = port.split("-").first();
pixhawk's avatar
pixhawk committed
    port = port.remove(" ");

    if (this->link->getPortName() != port) {
pixhawk's avatar
pixhawk committed
        link->setPortName(port);
    }
    userConfigured = true;
}

void SerialConfigurationWindow::setLinkName(QString name)
{
lm's avatar
lm committed
    Q_UNUSED(name);
    // FIXME
pixhawk's avatar
pixhawk committed
    action->setText(tr("Configure ") + link->getName());
    action->setStatusTip(tr("Configure ") + link->getName());
    setWindowTitle(tr("Configuration of ") + link->getName());
}