SerialConfigurationWindow.cc 8.1 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>
35
#include <QSerialPortInfo>
pixhawk's avatar
pixhawk committed
36

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

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

44 45 46 47 48 49
SerialConfigurationWindow::SerialConfigurationWindow(SerialConfiguration *config, QWidget *parent, Qt::WindowFlags flags)
    : QWidget(parent, flags)
{
    _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
    // Connect the individual user interface inputs
115 116 117 118 119 120 121 122 123
    connect(_ui.portName,           SIGNAL(currentIndexChanged(int)), this,  SLOT(setPortName(int)));
    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)));
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    _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
145

146 147 148 149 150 151 152 153 154 155 156
    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
157

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

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

}

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

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

179
bool SerialConfigurationWindow::setupPortList()
pixhawk's avatar
pixhawk committed
180
{
181 182 183 184
    bool changed = false;
    // Iterate found ports
    QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
    foreach (const QSerialPortInfo &info, portList)
185
    {
186 187 188
        QString name = info.portName();
        // Append newly found port to the list
        if (_ui.portName->findText(name) < 0)
189
        {
190 191 192
            // We show the user the "short name" but store the full port name
            _ui.portName->addItem(name, QVariant(info.systemLocation()));
            changed = true;
pixhawk's avatar
pixhawk committed
193 194
        }
    }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    // See if configured port (if any) is present
    if(changed) {
        int idx = _ui.portName->count() - 1;
        if(!_config->portName().isEmpty()) {
            idx = _ui.portName->findData(QVariant(_config->portName()));
            if(idx < 0) {
                idx = 0;
            }
        }
        _ui.portName->setCurrentIndex(idx);
        if(_ui.portName->count() > 0) {
            _ui.portName->setEditText(_ui.portName->itemText(idx));
        }
        if(_config->portName().isEmpty()) {
            setPortName(idx);
        }
    }
    return (_ui.portName->count() > 0);
pixhawk's avatar
pixhawk committed
213 214 215 216
}

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

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

lm's avatar
lm committed
225
void SerialConfigurationWindow::setParityOdd(bool accept)
pixhawk's avatar
pixhawk committed
226
{
227
    if (accept) _config->setParity(QSerialPort::OddParity);
pixhawk's avatar
pixhawk committed
228 229
}

lm's avatar
lm committed
230
void SerialConfigurationWindow::setParityEven(bool accept)
pixhawk's avatar
pixhawk committed
231
{
232
    if (accept) _config->setParity(QSerialPort::EvenParity);
pixhawk's avatar
pixhawk committed
233 234
}

235
void SerialConfigurationWindow::setPortName(int index)
pixhawk's avatar
pixhawk committed
236
{
237 238 239 240
    // Get the full port name and store it in the config
    QString pname = _ui.portName->itemData(index).toString();
    if (_config->portName() != pname) {
        _config->setPortName(pname);
pixhawk's avatar
pixhawk committed
241 242 243
    }
}

244
void SerialConfigurationWindow::setBaudRate(int index)
Bryant's avatar
Bryant committed
245
{
246 247
    int baud = _ui.baudRate->itemData(index).toInt();
    _config->setBaud(baud);
Bryant's avatar
Bryant committed
248 249
}

250
void SerialConfigurationWindow::setDataBits(int bits)
Bryant's avatar
Bryant committed
251
{
252
    _config->setDataBits(bits);
Bryant's avatar
Bryant committed
253 254
}

255
void SerialConfigurationWindow::setStopBits(int bits)
Bryant's avatar
Bryant committed
256
{
257
    _config->setStopBits(bits);
Bryant's avatar
Bryant committed
258
}