forked from UnexplodedMinds/Stratofier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAHRSMainWin.cpp
489 lines (380 loc) · 15.5 KB
/
AHRSMainWin.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
Stratofier Stratux AHRS Display
(c) 2018 Allen K. Lair, Sky Fun
*/
#include <QTimer>
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QFont>
#include <QDesktopWidget>
#include <QScreen>
#include <QGuiApplication>
#include <QSettings>
#include <QPushButton>
#include <QSpacerItem>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QByteArray>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include "AHRSMainWin.h"
#include "AHRSCanvas.h"
#include "MenuDialog.h"
#include "Canvas.h"
#include "Keypad.h"
#include "Builder.h"
extern QSettings *g_pSet;
// Standard fonts used throughout the app
QFont itsy( "Droid Sans", 8, QFont::Normal );
QFont wee( "Droid Sans", 10, QFont::Normal );
QFont tiny( "Droid Sans", 14, QFont::Normal );
QFont small( "Droid Sans", 16, QFont::Normal );
QFont med( "Droid Sans", 18, QFont::Bold );
QFont large( "Droid Sans", 24, QFont::Bold );
Canvas::Units g_eUnitsAirspeed = Canvas::Knots;
// Setup minimal UI elements and make the connections
AHRSMainWin::AHRSMainWin( const QString &qsIP, bool bPortrait, StreamReader *pStream )
: QMainWindow( Q_NULLPTR, Qt::Window | Qt::FramelessWindowHint ),
m_pStratuxStream( pStream ),
m_bStartup( true ),
m_pMenuDialog( nullptr ),
m_qsIP( qsIP ),
m_bPortrait( bPortrait ),
m_iTimerSeconds( 0 ),
m_bTimerActive( false ),
m_iReconnectTimer( -1 ),
m_iTimerTimer( -1 ),
m_bRecording( false )
{
m_pStratuxStream->setUnits( static_cast<Canvas::Units>( g_pSet->value( "UnitsAirspeed" ).toInt() ) );
itsy.setLetterSpacing( QFont::PercentageSpacing, 120.0 );
wee.setLetterSpacing( QFont::PercentageSpacing, 120.0 );
tiny.setLetterSpacing( QFont::PercentageSpacing, 120.0 );
med.setLetterSpacing( QFont::PercentageSpacing, 120.0 );
large.setLetterSpacing( QFont::PercentageSpacing, 120.0 );
setupUi( this );
m_pAHRSDisp->setPortrait( bPortrait );
m_pSplashLabel->resize( 250 * width() / 640, 318 * height() / 800 );
m_lastStatusUpdate = QDateTime::currentDateTime();
connect( m_pStratuxStream, SIGNAL( newSituation( StratuxSituation ) ), m_pAHRSDisp, SLOT( situation( StratuxSituation ) ) );
connect( m_pStratuxStream, SIGNAL( newTraffic( StratuxTraffic ) ), m_pAHRSDisp, SLOT( traffic( StratuxTraffic ) ) );
connect( m_pStratuxStream, SIGNAL( newStatus( bool, bool, bool, bool ) ), this, SLOT( statusUpdate( bool, bool, bool, bool ) ) );
connect( m_pStratuxStream, SIGNAL( newWTStatus( bool ) ), this, SLOT( WTUpdate( bool ) ) );
m_pStratuxStream->connectStreams();
QTimer::singleShot( 500, this, SLOT( init() ) );
QScreen *pScreen = QGuiApplication::primaryScreen();
pScreen->setOrientationUpdateMask( Qt::PortraitOrientation | Qt::InvertedPortraitOrientation | Qt::LandscapeOrientation | Qt::InvertedLandscapeOrientation );
connect( pScreen, SIGNAL( orientationChanged( Qt::ScreenOrientation ) ), this, SLOT( orient( Qt::ScreenOrientation ) ) );
m_iReconnectTimer = startTimer( 5000 ); // Forever timer to periodically check if we need to reconnect
QTimer::singleShot( 5000, this, SLOT( splashOff() ) );
}
void AHRSMainWin::init()
{
m_pAHRSDisp->orient( m_bPortrait );
m_pStatusIndicator->setMinimumHeight( height() * (m_bPortrait ? 0.0125 : 0.025) );
}
// Delete the stream reader
AHRSMainWin::~AHRSMainWin()
{
delete m_pStratuxStream;
m_pStratuxStream = nullptr;
delete m_pAHRSDisp;
m_pAHRSDisp = nullptr;
}
void AHRSMainWin::splashOff()
{
delete m_pSplashLabel;
m_pSplashLabel = nullptr;
}
// Status stream is received here instead of the canvas since here is where the indicators are
void AHRSMainWin::statusUpdate( bool bStratux, bool bAHRS, bool bGPS, bool bTraffic )
{
QString qsOn( "QLabel { border: none; background-color: LimeGreen; color: black; margin: 0px; }" );
QString qsOff( "QLabel { border: none; background-color: LightCoral; color: black; margin: 0px; }" );
m_pStatusIndicator->setStyleSheet( bStratux ? qsOn : qsOff );
m_pAHRSIndicator->setStyleSheet( bAHRS ? qsOn : qsOff );
m_pTrafficIndicator->setStyleSheet( bTraffic ? qsOn : qsOff );
m_pGPSIndicator->setStyleSheet( bGPS ? qsOn : qsOff );
m_lastStatusUpdate = QDateTime::currentDateTime();
}
// Valid WingThing data is being received
void AHRSMainWin::WTUpdate( bool bValid )
{
if( bValid )
m_pSensIndicator->setStyleSheet( "QLabel { border: none; background-color: LimeGreen; color: black; margin: 0px; }" );
else
m_pSensIndicator->setStyleSheet( "QLabel { border: none; background-color: LightCoral; color: black; margin: 0px; }" );
}
// Display the menu dialog and handle specific returns
void AHRSMainWin::menu()
{
if( m_pMenuDialog == nullptr )
{
CanvasConstants c = m_pAHRSDisp->canvas()->constants();
m_pMenuDialog = new MenuDialog( this, m_bPortrait, m_bRecording );
// Scale the menu dialog according to screen resolution
m_pMenuDialog->setMinimumWidth( static_cast<int>( c.dW4 ) );
m_pMenuDialog->setGeometry( c.dW - c.dW2, 0.0, static_cast<int>( c.dW2 ), static_cast<int>( c.dH ) );
m_pAHRSDisp->dark( true );
m_pMenuDialog->show();
connect( m_pMenuDialog, SIGNAL( resetLevel() ), this, SLOT( resetLevel() ) );
connect( m_pMenuDialog, SIGNAL( resetGMeter() ), this, SLOT( resetGMeter() ) );
connect( m_pMenuDialog, SIGNAL( upgradeStratofier() ), this, SLOT( upgradeStratofier() ) );
connect( m_pMenuDialog, SIGNAL( shutdownStratux() ), this, SLOT( shutdownStratux() ) );
connect( m_pMenuDialog, SIGNAL( shutdownStratofier() ), this, SLOT( shutdownStratofier() ) );
connect( m_pMenuDialog, SIGNAL( timer() ), this, SLOT( changeTimer() ) );
connect( m_pMenuDialog, SIGNAL( fuelTanks( FuelTanks ) ), this, SLOT( fuelTanks( FuelTanks ) ) );
connect( m_pMenuDialog, SIGNAL( stopFuelFlow() ), this, SLOT( stopFuelFlow() ) );
connect( m_pMenuDialog, SIGNAL( unitsAirspeed() ), this, SLOT( unitsAirspeed() ) );
connect( m_pMenuDialog, SIGNAL( setSwitchableTanks( bool ) ), this, SLOT( setSwitchableTanks( bool ) ) );
connect( m_pMenuDialog, SIGNAL( settingsClosed() ), this, SLOT( settingsClosed() ) );
connect( m_pMenuDialog, SIGNAL( magDev( int ) ), this, SLOT( magDev( int ) ) );
connect( m_pMenuDialog, SIGNAL( recordFlight( bool ) ), this, SLOT( recordFlight( bool ) ) );
}
else
{
delete m_pMenuDialog;
m_pMenuDialog = nullptr;
}
}
void AHRSMainWin::resetLevel()
{
emptyHttpPost( "cageAHRS" );
m_pStratuxStream->snapshotOrientation();
}
void AHRSMainWin::resetGMeter()
{
emptyHttpPost( "resetGMeter" );
}
void AHRSMainWin::emptyHttpPost( const QString &qsToken )
{
QNetworkAccessManager manager;
manager.post( QNetworkRequest( QUrl( QString( "http://%1/%2" ).arg( m_qsIP ).arg( qsToken ) ) ), QByteArray() );
QNetworkReply *pResp = manager.get( QNetworkRequest( QUrl( QString( "http://%1/cageAHRS" ).arg( m_qsIP ) ) ) );
QEventLoop eventloop;
connect( pResp, SIGNAL( finished() ), &eventloop, SLOT( quit() ) );
eventloop.exec();
QString throwaway = pResp->readAll();
delete m_pMenuDialog;
m_pMenuDialog = nullptr;
}
void AHRSMainWin::upgradeStratofier()
{
delete m_pMenuDialog;
m_pMenuDialog = nullptr;
if( QMessageBox::question( this, "UPGRADE", "Upgrading Stratofier requires an active internet connection.\n\n"
"Select 'Yes' to download and install the latest Stratofier version.",
QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
{
if( system( "/home/pi/Stratofier/upgrade.sh > /dev/null 2>&1 &" ) != 0 )
qDebug() << "Error executing upgrade script.";
QApplication::processEvents();
qApp->closeAllWindows();
}
}
void AHRSMainWin::shutdownStratofier()
{
qApp->exit( 0 );
}
void AHRSMainWin::shutdownStratux()
{
qApp->exit( 0 );
if( system( "sudo shutdown -h now" ) != 0 )
qDebug() << "Error shutting down.";
}
// Use the escape key to close the app since there is no title bar
void AHRSMainWin::keyReleaseEvent( QKeyEvent *pEvent )
{
if( pEvent->key() == Qt::Key_Escape )
qApp->closeAllWindows();
pEvent->accept();
}
void AHRSMainWin::timerEvent( QTimerEvent *pEvent )
{
if( pEvent == Q_NULLPTR )
return;
// If we haven't gotten a status update for over ten seconds, force a reconnect
if( pEvent->timerId() == m_iReconnectTimer )
{
if( m_lastStatusUpdate.secsTo( QDateTime::currentDateTime() ) > 10 )
{
m_pStratuxStream->disconnectStreams();
QTimer::singleShot( 1000, m_pStratuxStream, SLOT( connectStreams() ) );
}
}
else if( pEvent->timerId() == m_iTimerTimer )
{
QDateTime now = QDateTime::currentDateTime();
int iTimer = m_iTimerSeconds - static_cast<int>( m_timerStart.secsTo( now ) );
int iMinutes = iTimer / 60;
int iSeconds = iTimer - (iMinutes * 60);
m_pAHRSDisp->timerReminder( iMinutes, iSeconds );
}
}
void AHRSMainWin::changeTimer()
{
Keypad keypad( this, "TIMER", true );
m_pAHRSDisp->canvas()->setKeypadGeometry( &keypad );
delete m_pMenuDialog;
m_pMenuDialog = nullptr;
if( keypad.exec() == QDialog::Accepted )
{
QString qsTime = keypad.textValue();
m_timerStart = QDateTime::currentDateTime();
m_iTimerSeconds = (qsTime.left( 2 ).toInt() * 60) + qsTime.right( 2 ).toInt();
m_iTimerTimer = startTimer( 500 );
m_bTimerActive = true;
}
else
stopTimer();
}
void AHRSMainWin::restartTimer()
{
m_timerStart = QDateTime::currentDateTime();
m_bTimerActive = true;
}
void AHRSMainWin::stopTimer()
{
if( m_iTimerTimer == -1 )
killTimer( m_iTimerTimer );
m_pAHRSDisp->timerReminder( -1, -1 );
m_iTimerTimer = -1;
m_bTimerActive = false;
}
void AHRSMainWin::orient( Qt::ScreenOrientation o )
{
m_pAHRSDisp->orient( (o == Qt::PortraitOrientation) || (o == Qt::InvertedPortraitOrientation) );
}
void AHRSMainWin::fuelTanks( FuelTanks tanks )
{
if( !tanks.bDualTanks )
{
tanks.dRightCapacity = 0.0;
tanks.dRightRemaining = 0.0;
}
m_pAHRSDisp->setFuelTanks( tanks );
m_pAHRSDisp->m_bFuelFlowStarted = true;
QTimer::singleShot( 10, this, SLOT( fuelTanks2() ) );
}
void AHRSMainWin::fuelTanks2()
{
delete m_pMenuDialog;
m_pMenuDialog = nullptr;
}
void AHRSMainWin::stopFuelFlow()
{
m_pAHRSDisp->m_bFuelFlowStarted = false;
QTimer::singleShot( 10, this, SLOT( fuelTanks2() ) );
}
void AHRSMainWin::unitsAirspeed()
{
MenuDialog *pDlg = static_cast<MenuDialog *>( sender() );
int i = static_cast<int>( g_eUnitsAirspeed );
i++;
if( i > static_cast<int>( Canvas::KPH ) )
i = static_cast<int>( Canvas::MPH );
g_eUnitsAirspeed = static_cast<Canvas::Units>( i );
if( pDlg != nullptr )
{
if( g_eUnitsAirspeed == Canvas::MPH )
pDlg->m_pUnitsAirspeedButton->setText( "MPH" );
else if( g_eUnitsAirspeed == Canvas::Knots )
pDlg->m_pUnitsAirspeedButton->setText( "KNOTS" );
else
pDlg->m_pUnitsAirspeedButton->setText( "KPH" );
}
g_pSet->setValue( "UnitsAirspeed", static_cast<int>( g_eUnitsAirspeed ) );
g_pSet->sync();
m_pStratuxStream->setUnits( g_eUnitsAirspeed );
m_pAHRSDisp->update();
}
void AHRSMainWin::setSwitchableTanks( bool bSwitchable )
{
m_pAHRSDisp->setSwitchableTanks( bSwitchable );
}
void AHRSMainWin::magDev( int iMagDev )
{
m_pAHRSDisp->setMagDev( iMagDev );
}
void AHRSMainWin::recordFlight( bool bRec )
{
if( bRec )
m_Track.clear();
else
{
QString qsInternal;
Airport ap = TrafficMath::getCurrentAirport();
Builder::getStorage( &qsInternal );
qsInternal.append( QString( "/data/space.skyfun.stratofier/Stratofier_%1_%2.kml" ).arg( ap.qsID ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ).remove( ':' ).remove( '-' ) ) );
TrackPoint tp;
QDomDocument kml;
QDomElement xRoot, xDoc, xPlacemark, xStyle, xP, xL;
QDomNode xDocNode, xPlacemarkNode, xStyleNode;
QDomText nameText;
QFile track( qsInternal );
QString qsTrack;
kml.setContent( QString( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"
"<Document>\n"
"<Style id=\"stratofier_track\"><LineStyle><color>ff0055ff</color><width>3</width></LineStyle></Style>\n"
"<StyleMap id=\"m_stratofier_track\"><Pair><key>normal</key><styleUrl>stratofier_track</styleUrl></Pair></StyleMap>\n"
"</Document>\n"
"</kml>\n" ).toLatin1() );
if( track.open( QIODevice::WriteOnly ) )
{
xRoot = kml.documentElement(); // <klm>
xDocNode = xRoot.firstChild(); // <Document>
xDoc = xDocNode.toElement();
xPlacemark = kml.createElement( "name" );
nameText = kml.createTextNode( QString( "Stratofier_%1_%2.kml" ).arg( ap.qsID ).arg( QDateTime::currentDateTime().toString( Qt::ISODate ).remove( ':' ).remove( '-' ) ) );
xPlacemark.appendChild( nameText );
xStyleNode = xDoc.firstChild();
xDoc.insertBefore( xPlacemarkNode, xStyleNode );
xPlacemark = kml.createElement( "Placemark" );
xDoc.appendChild( xPlacemark );
xP = kml.createElement( "name" );
nameText = kml.createTextNode( "Track" );
xP.appendChild( nameText );
xPlacemark.appendChild( xP );
xP = kml.createElement( "description" );
nameText = kml.createTextNode( "Recorded by Stratofier" );
xP.appendChild( nameText );
xPlacemark.appendChild( xP );
xP = kml.createElement( "styleUrl" );
nameText = kml.createTextNode( "#m_stratofier_track" );
xP.appendChild( nameText );
xPlacemark.appendChild( xP );
xL = kml.createElement( "LineString" );
xPlacemark.appendChild( xL );
xP = kml.createElement( "extrude" );
nameText = kml.createTextNode( "1" );
xP.appendChild( nameText );
xL.appendChild( xP );
xP = kml.createElement( "tesselate" );
nameText = kml.createTextNode( "1" );
xP.appendChild( nameText );
xL.appendChild( xP );
xP = kml.createElement( "altitudeMode" );
nameText = kml.createTextNode( "absolute" );
xP.appendChild( nameText );
xL.appendChild( xP );
foreach( tp, m_Track )
qsTrack.append( QString( "%1,%2,%3\n" ).arg( tp.dLat ).arg( tp.dLong ).arg( tp.dAlt ) );
xP = kml.createElement( "coordinates" );
nameText = kml.createTextNode( qsTrack );
xP.appendChild( nameText );
xL.appendChild( xP );
track.write( kml.toString( 4 ).toLatin1() );
track.close();
}
}
m_bRecording = bRec;
}
void AHRSMainWin::settingsClosed()
{
QTimer::singleShot( 100, this, SLOT( menu() ) );
}