LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / config / configoplinkwidget.cpp
blob56a42658e0c53f0954c3751b04edc348ba85cf65
1 /**
2 ******************************************************************************
4 * @file configtxpidswidget.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup ConfigPlugin Config Plugin
9 * @{
10 * @brief The Configuration Gadget used to configure the PipXtreme
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 "configoplinkwidget.h"
30 #include <coreplugin/generalsettings.h>
31 #include <oplinksettings.h>
32 #include <oplinkstatus.h>
33 #include <QMessageBox>
35 // Channel range and Frequency display
36 static const int MAX_CHANNEL_NUM = 250;
37 static const int MIN_CHANNEL_RANGE = 10;
38 static const float FIRST_FREQUENCY = 430.000;
39 static const float FREQUENCY_STEP = 0.040;
41 ConfigOPLinkWidget::ConfigOPLinkWidget(QWidget *parent) : ConfigTaskWidget(parent)
43 m_oplink = new Ui_OPLinkWidget();
44 m_oplink->setupUi(this);
46 // Connect to the OPLinkStatus object updates
47 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
48 UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
49 oplinkStatusObj = dynamic_cast<UAVDataObject *>(objManager->getObject("OPLinkStatus"));
50 Q_ASSERT(oplinkStatusObj);
51 connect(oplinkStatusObj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(updateStatus(UAVObject *)));
53 // Connect to the OPLinkSettings object updates
54 oplinkSettingsObj = dynamic_cast<OPLinkSettings *>(objManager->getObject("OPLinkSettings"));
55 Q_ASSERT(oplinkSettingsObj);
56 connect(oplinkSettingsObj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(updateSettings(UAVObject *)));
58 Core::Internal::GeneralSettings *settings = pm->getObject<Core::Internal::GeneralSettings>();
59 if (!settings->useExpertMode()) {
60 m_oplink->Apply->setVisible(false);
62 addApplySaveButtons(m_oplink->Apply, m_oplink->Save);
64 addWidgetBinding("OPLinkSettings", "MainPort", m_oplink->MainPort);
65 addWidgetBinding("OPLinkSettings", "FlexiPort", m_oplink->FlexiPort);
66 addWidgetBinding("OPLinkSettings", "VCPPort", m_oplink->VCPPort);
67 addWidgetBinding("OPLinkSettings", "MaxRFPower", m_oplink->MaxRFTxPower);
68 addWidgetBinding("OPLinkSettings", "MinChannel", m_oplink->MinimumChannel);
69 addWidgetBinding("OPLinkSettings", "MaxChannel", m_oplink->MaximumChannel);
70 addWidgetBinding("OPLinkSettings", "CoordID", m_oplink->CoordID);
71 addWidgetBinding("OPLinkSettings", "Coordinator", m_oplink->Coordinator);
72 addWidgetBinding("OPLinkSettings", "OneWay", m_oplink->OneWayLink);
73 addWidgetBinding("OPLinkSettings", "PPMOnly", m_oplink->PPMOnly);
74 addWidgetBinding("OPLinkSettings", "PPM", m_oplink->PPM);
75 addWidgetBinding("OPLinkSettings", "ComSpeed", m_oplink->ComSpeed);
77 addWidgetBinding("OPLinkStatus", "DeviceID", m_oplink->DeviceID);
78 addWidgetBinding("OPLinkStatus", "RxGood", m_oplink->Good);
79 addWidgetBinding("OPLinkStatus", "RxCorrected", m_oplink->Corrected);
80 addWidgetBinding("OPLinkStatus", "RxErrors", m_oplink->Errors);
81 addWidgetBinding("OPLinkStatus", "RxMissed", m_oplink->Missed);
82 addWidgetBinding("OPLinkStatus", "RxFailure", m_oplink->RxFailure);
83 addWidgetBinding("OPLinkStatus", "UAVTalkErrors", m_oplink->UAVTalkErrors);
84 addWidgetBinding("OPLinkStatus", "TxDropped", m_oplink->Dropped);
85 addWidgetBinding("OPLinkStatus", "TxFailure", m_oplink->TxFailure);
86 addWidgetBinding("OPLinkStatus", "Resets", m_oplink->Resets);
87 addWidgetBinding("OPLinkStatus", "Timeouts", m_oplink->Timeouts);
88 addWidgetBinding("OPLinkStatus", "RSSI", m_oplink->RSSI);
89 addWidgetBinding("OPLinkStatus", "HeapRemaining", m_oplink->FreeHeap);
90 addWidgetBinding("OPLinkStatus", "LinkQuality", m_oplink->LinkQuality);
91 addWidgetBinding("OPLinkStatus", "RXSeq", m_oplink->RXSeq);
92 addWidgetBinding("OPLinkStatus", "TXSeq", m_oplink->TXSeq);
93 addWidgetBinding("OPLinkStatus", "RXRate", m_oplink->RXRate);
94 addWidgetBinding("OPLinkStatus", "TXRate", m_oplink->TXRate);
96 // Connect the bind buttons
97 connect(m_oplink->Bind1, SIGNAL(clicked()), this, SLOT(bind()));
98 connect(m_oplink->Bind2, SIGNAL(clicked()), this, SLOT(bind()));
99 connect(m_oplink->Bind3, SIGNAL(clicked()), this, SLOT(bind()));
100 connect(m_oplink->Bind4, SIGNAL(clicked()), this, SLOT(bind()));
102 // Connect the selection changed signals.
103 connect(m_oplink->PPMOnly, SIGNAL(toggled(bool)), this, SLOT(ppmOnlyChanged()));
104 connect(m_oplink->MinimumChannel, SIGNAL(valueChanged(int)), this, SLOT(minChannelChanged()));
105 connect(m_oplink->MaximumChannel, SIGNAL(valueChanged(int)), this, SLOT(maxChannelChanged()));
107 m_oplink->MinimumChannel->setKeyboardTracking(false);
108 m_oplink->MaximumChannel->setKeyboardTracking(false);
110 m_oplink->MaximumChannel->setMaximum(MAX_CHANNEL_NUM);
111 m_oplink->MinimumChannel->setMaximum(MAX_CHANNEL_NUM - MIN_CHANNEL_RANGE);
113 // Request and update of the setting object.
114 settingsUpdated = false;
115 autoLoadWidgets();
116 disableMouseWheelEvents();
117 updateEnableControls();
120 ConfigOPLinkWidget::~ConfigOPLinkWidget()
124 \brief Called by updates to @OPLinkStatus
126 void ConfigOPLinkWidget::updateStatus(UAVObject *object)
128 // Request and update of the setting object if we haven't received it yet.
129 if (!settingsUpdated) {
130 oplinkSettingsObj->requestUpdate();
133 // Update the link state
134 UAVObjectField *linkField = object->getField("LinkState");
135 m_oplink->LinkState->setText(linkField->getValue().toString());
136 bool linkConnected = (linkField->getValue() == linkField->getOptions().at(OPLinkStatus::LINKSTATE_CONNECTED));
137 bool modemEnabled = linkConnected || (linkField->getValue() == linkField->getOptions().at(OPLinkStatus::LINKSTATE_DISCONNECTED)) ||
138 (linkField->getValue() == linkField->getOptions().at(OPLinkStatus::LINKSTATE_ENABLED));
140 UAVObjectField *pairRssiField = object->getField("PairSignalStrengths");
142 bool bound;
143 bool ok;
144 quint32 boundPairId = m_oplink->CoordID->text().toUInt(&ok, 16);
146 // Update the detected devices.
147 UAVObjectField *pairIdField = object->getField("PairIDs");
148 quint32 pairid = pairIdField->getValue(0).toUInt();
149 bound = (pairid == boundPairId);
150 m_oplink->PairID1->setText(QString::number(pairid, 16).toUpper());
151 m_oplink->PairID1->setEnabled(false);
152 m_oplink->Bind1->setText(bound ? tr("Unbind") : tr("Bind"));
153 m_oplink->Bind1->setEnabled(pairid && modemEnabled);
154 m_oplink->PairSignalStrengthBar1->setValue(((bound && !linkConnected) || !modemEnabled) ? -127 : pairRssiField->getValue(0).toInt());
155 m_oplink->PairSignalStrengthLabel1->setText(QString("%1dB").arg(m_oplink->PairSignalStrengthBar1->value()));
157 pairid = pairIdField->getValue(1).toUInt();
158 bound = (pairid == boundPairId);
159 m_oplink->PairID2->setText(QString::number(pairid, 16).toUpper());
160 m_oplink->PairID2->setEnabled(false);
161 m_oplink->Bind2->setText(bound ? tr("Unbind") : tr("Bind"));
162 m_oplink->Bind2->setEnabled(pairid && modemEnabled);
163 m_oplink->PairSignalStrengthBar2->setValue(((bound && !linkConnected) || !modemEnabled) ? -127 : pairRssiField->getValue(1).toInt());
164 m_oplink->PairSignalStrengthLabel2->setText(QString("%1dB").arg(m_oplink->PairSignalStrengthBar2->value()));
166 pairid = pairIdField->getValue(2).toUInt();
167 bound = (pairid == boundPairId);
168 m_oplink->PairID3->setText(QString::number(pairid, 16).toUpper());
169 m_oplink->PairID3->setEnabled(false);
170 m_oplink->Bind3->setText(bound ? tr("Unbind") : tr("Bind"));
171 m_oplink->Bind3->setEnabled(pairid && modemEnabled);
172 m_oplink->PairSignalStrengthBar3->setValue(((bound && !linkConnected) || !modemEnabled) ? -127 : pairRssiField->getValue(2).toInt());
173 m_oplink->PairSignalStrengthLabel3->setText(QString("%1dB").arg(m_oplink->PairSignalStrengthBar3->value()));
175 pairid = pairIdField->getValue(3).toUInt();
176 bound = (pairid == boundPairId);
177 m_oplink->PairID4->setText(QString::number(pairid, 16).toUpper());
178 m_oplink->PairID4->setEnabled(false);
179 m_oplink->Bind4->setText(bound ? tr("Unbind") : tr("Bind"));
180 m_oplink->Bind4->setEnabled(pairid && modemEnabled);
181 m_oplink->PairSignalStrengthBar4->setValue(((bound && !linkConnected) || !modemEnabled) ? -127 : pairRssiField->getValue(3).toInt());
182 m_oplink->PairSignalStrengthLabel4->setText(QString("%1dB").arg(m_oplink->PairSignalStrengthBar4->value()));
184 // Update the Description field
185 // TODO use UAVObjectUtilManager::descriptionToStructure()
186 UAVObjectField *descField = object->getField("Description");
187 if (descField->getValue(0) != QChar(255)) {
189 * This looks like a binary with a description at the end:
190 * 4 bytes: header: "OpFw".
191 * 4 bytes: GIT commit tag (short version of SHA1).
192 * 4 bytes: Unix timestamp of compile time.
193 * 2 bytes: target platform. Should follow same rule as BOARD_TYPE and BOARD_REVISION in board define files.
194 * 26 bytes: commit tag if it is there, otherwise branch name. '-dirty' may be added if needed. Zero-padded.
195 * 20 bytes: SHA1 sum of the firmware.
196 * 20 bytes: SHA1 sum of the uavo definitions.
197 * 20 bytes: free for now.
199 char buf[OPLinkStatus::DESCRIPTION_NUMELEM];
200 for (unsigned int i = 0; i < 26; ++i) {
201 buf[i] = descField->getValue(i + 14).toChar().toLatin1();
203 buf[26] = '\0';
204 QString descstr(buf);
205 quint32 gitDate = descField->getValue(11).toChar().toLatin1() & 0xFF;
206 for (int i = 1; i < 4; i++) {
207 gitDate = gitDate << 8;
208 gitDate += descField->getValue(11 - i).toChar().toLatin1() & 0xFF;
210 QString date = QDateTime::fromTime_t(gitDate).toUTC().toString("yyyy-MM-dd HH:mm");
211 m_oplink->FirmwareVersion->setText(descstr + " " + date);
212 } else {
213 m_oplink->FirmwareVersion->setText(tr("Unknown"));
216 // Update the serial number field
217 UAVObjectField *serialField = object->getField("CPUSerial");
218 char buf[OPLinkStatus::CPUSERIAL_NUMELEM * 2 + 1];
219 for (unsigned int i = 0; i < OPLinkStatus::CPUSERIAL_NUMELEM; ++i) {
220 unsigned char val = serialField->getValue(i).toUInt() >> 4;
221 buf[i * 2] = ((val < 10) ? '0' : '7') + val;
222 val = serialField->getValue(i).toUInt() & 0xf;
223 buf[i * 2 + 1] = ((val < 10) ? '0' : '7') + val;
225 buf[OPLinkStatus::CPUSERIAL_NUMELEM * 2] = '\0';
226 m_oplink->SerialNumber->setText(buf);
228 updateEnableControls();
232 \brief Called by updates to @OPLinkSettings
234 void ConfigOPLinkWidget::updateSettings(UAVObject *object)
236 Q_UNUSED(object);
238 if (!settingsUpdated) {
239 settingsUpdated = true;
241 // Enable components based on the board type connected.
242 UAVObjectField *board_type_field = oplinkStatusObj->getField("BoardType");
243 switch (board_type_field->getValue().toInt()) {
244 case 0x09: // Revolution
245 m_oplink->MainPort->setVisible(false);
246 m_oplink->MainPortLabel->setVisible(false);
247 m_oplink->FlexiPort->setVisible(false);
248 m_oplink->FlexiPortLabel->setVisible(false);
249 m_oplink->VCPPort->setVisible(false);
250 m_oplink->VCPPortLabel->setVisible(false);
251 m_oplink->FlexiIOPort->setVisible(false);
252 m_oplink->FlexiIOPortLabel->setVisible(false);
253 m_oplink->PPM->setVisible(true);
254 break;
255 case 0x03: // OPLinkMini
256 m_oplink->MainPort->setVisible(true);
257 m_oplink->MainPortLabel->setVisible(true);
258 m_oplink->FlexiPort->setVisible(true);
259 m_oplink->FlexiPortLabel->setVisible(true);
260 m_oplink->VCPPort->setVisible(true);
261 m_oplink->VCPPortLabel->setVisible(true);
262 m_oplink->FlexiIOPort->setVisible(false);
263 m_oplink->FlexiIOPortLabel->setVisible(false);
264 m_oplink->PPM->setVisible(false);
265 break;
266 case 0x0A: // OPLink?
267 m_oplink->MainPort->setVisible(true);
268 m_oplink->MainPortLabel->setVisible(true);
269 m_oplink->FlexiPort->setVisible(true);
270 m_oplink->FlexiPortLabel->setVisible(true);
271 m_oplink->VCPPort->setVisible(true);
272 m_oplink->VCPPortLabel->setVisible(true);
273 m_oplink->FlexiIOPort->setVisible(true);
274 m_oplink->FlexiIOPortLabel->setVisible(true);
275 m_oplink->PPM->setVisible(false);
276 break;
277 default:
278 // This shouldn't happen.
279 break;
281 updateEnableControls();
285 void ConfigOPLinkWidget::updateEnableControls()
287 enableControls(true);
288 ppmOnlyChanged();
291 void ConfigOPLinkWidget::disconnected()
293 if (settingsUpdated) {
294 settingsUpdated = false;
298 void ConfigOPLinkWidget::bind()
300 QPushButton *bindButton = qobject_cast<QPushButton *>(sender());
302 if (bindButton) {
303 QLineEdit *editField = NULL;
304 if (bindButton == m_oplink->Bind1) {
305 editField = m_oplink->PairID1;
306 } else if (bindButton == m_oplink->Bind2) {
307 editField = m_oplink->PairID2;
308 } else if (bindButton == m_oplink->Bind3) {
309 editField = m_oplink->PairID3;
310 } else if (bindButton == m_oplink->Bind4) {
311 editField = m_oplink->PairID4;
313 Q_ASSERT(editField);
314 bool ok;
315 quint32 pairid = editField->text().toUInt(&ok, 16);
316 if (ok) {
317 quint32 boundPairId = m_oplink->CoordID->text().toUInt(&ok, 16);
318 (pairid != boundPairId) ? m_oplink->CoordID->setText(QString::number(pairid, 16).toUpper()) : m_oplink->CoordID->setText("0");
320 QMessageBox::information(this, tr("Information"), tr("To apply the changes when binding/unbinding the board must be rebooted or power cycled."), QMessageBox::Ok);
324 void ConfigOPLinkWidget::ppmOnlyChanged()
326 bool is_ppm_only = m_oplink->PPMOnly->isChecked();
328 m_oplink->PPM->setEnabled(!is_ppm_only);
329 m_oplink->OneWayLink->setEnabled(!is_ppm_only);
330 m_oplink->ComSpeed->setEnabled(!is_ppm_only);
333 void ConfigOPLinkWidget::minChannelChanged()
335 channelChanged(false);
338 void ConfigOPLinkWidget::maxChannelChanged()
340 channelChanged(true);
343 void ConfigOPLinkWidget::channelChanged(bool isMax)
345 int minChannel = m_oplink->MinimumChannel->value();
346 int maxChannel = m_oplink->MaximumChannel->value();
348 if ((maxChannel - minChannel) < MIN_CHANNEL_RANGE) {
349 if (isMax) {
350 minChannel = maxChannel - MIN_CHANNEL_RANGE;
351 } else {
352 maxChannel = minChannel + MIN_CHANNEL_RANGE;
355 if (maxChannel > MAX_CHANNEL_NUM) {
356 maxChannel = MAX_CHANNEL_NUM;
357 minChannel = MAX_CHANNEL_NUM - MIN_CHANNEL_RANGE;
360 if (minChannel < 0) {
361 minChannel = 0;
362 maxChannel = MIN_CHANNEL_RANGE;
366 m_oplink->MaximumChannel->setValue(maxChannel);
367 m_oplink->MinimumChannel->setValue(minChannel);
369 // Calculate and Display frequency in MHz
370 float minFrequency = FIRST_FREQUENCY + (minChannel * FREQUENCY_STEP);
371 float maxFrequency = FIRST_FREQUENCY + (maxChannel * FREQUENCY_STEP);
373 m_oplink->MinFreq->setText("(" + QString::number(minFrequency, 'f', 3) + " MHz)");
374 m_oplink->MaxFreq->setText("(" + QString::number(maxFrequency, 'f', 3) + " MHz)");