Subversion Repositories Projects

Rev

Rev 2335 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1
using GMap.NET;
2
using GMap.NET.MapProviders;
3
using GMap.NET.WindowsPresentation;
4
using MKLiveView.GMapCustomMarkers;
5
using System;
6
using System.Collections.Generic;
7
using System.ComponentModel;
8
using System.Data;
9
using System.Diagnostics;
10
using System.IO;
11
using System.Linq;
12
using System.Linq.Expressions;
13
using System.Runtime.InteropServices;
14
using System.Text;
15
using System.Threading;
16
using System.Threading.Tasks;
17
using System.Windows;
18
using System.Windows.Controls;
19
using System.Windows.Data;
20
using System.Windows.Documents;
21
using System.Windows.Input;
22
using System.Windows.Interop;
23
using System.Windows.Media;
24
using System.Windows.Media.Animation;
25
using System.Windows.Media.Imaging;
26
using System.Windows.Navigation;
27
using System.Windows.Shapes;
28
using System.Windows.Threading;
2313 - 29
using System.Globalization;
2324 - 30
using System.Xml;
31
using System.Xml.Linq;
2287 - 32
 
33
namespace MKLiveView
34
{
35
    /// <summary>
36
    /// Interaktionslogik für MainWindow.xaml
37
    /// </summary>
38
    public partial class MainWindow : Window
39
    {
2304 - 40
        #region declarations
41
 
2287 - 42
        GMapMarker copter;
43
        GMapMarker home;
44
        PointLatLng start;
45
        PointLatLng end;
46
        PointLatLng pHome;
2328 - 47
        GMapRoute mRouteWP;
48
        List<PointLatLng> wpList = new List<PointLatLng>();
2324 - 49
        #region NC-Errors
2287 - 50
        String[] NC_Error = new string[44]
51
        {
52
            "No Error",
53
            "FC not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A1_.22FC_not_compatible_.22",
54
            "MK3Mag not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A2_.22MK3Mag_not_compatible_.22",
55
            "no FC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A3_.22no_FC_communication_.22",
56
            "no compass communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A4_.22no_compass_communication_.22",
57
            "no GPS communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A5_.22no_GPS_communication_.22",
58
            "bad compass value" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A6_.22bad_compass_value.22",
59
            "RC Signal lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A7_.22RC_Signal_lost_.22",
60
            "FC spi rx error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A8_.22FC_spi_rx_error_.22",
61
            "ERR: no NC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A9:_.22ERR:_no_NC_communication.22",
62
            "ERR: FC Nick Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A10_.22ERR:_FC_Nick_Gyro.22",
63
            "ERR: FC Roll Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A11_.22ERR:_FC_Roll_Gyro.22",
64
            "ERR: FC Yaw Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A12_.22ERR:_FC_Yaw_Gyro.22",
65
            "ERR: FC Nick ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A13_.22ERR:_FC_Nick_ACC.22",
66
            "ERR: FC Roll ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A14_.22ERR:_FC_Roll_ACC.22",
67
            "ERR: FC Z-ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A15_.22ERR:_FC_Z-ACC.22",
68
            "ERR: Pressure sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A16_.22ERR:_Pressure_sensor.22",
69
            "ERR: FC I2C" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A17_.22ERR:_FC_I2C.22",
70
            "ERR: Bl Missing" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A18_.22ERR:_Bl_Missing.22",
71
            "Mixer Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A19_.22Mixer_Error.22",
72
            "FC: Carefree Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A20_.22FC:_Carefree_Error.22",
73
            "ERR: GPS lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A21_.22ERR:_GPS_lost.22",
74
            "ERR: Magnet Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A22_.22ERR:_Magnet_Error.22",
75
            "Motor restart" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A23_.22Motor_restart.22",
76
            "BL Limitation" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A24_.22BL_Limitation.22",
77
            "Waypoint range" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A25_.22Waypoint_range.22",
78
            "ERR:No SD-Card" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A26_.22ERR:No_SD-Card.22",
79
            "ERR:SD Logging aborted" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A27_.22ERR:SD_Logging_aborted.22",
80
            "ERR:Flying range!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A28_.22ERR:Flying_range.21.22",
81
            "ERR:Max Altitude" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A29_.22ERR:Max_Altitude.22",
82
            "No GPS Fix" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A30_.22No_GPS_Fix.22",
83
            "compass not calibrated" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A31_.22compass_not_calibrated.22",
84
            "ERR:BL selftest" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A32_.22ERR:BL_selftest.22",
85
            "no ext. compass" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A33_.22no_ext._compass.22",
86
            "compass sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A34_.22compass_sensor.22",
87
            "FAILSAFE pos.!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A35_.22FAILSAFE_pos..21__.22",
88
            "ERR:Redundancy" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A36_.22ERR:Redundancy__.22",
89
            "Redundancy test" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A37_.22Redundancy_test_.22",
90
            "GPS Update rate" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A38_.22GPS_Update_rate.22",
91
            "ERR:Canbus" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A39_.22ERR:Canbus.22",
92
            "ERR: 5V RC-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A40_.22ERR:_5V_RC-Supply.22",
93
            "ERR:Power-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A41_.22ERR:Power-Supply.22",
94
            "ACC not calibr." + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A42_.22ACC_not_calibr..22",
95
            "ERR:Parachute!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A43_.22ERR:Parachute.21.22"
96
        };
2324 - 97
        #endregion NC-Errors
2287 - 98
        [FlagsAttribute]
99
        enum NC_HWError0 : short
100
        {
101
            None = 0,
102
            SPI_RX = 1,
103
            COMPASS_RX = 2,
104
            FC_INCOMPATIBLE = 4,
105
            COMPASS_INCOMPATIBLE = 8,
106
            GPS_RX = 16,
107
            COMPASS_VALUE = 32
108
        };
109
        [FlagsAttribute]
110
        enum FC_HWError0 : short
111
        {
112
            None = 0,
113
            GYRO_NICK = 1,
114
            GYRO_ROLL = 2,
115
            GYRO_YAW = 4,
116
            ACC_NICK = 8,
117
            ACC_ROLL = 16,
118
            ACC_TOP = 32,
119
            PRESSURE = 64,
120
            CAREFREE = 128
121
        };
122
        [FlagsAttribute]
123
        enum FC_HWError1 : short
124
        {
125
            None = 0,
126
            I2C = 1,
127
            BL_MISSING = 2,
128
            SPI_RX = 4,
129
            PPM = 8,
130
            MIXER = 16,
131
            RC_VOLTAGE = 32,
132
            ACC_NOT_CAL = 64,
133
            RES3 = 128
134
        };
135
        public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
136
        // Various colors for logging info
137
        private Color[] LogMsgTypeColor = { Color.FromArgb(255, 43, 145, 175), Colors.Green, Colors.Black, Colors.Orange, Colors.Red };
138
 
139
        bool _bCBInit = true;
140
        bool _init = true;
141
        bool check_HWError = false;
142
 
143
        string filePath = Directory.GetCurrentDirectory();
144
        bool bReadContinously = false;
145
        bool _debugDataAutorefresh = true;
146
        bool _navCtrlDataAutorefresh = true;
147
        bool _blctrlDataAutorefresh = true;
148
        bool _OSDAutorefresh = true;
149
        bool _bErrorLog = false;
150
        bool _bConnErr = false;
151
        bool _bFollowCopter = false;
2324 - 152
        bool _bGPXLog = false;
2287 - 153
 
154
        bool _bSaveWinStateNormal = true;
155
        bool _bSaveWinStateFull = true;
156
 
157
        double scaleNormalAll = 1;
158
        double scaleNormalTopBar = 1;
159
        double scaleNormalMotors = 1;
160
        double scaleNormalOSD = 1;
161
        double scaleNormalLOG = 1;
162
        double scaleNormalHorizon = 1;
163
 
164
        double scaleFullAll = 1;
165
        double scaleFullTopBar = 1;
166
        double scaleFullMotors = 1;
167
        double scaleFullOSD = 1;
168
        double scaleFullLOG = 1;
169
        double scaleFullHorizon = 1;
170
 
171
        int _iCtrlAct = 0;
172
        int iOSDPage = 0;
173
        int iOSDMax = 0;
174
        int _iLifeCounter = 0;
175
        int crcError = 0;
176
 
2291 - 177
        bool _bSatFix = false;
178
        Storyboard stbSatFixLostAnim;
179
        bool _bAnimSatFixActive = false;
180
        bool _bVoiceSatFixActive = false;
181
        bool _bVoiceSatFixPlay = false;
182
        int _iSatsLast = 0;
183
        int _iSatsJitter = 0;
184
 
185
        bool _bMagneticFieldOK = false;
186
        Storyboard stbMagneticFieldAnim;
187
        bool _bAnimMagneticFieldActive = false;
188
        bool _bVoiceMagneticFieldActive = false;
189
        bool _bVoiceMagneticFieldPlay = false;
190
        int _iMagneticFieldLast = 0;
191
        int _iMagneticFieldJitter = 0;
192
 
193
        bool _bRCLevelOK = false;
194
        Storyboard stbRCLevelAnim;
195
        bool _bAnimRCLevelActive = false;
196
        bool _bVoiceRCLevelActive = false;
197
        bool _bVoiceRCLevelPlay = false;
198
        int _iRCLevelLast = 0;
199
        int _iRCLevelJitter = 0;
200
 
2287 - 201
        int _iMotors = 4;
202
        int _LipoCells = 4;
203
 
204
        double _dLipoVMax = 16.88;
205
        double _dLipoVMin = 12;
206
        double _dThresholdVoltageWarn = 0;
207
        double _dThresholdVoltageCrit = 0;
208
        Storyboard stbVoltageCritAnim;
209
        bool _bCritAnimVoltActive = false;
210
        bool _bCritVoiceVoltActive = false;
2291 - 211
        bool _bWarnVoiceVoltActive = false;
2287 - 212
        bool _bVoiceVoltPlay = false;
213
        double _dVoltLast = 0;
214
        int _iVoltJitter = 0;
215
 
2291 - 216
        double _dThresholdDistanceWarn = 100;
217
        Storyboard stbDistanceWarnAnim;
218
        bool _bAnimDistanceActive = false;
219
        bool _bVoiceDistanceActive = false;
220
        bool _bVoiceDistancePlay = false;
221
        double _dDistanceLast = 0;
222
        int _iDistanceJitter = 0;
223
 
224
        double _dThresholdDistanceMax = 1000;
225
        int _iThresholdRC = 160;
226
        int _iThresholdMagField = 15;
227
 
2295 - 228
        bool _bAutoHome = false;
229
        bool _bFirstSatFix = false;
230
        int _iFirstSatFix = 0;
2291 - 231
 
2287 - 232
        double _dTopHeight = 36;
233
 
234
        int[] serChan = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
235
        int[] serChan_sub = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
236
        string[] serChanTitle = new string[12];
237
 
238
        string[] sAnalogLabel = new string[32];
239
        string[] sAnalogData = new string[32];
2324 - 240
        int[] iAnalogData = new int[32];
2287 - 241
 
242
        int[] iTimings = new int[] {100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000 };
243
        int[] iMotors = new int[] {3,4,5,6,7,8,9,10,11,12 };
244
        string[] sLiPoCells = new string[] { "3s", "4s", "5s", "6s" };
245
        /// <summary>
246
        /// interval for sending debugdata (multiplied by 10ms)
247
        /// </summary>
248
        byte debugInterval = 10; //(=> 100ms)
249
        /// <summary>
250
        /// interval for sending BL-CTRL status (multiplied by 10ms)
251
        /// </summary>
252
        byte blctrlInterval = 75;
253
        /// <summary>
254
        /// interval for sending NAV-CTRL status (multiplied by 10ms)
255
        /// </summary>
256
        byte navctrlInterval = 80;
257
        /// <summary>
258
        /// interval for sending OSD page update (multiplied by 10ms)
259
        /// </summary>
260
        byte OSDInterval = 85;
261
        /// <summary>
262
        /// datatable for the debug data array - displayed on settings tabpage in datagridview
263
        /// </summary>
264
        DataTable dtAnalog = new DataTable();
265
        /// <summary>
266
        /// datatable for motordata (current,temp)
267
        /// </summary>
2315 - 268
        DataTable dtMotors = new DataTable();
2287 - 269
 
270
        DataTable dtWaypoints = new DataTable();
2313 - 271
        static volatile int _iWPCount = -1;
272
        static volatile int _iWPIndex = -1;
273
        int _iWPTimeout = 1000;
2287 - 274
        bool _bGetWP = false;
2328 - 275
        bool _bShowWPRoute = false;
2287 - 276
        static volatile bool _bGetWPCount = false;
2324 - 277
        DataTable dtGPX = new DataTable();
278
        DataRow drGPX;
279
        bool _bAirborne = false;
2340 - 280
        int _wpIndex = -1, _wpCount = 0;
281
 
2287 - 282
        DispatcherTimer timer = new DispatcherTimer();
283
 
284
        /// <summary>
285
        /// stuff for enabeling touch zoom for the map
286
        /// </summary>
287
        Point pTouch1 = new Point(0,0), pTouch2 = new Point(0,0);
288
        int iFirstStylusID = -1;
289
        public string connectButtonText
290
        {
291
            get
292
            {
293
                return bReadContinously ? "stop polling data" + System.Environment.NewLine + "from copter" : "start polling data" + System.Environment.NewLine + "from copter";
294
            }
295
        }
296
 
297
        WinState winState = new WinState();
2304 - 298
        #endregion declarations
2287 - 299
        public MainWindow()
300
        {
301
            InitializeComponent();
302
            _initForm();
303
            _dataTablesInit();
304
            _setupMap();
305
            _init = false;
306
            timer.Tick += new EventHandler(timerEvent);
307
            timer.Interval = new TimeSpan(0, 0, 1);
308
            timer.Start();
309
        }
310
 
311
        #region init
312
        void _initForm()
313
        {
314
            _readIni();
315
            if (_bSaveWinStateNormal)
316
                _setScaleSliders(false);
317
            cBoxTimingsDebug.ItemsSource =
318
                cBoxTimingsNav.ItemsSource =
319
                cBoxTimingsBl.ItemsSource =
320
                cBoxTimingsOSD.ItemsSource =
321
                iTimings;
322
            cBoxLiPoCells.ItemsSource = sLiPoCells;
323
            cBoxLiPoCells.SelectedItem = _LipoCells.ToString() + "s";
324
            _LipoMinMax();
325
            sliderThresholdVoltageWarn.Value = _dThresholdVoltageWarn;
326
            sliderThresholdVoltageCrit.Value = _dThresholdVoltageCrit;
327
            checkBoxThresholdVoltageVoice.IsChecked = _bVoiceVoltPlay;
2291 - 328
            checkBoxSatfixLost.IsChecked = _bVoiceSatFixPlay;
329
            checkBoxMagneticField.IsChecked = _bVoiceMagneticFieldPlay;
330
            checkBoxThresholdDistanceVoice.IsChecked = _bVoiceDistancePlay;
331
            sliderThresholdDistanceWarn.Value = _dThresholdDistanceWarn;
332
            checkBoxRClevel.IsChecked = _bVoiceRCLevelPlay;
2287 - 333
 
2291 - 334
            sliderThresholdDistanceWarn.Maximum = _dThresholdDistanceMax;
335
 
2287 - 336
            cBoxMotors.ItemsSource = iMotors;
337
            cBoxMotors.SelectedItem = _iMotors;
338
 
339
            serialPortCtrl.PortClosed += serialPortCtrl_PortClosed;
340
            serialPortCtrl.PortOpened += serialPortCtrl_PortOpened;
341
            serialPortCtrl.DataReceived += processMessage;
342
 
343
            chkbAutoBL.IsChecked = _blctrlDataAutorefresh;
344
            chkbAutoDbg.IsChecked = _debugDataAutorefresh;
345
            chkbAutoNav.IsChecked = _navCtrlDataAutorefresh;
346
            chkbAutoOSD.IsChecked = _OSDAutorefresh;
347
 
348
            cBoxTimingsDebug.SelectedItem = debugInterval * 10;
349
            cBoxTimingsNav.SelectedItem = navctrlInterval * 10;
350
            cBoxTimingsBl.SelectedItem = blctrlInterval * 10;
351
            cBoxTimingsOSD.SelectedItem = OSDInterval * 10;
2295 - 352
            checkBoxAutoSetHP.IsChecked = _bAutoHome;
2287 - 353
            checkBoxFollowCopter.IsChecked = _bFollowCopter;
2324 - 354
            checkBoxGPXLog.IsChecked = _bGPXLog;
2328 - 355
            checkBoxShowWPRoute.IsChecked = _bShowWPRoute;
2287 - 356
 
357
        }
358
        /// <summary>
359
        /// initialize the datatables
360
        /// with columnnames etc
361
        /// </summary>
362
        void _dataTablesInit()
363
        {
2315 - 364
            dtMotors.Columns.Add("#");
2313 - 365
            if (Thread.CurrentThread.CurrentUICulture.Name == "de-DE")
2315 - 366
                dtMotors.Columns.Add("Strom");
2313 - 367
            else
2315 - 368
                dtMotors.Columns.Add("Current");
369
            dtMotors.Columns.Add("Temp");
370
            dgvMotors1.DataContext = dtMotors.DefaultView;
2287 - 371
            _initDTMotors();
372
 
373
            Setter setter = new Setter(ContentControl.PaddingProperty, new Thickness(5,0,5,0));
374
            Style style = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
375
            style.Setters.Add(setter);
376
            setter = new Setter(ContentControl.BackgroundProperty, new SolidColorBrush(Colors.Transparent));
377
            style.Setters.Add(setter);
378
            setter = new Setter(ContentControl.ForegroundProperty, new SolidColorBrush(Colors.White));
379
            style.Setters.Add(setter);
2313 - 380
 
381
            dtWaypoints.Columns.Add("Index",typeof(int));
2315 - 382
            dtWaypoints.Columns.Add("Type", typeof(int));
2313 - 383
            dtWaypoints.Columns.Add("Name",typeof(string));
2315 - 384
            dtWaypoints.Columns.Add("Latitude",typeof(double));
385
            dtWaypoints.Columns.Add("Longitude",typeof(double));
2313 - 386
            dtWaypoints.Columns.Add("Altitude",typeof(string));
387
            dtWaypoints.Columns.Add("Heading",typeof(string));
388
            dtWaypoints.Columns.Add("Speed",typeof(string));
389
            dtWaypoints.Columns.Add("ClimbRate",typeof(string));
390
            dtWaypoints.Columns.Add("Radius",typeof(string));
391
            dtWaypoints.Columns.Add("HoldTime",typeof(string));
392
            dtWaypoints.Columns.Add("AutoTrigger",typeof(string));
393
            dtWaypoints.Columns.Add("CamAngle",typeof(string));
394
            dtWaypoints.Columns.Add("Event",typeof(string));
395
            dtWaypoints.Columns.Add("Out1Timer",typeof(string));
396
            dtWaypoints.Columns.Add("Status",typeof(string));
397
            dtWaypoints.PrimaryKey = new DataColumn[] { dtWaypoints.Columns["Index"] };
398
            dgvWP.ItemsSource = dtWaypoints.DefaultView;
2287 - 399
            dgvWP.ColumnHeaderStyle = new Style();
400
            dgvWP.ColumnHeaderStyle = style;
2313 - 401
 
2324 - 402
            dtGPX.Columns.Add("Index",typeof(int));
403
            dtGPX.Columns.Add("Latitude",typeof(double));
404
            dtGPX.Columns.Add("Longitude",typeof(double));
405
            dtGPX.Columns.Add("Elevation",typeof(int));
406
            dtGPX.Columns.Add("Time",typeof(string));
407
            dtGPX.PrimaryKey = new DataColumn[] { dtGPX.Columns["Index"] };
408
            drGPX = dtGPX.NewRow();
2313 - 409
 
2287 - 410
        }
411
        /// <summary>
2315 - 412
        /// initialize the datatable dtMotors for motor values
2313 - 413
        /// DataGridView dgvMotors1 is bound to dtMotors1
2287 - 414
        /// </summary>
415
        void _initDTMotors()
416
        {
417
            for (int i = 0; i < 12; i++)
418
            {
2315 - 419
                if (dtMotors.Rows.Count < 12)
420
                    dtMotors.Rows.Add((i + 1).ToString(), "NA", "NA");
2287 - 421
                else
422
                {
2315 - 423
                    dtMotors.Rows[i].SetField(1, "NA");
424
                    dtMotors.Rows[i].SetField(2, "NA");
2287 - 425
                }
426
            }
427
        }
428
 
429
        #endregion init
430
 
431
        #region events
432
        private void serialPortCtrl_PortOpened()
433
        {
434
            Dispatcher.Invoke(() => imageWiFi.Source = new BitmapImage(new Uri("Images/WiFi_G.png", UriKind.Relative)));
435
            _getVersion();
436
            Thread.Sleep(100);
437
            //_OSDMenue(0);
438
            //Thread.Sleep(200);
439
            //_sendSerialData();
440
            _readCont(true);
441
        }
442
        private void serialPortCtrl_PortClosed()
443
        {
444
            Dispatcher.Invoke(() => imageWiFi.Source = new BitmapImage(new Uri("Images/WiFi_W.png", UriKind.Relative)));
445
            _readCont(false);
446
        }
2324 - 447
        bool _bToggle = false;
2287 - 448
        void timerEvent(object sender, EventArgs e)
449
        {
450
            if (bReadContinously)
451
            {
452
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
453
 
454
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
455
 
456
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
457
                check_HWError = true;
458
                _getVersion();
459
                Thread.Sleep(10);
460
                if (_OSDAutorefresh)
461
                {
462
                    if (iOSDMax == 0 | cbOSD.Items.Count != iOSDMax)
463
                        _initOSDCB();
464
                    _OSDMenueAutoRefresh();
465
                }
466
                if (_iLifeCounter > 0)
467
                {
468
                    Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_G.png", UriKind.Relative)));
469
                  //  Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.LightGreen));
470
                    _iLifeCounter = 0;
471
                    _bConnErr = false;
2327 - 472
                    if(_bAirborne && _bGPXLog)
2324 - 473
                    {
474
                        drGPX[0] = dtGPX.Rows.Count;
475
                        dtGPX.Rows.Add(new object[] { drGPX[0], drGPX[1], drGPX[2], drGPX[3], drGPX[4] });
476
                    }
2327 - 477
                    Dispatcher.Invoke((Action)(() => tbSideBarGPXLog.Background = (_bAirborne && _bGPXLog && _bToggle) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));
478
                    Dispatcher.Invoke((Action)(() => tbSideBarGPXLog.Foreground = (_bAirborne && _bGPXLog) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));
479
                    Dispatcher.Invoke((Action)(() => tbSideBarGPXLog.BorderBrush = (_bAirborne && _bGPXLog) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));
2324 - 480
                    _bToggle = !_bToggle;
2287 - 481
                }
482
                else
483
                {
484
                    if (!_bConnErr)
485
                    {
486
                        Log(LogMsgType.Error, "No communication to NC/FC!");
487
                        Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_R.png", UriKind.Relative)));
488
                       // Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.Red));
489
                        _bConnErr = true;
490
                    }
491
                }
492
            }
493
        }
494
 
495
        private void labelData_MouseDown(object sender, MouseButtonEventArgs e)
496
        {
497
            GridData.Visibility = GridData.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
498
            GridSettings.Visibility = GridWP.Visibility = Visibility.Collapsed;
499
        }
500
        private void labelMotorData_MouseDown(object sender, MouseButtonEventArgs e)
501
        {
502
            GridMotors.Visibility = GridMotors.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden;
503
            if (GridMotors.IsVisible)
504
                _setMotorGridSize();
505
        }
506
        private void labelSettings_MouseDown(object sender, MouseButtonEventArgs e)
507
        {
508
            GridData.Visibility = GridWP.Visibility = Visibility.Collapsed;
509
            GridSettings.Visibility = GridSettings.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
510
        }
511
        private void labelLog_MouseDown(object sender, MouseButtonEventArgs e)
512
        {
513
            GridLog.Visibility = GridLog.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
514
        }
515
        private void labelOSD_MouseDown(object sender, MouseButtonEventArgs e)
516
        {
517
            GridOSD.Visibility = GridOSD.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden;
518
        }
519
        private void labelWaypoints_MouseDown(object sender, MouseButtonEventArgs e)
520
        {
521
            GridData.Visibility = GridSettings.Visibility = Visibility.Collapsed;
522
            GridWP.Visibility = GridWP.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
523
        }
524
 
525
        private void btnGetWP_Click(object sender, RoutedEventArgs e)
526
        {
527
            Thread t = new Thread(new ThreadStart(_getWP));
528
            t.Start();
529
        }
530
        private void btnSendWPList_Click(object sender, RoutedEventArgs e)
531
        {
2313 - 532
            Thread t = new Thread(new ThreadStart(_sendWPList));
533
            t.Start();
2287 - 534
        }
535
 
536
        private void btnConnectToCopter_Click(object sender, RoutedEventArgs e)
537
        {
538
            if (!serialPortCtrl.Port.IsOpen)
539
                serialPortCtrl.Connect(true);
540
            else
541
                _readCont(!bReadContinously);
542
        }
543
        private void btnSetHP_Click(object sender, RoutedEventArgs e)
544
        {
545
            setHomePos();
546
        }
547
        private void btnClearHP_Click(object sender, RoutedEventArgs e)
548
        {
549
            clearHomePos();
550
        }
551
        private void btnGotoHP_Click(object sender, RoutedEventArgs e)
552
        {
553
            if (home != null && MainMap.Markers.Contains(home))
554
                MainMap.Position = home.Position;
555
        }
2295 - 556
        private void checkBoxAutoSetHP_Click(object sender, RoutedEventArgs e)
557
        {
558
            _bAutoHome = (bool)checkBoxAutoSetHP.IsChecked;
559
        }
2287 - 560
        private void chkBoxSaveNormalState_Click(object sender, RoutedEventArgs e)
561
        {
562
            _bSaveWinStateNormal = (bool)chkBoxSaveNormalState.IsChecked;
563
        }
564
        private void chkBoxSaveFullScreenState_Click(object sender, RoutedEventArgs e)
565
        {
566
            _bSaveWinStateFull = (bool)chkBoxSaveFullScreenState.IsChecked;
567
        }
568
 
2291 - 569
        #region thresholds
2287 - 570
        private void sliderThresholdVoltageWarn_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
571
        {
572
            if(!_init)
573
                _dThresholdVoltageWarn = sliderThresholdVoltageWarn.Value;
574
        }
575
        private void sliderThresholdVoltageCrit_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
576
        {
577
            if(!_init)
578
                _dThresholdVoltageCrit = sliderThresholdVoltageCrit.Value;
579
        }
580
        private void checkBoxThresholdVoltageVoice_Click(object sender, RoutedEventArgs e)
581
        {
582
            _bVoiceVoltPlay = (bool)checkBoxThresholdVoltageVoice.IsChecked;
583
        }
2291 - 584
 
585
        private void sliderThresholdDistanceWarn_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
586
        {
587
            if(!_init)
588
                _dThresholdDistanceWarn = sliderThresholdDistanceWarn.Value;
589
        }
590
        private void checkBoxThresholdDistanceVoice_Click(object sender, RoutedEventArgs e)
591
        {
592
            _bVoiceDistancePlay = (bool)checkBoxThresholdDistanceVoice.IsChecked;
593
        }
594
 
595
        private void checkBoxSatfixLost_Click(object sender, RoutedEventArgs e)
596
        {
597
            _bVoiceSatFixPlay = (bool)checkBoxSatfixLost.IsChecked;
598
        }
599
        private void checkBoxMagneticField_Click(object sender, RoutedEventArgs e)
600
        {
601
            _bVoiceMagneticFieldPlay = (bool)checkBoxMagneticField.IsChecked;
602
        }
603
        private void checkBoxRClevel_Click(object sender, RoutedEventArgs e)
604
        {
605
            _bVoiceRCLevelPlay = (bool)checkBoxRClevel.IsChecked;
606
        }
607
        #endregion thresholds
608
 
2287 - 609
        private void buttonSwitchNC_Click(object sender, RoutedEventArgs e)
610
        {
611
            _switchToNC();
612
        }
613
 
614
        private void Window_Loaded(object sender, RoutedEventArgs e)
615
        {
616
            stbVoltageCritAnim = TryFindResource("VoltageCritAnim") as Storyboard;
2291 - 617
            stbSatFixLostAnim = TryFindResource("SatFixLostAnim") as Storyboard;
618
            stbMagneticFieldAnim = TryFindResource("MagneticFieldCritAnim") as Storyboard;
619
            stbDistanceWarnAnim = TryFindResource("DistanceCritAnim") as Storyboard;
620
            stbRCLevelAnim = TryFindResource("RCCritAnim") as Storyboard;
2287 - 621
            _setMotorGridSize();
622
        }
2310 - 623
        private void Window_Closing(object sender, CancelEventArgs e)
624
        {
625
            _writeIni();
2324 - 626
            if(_bGPXLog && dtGPX.Rows.Count > 0)
627
                _saveGPXLog();
2310 - 628
        }
2287 - 629
        #endregion events
630
 
631
        #region GMap
632
 
633
        void _setupMap()
634
        {
635
            MainMap.Manager.Mode = AccessMode.ServerAndCache;
636
            MainMap.MapProvider = GMapProviders.BingHybridMap;
637
            MainMap.SetPositionByKeywords("Landshut");
638
            MainMap.MinZoom = 0;
639
            MainMap.MaxZoom = 24;
640
            MainMap.Zoom = 16;
641
            MainMap.ShowCenter = true;
642
            MainMap.ShowTileGridLines = false;
643
            sliderMapZoom.Value = 16;
644
            comboBoxMapType.ItemsSource = providerList;
645
            comboBoxMapType.DisplayMemberPath = "Name";
646
            comboBoxMapType.SelectedItem = MainMap.MapProvider;
647
 
648
            // acccess mode
649
            comboBoxMode.ItemsSource = Enum.GetValues(typeof(AccessMode));
650
            comboBoxMode.SelectedItem = MainMap.Manager.Mode;
651
 
652
        }
653
 
654
        /// <summary>
655
        /// selection of relevant map providers --> if You need more, You can change the line:
656
        ///     comboBoxMapType.ItemsSource = providerList; 
657
        /// to:
658
        ///     comboBoxMapType.ItemsSource = GMapProviders.List;
659
        /// in _setupMap()
660
        /// or add items here:
661
        /// </summary>
662
        List<GMap.NET.MapProviders.GMapProvider> providerList = new List<GMap.NET.MapProviders.GMapProvider>
663
        { GMap.NET.MapProviders.GMapProviders.OpenCycleMap, GMap.NET.MapProviders.GMapProviders.OpenCycleLandscapeMap, GMap.NET.MapProviders.GMapProviders.OpenCycleTransportMap,
664
        GMap.NET.MapProviders.GMapProviders.BingMap,GMap.NET.MapProviders.GMapProviders.BingSatelliteMap,GMap.NET.MapProviders.GMapProviders.BingHybridMap,
665
        GMap.NET.MapProviders.GMapProviders.GoogleMap,GMap.NET.MapProviders.GMapProviders.GoogleSatelliteMap,GMap.NET.MapProviders.GMapProviders.GoogleHybridMap,GMap.NET.MapProviders.GMapProviders.GoogleTerrainMap,
666
        GMap.NET.MapProviders.GMapProviders.OviMap,GMap.NET.MapProviders.GMapProviders.OviSatelliteMap,GMap.NET.MapProviders.GMapProviders.OviHybridMap,GMap.NET.MapProviders.GMapProviders.OviTerrainMap};
667
 
668
        private void MainMap_Loaded(object sender, RoutedEventArgs e)
669
        {
670
            MainMap.Manager.Mode = AccessMode.ServerAndCache;
671
            copter = new GMapMarker(MainMap.Position);
2318 - 672
 
673
            copter.Shape = new CustomMarkerCopter(this, copter, MainMap.Position.Lat.ToString("0.#######°") + System.Environment.NewLine + MainMap.Position.Lng.ToString("0.#######°"),"red");
674
            if (comboBoxCopterColor.SelectionBoxItem != null)
675
            {
676
                string s = comboBoxCopterColor.SelectionBoxItem.ToString();
677
                ((CustomMarkerCopter)(copter.Shape)).setColor(s);
678
            }
2287 - 679
            copter.Offset = new System.Windows.Point(-18, -18);
680
            copter.ZIndex = int.MaxValue;
681
            MainMap.Markers.Add(copter);
682
            copter.Position = MainMap.Position;
683
        }
684
        void setHomePos()
685
        {
686
            pHome = MainMap.Position;
687
            if (!MainMap.Markers.Contains(home))
688
            {
689
                home = new GMapMarker(MainMap.Position);
690
                home.Shape = new CustomMarkerHome(this, home, MainMap.Position.Lat.ToString("0.#######°") + System.Environment.NewLine + MainMap.Position.Lng.ToString("0.#######°"));
691
                home.Offset = new System.Windows.Point(-18, -18);
692
                // home.ZIndex = int.MaxValue;
693
                MainMap.Markers.Add(home);
694
            }
695
            home.Position = MainMap.Position;
696
            ((CustomMarkerHome)(home.Shape)).setText(MainMap.Position.Lat.ToString("0.#######°") + System.Environment.NewLine + MainMap.Position.Lng.ToString("0.#######°"));
697
        }
698
        void clearHomePos()
699
        {
700
            MainMap.Markers.Remove(home);
701
        }
2335 - 702
        void _clearMapMarkers(Type markerType)
703
        {
704
            for (int k = 0; k < MainMap.Markers.Count;)
705
            {
706
                GMapMarker p = MainMap.Markers[k];
2340 - 707
                if (p.GetType() == markerType | (p.Shape != null && p.Shape.GetType() == markerType))
2335 - 708
                    MainMap.Markers.Remove(p);
709
                else
710
                    k++;
711
            }
2287 - 712
 
2335 - 713
        }
714
 
2287 - 715
        // access mode
716
        private void comboBoxMode_DropDownClosed(object sender, EventArgs e)
717
        {
718
            MainMap.Manager.Mode = (AccessMode)comboBoxMode.SelectedItem;
719
            MainMap.ReloadMap();
720
        }
721
        // zoom up
722
        private void czuZoomUp_Click(object sender, RoutedEventArgs e)
723
        {
724
            MainMap.Zoom = ((int)MainMap.Zoom) + 1;
725
        }
726
 
727
        // zoom down
728
        private void czuZoomDown_Click(object sender, RoutedEventArgs e)
729
        {
730
            MainMap.Zoom = ((int)(MainMap.Zoom + 0.99)) - 1;
731
        }
732
 
733
        // prefetch
734
        private void buttonPrefetch_Click(object sender, RoutedEventArgs e)
735
        {
736
            RectLatLng area = MainMap.SelectedArea;
737
            if (!area.IsEmpty)
738
            {
739
                for (int i = (int)MainMap.Zoom; i <= MainMap.MaxZoom; i++)
740
                {
741
                    MessageBoxResult res = MessageBox.Show("Ready ripp at Zoom = " + i + " ?", "GMap.NET", MessageBoxButton.YesNoCancel);
742
 
743
                    if (res == MessageBoxResult.Yes)
744
                    {
745
                        TilePrefetcher obj = new TilePrefetcher();
746
                        obj.Owner = this;
747
                        obj.ShowCompleteMessage = true;
748
                        obj.Start(area, i, MainMap.MapProvider, 100);
749
                    }
750
                    else if (res == MessageBoxResult.No)
751
                    {
752
                        continue;
753
                    }
754
                    else if (res == MessageBoxResult.Cancel)
755
                    {
756
                        break;
757
                    }
758
                }
759
            }
760
            else
761
            {
762
                MessageBox.Show("Select map area holding ALT", "GMap.NET", MessageBoxButton.OK, MessageBoxImage.Exclamation);
763
            }
764
        }
765
 
766
        // goto by geocoder
767
        private void buttonGeoCoding_Click(object sender, RoutedEventArgs e)
768
        {
769
            _goto_byGeoCoder();
770
        }
771
        // goto by geocoder
772
        private void textBoxGeo_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
773
        {
774
            if (e.Key == System.Windows.Input.Key.Enter)
775
                _goto_byGeoCoder();
776
        }
777
        void _goto_byGeoCoder()
778
        {
779
            GeoCoderStatusCode status = MainMap.SetPositionByKeywords(textBoxGeo.Text);
780
            if (status != GeoCoderStatusCode.G_GEO_SUCCESS)
781
            {
782
                MessageBox.Show("Geocoder can't find: '" + textBoxGeo.Text + "', reason: " + status.ToString(), "GMap.NET", MessageBoxButton.OK, MessageBoxImage.Exclamation);
783
            }
784
        }
785
        private void buttonGeoLoc_Click(object sender, RoutedEventArgs e)
786
        {
787
            try
788
            {
789
                double lat = double.Parse(textBoxLat.Text, System.Globalization.CultureInfo.InvariantCulture);
790
                double lng = double.Parse(textBoxLng.Text, System.Globalization.CultureInfo.InvariantCulture);
791
 
792
                MainMap.Position = new PointLatLng(lat, lng);
793
            }
794
            catch (Exception ex)
795
            {
796
                MessageBox.Show("incorrect coordinate format: " + ex.Message);
797
            }
798
 
799
        }
800
 
801
        private void ReloadMap_Click(object sender, RoutedEventArgs e)
802
        {
803
            MainMap.ReloadMap();
804
        }
805
 
806
        private void MainMap_OnPositionChanged(PointLatLng point)
807
        {
808
            if (_bFollowCopter)
809
                _setCopterData(MainMap.Position);
810
        }
811
 
812
        private void MainMap_OnMapZoomChanged()
813
        {
814
            if (_bFollowCopter)
815
                _setCopterData(MainMap.Position);
816
            if((int)sliderMapZoom.Value != MainMap.Zoom)
817
                sliderMapZoom.Value = MainMap.Zoom;
818
        }
819
 
820
        void _setCopterData(PointLatLng p)
821
        {
822
            Dispatcher.Invoke(() =>
823
            {
824
                copter.Position = p;
825
                ((CustomMarkerCopter)(copter.Shape)).setText(p.Lat.ToString("0.#######°") + System.Environment.NewLine + p.Lng.ToString("0.#######°"));
826
                textBoxLat_currentPos.Text = p.Lat.ToString() + "°";
827
                textBoxLng_currentPos.Text = p.Lng.ToString() + "°";
828
            });
829
            if (home != null && MainMap.Markers.Contains(home))
830
            {
831
                Dispatcher.Invoke(() => ArtHor.rotateHome = GMapProviders.EmptyProvider.Projection.GetBearing(copter.Position, home.Position));
832
                double d = GMapProviders.EmptyProvider.Projection.GetDistance(home.Position, copter.Position);
833
                Dispatcher.Invoke(() => tbTopDistanceHP.Text = (d * 1000).ToString("0.0 m"));
2291 - 834
 
835
                if(d*1000 < _dThresholdDistanceWarn)
836
                {
837
                    _iDistanceJitter = 0; _bVoiceDistanceActive = false;
838
                    if (stbDistanceWarnAnim != null && _bAnimDistanceActive)
839
                    {
840
                        Dispatcher.Invoke(() => stbDistanceWarnAnim.Stop());
841
                        _bAnimDistanceActive = false;
842
                    }
843
                }
844
                else
845
                {
846
                    if (_iDistanceJitter < 20)
847
                    { _iDistanceJitter++; }
848
                    if (_iDistanceJitter == 20)
849
                    {
850
                        if (stbDistanceWarnAnim != null && !_bAnimDistanceActive)
851
                        {
852
                            Dispatcher.Invoke(() => stbDistanceWarnAnim.Begin());
853
                            _bAnimDistanceActive = true;
854
                        }
855
                        if (_bVoiceDistancePlay && !_bVoiceDistanceActive)
856
                        {
2295 - 857
                            Thread t = new Thread(()=>_mediaPlayer("Voice\\Distance.mp3"));
858
                            t.Start();
2291 - 859
                            _bVoiceDistanceActive = true;
860
                        }
861
                        _iDistanceJitter++;
862
                    }
863
                }
2287 - 864
            }
865
        }
866
 
867
        private void checkBoxFollowCopter_Click(object sender, RoutedEventArgs e)
868
        {
869
            _bFollowCopter = (bool)checkBoxFollowCopter.IsChecked;
870
        }
871
 
872
        private void sliderMapZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
873
        {
874
            if (MainMap.Zoom != sliderMapZoom.Value)
875
                MainMap.Zoom = (int)sliderMapZoom.Value;
876
        }
877
        #region Touch zooming hackattack ;) 
878
        /// <summary>
879
        /// inspired by http://www.codeproject.com/Articles/692286/WPF-and-multi-touch
880
        /// </summary>
881
        bool bFirstAccess = true;
882
        double dDistance = 0;
883
        int iZoom;
884
        private void MainMap_StylusDown(object sender, StylusDownEventArgs e)
885
        {
886
            var id = e.StylusDevice.Id;
887
            e.StylusDevice.Capture(MainMap);
888
            if (iFirstStylusID == -1)
889
            {
890
                iFirstStylusID = id;
891
            }
892
            else
893
            {
894
 
895
                MainMap.CanDragMap = false;
896
            }
897
        }
898
        private void MainMap_StylusUp(object sender, StylusEventArgs e)
899
        {
900
            MainMap.ReleaseStylusCapture();
901
            iFirstStylusID = -1;
902
            bFirstAccess = true;
903
            MainMap.CanDragMap = true;
904
            iZoom = 0;
905
        }
906
        private void MainMap_StylusMove(object sender, StylusEventArgs e)
907
        {
908
            var id = e.StylusDevice.Id;
909
            var tp = e.GetPosition(MainMap);
910
 
911
            // This is the first Stylus point; just record its position. 
912
            if (id == iFirstStylusID)
913
            {
914
                pTouch1.X = tp.X;
915
                pTouch1.Y = tp.Y;
916
            }
917
            else
918
            if (iFirstStylusID > -1)
919
            {
920
                pTouch2.X = tp.X;
921
                pTouch2.Y = tp.Y;
922
                double distance = Point.Subtract(pTouch1, pTouch2).Length;
923
                if (!bFirstAccess)
924
                {
925
                    if (distance > dDistance)
926
                        iZoom++;
927
                    else
928
                        if (distance < dDistance)
929
                        iZoom--;
930
                }
931
                if (iZoom > 30)
932
                {
933
                    iZoom = 0;
934
                    Dispatcher.Invoke(() => sliderMapZoom.Value += 1);
935
                }
936
                if (iZoom < -30)
937
                {
938
                    iZoom = 0;
939
                    Dispatcher.Invoke(() => sliderMapZoom.Value -= 1);
940
                }
941
                dDistance = distance;
942
                bFirstAccess = false;
943
            }
944
        }
945
        #endregion Touch zooming hackattack ;)
946
 
947
        #endregion GMap
948
 
949
        #region settings
950
        private void cBoxTimingsDebug_DropDownClosed(object sender, EventArgs e)
951
        {
952
            if(! _bCBInit && cBoxTimingsDebug.SelectedIndex > -1)
953
                debugInterval = (byte)(Convert.ToInt16(cBoxTimingsDebug.SelectedItem) / 10);
954
        }
955
        private void cBoxTimingsNav_DropDownClosed(object sender, EventArgs e)
956
        {
957
            if(! _bCBInit && cBoxTimingsNav.SelectedIndex > -1)
958
                navctrlInterval = (byte)(Convert.ToInt16(cBoxTimingsNav.SelectedItem) / 10);
959
        }
960
        private void cBoxTimingsBl_DropDownClosed(object sender, EventArgs e)
961
        {
962
            if (!_bCBInit && cBoxTimingsBl.SelectedIndex > -1)
963
                blctrlInterval = (byte)(Convert.ToInt16(cBoxTimingsBl.SelectedItem) / 10);
964
        }
965
        private void cBoxTimingsOSD_DropDownClosed(object sender, EventArgs e)
966
        {
967
            if (!_bCBInit && cBoxTimingsOSD.SelectedIndex > -1)
968
                OSDInterval = (byte)(Convert.ToInt16(cBoxTimingsOSD.SelectedItem) / 10);
969
        }
970
        private void chkbAutoDbg_Click(object sender, RoutedEventArgs e)
971
        {
972
            if (!_init) _debugDataAutorefresh = (bool)chkbAutoDbg.IsChecked;
973
        }
974
        private void chkbAutoNav_Click(object sender, RoutedEventArgs e)
975
        {
976
            if (!_init) _navCtrlDataAutorefresh = (bool)chkbAutoNav.IsChecked;
977
        }
978
        private void chkbAutoBL_Click(object sender, RoutedEventArgs e)
979
        {
980
            if (!_init) _blctrlDataAutorefresh = (bool)chkbAutoBL.IsChecked;
981
        }
982
        private void chkbAutoOSD_Click(object sender, RoutedEventArgs e)
983
        {
984
            if (!_init) _OSDAutorefresh = (bool)chkbAutoOSD.IsChecked;
985
        }
986
 
987
        private void cBoxLiPoCells_DropDownClosed(object sender, EventArgs e)
988
        {
989
            if (cBoxLiPoCells.SelectedIndex > -1)
990
            {
991
                _LipoCells = cBoxLiPoCells.SelectedIndex + 3;
992
                _LipoMinMax();
993
            }
994
        }
995
        void _LipoMinMax()
996
        {
997
            _dLipoVMax = (double)(_LipoCells) * 4.22;
998
            _dLipoVMin = (double)_LipoCells * 3;
999
            pbTopVoltage.Maximum = _dLipoVMax;
1000
            pbTopVoltage.Minimum = _dLipoVMin;
1001
            sliderThresholdVoltageWarn.Maximum = _dLipoVMax;
1002
            sliderThresholdVoltageWarn.Minimum = _dLipoVMin;
1003
        }
1004
        private void cBoxMotors_DropDownClosed(object sender, EventArgs e)
1005
        {
1006
            if (cBoxMotors.SelectedIndex > -1)
1007
            {
1008
                _iMotors = cBoxMotors.SelectedIndex + 3;
1009
                Dispatcher.Invoke(() =>
1010
                {
1011
                    dgvMotors1.Height = (272 / 12.6) * (_iMotors + 1);  //272 / 12.6 --> Workaround, cause the headerheight = NaN...?
1012
                    GridMotors.Height = dgvMotors1.Height + 10;
1013
                });
1014
            }
1015
        }
1016
        void _setMotorGridSize()
1017
        {
1018
            if (dgvMotors1.Columns.Count > 2)
1019
            {
1020
                dgvMotors1.Columns[0].Width = 24;
1021
                dgvMotors1.Columns[1].Width = 50;
1022
                dgvMotors1.Columns[2].Width = 50;
1023
                dgvMotors1.Height = (272 / 12.6) * (_iMotors + 1);
1024
                GridMotors.Height = dgvMotors1.Height + 10;
1025
            }
1026
 
1027
        }
1028
        #endregion settings
1029
 
1030
        #region functions  
1031
 
1032
        #region logging      
1033
        /// <summary> Log data to the terminal window. </summary>
1034
        /// <param name="msgtype"> The type of message to be written. </param>
1035
        /// <param name="msg"> The string containing the message to be shown. </param>
1036
        private void Log(LogMsgType msgtype, string msg)
1037
        {
1038
            Dispatcher.Invoke(() =>
1039
            {
1040
               // rtfTerminal.CaretPosition = rtfTerminal.CaretPosition.DocumentEnd;
1041
               // rtfTerminal.Foreground = new SolidColorBrush(LogMsgTypeColor[(int)msgtype]);
1042
//                rtfTerminal.AppendText(msg + "\r");
1043
                TextRange tr = new TextRange(rtfTerminal.Document.ContentEnd,rtfTerminal.Document.ContentEnd);
1044
                tr.Text = msg;
1045
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(LogMsgTypeColor[(int)msgtype]));
1046
                rtfTerminal.AppendText("\r");
1047
                rtfTerminal.ScrollToEnd();
1048
            });
1049
        }
1050
        private void ErrorLog(LogMsgType msgtype, string msg)
1051
        {
1052
            Dispatcher.Invoke(() =>
1053
            {
1054
                TextRange tr = new TextRange(rtfError.Document.ContentEnd, rtfError.Document.ContentEnd);
1055
                tr.Text = msg;
1056
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(LogMsgTypeColor[(int)msgtype]));
1057
                rtfError.AppendText("\r");
1058
                rtfError.ScrollToEnd();
1059
 
1060
                _bErrorLog = true;
1061
            });
1062
        }
1063
        /// <summary>
1064
        /// Clear the line in the  errorlog window 
1065
        /// containing the error string when error has ceased
1066
        /// </summary>
1067
        /// <param name="s">substring of errrormessage</param>
1068
        void _clearErrorLog(string s)
1069
        {
1070
            Dispatcher.Invoke((Action)(() =>
1071
            {
1072
                TextRange searchRange = new TextRange(rtfError.Document.ContentStart, rtfError.Document.ContentEnd);
1073
                TextRange foundRange = FindTextInRange(searchRange, s);
1074
 
1075
                int iStart = searchRange.Text.IndexOf(s, StringComparison.OrdinalIgnoreCase);
1076
 
1077
 
1078
                if (iStart > -1)
1079
                {
1080
                    int iLength = 0;
1081
                    int iEnd = searchRange.Text.IndexOf('\r', iStart);
1082
                    if (iEnd > 0)
1083
                    {
1084
                        iLength = iEnd + 1;
1085
                        int iHttp = searchRange.Text.IndexOf("http", iEnd);
1086
                        if (iHttp == iLength)
1087
                        {
1088
                            int iEnd2 = searchRange.Text.IndexOf('\r', iLength);
1089
                            if (iEnd2 > iLength)
1090
                            {
1091
                                iLength = iEnd2 + 1;
1092
                              //  TextRange result = new TextRange(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), GetTextPositionAtOffset(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), iLength));
1093
 
1094
                                rtfError.Selection.Select(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), GetTextPositionAtOffset(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), iLength));
1095
                                rtfError.Selection.Text = string.Empty;
1096
                                if (rtfError.Document.ContentEnd.GetTextRunLength(LogicalDirection.Backward) < 2) _bErrorLog = false;
1097
                            }
1098
 
1099
                        }
1100
                        else
1101
                        {
1102
                            rtfError.Selection.Select(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), GetTextPositionAtOffset(rtfError.Document.ContentStart.GetPositionAtOffset(iStart), iLength));
1103
                            rtfError.Selection.Text = string.Empty;
1104
                            if (rtfError.Document.ContentEnd.GetTextRunLength(LogicalDirection.Backward) < 2) _bErrorLog = false;
1105
                        }
1106
                    }
1107
                }
1108
            }));
1109
 
1110
        }
1111
        public TextRange FindTextInRange(TextRange searchRange, string searchText)
1112
        {
1113
            int offset = searchRange.Text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
1114
            if (offset < 0)
1115
                return null;  // Not found
1116
 
1117
            var start = GetTextPositionAtOffset(searchRange.Start, offset);
1118
            TextRange result = new TextRange(start, GetTextPositionAtOffset(start, searchText.Length));
1119
 
1120
            return result;
1121
        }
1122
        TextPointer GetTextPositionAtOffset(TextPointer position, int characterCount)
1123
        {
1124
            while (position != null)
1125
            {
1126
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
1127
                {
1128
                    int count = position.GetTextRunLength(LogicalDirection.Forward);
1129
                    if (characterCount <= count)
1130
                    {
1131
                        return position.GetPositionAtOffset(characterCount);
1132
                    }
1133
 
1134
                    characterCount -= count;
1135
                }
1136
 
1137
                TextPointer nextContextPosition = position.GetNextContextPosition(LogicalDirection.Forward);
1138
                if (nextContextPosition == null)
1139
                    return position;
1140
 
1141
                position = nextContextPosition;
1142
            }
1143
 
1144
            return position;
1145
        }
1146
        #endregion logging
1147
 
1148
        #region processing received data
1149
 
1150
        private void processMessage(byte[] message)
1151
        {
1152
            if (message.Length > 0)
1153
            {
1154
                _iLifeCounter++;
1155
                //Log(LogMsgType.Incoming, BitConverter.ToString(message));
1156
                //Log(LogMsgType.Incoming, message.Length.ToString());
1157
                string s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, message.Length));
1158
                char cmdID;
1159
                byte adr;
1160
                byte[] data;
1161
                byte[] tmp = null;
1162
                if (message[0] != '#')
1163
                {
1164
                    int iFound = -1;
1165
                    for (int i = 0; i < message.Length; i++)   //Sometimes the FC/NC sends strings without termination (like WP messages)
1166
                    {                                   //so this is a workaround to not spam the log box
1167
                        if (message[i] == 35)
1168
                        {
1169
                            iFound = i;
1170
                            break;
1171
                        }
1172
                    }
1173
                    if (iFound > 0)
1174
                    {
1175
                        s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, iFound));
1176
                        tmp = new byte[message.Length - iFound];
1177
                        Buffer.BlockCopy(message, iFound, tmp, 0, message.Length - iFound);
1178
                    }
1179
                    s = s.Trim('\0', '\n', '\r');
1180
                    if (s.Length > 0)
1181
                        Log(LogMsgType.Normal, s);
1182
                    if (tmp != null)
1183
                    {
1184
                        s = new string(ASCIIEncoding.ASCII.GetChars(tmp, 0, tmp.Length));
1185
                        processMessage(tmp);
1186
                    }
1187
                }
1188
                //Debug.Print(s);
1189
                else
1190
                {
1191
                    FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data);
1192
 
1193
                    if (adr == 255) { crcError++; }
1194
                    else crcError = 0;
1195
                    Dispatcher.Invoke(() => tbCrc.Text = crcError.ToString());
1196
                    //display the active controller (FC / NC) 
1197
                    if (adr > 0 && adr < 3 && adr != _iCtrlAct) //adr < 3: temporary workaround cause when I've connected the FC alone it always switches between mk3mag & FC every second...???
1198
                    {
1199
                        _iCtrlAct = adr;
1200
                        switch (adr)
1201
                        {
1202
                            case 1:
1203
                                Dispatcher.Invoke(() => tbCtrl.Text = "FC");
2298 - 1204
                                Dispatcher.Invoke(() => buttonSwitchNC.Visibility = Visibility.Visible);
2287 - 1205
                                //Dispatcher.Invoke(() => labelSwitchToNavi.Visibility = Visibility.Visible);
1206
                              //  _setFieldsNA(); //display fields NA for FC 
1207
                                break;
1208
                            case 2:
1209
                                Dispatcher.Invoke(() => tbCtrl.Text = "NC");
1210
                                //Dispatcher.Invoke(() => buttonSwitchNC.Visibility = Visibility.Hidden);
1211
                                //Dispatcher.Invoke(() => labelSwitchToNavi.Visibility = Visibility.Hidden);
1212
                                break;
1213
                            //case 3:
1214
                            //    lblCtrl.Invoke((Action)(() => lblCtrl.Text = "MK3MAG"));
1215
                            //    break;
1216
                            //case 4:
1217
                            //    lblCtrl.Invoke((Action)(() => lblCtrl.Text = "BL-CTRL"));
1218
                            //    break;
1219
                            default:
1220
                                Dispatcher.Invoke(() => tbCtrl.Text = "NA");
1221
                                break;
1222
                        }
1223
                       // _loadLabelNames();
1224
                    }
1225
                    // else
1226
                    //     Debug.Print("Address == 0?");
1227
 
1228
                    if (data != null && data.Length > 0)
1229
                    {
1230
                        s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1));
1231
                        s = s.Trim('\0', '\n');
1232
 
1233
                        switch (cmdID)
1234
                        {
1235
                            //case 'A': //Label names
1236
                            //    _processLabelNames(s);
1237
                            //    break;
1238
 
1239
                            case 'D': //Debug data
1240
                                _processDebugVals(adr, data);
1241
                                break;
1242
 
1243
                            case 'V': //Version
1244
                                _processVersion(adr, data);
1245
                                break;
1246
 
1247
                            case 'K'://BL-CTRL data
1248
                                _processBLCtrl(data);
1249
                                break;
1250
 
1251
                            case 'O': //NC Data
1252
                                _processNCData(data);
1253
                                break;
1254
 
1255
                            case 'E': //NC error-string
1256
                                ErrorLog(LogMsgType.Error, "NC Error: " + s);
1257
                                break;
1258
 
1259
                            case 'L': //OSD Menue (called by pagenumber)
1260
                                _processOSDSingle(data);
1261
                                break;
1262
 
1263
                            case 'H': //OSD Menue (with autoupdate - called by Key)
1264
                                _processOSDAuto(data);
1265
                                break;
1266
 
1267
                            case 'X': //Waypoint data
1268
                                _processWPData(data);
1269
                                break;
1270
 
2313 - 1271
                            case 'W': //return new Waypoint items count after sending waypoint to copter
1272
                                _iWPCount = data[0];
1273
                                break;
2287 - 1274
                                //default:
1275
                                //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
1276
                                //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
1277
                                //    break;
1278
                        }
1279
                    }
1280
                    //else
1281
                    //{
1282
                    //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
1283
                    //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
1284
                    //}
1285
                }
1286
            }
1287
        }
1288
        /// <summary>
1289
        /// Analog label names 'A'
1290
        /// each label name is returned as a single string 
1291
        /// and added to string array sAnalogLabel[]
1292
        /// and the datatable dtAnalog
1293
        /// </summary>
1294
        /// <param name="s">the label name</param>
1295
        void _processLabelNames(string s)
1296
        {
1297
            //if (iLableIndex < 32)
1298
            //{
1299
            //    sAnalogLabel[iLableIndex] = s;
1300
            //    if (dtAnalog.Rows.Count < 32)
1301
            //        dtAnalog.Rows.Add(s, "");
1302
            //    else
1303
            //        dtAnalog.Rows[iLableIndex].SetField(0, s);
1304
 
1305
            //  //  _getAnalogLabels(iLableIndex + 1);
1306
            //}
1307
            //Debug.Print(s);
1308
        }
1309
        /// <summary>
1310
        /// Debug values 'D'
1311
        /// </summary>
1312
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
1313
        /// <param name="data">the received byte array to process</param>
1314
        void _processDebugVals(byte adr, byte[] data)
1315
        {
1316
            if (data.Length == 66)
1317
            {
2324 - 1318
 
2287 - 1319
                double v;
1320
                int index = 0;
1321
                Int16 i16 = 0;
1322
                double dTemp = 0;
1323
                for (int i = 2; i < 66; i += 2)
1324
                {
1325
                    i16 = data[i + 1];
1326
                    i16 = (Int16)(i16 << 8);
1327
                    iAnalogData[index] = data[i] + i16;
1328
                    sAnalogData[index] = (data[i] + i16).ToString();
1329
                   // dtAnalog.Rows[index].SetField(1, sAnalogData[index]);
1330
 
1331
                    if (adr == 2) //NC
1332
                    {
1333
                        switch (index)
1334
                        {
1335
                            case 0: //pitch (German: nick)
1336
                                Dispatcher.Invoke(() => ArtHor.Pitch = ((double)iAnalogData[index] / (double)10));
1337
                                Dispatcher.Invoke((Action)(() => tbPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
1338
                                break;
1339
                            case 1: //roll
1340
                                Dispatcher.Invoke(() => ArtHor.Roll = ((double)iAnalogData[index] / (double)10));
1341
                                Dispatcher.Invoke((Action)(() => tbRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
1342
                                break;
1343
                            case 4: //altitude
1344
                                Dispatcher.Invoke(() => tbAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"));
1345
                                Dispatcher.Invoke(() => tbTopHeight.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"));
2324 - 1346
                                Dispatcher.Invoke(() => { drGPX[3] = (double)iAnalogData[index] / (double)10; });
2287 - 1347
                                break;
1348
                            case 7: //Voltage
1349
                                v = (double)iAnalogData[index] / (double)10;
1350
                                Dispatcher.Invoke(() => tbVolt.Text = v.ToString("0.0 V"));
1351
                                Dispatcher.Invoke(() => tbTopVoltage.Text = v.ToString("0.0 V"));
1352
                                Dispatcher.Invoke(() => pbTopVoltage.Value = v);
1353
                                if (v - _dLipoVMin < 1 | v < _dThresholdVoltageWarn)
1354
                                {
1355
                                    if (v == _dVoltLast)
1356
                                        if(_iVoltJitter < 20) _iVoltJitter++;
1357
                                    else
1358
                                    {
1359
                                        _iVoltJitter = 0;
1360
                                        _dVoltLast = v;
1361
                                    }
1362
                                    if (_iVoltJitter == 20)
1363
                                    {
1364
                                        Dispatcher.Invoke(() => pbTopVoltage.Foreground = Brushes.Orange);
1365
 
1366
                                        if (v - _dLipoVMin < 1 | v < _dThresholdVoltageCrit)
1367
                                        {
1368
                                            Dispatcher.Invoke(() => pbTopVoltage.Foreground = Brushes.Red);
1369
                                            if (stbVoltageCritAnim != null && !_bCritAnimVoltActive)
1370
                                            {
1371
                                                Dispatcher.Invoke(() => stbVoltageCritAnim.Begin());
1372
                                                _bCritAnimVoltActive = true;
1373
                                            }
1374
                                            if (_bVoiceVoltPlay && !_bCritVoiceVoltActive)
1375
                                            {
2295 - 1376
                                                Thread t = new Thread(() => _mediaPlayer("Voice\\CriticalBattery.mp3"));
1377
                                                t.Start();
2287 - 1378
                                                _bCritVoiceVoltActive = true;
1379
                                            }
1380
                                        }
1381
                                        else
1382
                                        {
1383
                                            if (stbVoltageCritAnim != null && _bCritAnimVoltActive)
1384
                                            {
1385
                                                Dispatcher.Invoke(() => stbVoltageCritAnim.Stop());
1386
                                                _bCritAnimVoltActive = false;
1387
                                            }
1388
                                            _bCritVoiceVoltActive = false;
1389
 
2291 - 1390
                                            if (_bVoiceVoltPlay && !_bWarnVoiceVoltActive)
2287 - 1391
                                            {
2295 - 1392
                                                Thread t = new Thread(() => _mediaPlayer("Voice\\LowBattery.mp3"));
1393
                                                t.Start();
2291 - 1394
                                                _bWarnVoiceVoltActive = true;
2287 - 1395
                                            }
1396
                                        }
1397
                                    }
1398
                                }
1399
                                else
1400
                                {
1401
                                    Dispatcher.Invoke(() => pbTopVoltage.Foreground = new SolidColorBrush(Color.FromArgb(255, 107, 195, 123)));
1402
                                    if (stbVoltageCritAnim != null && _bCritAnimVoltActive)
1403
                                    {
1404
                                        Dispatcher.Invoke(() => stbVoltageCritAnim.Stop());
1405
                                        _bCritAnimVoltActive = false;
1406
                                    }
1407
                                    _bCritVoiceVoltActive = false;
2291 - 1408
                                    _bWarnVoiceVoltActive = false;
2287 - 1409
                                    _iVoltJitter = 0;
1410
                                }
1411
                                break;
1412
                            case 8: // Current
1413
                                Dispatcher.Invoke(() => tbCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"));
1414
                                Dispatcher.Invoke(() => tbTopCurrent.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"));
1415
                                break;
1416
                            case 10: //heading
2291 - 1417
                                Dispatcher.Invoke((Action)(() => tbHeading.Text = sAnalogData[index] + "°"));
2287 - 1418
                                Dispatcher.Invoke(() => ArtHor.rotate = iAnalogData[index]);
1419
                                break;
1420
                            case 12: // SPI error
2291 - 1421
                                Dispatcher.Invoke((Action)(() => tbSPI.Text = sAnalogData[index]));
2287 - 1422
                                break;
1423
                            case 14: //i2c error
2291 - 1424
                                Dispatcher.Invoke((Action)(() => tbI2C.Text = sAnalogData[index]));
2287 - 1425
                                break;
1426
                            case 20: //Earthmagnet field
2291 - 1427
                                Dispatcher.Invoke((Action)(() => tbMagF.Text = sAnalogData[index] + "%"));
1428
                                Dispatcher.Invoke((Action)(() => tbTopEarthMag.Text = sAnalogData[index] + "%"));
1429
 
1430
                                if (Math.Abs(100 - iAnalogData[index]) < _iThresholdMagField)
1431
                                {
1432
                                    Dispatcher.Invoke(() => imageEarthMag.Source = new BitmapImage(new Uri("Images/EarthMag.png", UriKind.Relative)));
1433
                                    _iMagneticFieldJitter = 0; _bVoiceMagneticFieldActive = false;
1434
                                    if (stbMagneticFieldAnim != null && _bAnimMagneticFieldActive)
1435
                                    {
1436
                                        Dispatcher.Invoke(() => stbMagneticFieldAnim.Stop());
1437
                                        _bAnimMagneticFieldActive = false;
1438
                                    }
1439
                                }
1440
                                else
1441
                                {
1442
                                    Dispatcher.Invoke(() => imageEarthMag.Source = new BitmapImage(new Uri("Images/EarthMag_R.png", UriKind.Relative)));
1443
                                    if(_iMagneticFieldLast >= Math.Abs(100 - iAnalogData[index]))
1444
                                    {
1445
                                        if (_iMagneticFieldJitter < 20)
1446
                                            _iMagneticFieldJitter++;
1447
                                    }
1448
                                    else
1449
                                    {
1450
                                        _iMagneticFieldJitter = 0;
1451
                                        _iMagneticFieldLast = Math.Abs(100 - iAnalogData[index]);
1452
                                    }
1453
                                    if(_iMagneticFieldJitter == 20)
1454
                                    {
1455
                                        if (stbMagneticFieldAnim != null && !_bAnimMagneticFieldActive)
1456
                                        {
1457
                                            Dispatcher.Invoke(() => stbMagneticFieldAnim.Begin());
1458
                                            _bAnimMagneticFieldActive = true;
1459
                                        }
1460
                                        if (_bVoiceMagneticFieldPlay && !_bVoiceMagneticFieldActive)
1461
                                        {
2295 - 1462
                                            Thread t = new Thread(() => _mediaPlayer("Voice\\MagneticField.mp3"));
1463
                                            t.Start();
2291 - 1464
                                            _bVoiceMagneticFieldActive = true;
1465
                                        }
1466
                                    }
1467
                                }
2287 - 1468
                                break;
1469
                            case 21: //GroundSpeed
1470
                                     Dispatcher.Invoke((Action)(() => tbSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s")));
1471
                                     Dispatcher.Invoke((Action)(() => tbTopSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s")));
1472
                                break;
2291 - 1473
 
1474
                            ///**********   needs testing --> not sure what position this is  ***************
2287 - 1475
                            case 28: //Distance East from saved home position -> calculate distance with distance N + height
1476
                                    dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2);
1477
                                    dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E
1478
                                                                           //  lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00")));
1479
                                    dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation
1480
                                    dTemp = Math.Sqrt(dTemp) / (double)10;
1481
                               //     Dispatcher.Invoke((Action)(() => tbTopDistanceHP.Text = dTemp.ToString("0.0 m")));
1482
                                    Dispatcher.Invoke((Action)(() => tbHP1.Text = dTemp.ToString("0.0 m")));
1483
                                break;
2291 - 1484
 
2287 - 1485
                            case 31: //Sats used
1486
                                     Dispatcher.Invoke((Action)(() => tbSats.Text = sAnalogData[index]));
1487
                                    // Dispatcher.Invoke((Action)(() => tbTopSats.Text = sAnalogData[index]));                                   
1488
                                break;
1489
                        }
1490
                    }
1491
                    index++;
1492
                }
1493
            }
1494
            else
1495
                Debug.Print("wrong data-length (66): " + data.Length.ToString());
1496
        }
1497
        /// <summary>
1498
        /// Version string 'V'
1499
        /// </summary>
1500
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
1501
        /// <param name="data">the received byte array to process</param>
1502
        void _processVersion(byte adr, byte[] data)
1503
        {
1504
            if (data.Length == 12)
1505
            {
1506
                if (!check_HWError)
1507
                {
1508
                    string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " };
1509
                    string sVersion = "";
1510
                    //sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b));
1511
                    Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: ");
1512
                    sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString();
1513
                    Log(LogMsgType.Incoming, sVersion);
1514
                    sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString();
1515
                    Log(LogMsgType.Incoming, sVersion);
1516
                    Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString());
1517
                }
1518
                if (data[5] > 0) //error0 
1519
                {
1520
                    if (adr == 1)
1521
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString());
1522
                    if (adr == 2)
1523
                        ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString());
1524
                }
1525
                if (data[6] > 0) //error1 
1526
                {
1527
                    if (adr == 1)
1528
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString());
1529
                    if (adr == 2)
1530
                        ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field
1531
                }
1532
                if ((data[5] + data[6] == 0) && _bErrorLog)
1533
                    _clearErrorLog(adr == 1 ? "FC - HW-Error" : "NC - HW-Error");
1534
 
1535
            }
1536
            check_HWError = false;
1537
        }
1538
        /// <summary>
1539
        /// BL-Ctrl data 'K'
1540
        /// for FC you have to use a customized firmware
1541
        /// </summary>
1542
        /// <param name="data">the received byte array to process</param>
1543
        void _processBLCtrl(byte[] data)
1544
        {
1545
            if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard!
1546
            {
1547
                bool bAvailable = false;
1548
                for (int i = 0; i < data.Length && data[i] < _iMotors; i += 6) // data[i] < _iMotors -- only show set number of motors (12 max @ moment)
1549
                {
1550
 
1551
                    if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available
1552
                        bAvailable = true;
1553
                    else
1554
                        bAvailable = false;
1555
 
1556
                    if (data[i] < _iMotors)
1557
                    {
1558
                        if (bAvailable)
1559
                        {
2315 - 1560
                            dtMotors.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
1561
                            dtMotors.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C"));
2287 - 1562
                        }
1563
                        else
1564
                        {
2315 - 1565
                            dtMotors.Rows[data[i]].SetField(1, "NA");
1566
                            dtMotors.Rows[data[i]].SetField(2, "NA");
2287 - 1567
                        }
1568
                    }
1569
                    //if (data[i] > 3 && data[i] < 8)
1570
                    //{
1571
                    //    if (bAvailable)
1572
                    //    {
1573
                    //        dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
1574
                    //        dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C"));
1575
                    //    }
1576
                    //    else
1577
                    //    {
1578
                    //        dtMotors2.Rows[data[i] - 4].SetField(1, "NA");
1579
                    //        dtMotors2.Rows[data[i] - 4].SetField(2, "NA");
1580
                    //    }
1581
                    //}
1582
                }
1583
            }
1584
        }
1585
        /// <summary>
1586
        /// Navi-Ctrl data 'O'
1587
        /// GPS-Position, capacatiy, flying time...
1588
        /// </summary>
1589
        /// <param name="data">the received byte array to process</param>
1590
        void _processNCData(byte[] data)
1591
        {
1592
            int i_32, i_16, iVal;
1593
            double d;
1594
            i_32 = data[4];
1595
            iVal = i_32 << 24;
1596
            i_32 = data[3];
1597
            iVal += i_32 << 16;
1598
            i_32 = data[2];
1599
            iVal += i_32 << 8;
1600
            iVal += data[1];
1601
            d = (double)iVal / Math.Pow(10, 7);
2324 - 1602
            Dispatcher.Invoke(() => { drGPX[2] = d; });
1603
 
2287 - 1604
            PointLatLng p = new PointLatLng();
1605
 
1606
            p.Lng = d;
1607
          //  lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree
1608
            //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds
1609
 
1610
            i_32 = data[8];
1611
            iVal = i_32 << 24;
1612
            i_32 = data[7];
1613
            iVal += i_32 << 16;
1614
            i_32 = data[6];
1615
            iVal += i_32 << 8;
1616
            iVal += data[5];
1617
            d = (double)iVal / Math.Pow(10, 7);
2324 - 1618
            Dispatcher.Invoke(() => { drGPX[1] = d; });
1619
            Dispatcher.Invoke(() => { drGPX[4] = DateTime.UtcNow.ToString("u", System.Globalization.CultureInfo.InvariantCulture); }); //2011-01-14T01:59:01Z });
2287 - 1620
            p.Lat = d;
2291 - 1621
            if (data[50] > 4)//if more than 4 sats in use . otherwise the map would jump and scroll insane at beginning
1622
            {
1623
                _bSatFix = true; _iSatsJitter = 0; _bVoiceSatFixActive = false;
2295 - 1624
                if(_bAutoHome && !_bFirstSatFix)
1625
                {
1626
                    if (_iFirstSatFix < 3)
1627
                        _iFirstSatFix++;
1628
                    else
1629
                    {
1630
                        _bFirstSatFix = true;
1631
                        Dispatcher.Invoke(() => setHomePos());
1632
                    }
1633
                }
2291 - 1634
                if (stbSatFixLostAnim != null && _bAnimSatFixActive)
1635
                {
1636
                    Dispatcher.Invoke(() => stbSatFixLostAnim.Stop());
1637
                    _bAnimSatFixActive = false;
1638
                }
1639
                if (!_bFollowCopter)
1640
                {
1641
                    _setCopterData(p);
1642
                    if (!MainMap.ViewArea.Contains(p))
1643
                        Dispatcher.Invoke(() => MainMap.Position = p);
2287 - 1644
 
2291 - 1645
                }
1646
                else
2287 - 1647
                    Dispatcher.Invoke(() => MainMap.Position = p);
1648
            }
1649
            else
2291 - 1650
            {
1651
                if(_bSatFix)
1652
                {
1653
                    if (data[50] == _iSatsLast)
1654
                    {
1655
                        if (_iSatsJitter < 20) _iSatsJitter++;
1656
                    }
1657
                    else
1658
                    {
1659
                        _iSatsJitter = 0;
1660
                        _iSatsLast = data[50];
1661
                    }
2287 - 1662
 
2291 - 1663
                    if (_iSatsJitter == 20)
1664
                    {
1665
                        if (stbSatFixLostAnim != null && !_bAnimSatFixActive)
1666
                        {
1667
                            Dispatcher.Invoke(() => stbSatFixLostAnim.Begin());
1668
                            _bAnimSatFixActive = true;
1669
                        }
1670
                        if (_bVoiceSatFixPlay && !_bVoiceSatFixActive)
1671
                        {
2335 - 1672
                            Thread th = new Thread(() => _mediaPlayer("Voice\\SatFixLost.mp3"));
2295 - 1673
                            th.Start();
2291 - 1674
                            _bVoiceSatFixActive = true;
1675
                        }
1676
 
1677
                        _bSatFix = false;
1678
                    }
1679
                }
1680
            }
1681
 
2287 - 1682
            //  lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree
1683
            //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds
1684
 
1685
            i_16 = data[28];
1686
            i_16 = (Int16)(i_16 << 8);
1687
            iVal = data[27] + i_16;
1688
            Dispatcher.Invoke((Action)(() => tbWP.Text = ((double)iVal / (double)10).ToString("0.0 m"))); //Distance to next WP
1689
 
1690
            i_16 = data[45];
1691
            i_16 = (Int16)(i_16 << 8);
1692
            iVal = data[44] + i_16;
1693
        //    Dispatcher.Invoke((Action)(() => tbTopDistanceHP.Text = ((double)iVal / (double)10).ToString("0.0 m"))); //Distance to HP set by GPS on
1694
            Dispatcher.Invoke((Action)(() => tbHP.Text = ((double)iVal / (double)10).ToString("0.0 m"))); //Distance to HP set by GPS on
1695
 
1696
            Dispatcher.Invoke((Action)(() => tbWPIndex.Text = data[48].ToString())); //Waypoint index
2340 - 1697
            _wpIndex = data[48];
1698
            //if(wpList.Count > 0 && ((data[67] & 2) == 2))
1699
            //{
1700
            //    Dispatcher.Invoke(() =>
1701
            //    {
1702
            //        if(data[48] < wpList.Count)
1703
            //        {
1704
            //            DataGridRow row;
1705
            //            if (data[48] - 1 > -1)
1706
            //            {
1707
            //                row = (DataGridRow)dgvWP.ItemContainerGenerator.ContainerFromIndex(data[48] - 1);
1708
            //                row.Background = new SolidColorBrush(Colors.Transparent);
1709
            //            }
1710
            //            row = (DataGridRow)dgvWP.ItemContainerGenerator.ContainerFromIndex(data[48]);
1711
            //            row.Background = new SolidColorBrush(Color.FromArgb(50, 0, 100, 255));
1712
            //        }
1713
            //    });
1714
            //}
2287 - 1715
            Dispatcher.Invoke((Action)(() => tbWPCount.Text = data[49].ToString())); //Waypoints count
2340 - 1716
            _wpCount = data[49];
2287 - 1717
            Dispatcher.Invoke((Action)(() => tbTopSats.Text = data[50].ToString())); //Satellites
1718
 
1719
            //--------------- Capacity used ------------------------
1720
            i_16 = data[81];
1721
            i_16 = (Int16)(i_16 << 8);
1722
            iVal = data[80] + i_16;
1723
            Dispatcher.Invoke((Action)(() => tbCapacity.Text = iVal.ToString() + " mAh"));
1724
            Dispatcher.Invoke((Action)(() => tbTopCapacity.Text = iVal.ToString() + " mAh"));
1725
 
1726
            //--------------- Flying time ------------------------
1727
            i_16 = data[56];
1728
            i_16 = (Int16)(i_16 << 8);
1729
            iVal = data[55] + i_16;
1730
            TimeSpan t = TimeSpan.FromSeconds(iVal);
1731
            string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2");
1732
            Dispatcher.Invoke((Action)(() => tbFTime.Text = Text.ToString()));
1733
            Dispatcher.Invoke((Action)(() => tbTopFTime.Text = Text.ToString()));
1734
 
1735
            //--------------- RC quality ------------------------
1736
            Dispatcher.Invoke((Action)(() => tbRCQ.Text = data[66].ToString()));
1737
            Dispatcher.Invoke((Action)(() => tbTopRC.Text = data[66].ToString()));
1738
 
2291 - 1739
            if(data[66] > _iThresholdRC)
1740
            {
1741
                _iRCLevelJitter = 0; _bVoiceRCLevelActive = false;
1742
                if (stbRCLevelAnim != null && _bAnimRCLevelActive)
1743
                {
1744
                    Dispatcher.Invoke(() => stbRCLevelAnim.Stop());
1745
                    _bAnimRCLevelActive = false;
1746
                }
1747
            }
1748
            else
1749
            {
1750
                if (_iRCLevelJitter < 20) _iRCLevelJitter++;
1751
                if (_iRCLevelJitter == 20)
1752
                {
1753
                    if (stbRCLevelAnim != null && !_bAnimRCLevelActive)
1754
                    {
1755
                        Dispatcher.Invoke(() => stbRCLevelAnim.Begin());
1756
                        _bAnimRCLevelActive = true;
1757
                    }
1758
                    if (_bVoiceRCLevelPlay && !_bVoiceRCLevelActive)
1759
                    {
2295 - 1760
                        Thread th = new Thread(() => _mediaPlayer("Voice\\RCLevel.mp3"));
1761
                        th.Start();
2291 - 1762
                        _bVoiceRCLevelActive = true;
1763
                    }
1764
                        _iRCLevelJitter++;
1765
                }
1766
            }
1767
 
2287 - 1768
            //--------------- NC Error ------------------------
1769
            Dispatcher.Invoke((Action)(() => tbNCErr.Text = data[69].ToString()));  //NC Errornumber
1770
            if (data[69] > 0)
1771
                _readNCError();
1772
            if (data[69] > 0 & data[69] < 44)
1773
                ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]);
1774
            else
1775
                if (_bErrorLog) _clearErrorLog("NC Error");
1776
 
2298 - 1777
            //-------------FC / NC Status Flags
1778
            Dispatcher.Invoke((Action)(() => FC1_1.Background = ((data[67] & 1) ==1) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_MOTOR_RUN                             0x01
1779
            Dispatcher.Invoke((Action)(() => FC1_2.Background = ((data[67] & 2) ==2) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_FLY                                   0x02
1780
            Dispatcher.Invoke((Action)(() => FC1_3.Background = ((data[67] & 4) ==4) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_CALIBRATE                             0x04
1781
            Dispatcher.Invoke((Action)(() => FC1_4.Background = ((data[67] & 8) ==8) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_START                                 0x08
1782
            Dispatcher.Invoke((Action)(() => FC1_5.Background = ((data[67] & 16) ==16) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_EMERGENCY_LANDING                      0x10
1783
            Dispatcher.Invoke((Action)(() => FC1_6.Background = ((data[67] & 32) ==32) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_LOWBAT                         0x20
1784
 
1785
            Dispatcher.Invoke((Action)(() => FC2_1.Background = ((data[74] & 1) ==1) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_CAREFREE                             0x01
1786
            Dispatcher.Invoke((Action)(() => FC2_2.Background = ((data[74] & 2) ==2) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_ALTITUDE_CONTROL                     0x02
1787
            Dispatcher.Invoke((Action)(() => FC2_3.Background = ((data[74] & 4) ==4) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_RC_FAILSAVE_ACTIVE                      0x04
1788
            Dispatcher.Invoke((Action)(() => FC2_4.Background = ((data[74] & 8) ==8) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_OUT1_ACTIVE                          0x08
1789
            Dispatcher.Invoke((Action)(() => FC2_5.Background = ((data[74] & 16) ==16) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_OUT2_ACTIVE                        0x10
1790
            Dispatcher.Invoke((Action)(() => FC2_6.Background = ((data[74] & 32) ==32) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_WAIT_FOR_TAKEOFF                   0x20   // Motor Running, but still on the ground
1791
            Dispatcher.Invoke((Action)(() => FC2_7.Background = ((data[74] & 64) ==64) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_AUTO_STARTING                              0x40
1792
            Dispatcher.Invoke((Action)(() => FC2_8.Background = ((data[74] & 128) ==128) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_AUTO_LANDING                             0x80
1793
 
1794
            Dispatcher.Invoke((Action)(() => NC1_2.Background = ((data[68] & 2) == 2) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_PH                                     0x02
1795
            Dispatcher.Invoke((Action)(() => NC1_3.Background = ((data[68] & 4) == 4) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_CH                                     0x04
1796
            Dispatcher.Invoke((Action)(() => NC1_4.Background = ((data[68] & 8) == 8) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_RANGE_LIMIT                               0x08
1797
            Dispatcher.Invoke((Action)(() => NC1_5.Background = ((data[68] & 16) == 16) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_NOSERIALLINK                            0x10
1798
            Dispatcher.Invoke((Action)(() => NC1_6.Background = ((data[68] & 32) == 32) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_TARGET_REACHED                               0x20
1799
            Dispatcher.Invoke((Action)(() => NC1_7.Background = ((data[68] & 64) == 64) ? new SolidColorBrush(Colors.DodgerBlue) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_MANUAL_CONTROL                          0x40
1800
            Dispatcher.Invoke((Action)(() => NC1_8.Background = ((data[68] & 128) == 128) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_GPS_OK                                     0x80
2299 - 1801
 
1802
            //Sidebar StatusSymbols
1803
            Dispatcher.Invoke((Action)(() => tbSideBarStatusMotors.Background = ((data[67] & 1) ==1) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_MOTOR_RUN                             0x01
1804
            Dispatcher.Invoke((Action)(() => tbSideBarStatusMotors.Foreground = ((data[67] & 1) ==1) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255,211,210,210))));// FC_STATUS_MOTOR_RUN                                0x01
1805
            Dispatcher.Invoke((Action)(() => tbSideBarStatusMotors.BorderBrush = ((data[67] & 1) ==1) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS_MOTOR_RUN                            0x01
1806
 
1807
            Dispatcher.Invoke((Action)(() => tbSideBarStatusCF.Background = ((data[74] & 1) == 1) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_CAREFREE                                0x01
1808
            Dispatcher.Invoke((Action)(() => tbSideBarStatusCF.Foreground = ((data[74] & 1) == 1) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS2_CAREFREE                                0x01
1809
            Dispatcher.Invoke((Action)(() => tbSideBarStatusCF.BorderBrush = ((data[74] & 1) ==1) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS2_CAREFREE                                0x01
1810
 
1811
            Dispatcher.Invoke((Action)(() => tbSideBarStatusEmergencyLanding.Background = ((data[67] & 16) == 16) ? new SolidColorBrush(Colors.LightCoral) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS_EMERGENCY_LANDING                   0x10
2304 - 1812
            Dispatcher.Invoke((Action)(() => tbSideBarStatusEmergencyLanding.Foreground = ((data[67] & 16) == 16) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS_EMERGENCY_LANDING                0x10
1813
            Dispatcher.Invoke((Action)(() => tbSideBarStatusEmergencyLanding.BorderBrush = ((data[67] & 16) == 16) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS_EMERGENCY_LANDING               0x10
2299 - 1814
 
1815
            Dispatcher.Invoke((Action)(() => tbSideBarStatusAC.Background = ((data[74] & 2) ==2) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// FC_STATUS2_ALTITUDE_CONTROL                         0x02
1816
            Dispatcher.Invoke((Action)(() => tbSideBarStatusAC.Foreground = ((data[74] & 2) ==2) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS2_ALTITUDE_CONTROL                         0x02
1817
            Dispatcher.Invoke((Action)(() => tbSideBarStatusAC.BorderBrush = ((data[74] & 2) ==2) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// FC_STATUS2_ALTITUDE_CONTROL                        0x02
1818
 
1819
            Dispatcher.Invoke((Action)(() => tbSideBarStatusPH.Text = ((data[68] & 4) == 4) ? "CH" : "PH"));// NC_FLAG_PH 0x02 / NC_FLAG_CH 0x04
2304 - 1820
            Dispatcher.Invoke((Action)(() => tbSideBarStatusPH.Background = (((data[68] & 2) == 2)|((data[68] & 4) == 4)) ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent)));// NC_FLAG_PH 0x02 / NC_FLAG_CH 0x04
1821
            Dispatcher.Invoke((Action)(() => tbSideBarStatusPH.Foreground = (((data[68] & 2) == 2)|((data[68] & 4) == 4)) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// NC_FLAG_PH 0x02 / NC_FLAG_CH 0x04
1822
            Dispatcher.Invoke((Action)(() => tbSideBarStatusPH.BorderBrush = (((data[68] & 2) == 2)|((data[68] & 4) == 4)) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Color.FromArgb(255, 211, 210, 210))));// NC_FLAG_PH 0x02 / NC_FLAG_CH 0x04
2299 - 1823
 
2324 - 1824
            _bAirborne = (data[67] & 2) == 2 ? true : false;
2287 - 1825
        }
1826
        /// <summary>
1827
        /// Navi-Ctrl WP data struct 'X'
1828
        /// called by index
1829
        /// </summary>
1830
        /// <param name="data">the received byte array to process</param>
1831
        void _processWPData(byte[] data)
1832
        {
2313 - 1833
            _iWPCount = data[0];
2287 - 1834
            _bGetWPCount = false;
1835
            if (data.Length >= 28)
1836
            {
1837
                Dispatcher.Invoke(() => lblWPIndex.Content = data[1].ToString());
1838
                Dispatcher.Invoke(() => lblWPCount.Content = data[0].ToString());
1839
                if (_bGetWP)
1840
                {
1841
                    if (data[1] == 1)
2315 - 1842
                    {
2333 - 1843
                        if(mRouteWP != null)
1844
                            Dispatcher.Invoke(() => MainMap.Markers.Remove(mRouteWP));
2328 - 1845
                        wpList.Clear();
1846
 
1847
                        Dispatcher.Invoke(() => lblWPRouteDistance.Content = "0 m");
2287 - 1848
                        dtWaypoints.Rows.Clear();
2333 - 1849
                        Dispatcher.Invoke(() =>
1850
                        {
2318 - 1851
                            for (int k = 0; k < MainMap.Markers.Count;)
1852
                            {
1853
                                GMapMarker p = MainMap.Markers[k];
1854
                                if (p.Shape.GetType() == typeof(CustomMarkerWP))
1855
                                    MainMap.Markers.Remove(p);
1856
                                else
1857
                                    k++;
1858
                            }
1859
                        });
2315 - 1860
                    }
2287 - 1861
                    DataRow dr = dtWaypoints.NewRow();
1862
                    dr = Waypoints.toDataRow(data, dr);
2313 - 1863
                    dtWaypoints.Rows.Add(dr);
2318 - 1864
                    Dispatcher.Invoke(() => {
1865
                        GMapMarker wp = new GMapMarker(new PointLatLng((double)dr[3], (double)dr[4]));
1866
                        wp.Shape = new CustomMarkerWP(this, wp, (string)dr[2],(int)dr[1]);
2333 - 1867
                        wp.Offset = new System.Windows.Point(-11.5, -11.5);
2318 - 1868
                        _setMarkerColor(wp, (int)dr[1]);
1869
                        MainMap.Markers.Add(wp);
1870
                    });
2313 - 1871
                    Dispatcher.Invoke(() => dgvWP.Items.Refresh());
1872
                    Dispatcher.Invoke(() => _iWPIndex = data[1]);
2335 - 1873
                    if ((int)dr[1] == 0)
1874
                        wpList.Add(new PointLatLng((double)dr[3], (double)dr[4]));
2287 - 1875
                    if (data[1] == data[0])
1876
                    {
1877
                        _bGetWP = false;
1878
                        Dispatcher.Invoke(() => dgvWP.Items.Refresh());
2328 - 1879
 
1880
                        Dispatcher.Invoke(() =>
1881
                        {
1882
                            if (comboBoxRouteColor.SelectionBoxItem != null)
1883
                            {
1884
                                string s = comboBoxRouteColor.SelectionBoxItem.ToString();
1885
                                mRouteWP = new GMapRoute(wpList, _getBrush(s));
1886
                            }
1887
                            else
1888
                                mRouteWP = new GMapRoute(wpList, null);
1889
 
1890
                            if (_bShowWPRoute)
1891
                                MainMap.Markers.Add(mRouteWP);
1892
                        });
1893
                        MapRoute mr = new MapRoute(wpList, "WPList");
2333 - 1894
                        Dispatcher.Invoke(() => lblWPRouteDistance.Content = (mr.Distance * 1000).ToString("0 m"));
2287 - 1895
                    }
1896
 
1897
                }
1898
            }
1899
            else
1900
            {
1901
                Dispatcher.Invoke(() => lblWPIndex.Content = 0);
1902
                Dispatcher.Invoke(() => lblWPCount.Content = 0);
1903
            }
1904
        }
1905
        /// <summary>
1906
        /// OSD Menue 'L'
1907
        /// single page called by pagenumber
1908
        /// no autoupdate
1909
        /// </summary>
1910
        /// <param name="data">the received byte array to process</param>
1911
        void _processOSDSingle(byte[] data)
1912
        {
1913
            if (data.Length == 84)
1914
            {
1915
                string sMessage = "";
1916
                iOSDPage = data[0];
1917
                iOSDMax = data[1];
1918
                if (cbOSD.Items.Count != iOSDMax) _initOSDCB();
1919
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4));
1920
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)+ "\r", true);
1921
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)+ "\r", false);
1922
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)+ "\r", false);
1923
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20), false);
1924
                Dispatcher.Invoke(() => { cbOSD.SelectedValue = iOSDPage; });
1925
              //  lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
1926
 
1927
            }
1928
            //else
1929
            //    OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)");
1930
 
1931
        }
1932
        /// <summary>
1933
        /// OSD Menue 'H'
1934
        /// called by keys (0x01,0x02,0x03,0x04)
1935
        /// autoupdate
1936
        /// </summary>
1937
        /// <param name="data">the received byte array to process</param>
1938
        void _processOSDAuto(byte[] data)
1939
        {
1940
            if (data.Length == 81)
1941
            {
1942
                string sMessage = "";
1943
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
1944
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)+ "\r", true);
1945
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)+ "\r", false);
1946
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)+ "\r", false);
1947
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20), false);
1948
 
1949
            }
1950
            //else
1951
            //    OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
1952
        }
1953
 
1954
        #endregion processing received data
1955
 
1956
        #region controller messages
1957
        /// <summary> send message to controller to request data
1958
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1959
        /// </summary>
1960
        /// <param name="CMDID"> the command ID </param>
1961
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1962
        private void _sendControllerMessage(char CMDID, byte address)
1963
        {
1964
            if (serialPortCtrl.Port.IsOpen)
1965
            {
1966
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1967
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address);
1968
                serialStream.Write(bytes, 0, bytes.Length);
1969
 
1970
            }
1971
            else
1972
                Log(LogMsgType.Error, "NOT CONNECTED!");
1973
        }
1974
        /// <summary> send message to controller to request data
1975
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1976
        /// </summary>
1977
        /// <param name="CMDID"> the command ID </param>
1978
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1979
        /// <param name="data"> additional data for the request</param>
1980
        private void _sendControllerMessage(char CMDID, byte address, byte[] data)
1981
        {
1982
            if (serialPortCtrl.Port.IsOpen)
1983
            {
1984
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1985
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address, data);
1986
                serialStream.Write(bytes, 0, bytes.Length);
1987
 
1988
            }
1989
            else
1990
                Log(LogMsgType.Error, "NOT CONNECTED!");
1991
        }
1992
 
1993
        /// <summary>
1994
        /// start/stop continous polling of controller values
1995
        /// </summary>
1996
        /// <param name="b">start/stop switch</param>
1997
        void _readCont(bool b)
1998
        {
1999
            bReadContinously = b;
2000
            if (bReadContinously)
2001
            {
2002
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
2003
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
2004
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
2005
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10); }
2006
                // Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.LightGreen));
2007
                Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_G.png", UriKind.Relative)));
2008
            }
2009
            else
2010
            {
2011
                // Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.LightGray));
2012
                Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_W.png", UriKind.Relative)));
2013
                _bConnErr = false;
2014
            }
2015
            _iLifeCounter = 0;
2016
        }
2017
 
2018
        private void _getVersion()
2019
        {
2020
            _sendControllerMessage('v', 0);
2021
        }
2022
        /// <summary>
2023
        /// get FC version struct via NC
2024
        /// by sending '1' as data (not documented in wiki...)
2025
        /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version)
2026
        /// </summary>
2027
        /// <param name="ctrl">controller number 1=FC</param> 
2028
        private void _getVersion(byte ctrl)
2029
        {
2030
            _sendControllerMessage('v', 0, new byte[1] { ctrl });
2031
        }
2032
        /// <summary>
2033
        /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00
2034
        /// </summary>
2035
        private void _switchToNC()
2036
        {
2037
            if (serialPortCtrl.Port.IsOpen)
2038
            {
2039
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2040
                byte[] bytes = new byte[5] { 0x1B, 0x1B, 0x55, 0xAA, 0x00 };
2041
                serialStream.Write(bytes, 0, bytes.Length);
2042
 
2043
                Thread.Sleep(100);
2044
                _getVersion();
2045
                Thread.Sleep(100);
2046
               // _OSDMenue(0);
2047
            }
2048
            else
2049
                Log(LogMsgType.Error, "NOT CONNECTED!");
2050
        }
2051
        /// <summary>
2052
        /// switch to FC
2053
        /// </summary>
2054
        private void _switchToFC()
2055
        {
2056
            _sendControllerMessage('u', 2, new byte[1] { (byte)0 });
2057
            Thread.Sleep(100);
2058
            _getVersion();
2059
            Thread.Sleep(100);
2060
          //  _OSDMenue(0);
2061
        }
2062
        /// <summary>
2063
        /// send RESET signal to FC
2064
        /// </summary>
2065
        private void _resetCtrl()
2066
        {
2067
            _sendControllerMessage('R', 1);
2068
        }
2069
        /// <summary>
2070
        /// poll the debug data (4sec subscription)
2071
        /// </summary>
2072
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
2073
        private void _readDebugData(bool auto)
2074
        {
2075
            byte interval = auto ? debugInterval : (byte)0;
2076
            _sendControllerMessage('d', 0, new byte[1] { debugInterval });
2077
        }
2078
        /// <summary>
2079
        /// poll the BL-CTRL status via NC (4sec subscription)
2080
        /// </summary>
2081
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
2082
        private void _readBLCtrl(bool auto)
2083
        {
2084
            byte interval = auto ? blctrlInterval : (byte)0;
2085
            _sendControllerMessage('k', 0, new byte[1] { interval });
2086
        }
2087
        /// <summary>
2088
        /// poll the NC data struct (4sec subscription)
2089
        /// </summary>
2090
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
2091
        private void _readNavData(bool auto)
2092
        {
2093
            byte interval = auto ? navctrlInterval : (byte)0;
2094
            _sendControllerMessage('o', 2, new byte[1] { interval });
2095
        }
2096
        /// <summary>
2097
        /// get the errortext for pending NC error
2098
        /// </summary>
2099
        private void _readNCError()
2100
        {
2101
            _sendControllerMessage('e', 2);
2102
        }
2103
        /// <summary>
2104
        /// request the Waypoint at index
2105
        /// </summary>
2106
        /// <param name="index"></param>
2107
        void _getpWP(int index)
2108
        {
2109
            if (serialPortCtrl.Port.IsOpen)
2110
            {
2111
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2112
                byte[] bytes = FlightControllerMessage.CreateMessage('x', 2, new byte[1] { (byte)index });
2113
                serialStream.Write(bytes, 0, bytes.Length);
2114
            }
2115
            else
2116
                Log(LogMsgType.Error, "NOT CONNECTED!");
2117
 
2118
        }
2119
        void _getWP()
2120
        {
2335 - 2121
            int iTimeout = 0;
2287 - 2122
            _bGetWPCount = true;
2315 - 2123
            _getpWP(1); //get the itemscount of wp
2335 - 2124
            while (_bGetWPCount & iTimeout < _iWPTimeout * 5)
2125
            {
2126
                Thread.Sleep(10);
2127
                iTimeout++;
2128
            }
2129
            if (_iWPCount > 0 & !_bGetWPCount)
2287 - 2130
                _getWPList();
2131
        }
2132
        void _getWPList()
2133
        {
2134
            _bGetWP = true;
2313 - 2135
            int iTimeout=0;
2136
            for (int j = 0; j < _iWPCount; j++)
2287 - 2137
            {
2138
                _getpWP(j + 1);
2313 - 2139
                iTimeout = 0;
2140
                while (_iWPIndex != j + 1 & iTimeout < _iWPTimeout * 5)
2141
                {
2142
                    Thread.Sleep(1);
2143
                    iTimeout++;
2144
                }
2287 - 2145
            }
2146
        }
2340 - 2147
 
2148
        void clearCopterWPList()
2313 - 2149
        {
2340 - 2150
            _clearCopterWPList();
2151
        }
2152
        bool _clearCopterWPList()
2153
        {
2335 - 2154
            int iTimeout = 0;
2313 - 2155
            if (serialPortCtrl.Port.IsOpen)
2156
            {
2157
                byte[] bData = new byte[30];
2158
                for (int i = 0; i < 30; i++)
2159
                    bData[i] = 0;
2160
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2161
                byte[] bytes = FlightControllerMessage.CreateMessage('w', 2, bData); //delete all WP on Copter by sending 'invalid'(==0) at index 0
2162
                serialStream.Write(bytes, 0, bytes.Length);
2287 - 2163
 
2313 - 2164
                _iWPCount = -1;
2340 - 2165
                while (_iWPCount == -1 & iTimeout < _iWPTimeout)
2335 - 2166
                {
2313 - 2167
                    Thread.Sleep(10);
2335 - 2168
                    iTimeout++;
2169
                }
2313 - 2170
                Dispatcher.Invoke(() => lblWPCount.Content = _iWPCount.ToString());
2335 - 2171
                if (_iWPCount > -1)
2340 - 2172
                    return true;
2173
                else
2174
                    Log(LogMsgType.Error, "Timeout while sending list!");
2175
            }
2176
            else
2177
                Log(LogMsgType.Error, "NOT CONNECTED!");
2178
 
2179
            return false;
2180
}
2181
        void _sendWPList()
2182
        {
2183
            int iTimeout = 0;
2184
            if (serialPortCtrl.Port.IsOpen)
2185
            {
2186
                if (_clearCopterWPList())
2313 - 2187
                {
2335 - 2188
                    for (int i = 0; i < dtWaypoints.Rows.Count; i++)
2189
                    {
2340 - 2190
                        _sendWayPoint(dtWaypoints.Rows[i],-1);
2313 - 2191
 
2335 - 2192
                        _iWPCount = -1;
2193
                        iTimeout = 0;
2194
                        while (_iWPCount == -1 & iTimeout < _iWPTimeout * 5)
2195
                        {
2196
                            Thread.Sleep(10);
2197
                            iTimeout++;
2198
                        }
2199
                        if (_iWPCount == -1)
2200
                        {
2201
                            Log(LogMsgType.Error, "Timeout while sending list!");
2202
                            break;
2203
                        }
2204
                        Dispatcher.Invoke(() => lblWPCount.Content = _iWPCount.ToString());
2205
                    }
2313 - 2206
                }
2207
            }
2208
            else
2209
                Log(LogMsgType.Error, "NOT CONNECTED!");
2210
 
2211
        }
2340 - 2212
        bool _sendWayPoint(DataRow dr, int index)
2213
        {
2313 - 2214
 
2340 - 2215
            if (serialPortCtrl.Port.IsOpen)
2216
            {
2217
                byte[] bData = new byte[30];
2218
                for (int i = 0; i < 30; i++)
2219
                    bData[i] = 0;
2220
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2221
                byte[] bytes;
2222
                int iVal;
2223
                double dVal;
2224
                NumberFormatInfo nfi = new NumberFormatInfo();
2225
                nfi.NumberDecimalSeparator = ",";
2226
 
2227
                //longitude
2228
                dVal = Convert.ToDouble(dr[4], nfi);
2229
                iVal = (int)(dVal * Math.Pow(10, 7));
2230
                bData[0] = (byte)(iVal & 0xff);
2231
                bData[1] = (byte)((iVal >> 8) & 0xff);
2232
                bData[2] = (byte)((iVal >> 16) & 0xff);
2233
                bData[3] = (byte)(iVal >> 24);
2234
                //latitude
2235
                dVal = Convert.ToDouble(dr[3], nfi);
2236
                iVal = (int)(dVal * Math.Pow(10, 7));
2237
                bData[4] = (byte)(iVal & 0xff);
2238
                bData[5] = (byte)((iVal >> 8) & 0xff);
2239
                bData[6] = (byte)((iVal >> 16) & 0xff);
2240
                bData[7] = (byte)(iVal >> 24);
2241
                //altitude
2242
                dVal = Convert.ToDouble(dr[5], nfi);
2243
                iVal = (int)(dVal * 10);
2244
                bData[8] = (byte)(iVal & 0xff);
2245
                bData[9] = (byte)((iVal >> 8) & 0xff);
2246
                bData[10] = (byte)((iVal >> 16) & 0xff);
2247
                bData[11] = (byte)(iVal >> 24);
2248
                //Status 'NEWDATA'
2249
                bData[12] = 1;
2250
                //heading
2251
                iVal = Convert.ToInt16(dr[6]);
2252
                bData[13] = (byte)(iVal & 0xff);
2253
                bData[14] = (byte)((iVal >> 8) & 0xff);
2254
                //ToleranceRadius
2255
                bData[15] = Convert.ToByte(dr[9]);
2256
                //HoldTime
2257
                bData[16] = Convert.ToByte(dr[10]);
2258
                //Event_Flag
2259
                bData[17] = Convert.ToByte(dr[13]);
2260
                //Index
2261
                bData[18] = index > 0 ? (byte)index : Convert.ToByte((int)dr[0]);
2262
                //Type
2263
                bData[19] = Convert.ToByte(dr[1]);
2264
                //WP_EventChannelValue
2265
                bData[20] = Convert.ToByte(dr[14]);
2266
                //AltitudeRate
2267
                bData[21] = Convert.ToByte(dr[8]);
2268
                //Speed
2269
                bData[22] = Convert.ToByte(dr[7]);
2270
                //CamAngle
2271
                bData[23] = (byte)Convert.ToInt16(dr[12]);
2272
                //Name
2273
                byte[] name = ASCIIEncoding.ASCII.GetBytes((string)dr[2]);
2274
                bData[24] = name.Length > 0 ? name[0] : (byte)0;
2275
                bData[25] = name.Length > 1 ? name[1] : (byte)0;
2276
                bData[26] = name.Length > 2 ? name[2] : (byte)0;
2277
                bData[27] = name.Length > 3 ? name[3] : (byte)0;
2278
                //Autotrigger
2279
                bData[28] = Convert.ToByte(dr[11]);
2280
 
2281
                bytes = FlightControllerMessage.CreateMessage('w', 2, bData);
2282
                serialStream.Write(bytes, 0, bytes.Length);
2283
 
2284
                return true;
2285
            }
2286
 
2287
            return false;
2288
        }
2287 - 2289
        #region OSD-Menue
2290
 
2291
        /// <summary>
2292
        /// one time query of the OSD Menue with pagenumber
2293
        /// </summary>
2294
        /// <param name="iMenue">Menue page</param>
2295
        void _OSDMenue(int iMenue)
2296
        {
2297
            if (serialPortCtrl.Port.IsOpen)
2298
            {
2299
                if (iMenue > iOSDMax)
2300
                    iMenue = 0;
2301
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2302
                byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue });
2303
                serialStream.Write(bytes, 0, bytes.Length);
2304
            }
2305
            else
2306
                Log(LogMsgType.Error, "NOT CONNECTED!");
2307
 
2308
        }
2309
        /// <summary>
2310
        /// call the OSDMenue and start autorefresh
2311
        ///  usually by sending a menuekey
2312
        /// a bit tricky - but by sending inverted value of 32 (32 = 0010 0000) you can start the OSD menue with autoupdate (abo) without switching the page with the keyvalues (0x1, 0x2)
2313
        /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle
2314
        /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33
2315
        /// </summary>
2316
        void _OSDMenueAutoRefresh()
2317
        {
2318
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)), OSDInterval });
2319
        }
2320
        void _OSDMenueAutoRefresh(byte key)
2321
        {
2322
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval });
2323
        }
2324
        /// <summary>
2325
        /// initialize the OSD menue combobox
2326
        /// combox is filled by numbers from 0 to max pagenumber
2327
        /// </summary>
2328
        void _initOSDCB()
2329
        {
2330
            _bCBInit = true;
2331
            if (iOSDMax == 0)
2332
            {
2333
                _OSDMenue(0);
2334
                Thread.Sleep(10);
2335
            }
2336
            Dispatcher.Invoke((Action)(() => cbOSD.Items.Clear()));
2337
            for (int i = 0; i <= iOSDMax; i++)
2338
            {
2339
                Dispatcher.Invoke((Action)(() => cbOSD.Items.Add(i)));
2340
            }
2341
            Dispatcher.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage));
2342
            _bCBInit = false;
2343
        }
2344
        private void btnOSDForward_Click(object sender, RoutedEventArgs e)
2345
        {
2346
            iOSDPage++;
2347
            if (iOSDPage > iOSDMax)
2348
                iOSDPage = 0;
2349
 
2350
            _OSDMenue(iOSDPage);
2351
        }
2352
        private void btnOSDBackward_Click(object sender, RoutedEventArgs e)
2353
        {
2354
            iOSDPage--;
2355
            if (iOSDPage < 0)
2356
                iOSDPage = iOSDMax;
2357
 
2358
            _OSDMenue(iOSDPage);
2359
        }
2360
        private void btnOSDLeave_Click(object sender, RoutedEventArgs e)
2361
        {
2362
            _OSDMenueAutoRefresh(8);
2363
        }
2364
        private void btnOSDEnter_Click(object sender, RoutedEventArgs e)
2365
        {
2366
            _OSDMenueAutoRefresh(4);
2367
        }
2368
        private void cbOSD_DropDownClosing(object sender, EventArgs e)
2369
        {
2370
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
2371
                _OSDMenue(cbOSD.SelectedIndex);
2372
        }
2373
        private void OSD(LogMsgType msgtype, string msg, bool bNew)
2374
        {
2375
            Dispatcher.Invoke(() =>
2376
            {
2377
                TextRange tr;
2378
                if (bNew)
2379
                {
2380
                    rtfOSD.SelectAll();
2381
                    rtfOSD.Selection.Text = string.Empty;
2382
                }
2383
                tr = new TextRange(rtfOSD.Document.ContentEnd, rtfOSD.Document.ContentEnd);
2384
                tr.Text = msg;
2385
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(LogMsgTypeColor[(int)msgtype]));
2386
                //     rtfOSD.AppendText("\r");
2387
                rtfOSD.ScrollToEnd();
2388
 
2389
            });
2390
        }
2391
 
2392
        #endregion OSD-Menue
2393
 
2394
        #endregion controller messages
2395
 
2295 - 2396
        void _mediaPlayer(string file)
2397
        {
2398
            if (File.Exists(file))
2399
            {
2400
                MediaPlayer.MediaPlayer mp = new MediaPlayer.MediaPlayer();
2401
                mp.Open(file);
2402
                mp.Play();
2403
            }
2404
        }
2310 - 2405
 
2406
        /// <summary>
2407
        /// Switch between fullscreen and normal window mode
2408
        /// save & restore scaling settings
2409
        /// </summary>
2410
        /// <param name="sender"></param>
2411
        /// <param name="e"></param>
2287 - 2412
        private void imageFullscreen_MouseDown(object sender, MouseButtonEventArgs e)
2413
        {
2414
            if (winState.isMaximized)
2415
            {
2416
                winState.Restore(this);
2417
                imageFullscreen.Source = new BitmapImage(new Uri("Images/Fullscreen.png", UriKind.Relative));
2418
                if(_bSaveWinStateFull)
2419
                    _saveScaleSliders(true);
2420
                if(_bSaveWinStateNormal)
2421
                    _setScaleSliders(false);
2422
            }
2423
            else
2424
            {
2425
                winState.Maximize(this);
2426
                imageFullscreen.Source = new BitmapImage(new Uri("Images/RestoreScreen.png", UriKind.Relative));
2427
                if(_bSaveWinStateNormal)
2428
                    _saveScaleSliders(false);
2429
                if(_bSaveWinStateFull)
2430
                    _setScaleSliders(true);
2431
            }
2432
        }
2433
 
2434
        /// <summary>
2435
        /// reset the scaling of all UI elements to default
2436
        /// </summary>
2437
        /// <param name="sender"></param>
2438
        /// <param name="e"></param>
2439
        private void buttonUIScaleReset_Click(object sender, RoutedEventArgs e)
2440
        {
2441
            UIScaleHorizonSlider.Value =
2442
                UIScaleLOGSlider.Value =
2443
                UIScaleMotorsSlider.Value =
2444
                UIScaleOSDSlider.Value =
2445
                UIScaleSlider.Value =
2446
                UIScaleTopSlider.Value = 1;
2447
        }
2448
        /// <summary>
2449
        /// adjust the top postion of UI elements below the top bar to fit the bottom position of the bar when scaling the top bar
2450
        /// </summary>
2451
        /// <param name="sender"></param>
2452
        /// <param name="e"></param>
2453
        private void UIScaleTopSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
2454
        {
2455
            GridSettings.Margin = new Thickness(GridSettings.Margin.Left, 36 * UIScaleTopSlider.Value, GridSettings.Margin.Right, GridSettings.Margin.Bottom);
2456
            if (GridMotors != null)
2457
                GridMotors.Margin = new Thickness(GridMotors.Margin.Left, 36 * UIScaleTopSlider.Value, GridMotors.Margin.Right, GridMotors.Margin.Bottom);
2458
            if (GridSideBar != null)
2459
                GridSideBar.Margin = new Thickness(GridSideBar.Margin.Left, 36 * UIScaleTopSlider.Value, GridSideBar.Margin.Right, GridSideBar.Margin.Bottom);
2460
            if (GridOSD != null)
2461
                GridOSD.Margin = new Thickness(GridOSD.Margin.Left, 36 * UIScaleTopSlider.Value, GridOSD.Margin.Right, GridOSD.Margin.Bottom);
2462
            if (GridData != null)
2463
                GridData.Margin = new Thickness(GridData.Margin.Left, 36 * UIScaleTopSlider.Value, GridData.Margin.Right, GridData.Margin.Bottom);
2464
            if (GridWP != null)
2465
                GridWP.Margin = new Thickness(GridWP.Margin.Left, 36 * UIScaleTopSlider.Value, GridWP.Margin.Right, GridWP.Margin.Bottom);
2466
        }
2310 - 2467
        /// <summary>
2468
        /// restore the saved scalings for the fullscreen/normal window mode
2469
        /// </summary>
2470
        /// <param name="bFull">the mode the window is set to</param>
2287 - 2471
        void _setScaleSliders(bool bFull)
2472
        {
2473
            if(bFull)
2474
            {
2475
                UIScaleSlider.Value = scaleFullAll;
2476
                UIScaleTopSlider.Value = scaleFullTopBar;
2477
                UIScaleMotorsSlider.Value = scaleFullMotors;
2478
                UIScaleOSDSlider.Value = scaleFullOSD;
2479
                UIScaleLOGSlider.Value = scaleFullLOG;
2480
                UIScaleHorizonSlider.Value = scaleFullHorizon;
2481
            }
2482
            else
2483
            {
2484
                UIScaleSlider.Value = scaleNormalAll;
2485
                UIScaleTopSlider.Value = scaleNormalTopBar;
2486
                UIScaleMotorsSlider.Value = scaleNormalMotors;
2487
                UIScaleOSDSlider.Value = scaleNormalOSD;
2488
                UIScaleLOGSlider.Value = scaleNormalLOG;
2489
                UIScaleHorizonSlider.Value = scaleNormalHorizon;
2490
            }
2491
        }
2492
 
2310 - 2493
        /// <summary>
2494
        /// save the scalings for the actual window mode
2495
        /// </summary>
2496
        /// <param name="bFull">the actual window mode</param>
2287 - 2497
        void _saveScaleSliders(bool bFull)
2498
        {
2499
 
2500
            if (bFull)
2501
            {
2502
                scaleFullAll = UIScaleSlider.Value;
2503
                scaleFullTopBar = UIScaleTopSlider.Value;
2504
                scaleFullMotors = UIScaleMotorsSlider.Value;
2505
                scaleFullOSD = UIScaleOSDSlider.Value;
2506
                scaleFullLOG = UIScaleLOGSlider.Value;
2507
                scaleFullHorizon = UIScaleHorizonSlider.Value;
2508
            }
2509
            else
2510
            {
2511
                scaleNormalAll = UIScaleSlider.Value;
2512
                scaleNormalTopBar = UIScaleTopSlider.Value;
2513
                scaleNormalMotors = UIScaleMotorsSlider.Value;
2514
                scaleNormalOSD = UIScaleOSDSlider.Value;
2515
                scaleNormalLOG = UIScaleLOGSlider.Value;
2516
                scaleNormalHorizon = UIScaleHorizonSlider.Value;
2517
            }
2518
        }
2519
 
2520
        /// <summary>
2521
        /// read settings from ini-file
2522
        /// </summary>
2523
        void _readIni()
2524
        {
2525
            if (!File.Exists(filePath + "\\MKLiveViewSettings.ini"))
2526
                _writeIni();
2527
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
2528
            ini.path = filePath + "\\MKLiveViewSettings.ini";
2312 - 2529
            try
2530
            {
2287 - 2531
 
2312 - 2532
                string sVal = ini.IniReadValue("timings", "AutorefreshDebugData");
2533
                if (sVal != "") _debugDataAutorefresh = Convert.ToBoolean(sVal);
2534
                sVal = ini.IniReadValue("timings", "AutorefreshNavCtrlData");
2535
                if (sVal != "") _navCtrlDataAutorefresh = Convert.ToBoolean(sVal);
2536
                sVal = ini.IniReadValue("timings", "AutorefreshBLCtrlData");
2537
                if (sVal != "") _blctrlDataAutorefresh = Convert.ToBoolean(sVal);
2538
                sVal = ini.IniReadValue("timings", "AutorefreshOSDData");
2539
                if (sVal != "") _OSDAutorefresh = Convert.ToBoolean(sVal);
2287 - 2540
 
2312 - 2541
                sVal = ini.IniReadValue("timings", "IntervalDebugData");
2542
                if (sVal != "") debugInterval = (byte)Convert.ToInt16(sVal);
2543
                sVal = ini.IniReadValue("timings", "IntervalNavCtrlData");
2544
                if (sVal != "") navctrlInterval = (byte)Convert.ToInt16(sVal);
2545
                sVal = ini.IniReadValue("timings", "IntervalBLCtrlData");
2546
                if (sVal != "") blctrlInterval = (byte)Convert.ToInt16(sVal);
2547
                sVal = ini.IniReadValue("timings", "IntervalOSDData");
2548
                if (sVal != "") OSDInterval = (byte)Convert.ToInt16(sVal);
2287 - 2549
 
2312 - 2550
                sVal = ini.IniReadValue("topBar", "voltage");
2551
                if (sVal != "") chkBoxTopBarShowVoltage.IsChecked = Convert.ToBoolean(sVal);
2552
                sVal = ini.IniReadValue("topBar", "capacity");
2553
                if (sVal != "") chkBoxTopBarShowCapacity.IsChecked = Convert.ToBoolean(sVal);
2554
                sVal = ini.IniReadValue("topBar", "current");
2555
                if (sVal != "") chkBoxTopBarShowCurrent.IsChecked = Convert.ToBoolean(sVal);
2556
                sVal = ini.IniReadValue("topBar", "flightTime");
2557
                if (sVal != "") chkBoxTopBarShowFlightTime.IsChecked = Convert.ToBoolean(sVal);
2558
                sVal = ini.IniReadValue("topBar", "distanceHP");
2559
                if (sVal != "") chkBoxTopBarShowDistanceHP.IsChecked = Convert.ToBoolean(sVal);
2560
                sVal = ini.IniReadValue("topBar", "height");
2561
                if (sVal != "") chkBoxTopBarShowHeight.IsChecked = Convert.ToBoolean(sVal);
2562
                sVal = ini.IniReadValue("topBar", "speed");
2563
                if (sVal != "") chkBoxTopBarShowSpeed.IsChecked = Convert.ToBoolean(sVal);
2564
                sVal = ini.IniReadValue("topBar", "magneticField");
2565
                if (sVal != "") chkBoxTopBarShowMF.IsChecked = Convert.ToBoolean(sVal);
2566
                sVal = ini.IniReadValue("topBar", "satellites");
2567
                if (sVal != "") chkBoxTopBarShowSatellites.IsChecked = Convert.ToBoolean(sVal);
2568
                sVal = ini.IniReadValue("topBar", "rc");
2569
                if (sVal != "") chkBoxTopBarShowRC.IsChecked = Convert.ToBoolean(sVal);
2287 - 2570
 
2312 - 2571
                sVal = ini.IniReadValue("style", "saveFullScreen");
2572
                if (sVal != "") chkBoxSaveFullScreenState.IsChecked = Convert.ToBoolean(sVal);
2573
                sVal = ini.IniReadValue("style", "saveNormalState");
2574
                if (sVal != "") chkBoxSaveNormalState.IsChecked = Convert.ToBoolean(sVal);
2287 - 2575
 
2312 - 2576
                sVal = ini.IniReadValue("style", "scaleNormalAll");
2577
                if (sVal != "") scaleNormalAll = Convert.ToDouble(sVal);
2578
                sVal = ini.IniReadValue("style", "scaleNormalTopBar");
2579
                if (sVal != "") scaleNormalTopBar = Convert.ToDouble(sVal);
2580
                sVal = ini.IniReadValue("style", "scaleNormalMotors");
2581
                if (sVal != "") scaleNormalMotors = Convert.ToDouble(sVal);
2582
                sVal = ini.IniReadValue("style", "scaleNormalOSD");
2583
                if (sVal != "") scaleNormalOSD = Convert.ToDouble(sVal);
2584
                sVal = ini.IniReadValue("style", "scaleNormalLOG");
2585
                if (sVal != "") scaleNormalLOG = Convert.ToDouble(sVal);
2586
                sVal = ini.IniReadValue("style", "scaleNormalHorizon");
2587
                if (sVal != "") scaleNormalHorizon = Convert.ToDouble(sVal);
2287 - 2588
 
2312 - 2589
                sVal = ini.IniReadValue("style", "scaleFullAll");
2590
                if (sVal != "") scaleFullAll = Convert.ToDouble(sVal);
2591
                sVal = ini.IniReadValue("style", "scaleFullTopBar");
2592
                if (sVal != "") scaleFullTopBar = Convert.ToDouble(sVal);
2593
                sVal = ini.IniReadValue("style", "scaleFullMotors");
2594
                if (sVal != "") scaleFullMotors = Convert.ToDouble(sVal);
2595
                sVal = ini.IniReadValue("style", "scaleFullOSD");
2596
                if (sVal != "") scaleFullOSD = Convert.ToDouble(sVal);
2597
                sVal = ini.IniReadValue("style", "scaleFullLOG");
2598
                if (sVal != "") scaleFullLOG = Convert.ToDouble(sVal);
2599
                sVal = ini.IniReadValue("style", "scaleFullHorizon");
2600
                if (sVal != "") scaleFullHorizon = Convert.ToDouble(sVal);
2287 - 2601
 
2312 - 2602
                sVal = ini.IniReadValue("general", "LiPoCells");
2603
                _LipoCells = Convert.ToInt16(sVal);
2604
                sVal = ini.IniReadValue("general", "Motors");
2605
                if (sVal != "") _iMotors = Convert.ToInt16(sVal);
2287 - 2606
 
2312 - 2607
                sVal = ini.IniReadValue("map", "followMe");
2608
                if (sVal != "") _bFollowCopter = Convert.ToBoolean(sVal);
2609
                sVal = ini.IniReadValue("map", "AutoSetHome");
2610
                if (sVal != "") _bAutoHome = Convert.ToBoolean(sVal);
2324 - 2611
                sVal = ini.IniReadValue("map", "GPXLog");
2612
                if (sVal != "") _bGPXLog = Convert.ToBoolean(sVal);
2287 - 2613
 
2312 - 2614
                sVal = ini.IniReadValue("threshold", "VoltageWarning");
2615
                if(sVal != "") _dThresholdVoltageWarn = Convert.ToDouble(sVal);
2616
                sVal = ini.IniReadValue("threshold", "VoltageCritical");
2617
                if(sVal != "") _dThresholdVoltageCrit = Convert.ToDouble(sVal);
2618
                sVal = ini.IniReadValue("threshold", "VoiceVoltageEnable");
2619
                if(sVal != "") _bVoiceVoltPlay = Convert.ToBoolean(sVal);
2620
                sVal = ini.IniReadValue("threshold", "VoiceSatFixEnable");
2621
                if(sVal != "") _bVoiceSatFixPlay = Convert.ToBoolean(sVal);
2622
                sVal = ini.IniReadValue("threshold", "VoiceMagFieldEnable");
2623
                if(sVal != "") _bVoiceMagneticFieldPlay = Convert.ToBoolean(sVal);
2624
                sVal = ini.IniReadValue("threshold", "DistanceWarning");
2625
                if(sVal != "") _dThresholdDistanceWarn = Convert.ToDouble(sVal);
2626
                sVal = ini.IniReadValue("threshold", "VoiceDistanceWarnEnable");
2627
                if(sVal != "") _bVoiceDistancePlay = Convert.ToBoolean(sVal);
2628
                sVal = ini.IniReadValue("threshold", "VoiceRCLevelWarnEnable");
2629
                if(sVal != "") _bVoiceRCLevelPlay = Convert.ToBoolean(sVal);
2630
                sVal = ini.IniReadValue("threshold", "MaxDistance");
2631
                if(sVal != "") _dThresholdDistanceMax = Convert.ToDouble(sVal);
2632
                sVal = ini.IniReadValue("threshold", "RCThreshold");
2633
                if(sVal != "") _iThresholdRC = Convert.ToInt32(sVal);
2634
                sVal = ini.IniReadValue("threshold", "MagFieldThreshold");
2635
                if(sVal != "") _iThresholdMagField = Convert.ToInt32(sVal);
2321 - 2636
 
2637
                sVal = ini.IniReadValue("waypoints", "wpcolor");
2638
                if(sVal != "") comboBoxWPColor.SelectedIndex = Convert.ToInt32(sVal);
2639
                sVal = ini.IniReadValue("waypoints", "poicolor");
2640
                if(sVal != "") comboBoxPOIColor.SelectedIndex = Convert.ToInt32(sVal);
2641
                sVal = ini.IniReadValue("waypoints", "fscolor");
2642
                if(sVal != "") comboBoxFSColor.SelectedIndex = Convert.ToInt32(sVal);
2643
                sVal = ini.IniReadValue("waypoints", "coptercolor");
2644
                if(sVal != "") comboBoxCopterColor.SelectedIndex = Convert.ToInt32(sVal);
2328 - 2645
                sVal = ini.IniReadValue("waypoints", "routecolor");
2646
                if(sVal != "") comboBoxRouteColor.SelectedIndex = Convert.ToInt32(sVal);
2647
                sVal = ini.IniReadValue("waypoints", "showWPRoute");
2648
                if(sVal != "") _bShowWPRoute = Convert.ToBoolean(sVal);
2312 - 2649
            }
2650
            catch (Exception e)
2651
            {
2287 - 2652
 
2312 - 2653
                MessageBox.Show("Error parsing ini-file!" + Environment.NewLine + e.Message,"Read ini-file" ,MessageBoxButton.OK,MessageBoxImage.Error);
2654
            }
2655
 
2287 - 2656
        }
2657
 
2658
        /// <summary>
2659
        /// save settings to ini-file
2660
        /// </summary>
2661
        void _writeIni()
2662
        {
2663
 
2664
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
2665
            ini.path = filePath + "\\MKLiveViewSettings.ini";
2666
 
2312 - 2667
            try
2668
            {
2287 - 2669
 
2312 - 2670
                ini.IniWriteValue("timings", "AutorefreshDebugData", _debugDataAutorefresh ? "true" : "false");
2671
                ini.IniWriteValue("timings", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true" : "false");
2672
                ini.IniWriteValue("timings", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true" : "false");
2673
                ini.IniWriteValue("timings", "AutorefreshOSDData", _OSDAutorefresh ? "true" : "false");
2287 - 2674
 
2312 - 2675
                ini.IniWriteValue("timings", "IntervalDebugData", debugInterval.ToString());
2676
                ini.IniWriteValue("timings", "IntervalNavCtrlData", navctrlInterval.ToString());
2677
                ini.IniWriteValue("timings", "IntervalBLCtrlData", blctrlInterval.ToString());
2678
                ini.IniWriteValue("timings", "IntervalOSDData", OSDInterval.ToString());
2287 - 2679
 
2312 - 2680
                ini.IniWriteValue("general", "LiPoCells", _LipoCells.ToString());
2681
                ini.IniWriteValue("general", "Motors", _iMotors.ToString());
2287 - 2682
 
2312 - 2683
                ini.IniWriteValue("map", "followMe", _bFollowCopter.ToString());
2684
                ini.IniWriteValue("map", "AutoSetHome", _bAutoHome.ToString());
2324 - 2685
                ini.IniWriteValue("map", "GPXLog", _bGPXLog.ToString());
2287 - 2686
 
2312 - 2687
                ini.IniWriteValue("topBar", "voltage", chkBoxTopBarShowVoltage.IsChecked.ToString());
2688
                ini.IniWriteValue("topBar", "capacity", chkBoxTopBarShowCapacity.IsChecked.ToString());
2689
                ini.IniWriteValue("topBar", "current", chkBoxTopBarShowCurrent.IsChecked.ToString());
2690
                ini.IniWriteValue("topBar", "flightTime", chkBoxTopBarShowFlightTime.IsChecked.ToString());
2691
                ini.IniWriteValue("topBar", "distanceHP", chkBoxTopBarShowDistanceHP.IsChecked.ToString());
2692
                ini.IniWriteValue("topBar", "height", chkBoxTopBarShowHeight.IsChecked.ToString());
2693
                ini.IniWriteValue("topBar", "speed", chkBoxTopBarShowSpeed.IsChecked.ToString());
2694
                ini.IniWriteValue("topBar", "magneticField", chkBoxTopBarShowMF.IsChecked.ToString());
2695
                ini.IniWriteValue("topBar", "satellites", chkBoxTopBarShowSatellites.IsChecked.ToString());
2696
                ini.IniWriteValue("topBar", "rc", chkBoxTopBarShowRC.IsChecked.ToString());
2287 - 2697
 
2312 - 2698
                ini.IniWriteValue("style", "saveFullScreen", chkBoxSaveFullScreenState.IsChecked.ToString());
2699
                ini.IniWriteValue("style", "saveNormalState", chkBoxSaveNormalState.IsChecked.ToString());
2287 - 2700
 
2312 - 2701
                ini.IniWriteValue("style", "scaleNormalAll", scaleNormalAll.ToString());
2702
                ini.IniWriteValue("style", "scaleNormalTopBar", scaleNormalTopBar.ToString());
2703
                ini.IniWriteValue("style", "scaleNormalMotors", scaleNormalMotors.ToString());
2704
                ini.IniWriteValue("style", "scaleNormalOSD", scaleNormalOSD.ToString());
2705
                ini.IniWriteValue("style", "scaleNormalLOG", scaleNormalLOG.ToString());
2706
                ini.IniWriteValue("style", "scaleNormalHorizon", scaleNormalHorizon.ToString());
2287 - 2707
 
2312 - 2708
                ini.IniWriteValue("style", "scaleFullAll", scaleFullAll.ToString());
2709
                ini.IniWriteValue("style", "scaleFullTopBar", scaleFullTopBar.ToString());
2710
                ini.IniWriteValue("style", "scaleFullMotors", scaleFullMotors.ToString());
2711
                ini.IniWriteValue("style", "scaleFullOSD", scaleFullOSD.ToString());
2712
                ini.IniWriteValue("style", "scaleFullLOG", scaleFullLOG.ToString());
2713
                ini.IniWriteValue("style", "scaleFullHorizon", scaleFullHorizon.ToString());
2287 - 2714
 
2312 - 2715
                ini.IniWriteValue("style", "horizon", chkBoxShowHorizon.IsChecked.ToString());
2716
 
2717
                ini.IniWriteValue("threshold", "VoltageWarning", _dThresholdVoltageWarn.ToString());
2718
                ini.IniWriteValue("threshold", "VoltageCritical", _dThresholdVoltageCrit.ToString());
2719
                ini.IniWriteValue("threshold", "VoiceVoltageEnable", _bVoiceVoltPlay.ToString());
2720
                ini.IniWriteValue("threshold", "VoiceSatFixEnable", _bVoiceSatFixPlay.ToString());
2721
                ini.IniWriteValue("threshold", "VoiceMagFieldEnable", _bVoiceMagneticFieldPlay.ToString());
2722
                ini.IniWriteValue("threshold", "VoiceDistanceWarnEnable", _bVoiceDistancePlay.ToString());
2723
                ini.IniWriteValue("threshold", "VoiceRCLevelWarnEnable", _bVoiceRCLevelPlay.ToString());
2724
                ini.IniWriteValue("threshold", "DistanceWarning", _dThresholdDistanceWarn.ToString());
2725
                ini.IniWriteValue("threshold", "MaxDistance", _dThresholdDistanceMax.ToString());
2726
                ini.IniWriteValue("threshold", "RCThreshold", _iThresholdRC.ToString());
2727
                ini.IniWriteValue("threshold", "MagFieldThreshold", _iThresholdMagField.ToString());
2321 - 2728
 
2729
                ini.IniWriteValue("waypoints", "wpcolor", comboBoxWPColor.SelectedIndex.ToString());
2730
                ini.IniWriteValue("waypoints", "poicolor", comboBoxPOIColor.SelectedIndex.ToString());
2731
                ini.IniWriteValue("waypoints", "fscolor", comboBoxFSColor.SelectedIndex.ToString());
2732
                ini.IniWriteValue("waypoints", "coptercolor", comboBoxCopterColor.SelectedIndex.ToString());
2328 - 2733
                ini.IniWriteValue("waypoints", "routecolor", comboBoxRouteColor.SelectedIndex.ToString());
2734
                ini.IniWriteValue("waypoints", "showWPRoute", _bShowWPRoute.ToString());
2312 - 2735
            }
2736
            catch (Exception e)
2737
            {
2738
 
2739
                MessageBox.Show("Error writing ini-file!" + Environment.NewLine + "Please make sure that the programm is in a location where it is allowed to write" + Environment.NewLine + e.Message, "Write ini-file", MessageBoxButton.OK, MessageBoxImage.Error);
2740
            }
2287 - 2741
        }
2312 - 2742
 
2315 - 2743
        #region WP
2312 - 2744
        void _readWPLFile()
2745
        {
2746
            Microsoft.Win32.OpenFileDialog fd = new Microsoft.Win32.OpenFileDialog();
2747
            fd.Filter = "Waypointlists | *.wpl";
2748
            fd.Multiselect = false;
2749
            if (fd.ShowDialog().Value)
2750
            {
2751
                string file = fd.SafeFileName;
2752
                IniFile ini = new IniFile(fd.SafeFileName);
2753
                ini.path = fd.FileName;
2754
 
2755
                try
2756
                {
2757
                    string sVal = ini.IniReadValue("General", "FileVersion");
2758
                    if (sVal == "")
2759
                        MessageBox.Show("The file has no version declared - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2760
                    else
2761
                    {
2762
                        if (Convert.ToInt16(sVal) < 3)
2763
                            MessageBox.Show("The file version is not supported - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2764
                        else
2765
                        {
2766
                            sVal = ini.IniReadValue("General", "NumberOfWaypoints");
2767
                            if (sVal == "")
2768
                                MessageBox.Show("The file has no number of waypoints declared - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2769
                            else
2770
                            {
2771
                                int wpnum = Convert.ToInt16(sVal);
2772
                                string wp;
2773
                                int i;
2313 - 2774
                                NumberFormatInfo nfi = new NumberFormatInfo();
2775
                                nfi.NumberDecimalSeparator = ".";
2312 - 2776
                                dtWaypoints.Rows.Clear();
2327 - 2777
                                //for(int k = 0; k < MainMap.Markers.Count;)
2778
                                //{
2779
                                //    GMapMarker p = MainMap.Markers[k];
2780
                                //    if (p.Shape.GetType() == typeof(CustomMarkerWP))
2781
                                //        MainMap.Markers.Remove(p);
2782
                                //    else
2783
                                //        k++;
2784
                                //}
2785
                                _clearMapMarkers(typeof(CustomMarkerWP));
2328 - 2786
                                wpList.Clear();
2787
                                if (mRouteWP != null)
2788
                                    MainMap.Markers.Remove(mRouteWP);
2789
                                Dispatcher.Invoke(() => lblWPRouteDistance.Content = "0 m");
2790
 
2312 - 2791
                                for (int k = 1; k <= wpnum; k++)
2792
                                {
2793
                                    DataRow dr = dtWaypoints.NewRow();
2794
                                    dr.ItemArray = new object[16];
2795
                                    object[] o = new object[16];
2796
                                    i = 0;
2797
                                    wp = "Point" + k.ToString();
2798
                                    o[i] = k;
2799
                                    i++;
2800
                                    sVal = ini.IniReadValue(wp, "Type");
2801
                                    if (sVal != "")
2315 - 2802
                                        o[i] = Convert.ToInt16(sVal) - 1;
2312 - 2803
                                    i++;
2804
                                    sVal = ini.IniReadValue(wp, "Prefix");
2805
                                    if (sVal != "")
2806
                                        o[i] = sVal == "0" ? "P" + k.ToString() : sVal + k.ToString();
2807
                                    i++;
2313 - 2808
                                    sVal = ini.IniReadValue(wp, "Latitude");
2312 - 2809
                                    if (sVal != "")
2313 - 2810
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2811
                                    i++;
2313 - 2812
                                    sVal = ini.IniReadValue(wp, "Longitude");
2312 - 2813
                                    if (sVal != "")
2313 - 2814
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2815
                                    i++;
2816
                                    sVal = ini.IniReadValue(wp, "Altitude");
2817
                                    if (sVal != "")
2313 - 2818
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2819
                                    i++;
2820
                                    sVal = ini.IniReadValue(wp, "Heading");
2821
                                    if (sVal != "")
2822
                                        o[i] = Convert.ToInt16(sVal);
2823
                                    i++;
2824
                                    sVal = ini.IniReadValue(wp, "Speed");
2825
                                    if (sVal != "")
2315 - 2826
                                        o[i] = Convert.ToInt16(sVal);
2312 - 2827
                                    i++;
2828
                                    sVal = ini.IniReadValue(wp, "ClimbRate");
2829
                                    if (sVal != "")
2830
                                        o[i] = Convert.ToInt16(sVal);
2831
                                    i++;
2832
                                    sVal = ini.IniReadValue(wp, "Radius");
2833
                                    if (sVal != "")
2834
                                        o[i] = Convert.ToInt16(sVal);
2835
                                    i++;
2836
                                    sVal = ini.IniReadValue(wp, "DelayTime");
2837
                                    if (sVal != "")
2838
                                        o[i] = Convert.ToInt16(sVal);
2839
                                    i++;
2840
                                    sVal = ini.IniReadValue(wp, "AutoTrigger");
2841
                                    if (sVal != "")
2842
                                        o[i] = Convert.ToInt16(sVal); ;
2843
                                    i++;
2844
                                    sVal = ini.IniReadValue(wp, "CAM-Nick");
2845
                                    if (sVal != "")
2313 - 2846
                                        o[i] = Convert.ToInt16(sVal);
2312 - 2847
                                    i++;
2848
                                    o[i] = 0;
2849
                                    i++;
2850
                                    sVal = ini.IniReadValue(wp, "WP_Event_Channel_Value");
2851
                                    if (sVal != "")
2852
                                        o[i] = Convert.ToInt16(sVal);
2853
                                    i++;
2854
                                    o[i] = "New";
2855
 
2856
                                    dr.ItemArray = o;
2857
                                    dtWaypoints.Rows.Add(dr);
2315 - 2858
                                    GMapMarker wpMarker = new GMapMarker(new PointLatLng((double)o[3], (double)o[4]));
2859
                                    wpMarker.Shape = new CustomMarkerWP(this, wpMarker, (string)dr[2], (int)o[1]);
2328 - 2860
                                    wpMarker.Offset = new System.Windows.Point(-11.5, -11.5);
2861
                                    wpMarker.ZIndex = 100;
2315 - 2862
                                    _setMarkerColor(wpMarker, (int)o[1]);
2863
                                    MainMap.Markers.Add(wpMarker);
2328 - 2864
                                    if((int)o[1] == 0)
2865
                                        wpList.Add(new PointLatLng((double)o[3], (double)o[4]));
2315 - 2866
 
2312 - 2867
                                    Dispatcher.Invoke(() => lblWPIndex.Content = k.ToString());
2868
                                    Dispatcher.Invoke(() => lblWPCount.Content = k.ToString());
2869
                                    Dispatcher.Invoke(() => dgvWP.Items.Refresh());
2870
                                    Thread.Sleep(10);
2871
                                }
2328 - 2872
                                Dispatcher.Invoke(() =>
2873
                                {
2874
                                    if (comboBoxRouteColor.SelectionBoxItem != null)
2875
                                    {
2876
                                        string s = comboBoxRouteColor.SelectionBoxItem.ToString();
2877
                                        mRouteWP = new GMapRoute(wpList, _getBrush(s));
2878
                                    }
2879
                                    else
2880
                                        mRouteWP = new GMapRoute(wpList, null);
2881
 
2882
                                    if (_bShowWPRoute)
2883
                                        MainMap.Markers.Add(mRouteWP);
2884
                                });
2885
 
2886
                                MapRoute mr = new MapRoute(wpList, "WPList");
2887
                                Dispatcher.Invoke(() => lblWPRouteDistance.Content = (mr.Distance * 1000).ToString("0 m"));
2888
 
2312 - 2889
                            }
2890
                        }
2891
                    }
2892
 
2893
                }
2894
                catch (Exception e)
2895
                {
2896
 
2897
                    MessageBox.Show("Error parsing wpl-file!" + Environment.NewLine + e.Message, "Read wpl-file", MessageBoxButton.OK, MessageBoxImage.Error);
2898
                }
2899
 
2900
            }
2901
        }
2328 - 2902
        Brush _getBrush(string color)
2903
        {
2904
            switch (color)
2905
            {
2906
                case "red":
2907
                    return Brushes.Red;
2908
                case "green":
2909
                    return Brushes.Lime;
2910
                case "blue":
2911
                    return Brushes.Aqua;
2912
                case "pink":
2913
                    return Brushes.Magenta;
2914
                case "yellow":
2915
                    return Brushes.Yellow;
2916
                default:
2917
                    return Brushes.Magenta;
2918
            }
2919
        }
2315 - 2920
        void _setMarkerColor(GMapMarker wpMarker,int iType)
2921
        {
2922
            Dispatcher.Invoke(() =>
2923
            {
2924
                switch (iType)
2925
                {
2926
                    case 0:
2927
                        if (comboBoxWPColor.SelectionBoxItem != null)
2928
                        {
2929
                            string s = comboBoxWPColor.SelectionBoxItem.ToString();
2930
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor(s);
2931
                        }
2932
                        else
2933
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor("red");
2934
                        break;
2935
                    case 1:
2936
                        if (comboBoxPOIColor.SelectionBoxItem != null)
2937
                        {
2938
                            string s = comboBoxPOIColor.SelectionBoxItem.ToString();
2939
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor(s);
2940
                        }
2941
                        else
2942
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor("red");
2943
                        break;
2944
                    case 2:
2945
                        if (comboBoxFSColor.SelectionBoxItem != null)
2946
                        {
2947
                            string s = comboBoxFSColor.SelectionBoxItem.ToString();
2948
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor(s);
2949
                        }
2950
                        else
2951
                            ((CustomMarkerWP)(wpMarker.Shape)).setColor("red");
2952
                        break;
2953
                    default:
2954
                        ((CustomMarkerWP)(wpMarker.Shape)).setColor("red");
2955
                        break;
2956
                }
2957
            });
2312 - 2958
 
2315 - 2959
        }
2960
        private void comboBoxWPColor_DropDownClosed(object sender, EventArgs e)
2312 - 2961
        {
2315 - 2962
            Dispatcher.Invoke(() =>
2963
            {
2964
                if (comboBoxWPColor.SelectionBoxItem != null)
2965
                {
2966
                    for (int k = 0; k < MainMap.Markers.Count;k++)
2967
                    {
2968
                        GMapMarker p = MainMap.Markers[k];
2969
                        if (p.Shape.GetType() == typeof(CustomMarkerWP))
2970
                        {
2971
                            if (((CustomMarkerWP)p.Shape).WPType == 0)
2972
                            {
2973
                                string s = comboBoxWPColor.SelectionBoxItem.ToString();
2974
                                ((CustomMarkerWP)(p.Shape)).setColor(s);
2975
                            }
2976
                        }
2977
                    }
2978
                }
2979
            });
2312 - 2980
        }
2315 - 2981
        private void comboBoxPOIColor_DropDownClosed(object sender, EventArgs e)
2982
        {
2983
            Dispatcher.Invoke(() =>
2984
            {
2985
                if (comboBoxPOIColor.SelectionBoxItem != null)
2986
                {
2987
                    for (int k = 0; k < MainMap.Markers.Count; k++)
2988
                    {
2989
                        GMapMarker p = MainMap.Markers[k];
2990
                        if (p.Shape.GetType() == typeof(CustomMarkerWP))
2991
                        {
2992
                            if (((CustomMarkerWP)p.Shape).WPType == 1)
2993
                            {
2994
                                string s = comboBoxPOIColor.SelectionBoxItem.ToString();
2995
                                ((CustomMarkerWP)(p.Shape)).setColor(s);
2996
                            }
2997
                        }
2998
                    }
2999
                }
3000
            });
2287 - 3001
 
2315 - 3002
        }
3003
        private void comboBoxFSColor_DropDownClosed(object sender, EventArgs e)
2287 - 3004
        {
2315 - 3005
            Dispatcher.Invoke(() =>
3006
            {
3007
                if (comboBoxFSColor.SelectionBoxItem != null)
3008
                {
3009
                    for (int k = 0; k < MainMap.Markers.Count; k++)
3010
                    {
3011
                        GMapMarker p = MainMap.Markers[k];
3012
                        if (p.Shape.GetType() == typeof(CustomMarkerWP))
3013
                        {
3014
                            if (((CustomMarkerWP)p.Shape).WPType == 2)
3015
                            {
3016
                                string s = comboBoxFSColor.SelectionBoxItem.ToString();
3017
                                ((CustomMarkerWP)(p.Shape)).setColor(s);
3018
                            }
3019
                        }
3020
                    }
3021
                }
3022
            });
2287 - 3023
        }
2318 - 3024
        private void comboBoxCopterColor_DropDownClosed(object sender, EventArgs e)
3025
        {
3026
            Dispatcher.Invoke(() => {
3027
                if (comboBoxCopterColor.SelectionBoxItem != null)
3028
                {
3029
                    string s = comboBoxCopterColor.SelectionBoxItem.ToString();
3030
                    ((CustomMarkerCopter)(copter.Shape)).setColor(s);
3031
                }
3032
                else
3033
                    ((CustomMarkerCopter)(copter.Shape)).setColor("red");
3034
            });
3035
        }
2328 - 3036
        private void comboBoxRouteColor_DropDownClosed(object sender, EventArgs e)
3037
        {
3038
            Dispatcher.Invoke(() =>
3039
            {
3040
                if (comboBoxRouteColor.SelectionBoxItem != null)
3041
                {
3042
                    string s = comboBoxRouteColor.SelectionBoxItem.ToString();
3043
                    MainMap.Markers.Remove(mRouteWP);
3044
                    mRouteWP = new GMapRoute(wpList, _getBrush(s));
3045
                    if (_bShowWPRoute)
3046
                        MainMap.Markers.Add(mRouteWP);
3047
                }
3048
            });
3049
        }
3050
        private void checkBoxShowWPRoute_Click(object sender, RoutedEventArgs e)
3051
        {
3052
            _bShowWPRoute = (bool)checkBoxShowWPRoute.IsChecked;
3053
            if (_bShowWPRoute)
3054
            {
3055
                if (mRouteWP != null)
3056
                    MainMap.Markers.Add(mRouteWP);
3057
            }
3058
            else
3059
            {
3060
                if (mRouteWP != null)
3061
                    MainMap.Markers.Remove(mRouteWP);
3062
            }
3063
        }
2315 - 3064
        private void btnLoadWPLFile_Click(object sender, RoutedEventArgs e)
2287 - 3065
        {
2315 - 3066
            _readWPLFile();
2287 - 3067
        }
2335 - 3068
        private void btnClearWPList_Click(object sender, RoutedEventArgs e)
3069
        {
3070
            _clearMapMarkers(typeof(CustomMarkerWP));
3071
            wpList.Clear();
3072
            if (mRouteWP != null)
3073
                MainMap.Markers.Remove(mRouteWP);
3074
            dtWaypoints.Rows.Clear();
3075
            Dispatcher.Invoke(() => dgvWP.Items.Refresh());
3076
            Dispatcher.Invoke(() => lblWPIndex.Content = 0);
3077
            Dispatcher.Invoke(() => lblWPCount.Content = 0);
3078
            Dispatcher.Invoke(() => lblWPRouteDistance.Content = "0 m");
3079
        }
2340 - 3080
        private void btnClearCopterList_Click(object sender, RoutedEventArgs e)
3081
        {
3082
            Thread t = new Thread(new ThreadStart(clearCopterWPList));
3083
            t.Start();
3084
        }
3085
        private void dgvWP_MouseUp(object sender, MouseButtonEventArgs e)
3086
        {
3087
            if (dgvWP.SelectedIndex > -1 && dgvWP.SelectedIndex < dtWaypoints.Rows.Count)
3088
            {
3089
                if(_wpCount > 0)
3090
                    _sendWayPoint(dtWaypoints.Rows[dgvWP.SelectedIndex],-1);
3091
                else
3092
                    _sendWayPoint(dtWaypoints.Rows[dgvWP.SelectedIndex],1);
3093
            }
3094
        }
3095
 
2315 - 3096
        #endregion WP
2335 - 3097
 
2324 - 3098
        #region GPX
3099
        private void checkBoxGPXLog_Click(object sender, RoutedEventArgs e)
3100
        {
3101
            _bGPXLog = (bool)checkBoxGPXLog.IsChecked;
3102
        }
3103
        void _gpxAdd(double lat,double lon, int elevation)
3104
        {
3105
            DataRow dr = dtGPX.NewRow();
3106
            dr[0] = dtGPX.Rows.Count - 1;
3107
            dr[1] = lat;
3108
            dr[2] = lon;
3109
            dr[3] = elevation;
3110
            dr[4] = DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture); //2011-01-14T01:59:01Z
3111
            dtGPX.Rows.Add(dr);
3112
        }
3113
        void _saveGPXLog()
3114
        {
3115
            if (!Directory.Exists("GPXLog"))
3116
                Directory.CreateDirectory("GPXLog");
3117
            string SaveFileName = "GPXLog\\" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".gpx";
3118
            XmlTextWriter myXmlTextWriter = null;
3119
            myXmlTextWriter = new XmlTextWriter(SaveFileName, null);
3120
            NumberFormatInfo nfi = new NumberFormatInfo();
3121
            nfi.NumberDecimalSeparator = ".";
2315 - 3122
 
2324 - 3123
            try
3124
            {
3125
                myXmlTextWriter.Formatting = Formatting.Indented;
3126
 
3127
                myXmlTextWriter.WriteStartDocument();
3128
 
3129
                myXmlTextWriter.WriteStartElement("gpx");
3130
 
3131
                myXmlTextWriter.WriteAttributeString("version", "1.0");
3132
                myXmlTextWriter.WriteAttributeString("creator", "MKLiveView v1.0");
3133
                myXmlTextWriter.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
3134
                myXmlTextWriter.WriteAttributeString("xmlns", "http://www.topografix.com/GPX/1/1");
3135
                myXmlTextWriter.WriteAttributeString("xsi:schemaLocation", "http://www.topografix.com/GPX/1/1/gpx.xsd");
3136
 
3137
                myXmlTextWriter.WriteElementString("time", DateTime.UtcNow.ToString("u", System.Globalization.CultureInfo.InvariantCulture));
3138
 
3139
                myXmlTextWriter.WriteStartElement("trk");
3140
                myXmlTextWriter.WriteStartElement("trkseg");
3141
                for(int i = 0; i< dtGPX.Rows.Count;i++)
3142
                {
3143
                    myXmlTextWriter.WriteStartElement("trkpt");
3144
                    myXmlTextWriter.WriteAttributeString("lat", dtGPX.Rows[i][1].ToString() != "" ? ((double)dtGPX.Rows[i][1]).ToString(nfi) : "");
3145
                    myXmlTextWriter.WriteAttributeString("lon", dtGPX.Rows[i][2].ToString() != "" ? ((double)dtGPX.Rows[i][2]).ToString(nfi) : "");
3146
                    myXmlTextWriter.WriteElementString("ele", dtGPX.Rows[i][3].ToString());
3147
                    myXmlTextWriter.WriteElementString("time", dtGPX.Rows[i][4].ToString());
3148
                    myXmlTextWriter.WriteEndElement();
3149
                }
3150
                myXmlTextWriter.WriteEndElement();
3151
                myXmlTextWriter.WriteEndElement();
3152
                myXmlTextWriter.WriteEndElement();
3153
            }
3154
            catch (Exception e)
3155
            {
3156
                Console.WriteLine("Exception: {0}", e.ToString());
3157
            }
3158
            finally
3159
            {
3160
                if (myXmlTextWriter != null)
3161
                {
3162
                    myXmlTextWriter.Close();
3163
                }
3164
            }
3165
        }
3166
 
3167
        private void btnLoadGPXLog_Click(object sender, RoutedEventArgs e)
3168
        {
3169
            _loadGPXLog();
3170
        }
3171
 
2327 - 3172
        private void btnClearRoute_Click(object sender, RoutedEventArgs e)
3173
        {
3174
            _clearMapMarkers(typeof(GMapRoute));
3175
        }
3176
 
2340 - 3177
 
2324 - 3178
        void _loadGPXLog()
3179
        {
3180
 
3181
            Microsoft.Win32.OpenFileDialog fd = new Microsoft.Win32.OpenFileDialog();
3182
            fd.Filter = "GPX-Logfile | *.gpx";
3183
            fd.Multiselect = false;
3184
            if (fd.ShowDialog().Value)
3185
            {
3186
                string file = fd.FileName;
3187
                try
3188
                {
3189
                    XDocument gpxDoc = XDocument.Load(file);
3190
                    XNamespace gpx = XNamespace.Get("http://www.topografix.com/GPX/1/1");
3191
 
3192
                    NumberFormatInfo nfi = new NumberFormatInfo();
3193
                    nfi.NumberDecimalSeparator = ".";
3194
 
3195
                    var tracks = from track in gpxDoc.Descendants(gpx + "trk")
3196
                                 select new
3197
                                 {
3198
                                     Name = track.Element(gpx + "name") != null ? track.Element(gpx + "name").Value : null,
3199
                                     Segs = (
3200
                                         from trackpoint in track.Descendants(gpx + "trkpt")
3201
                                         select new
3202
                                         {
3203
                                             Latitude = trackpoint.Attribute("lat").Value,
3204
                                             Longitude = trackpoint.Attribute("lon").Value,
3205
                                             Elevation = trackpoint.Element(gpx + "ele") != null ?
3206
                                             trackpoint.Element(gpx + "ele").Value : null,
3207
                                             Time = trackpoint.Element(gpx + "time") != null ?
3208
                                             trackpoint.Element(gpx + "time").Value : null
3209
                                         }
3210
                                     )
3211
                                 };
3212
 
3213
                    List<PointLatLng> wpl = new List<PointLatLng>();
3214
                    foreach(var trk in tracks)
3215
                    {
3216
                        foreach(var trkseg in trk.Segs)
3217
                        {
3218
                            if(trkseg.Latitude != "" && trkseg.Longitude !="")
3219
                            wpl.Add(new PointLatLng(Convert.ToDouble(trkseg.Latitude, nfi), Convert.ToDouble(trkseg.Longitude, nfi)));
3220
                        }
3221
 
3222
                    }
3223
                    if(wpl.Count() > 0)
3224
                    {
2327 - 3225
                        _clearMapMarkers(typeof(GMapRoute));
3226
                        //for (int k = 0; k < MainMap.Markers.Count;)
3227
                        //{
3228
                        //    GMapMarker p = MainMap.Markers[k];
3229
                        //    if (p.Shape.GetType() == typeof(GMapRoute))
3230
                        //        MainMap.Markers.Remove(p);
3231
                        //    else
3232
                        //        k++;
3233
                        //}
2324 - 3234
                        MapRoute mr = new MapRoute(wpl, "flying");
2328 - 3235
                        Dispatcher.Invoke(() =>
3236
                        {
3237
                            GMapRoute mRoute;
3238
                            if (comboBoxRouteColor.SelectionBoxItem != null)
3239
                            {
3240
                                string s = comboBoxRouteColor.SelectionBoxItem.ToString();
3241
                                mRoute = new GMapRoute(wpList, _getBrush(s));
3242
                            }
3243
                            else
3244
                                mRoute = new GMapRoute(wpList, null);
2324 - 3245
 
2328 - 3246
                            MainMap.Markers.Add(mRoute);
3247
                        });
2324 - 3248
                    }
3249
 
3250
                }
3251
                catch (Exception e)
3252
                {
3253
                    Console.WriteLine("Exception: {0}", e.ToString());
3254
                }
3255
            }
3256
        }
3257
        #endregion GPX
2315 - 3258
        #endregion functions
2287 - 3259
    }
2315 - 3260
     /// <summary>
3261
    /// formats the wp datatable values for display in datagrid - this is bound in the datagrid as a converter
3262
    /// </summary>
2313 - 3263
    public class waypointsConverter : IValueConverter
3264
    {
3265
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
3266
        {
3267
            if (value != null)
3268
            {
3269
                switch ((string)parameter)
3270
                {
3271
                    case "Latitude":
3272
                        return value.ToString() + " °";
3273
                    case "Longitude":
3274
                        return value.ToString() + " °";
3275
                    case "Radius":
3276
                        return value.ToString() + " m";
3277
                    case "Altitude":
3278
                        return value.ToString() + " m";
3279
                    case "ClimbRate":
3280
                        return value.ToString() == "255" ? "Auto" : (System.Convert.ToDouble(value) / 10).ToString("0.0 m/s");
3281
                    case "DelayTime":
3282
                        return value.ToString() + " s";
3283
                    case "Heading":
3284
                        return Waypoints.Heading(System.Convert.ToInt32(value));
3285
                    case "Speed":
3286
                        return Waypoints.WPSpeed(System.Convert.ToInt16(value));
3287
                    case "CamAngle":
3288
                        return Waypoints.CAMAngle(System.Convert.ToInt16(value));
3289
                    case "Type":
3290
                        return ((Waypoints.pointType)(System.Convert.ToInt16(value))).ToString();
3291
                    case "AutoTrigger":
3292
                        return value.ToString() == "0" ? "- - -" : value.ToString() + " m";
3293
                    case "Status":
3294
                        return Waypoints.WPSpeed(System.Convert.ToInt16(value));
3295
                }
3296
 
3297
                return value.ToString();
3298
            }
3299
            else return value;
3300
        }
3301
 
3302
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
3303
        {
3304
            throw new NotImplementedException();
3305
        }
3306
    }
3307
 
2287 - 3308
    public class IniFile
3309
    {
3310
        public string path;
3311
 
3312
        [DllImport("kernel32")]
3313
        private static extern long WritePrivateProfileString(string section,
3314
          string key, string val, string filePath);
3315
 
3316
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
3317
        static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,
3318
               uint nSize, string lpFileName);
3319
 
3320
        [DllImport("kernel32")]
3321
        private static extern int GetPrivateProfileString(string section,
3322
          string key, string def, StringBuilder retVal,
3323
          int size, string filePath);
3324
 
3325
        public IniFile(string INIPath)
3326
        {
3327
            path = INIPath;
3328
        }
3329
 
3330
        public void IniWriteValue(string Section, string Key, string Value)
3331
        {
3332
            WritePrivateProfileString(Section, Key, Value, this.path);
3333
        }
3334
 
3335
        public string IniReadValue(string Section, string Key)
3336
        {
3337
            StringBuilder temp = new StringBuilder(255);
3338
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
3339
            return temp.ToString();
3340
        }
3341
        //Ini_sections auslesen in String-Array
3342
        public string[] IniSectionNames()
3343
        {
3344
 
3345
            //  uint MAX_BUFFER = 32767;
3346
            uint MAX_BUFFER = 8388608;
3347
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
3348
            uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
3349
            if (bytesReturned == 0)
3350
            {
3351
                Marshal.FreeCoTaskMem(pReturnedString);
3352
                return null;
3353
            }
3354
            string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
3355
            Marshal.FreeCoTaskMem(pReturnedString);
3356
            //use of Substring below removes terminating null for split
3357
            return local.Substring(0, local.Length - 1).Split('\0');
3358
 
3359
 
3360
        }
3361
    }
3362
 
3363
    /// <summary>
3364
    /// Selected Win AI Function Calls
3365
    /// </summary>
3366
    public class WinApi
3367
    {
3368
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
3369
        public static extern int GetSystemMetrics(int which);
3370
        [DllImport("user32.dll")]
3371
        public static extern void
3372
                SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
3373
                             int X, int Y, int width, int height, uint flags);
3374
 
3375
        private const int SM_CXSCREEN = 0;
3376
        private const int SM_CYSCREEN = 1;
3377
        private static IntPtr HWND_TOP = IntPtr.Zero;
3378
        private const int SWP_SHOWWINDOW = 64; // 0x0040
3379
 
3380
        public static int ScreenX
3381
        {
3382
            get { return GetSystemMetrics(SM_CXSCREEN); }
3383
        }
3384
 
3385
        public static int ScreenY
3386
        {
3387
            get { return GetSystemMetrics(SM_CYSCREEN); }
3388
        }
3389
 
3390
        public static void SetWinFullScreen(IntPtr hwnd)
3391
        {
3392
            SetWindowPos(hwnd, HWND_TOP, -8, -7, ScreenX+15, ScreenY+14, SWP_SHOWWINDOW);
3393
        }
3394
    }
3395
    /// <summary>
3396
    /// Class used to preserve / restore state of the window
3397
    /// </summary>
3398
    public class WinState
3399
    {
3400
        private WindowState winState;
3401
        private WindowStyle brdStyle;
3402
        private bool topMost;
3403
        private Rect restore;
3404
        private bool IsMaximized = false;
3405
 
3406
        public bool isMaximized
3407
        {
3408
            get { return IsMaximized; }
3409
        }
3410
        public void Maximize(Window targetForm)
3411
        {
3412
            if (!IsMaximized)
3413
            {
3414
                IsMaximized = true;
3415
                Save(targetForm);
3416
                targetForm.WindowState = WindowState.Maximized;
3417
                targetForm.WindowStyle = WindowStyle.None;
3418
                targetForm.Topmost = true;
3419
                WinApi.SetWinFullScreen(new WindowInteropHelper(targetForm).Handle);
3420
            }
3421
        }
3422
 
3423
        public void Save(Window targetForm)
3424
        {
3425
            winState = targetForm.WindowState;
3426
            brdStyle = targetForm.WindowStyle;
3427
            topMost = targetForm.Topmost;
3428
            restore = targetForm.RestoreBounds;
3429
        }
3430
        public void Restore(Window targetForm)
3431
        {
3432
            targetForm.WindowState = winState;
3433
            targetForm.WindowStyle = brdStyle;
3434
            targetForm.Topmost = topMost;
3435
 
3436
            targetForm.Left = restore.Left;
3437
            targetForm.Top = restore.Top;
3438
            targetForm.Height = restore.Height;
3439
            targetForm.Width = restore.Width;
3440
            IsMaximized = false;
3441
        }
3442
    }
3443
 
3444
}