Subversion Repositories Projects

Rev

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