forked from xtremekforever/XKSimulator
/
mainwindow.cpp
406 lines (315 loc) · 10.8 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* This source file is part of the XKSimulator project */
/***************************************************************************
mainwindow.cpp
The main window class for the project.
Author: Jesse L. Zamora <xtremekforever@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
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 this program; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
***************************************************************************/
#include <QMessageBox>
#include <QSettings>
#include <QTimer>
#include <QtDebug>
#include <qextserialenumerator.h>
#include <qextserialport.h>
#include <nmea/nmea.h>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "defines.h"
#include "serial.h"
#include "travelline.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(QString("XKSimulator %1").arg(getVersionString()));
m_serial = new SerialPort();
connect(m_serial, SIGNAL(serialError(QString)), this, SLOT(serialError(QString)));
connect(m_serial, SIGNAL(serialStatus(QString)), this, SLOT(serialStatus(QString)));
m_error = new QLabel("");
statusBar()->addWidget(m_error);
m_enum = new QextSerialEnumerator();
#ifdef Q_OS_WIN
connect(m_enum, SIGNAL(deviceDiscovered(QextPortInfo)), this, SLOT(deviceDiscovered(QextPortInfo)));
connect(m_enum, SIGNAL(deviceRemoved(QextPortInfo)), this, SLOT(deviceRemoved(QextPortInfo)));
m_enum->setUpNotifications();
#endif
m_gpsTimer = new QTimer(this);
connect(m_gpsTimer, SIGNAL(timeout()), this, SLOT(gpsTimer()));
// Load the application settings (not the command settings)
loadAppSettings();
// Load the list of available serial ports
loadComPorts();
m_serial->open(m_tty); // Open the selected serial port
}
MainWindow::~MainWindow()
{
saveAppSettings();
delete m_enum;
delete m_serial;
delete ui;
}
void MainWindow::loadAppSettings()
{
QSettings settings;
#ifdef Q_OS_WIN
m_tty = settings.value(APPLICATION_SETTINGS_PREFIX + "SerialPort", "COM1").toString();
#else
m_tty = settings.value(APPLICATION_SETTINGS_PREFIX + "SerialPort", "/dev/ttyS0").toString();
#endif
// Load last settings
double lat = settings.value(APPLICATION_SETTINGS_PREFIX + "Latitude", "0.0").toDouble();
double lon = settings.value(APPLICATION_SETTINGS_PREFIX + "Longitude", "0.0").toDouble();
int ns = settings.value(APPLICATION_SETTINGS_PREFIX + "NorthSouth", 0).toInt();
int ew = settings.value(APPLICATION_SETTINGS_PREFIX + "EastWest", 0).toInt();
int alt = settings.value(APPLICATION_SETTINGS_PREFIX + "Altitude", 0).toInt();
// Setup the UI
ui->latitudeSpinBox->setValue(lat);
ui->longitudeSpinBox->setValue(lon);
ui->northSouthComboBox->setCurrentIndex(ns);
ui->eastWestComboBox->setCurrentIndex(ew);
ui->altitudeSpinBox->setValue(alt);
}
void MainWindow::saveAppSettings()
{
QSettings settings;
settings.setValue(APPLICATION_SETTINGS_PREFIX + "SerialPort",
m_tty);
settings.setValue(APPLICATION_SETTINGS_PREFIX + "Latitude",
ui->latitudeSpinBox->value());
settings.setValue(APPLICATION_SETTINGS_PREFIX + "Longitude",
ui->longitudeSpinBox->value());
settings.setValue(APPLICATION_SETTINGS_PREFIX + "NorthSouth",
ui->northSouthComboBox->currentIndex());
settings.setValue(APPLICATION_SETTINGS_PREFIX + "EastWest",
ui->eastWestComboBox->currentIndex());
settings.setValue(APPLICATION_SETTINGS_PREFIX + "Altitude",
ui->altitudeSpinBox->value());
}
void MainWindow::loadComPorts()
{
ui->menuFile->clear();
m_comPorts = new QActionGroup(this);
m_comPorts->setExclusive(true);
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
for (int i = 0; i < ports.size(); i++) {
QString fullName;
QString friendName = ports.at(i).friendName;
QString name = ports.at(i).portName;
if (name.contains("LPT")) {
continue;
}
#ifdef Q_OS_WIN
name.remove("\\\\.\\");
fullName = QString("%1").arg(friendName);
#else
#ifdef Q_OS_LINUX
name.prepend("/dev/");
#endif
if (friendName.size() > 0) {
fullName = QString("%1 (%2)").arg(friendName)
.arg(name);
} else {
QChar num = name.at(name.size() - 1);
if (name.contains("ttyS")) {
fullName = QString("Serial Port %1 (%2)").arg(num).arg(name);
} else if (name.contains("ttyACM")) {
fullName = QString("Serial ACM %1 (%2)").arg(num).arg(name);
} else if (name.contains("ttyUSB")) {
fullName = QString("Serial USB %1 (%2)").arg(num).arg(name);
}
}
#endif
QAction * com = new QAction(fullName, this);
com->setToolTip(name);
com->setCheckable(true);
if (com->toolTip() == m_tty) {
com->setChecked(true);
}
m_comPorts->addAction(com);
}
connect(m_comPorts, SIGNAL(triggered(QAction*)),
this, SLOT(comAction_triggered(QAction*)));
ui->menuFile->addActions(m_comPorts->actions());
ui->menuFile->addSeparator();
ui->menuFile->addAction(ui->actionRefresh);
ui->menuFile->addSeparator();
ui->menuFile->addAction(ui->actionQuit);
}
void MainWindow::comAction_triggered(QAction * action)
{
m_tty = action->toolTip();
if (m_serial->isOpen()) {
m_serial->close();
}
m_serial->open(m_tty);
}
void MainWindow::on_actionRefresh_triggered()
{
loadComPorts();
}
void MainWindow::serialError(QString error)
{
#ifdef Q_OS_UNIX
if (m_serial->currentError() != E_NO_ERROR) {
if (m_serial->currentPort().contains("ACM") ||
m_serial->currentPort().contains("USB")) {
m_serial->close();
}
}
#endif
m_error->setText(error);
m_error->setStyleSheet("QLabel { color : red; }");
}
void MainWindow::serialStatus(QString status)
{
m_error->setText(status);
m_error->setStyleSheet("QLabel { color : black; }");
}
void MainWindow::on_actionAbout_triggered(bool )
{
QMessageBox::about(this, "About XKSimulator", getAboutString());
}
void MainWindow::on_actionAboutQt_triggered(bool )
{
QMessageBox::aboutQt(this, "About Qt");
}
void MainWindow::speedOrBearingUpdated()
{
if (m_lines.size() > 0) {
// End the last speed for time
m_lines.last().end();
}
if (m_info.speed > 0) {
// Add a new speed for time
TravelLine st(m_info.speed, m_info.direction);
st.start();
m_lines.append(st);
}
}
void MainWindow::on_speedSpinBox_valueChanged(int i)
{
// Update the speed
m_info.speed = i;
speedOrBearingUpdated();
}
void MainWindow::on_bearingSpinBox_valueChanged(int i)
{
m_info.direction = i;
speedOrBearingUpdated();
}
void MainWindow::on_startStopButton_pressed()
{
if (ui->startStopButton->text() == "Start") {
startSending();
// Disable position controls
ui->positionGroupBox->setEnabled(false);
// Disable com port selection
m_comPorts->setEnabled(false);
ui->startStopButton->setText("Stop");
} else {
ui->startStopButton->setText("Start");
m_gpsTimer->stop();
// Enable position controls
ui->positionGroupBox->setEnabled(true);
// Enable com port selection
m_comPorts->setEnabled(true);
}
}
void MainWindow::sendGpsInfo()
{
char buf[2048];
int size = nmea_generate(&buf[0], sizeof(buf), &m_info,
GPGGA | GPGSA | GPGSV | GPRMC);
buf[size] = 0;
qDebug() << buf;
m_serial->write(QByteArray(buf, size));
}
void MainWindow::gpsTimer()
{
if (m_info.speed > 0) {
m_lines.last().end();
}
double total_distance = 0.0;
foreach(TravelLine line, m_lines) {
// Get the new position
_nmeaPOS startPos;
_nmeaPOS endPos;
// First get the start position
nmea_info2pos(&m_info, &startPos);
// Calculate the new position based on the bearing and distance
nmea_move_horz(&startPos, &endPos, line.bearing(), line.distance() / 1000.0);
// Update the info struct
nmea_pos2info(&endPos, &m_info);
total_distance += line.distance();
}
qDebug() << "Distance Traveled: " << total_distance;
m_lines.clear();
double lat = nmea_ndeg2degree(m_info.lat);
double lon = nmea_ndeg2degree(m_info.lon);
if (lat < 0.0) {
lat = -lat;
ui->northSouthComboBox->setCurrentIndex(1);
} else {
ui->northSouthComboBox->setCurrentIndex(0);
}
if (lon < 0.0) {
lon = -lon;
ui->eastWestComboBox->setCurrentIndex(1);
} else {
ui->eastWestComboBox->setCurrentIndex(0);
}
ui->latitudeSpinBox->setValue(lat);
ui->longitudeSpinBox->setValue(lon);
nmea_time_now(&m_info.utc);
// Finally send the GPS info
sendGpsInfo();
// Calculate the speed again
if (m_info.speed > 0) {
TravelLine line(m_info.speed, m_info.direction);
line.start();
m_lines.append(line);
}
}
void MainWindow::startSending()
{
nmea_zero_INFO(&m_info);
double lat = ui->latitudeSpinBox->value();
double lon = ui->longitudeSpinBox->value();
if (ui->northSouthComboBox->currentText() == "S") {
lat = -lat;
}
if (ui->eastWestComboBox->currentText() == "W") {
lon = -lon;
}
// Get current time
nmea_time_now(&m_info.utc);
m_info.sig = 3;
m_info.fix = ui->gpsFixComboBox->currentIndex() + 1;
m_info.lat = nmea_degree2ndeg(lat);
m_info.lon = nmea_degree2ndeg(lon);
m_info.speed = ui->speedSpinBox->value();
m_info.direction = ui->bearingSpinBox->value();
m_info.elv = ui->altitudeSpinBox->value();
m_info.satinfo.inuse = 8;
m_info.satinfo.inview = 12;
// Create the first speed for time
if (m_info.speed > 0) {
TravelLine line(m_info.speed, m_info.direction);
line.start();
m_lines.append(line);
}
// Start the GPS timer
m_gpsTimer->start(1000);
}