SerialConfigurationWindow.cc 7.96 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
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

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

36 37
#include <SerialConfigurationWindow.h>
#include <SerialLink.h>
Bryant's avatar
Bryant committed
38

39 40 41
#ifndef USE_ANCIENT_RATES
#define USE_ANCIENT_RATES 0
#endif
pixhawk's avatar
pixhawk committed
42

43 44 45 46 47 48 49
SerialConfigurationWindow::SerialConfigurationWindow(SerialConfiguration *config, QWidget *parent, Qt::WindowFlags flags)
    : QWidget(parent, flags)
    , _userConfigured(false)
{
    _ui.setupUi(this);
    Q_ASSERT(config != NULL);
    _config = config;
pixhawk's avatar
pixhawk committed
50

51 52 53 54
    // Scan for serial ports. Let the user know if none were found for debugging purposes
    if (!setupPortList()) {
        qDebug() << "No serial ports found.";
    }
55

56 57
    // Set up baud rates
    _ui.baudRate->clear();
58

59 60 61
    // Keep track of all desired baud rates by OS. These are iterated through
    // later and added to _ui.baudRate.
    QList<int> supportedBaudRates;
62

63 64
#if USE_ANCIENT_RATES
    // Baud rates supported only by POSIX systems
65
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
66 67 68 69 70 71
    supportedBaudRates << 50;
    supportedBaudRates << 75;
    supportedBaudRates << 134;
    supportedBaudRates << 150;
    supportedBaudRates << 200;
    supportedBaudRates << 1800;
72
#endif
73
#endif //USE_ANCIENT_RATES
74

75
    // Baud rates supported only by Windows
76
#if defined(Q_OS_WIN)
77 78 79 80
    supportedBaudRates << 14400;
    supportedBaudRates << 56000;
    supportedBaudRates << 128000;
    supportedBaudRates << 256000;
81
#endif
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    // Baud rates supported by everyone
#if USE_ANCIENT_RATES
    supportedBaudRates << 110;
    supportedBaudRates << 300;
    supportedBaudRates << 600;
    supportedBaudRates << 1200;
#endif //USE_ANCIENT_RATES
    supportedBaudRates << 2400;
    supportedBaudRates << 4800;
    supportedBaudRates << 9600;
    supportedBaudRates << 19200;
    supportedBaudRates << 38400;
    supportedBaudRates << 57600;
    supportedBaudRates << 115200;
    supportedBaudRates << 230400;
    supportedBaudRates << 460800;
99 100

#if defined(Q_OS_LINUX)
101 102 103
    // Baud rates supported only by Linux
    supportedBaudRates << 500000;
    supportedBaudRates << 576000;
104 105
#endif

106
    supportedBaudRates << 921600;
pixhawk's avatar
pixhawk committed
107

108 109 110 111 112
    // 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));
    }
pixhawk's avatar
pixhawk committed
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    // 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(int)), this,                SLOT(setBaudRate(int)));
    connect(_ui.flowControlCheckBox,SIGNAL(toggled(bool)), this,                 SLOT(enableFlowControl(bool)));
    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,             SLOT(setDataBits(int)));
    connect(_ui.stopBitsSpinBox,    SIGNAL(valueChanged(int)), this,             SLOT(setStopBits(int)));
    connect(_ui.advCheckBox,        SIGNAL(clicked(bool)), _ui.advGroupBox,      SLOT(setVisible(bool)));

    _ui.advCheckBox->setCheckable(true);
    _ui.advCheckBox->setChecked(false);
    _ui.advGroupBox->setVisible(false);

    switch(_config->parity()) {
    case QSerialPort::NoParity:
        _ui.parNone->setChecked(true);
        break;
    case QSerialPort::OddParity:
        _ui.parOdd->setChecked(true);
        break;
    case QSerialPort::EvenParity:
        _ui.parEven->setChecked(true);
        break;
    default:
        // Enforce default: no parity in link
        setParityNone(true);
        _ui.parNone->setChecked(true);
        break;
    }
pixhawk's avatar
pixhawk committed
146

147 148 149 150 151 152 153 154 155 156 157
    int idx = 0;
    _ui.flowControlCheckBox->setChecked(_config->flowControl() == QSerialPort::HardwareControl);
    idx = _ui.baudRate->findText(QString("%1").arg(_config->baud()));
    if(idx < 0) idx = _ui.baudRate->findText("57600");
    if(idx < 0) idx = 0;
    _ui.baudRate->setCurrentIndex(idx);
    _ui.dataBitsSpinBox->setValue(_config->dataBits());
    _ui.stopBitsSpinBox->setValue(_config->stopBits());
    _portCheckTimer = new QTimer(this);
    _portCheckTimer->setInterval(1000);
    connect(_portCheckTimer, SIGNAL(timeout()), this, SLOT(setupPortList()));
pixhawk's avatar
pixhawk committed
158

159 160
    // Display the widget
    setWindowTitle(tr("Serial Communication Settings"));
pixhawk's avatar
pixhawk committed
161 162
}

163 164
SerialConfigurationWindow::~SerialConfigurationWindow()
{
pixhawk's avatar
pixhawk committed
165 166 167 168 169

}

void SerialConfigurationWindow::showEvent(QShowEvent* event)
{
170
    Q_UNUSED(event);
171
    _portCheckTimer->start();
pixhawk's avatar
pixhawk committed
172 173 174 175
}

void SerialConfigurationWindow::hideEvent(QHideEvent* event)
{
176
    Q_UNUSED(event);
177
    _portCheckTimer->stop();
pixhawk's avatar
pixhawk committed
178 179
}

180
bool SerialConfigurationWindow::setupPortList()
pixhawk's avatar
pixhawk committed
181 182
{
    // Get the ports available on this system
183 184
    QList<QString> ports = SerialConfiguration::getCurrentPorts();
    QString storedName = _config->portName();
185
    bool storedFound = false;
pixhawk's avatar
pixhawk committed
186
    // Add the ports in reverse order, because we prepend them to the list
187
    for (int i = ports.count() - 1; i >= 0; --i)
188
    {
pixhawk's avatar
pixhawk committed
189
        // Prepend newly found port to the list
190
        if (_ui.portName->findText(ports[i]) < 0)
191
        {
192 193
            _ui.portName->insertItem(0, ports[i]);
            if (!_userConfigured) _ui.portName->setEditText(ports[i]);
pixhawk's avatar
pixhawk committed
194
        }
195
        // Check if the stored link name is still present
196
        if (ports[i].contains(storedName) || storedName.contains(ports[i]))
197
            storedFound = true;
pixhawk's avatar
pixhawk committed
198
    }
199
    if (storedFound)
200
        _ui.portName->setEditText(storedName);
201
    return (ports.count() > 0);
pixhawk's avatar
pixhawk committed
202 203 204 205
}

void SerialConfigurationWindow::enableFlowControl(bool flow)
{
206
    _config->setFlowControl(flow ? QSerialPort::HardwareControl : QSerialPort::NoFlowControl);
pixhawk's avatar
pixhawk committed
207 208
}

lm's avatar
lm committed
209
void SerialConfigurationWindow::setParityNone(bool accept)
pixhawk's avatar
pixhawk committed
210
{
211
    if (accept) _config->setParity(QSerialPort::NoParity);
pixhawk's avatar
pixhawk committed
212 213
}

lm's avatar
lm committed
214
void SerialConfigurationWindow::setParityOdd(bool accept)
pixhawk's avatar
pixhawk committed
215
{
216
    if (accept) _config->setParity(QSerialPort::OddParity);
pixhawk's avatar
pixhawk committed
217 218
}

lm's avatar
lm committed
219
void SerialConfigurationWindow::setParityEven(bool accept)
pixhawk's avatar
pixhawk committed
220
{
221
    if (accept) _config->setParity(QSerialPort::EvenParity);
pixhawk's avatar
pixhawk committed
222 223 224 225
}

void SerialConfigurationWindow::setPortName(QString port)
{
226
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
227
    port = port.split("-").first();
228
#endif
229 230 231
    port = port.trimmed();
    if (_config->portName() != port) {
        _config->setPortName(port);
pixhawk's avatar
pixhawk committed
232 233 234 235
    }
    userConfigured = true;
}

236
void SerialConfigurationWindow::setBaudRate(int index)
Bryant's avatar
Bryant committed
237
{
238 239
    int baud = _ui.baudRate->itemData(index).toInt();
    _config->setBaud(baud);
Bryant's avatar
Bryant committed
240 241
}

242
void SerialConfigurationWindow::setDataBits(int bits)
Bryant's avatar
Bryant committed
243
{
244
    _config->setDataBits(bits);
Bryant's avatar
Bryant committed
245 246
}

247
void SerialConfigurationWindow::setStopBits(int bits)
Bryant's avatar
Bryant committed
248
{
249
    _config->setStopBits(bits);
Bryant's avatar
Bryant committed
250
}