Subversion Repositories Projects

Rev

Rev 2312 | 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>
263
        DataTable dtMotors1 = new DataTable();
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
        {
353
            dtMotors1.Columns.Add("#");
2313 - 354
            if (Thread.CurrentThread.CurrentUICulture.Name == "de-DE")
355
                dtMotors1.Columns.Add("Strom");
356
            else
2287 - 357
                dtMotors1.Columns.Add("Current");
358
            dtMotors1.Columns.Add("Temp");
359
            dgvMotors1.DataContext = dtMotors1.DefaultView;
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));
371
            dtWaypoints.Columns.Add("Type",typeof(string));
372
            dtWaypoints.Columns.Add("Name",typeof(string));
373
            dtWaypoints.Columns.Add("Latitude",typeof(string));
374
            dtWaypoints.Columns.Add("Longitude",typeof(string));
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>
2313 - 394
        /// initialize the datatable dtMotors1 for motor values
395
        /// DataGridView dgvMotors1 is bound to dtMotors1
2287 - 396
        /// </summary>
397
        void _initDTMotors()
398
        {
399
            for (int i = 0; i < 12; i++)
400
            {
401
                if (dtMotors1.Rows.Count < 12)
402
                    dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA");
403
                else
404
                {
405
                    dtMotors1.Rows[i].SetField(1, "NA");
406
                    dtMotors1.Rows[i].SetField(2, "NA");
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
                        {
1511
                            dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
1512
                            dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C"));
1513
                        }
1514
                        else
1515
                        {
1516
                            dtMotors1.Rows[data[i]].SetField(1, "NA");
1517
                            dtMotors1.Rows[data[i]].SetField(2, "NA");
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)
1772
                        dtWaypoints.Rows.Clear();
1773
                    DataRow dr = dtWaypoints.NewRow();
1774
                    dr = Waypoints.toDataRow(data, dr);
2313 - 1775
                    dtWaypoints.Rows.Add(dr);
1776
                    Dispatcher.Invoke(() => dgvWP.Items.Refresh());
1777
                    Dispatcher.Invoke(() => _iWPIndex = data[1]);
2287 - 1778
                    if (data[1] == data[0])
1779
                    {
1780
                        _bGetWP = false;
1781
                        Dispatcher.Invoke(() => dgvWP.Items.Refresh());
1782
                    }
1783
 
1784
                }
1785
            }
1786
            else
1787
            {
1788
                Dispatcher.Invoke(() => lblWPIndex.Content = 0);
1789
                Dispatcher.Invoke(() => lblWPCount.Content = 0);
1790
            }
1791
        }
1792
        /// <summary>
1793
        /// OSD Menue 'L'
1794
        /// single page called by pagenumber
1795
        /// no autoupdate
1796
        /// </summary>
1797
        /// <param name="data">the received byte array to process</param>
1798
        void _processOSDSingle(byte[] data)
1799
        {
1800
            if (data.Length == 84)
1801
            {
1802
                string sMessage = "";
1803
                iOSDPage = data[0];
1804
                iOSDMax = data[1];
1805
                if (cbOSD.Items.Count != iOSDMax) _initOSDCB();
1806
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4));
1807
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)+ "\r", true);
1808
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)+ "\r", false);
1809
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)+ "\r", false);
1810
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20), false);
1811
                Dispatcher.Invoke(() => { cbOSD.SelectedValue = iOSDPage; });
1812
              //  lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
1813
 
1814
            }
1815
            //else
1816
            //    OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)");
1817
 
1818
        }
1819
        /// <summary>
1820
        /// OSD Menue 'H'
1821
        /// called by keys (0x01,0x02,0x03,0x04)
1822
        /// autoupdate
1823
        /// </summary>
1824
        /// <param name="data">the received byte array to process</param>
1825
        void _processOSDAuto(byte[] data)
1826
        {
1827
            if (data.Length == 81)
1828
            {
1829
                string sMessage = "";
1830
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
1831
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)+ "\r", true);
1832
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)+ "\r", false);
1833
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)+ "\r", false);
1834
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20), false);
1835
 
1836
            }
1837
            //else
1838
            //    OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
1839
        }
1840
 
1841
        #endregion processing received data
1842
 
1843
        #region controller messages
1844
        /// <summary> send message to controller to request data
1845
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1846
        /// </summary>
1847
        /// <param name="CMDID"> the command ID </param>
1848
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1849
        private void _sendControllerMessage(char CMDID, byte address)
1850
        {
1851
            if (serialPortCtrl.Port.IsOpen)
1852
            {
1853
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1854
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address);
1855
                serialStream.Write(bytes, 0, bytes.Length);
1856
 
1857
            }
1858
            else
1859
                Log(LogMsgType.Error, "NOT CONNECTED!");
1860
        }
1861
        /// <summary> send message to controller to request data
1862
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1863
        /// </summary>
1864
        /// <param name="CMDID"> the command ID </param>
1865
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1866
        /// <param name="data"> additional data for the request</param>
1867
        private void _sendControllerMessage(char CMDID, byte address, byte[] data)
1868
        {
1869
            if (serialPortCtrl.Port.IsOpen)
1870
            {
1871
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1872
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address, data);
1873
                serialStream.Write(bytes, 0, bytes.Length);
1874
 
1875
            }
1876
            else
1877
                Log(LogMsgType.Error, "NOT CONNECTED!");
1878
        }
1879
 
1880
        /// <summary>
1881
        /// start/stop continous polling of controller values
1882
        /// </summary>
1883
        /// <param name="b">start/stop switch</param>
1884
        void _readCont(bool b)
1885
        {
1886
            bReadContinously = b;
1887
            if (bReadContinously)
1888
            {
1889
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
1890
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
1891
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
1892
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10); }
1893
                // Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.LightGreen));
1894
                Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_G.png", UriKind.Relative)));
1895
            }
1896
            else
1897
            {
1898
                // Dispatcher.Invoke((Action)(() => rctConnection.Fill = Brushes.LightGray));
1899
                Dispatcher.Invoke(() => imageConn.Source = new BitmapImage(new Uri("Images/Data_W.png", UriKind.Relative)));
1900
                _bConnErr = false;
1901
            }
1902
            _iLifeCounter = 0;
1903
        }
1904
 
1905
        private void _getVersion()
1906
        {
1907
            _sendControllerMessage('v', 0);
1908
        }
1909
        /// <summary>
1910
        /// get FC version struct via NC
1911
        /// by sending '1' as data (not documented in wiki...)
1912
        /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version)
1913
        /// </summary>
1914
        /// <param name="ctrl">controller number 1=FC</param> 
1915
        private void _getVersion(byte ctrl)
1916
        {
1917
            _sendControllerMessage('v', 0, new byte[1] { ctrl });
1918
        }
1919
        /// <summary>
1920
        /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00
1921
        /// </summary>
1922
        private void _switchToNC()
1923
        {
1924
            if (serialPortCtrl.Port.IsOpen)
1925
            {
1926
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1927
                byte[] bytes = new byte[5] { 0x1B, 0x1B, 0x55, 0xAA, 0x00 };
1928
                serialStream.Write(bytes, 0, bytes.Length);
1929
 
1930
                Thread.Sleep(100);
1931
                _getVersion();
1932
                Thread.Sleep(100);
1933
               // _OSDMenue(0);
1934
            }
1935
            else
1936
                Log(LogMsgType.Error, "NOT CONNECTED!");
1937
        }
1938
        /// <summary>
1939
        /// switch to FC
1940
        /// </summary>
1941
        private void _switchToFC()
1942
        {
1943
            _sendControllerMessage('u', 2, new byte[1] { (byte)0 });
1944
            Thread.Sleep(100);
1945
            _getVersion();
1946
            Thread.Sleep(100);
1947
          //  _OSDMenue(0);
1948
        }
1949
        /// <summary>
1950
        /// send RESET signal to FC
1951
        /// </summary>
1952
        private void _resetCtrl()
1953
        {
1954
            _sendControllerMessage('R', 1);
1955
        }
1956
        /// <summary>
1957
        /// poll the debug data (4sec subscription)
1958
        /// </summary>
1959
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1960
        private void _readDebugData(bool auto)
1961
        {
1962
            byte interval = auto ? debugInterval : (byte)0;
1963
            _sendControllerMessage('d', 0, new byte[1] { debugInterval });
1964
        }
1965
        /// <summary>
1966
        /// poll the BL-CTRL status via NC (4sec subscription)
1967
        /// </summary>
1968
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1969
        private void _readBLCtrl(bool auto)
1970
        {
1971
            byte interval = auto ? blctrlInterval : (byte)0;
1972
            _sendControllerMessage('k', 0, new byte[1] { interval });
1973
        }
1974
        /// <summary>
1975
        /// poll the NC data struct (4sec subscription)
1976
        /// </summary>
1977
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1978
        private void _readNavData(bool auto)
1979
        {
1980
            byte interval = auto ? navctrlInterval : (byte)0;
1981
            _sendControllerMessage('o', 2, new byte[1] { interval });
1982
        }
1983
        /// <summary>
1984
        /// get the errortext for pending NC error
1985
        /// </summary>
1986
        private void _readNCError()
1987
        {
1988
            _sendControllerMessage('e', 2);
1989
        }
1990
        /// <summary>
1991
        /// request the Waypoint at index
1992
        /// </summary>
1993
        /// <param name="index"></param>
1994
        void _getpWP(int index)
1995
        {
1996
            if (serialPortCtrl.Port.IsOpen)
1997
            {
1998
                Stream serialStream = serialPortCtrl.Port.BaseStream;
1999
                byte[] bytes = FlightControllerMessage.CreateMessage('x', 2, new byte[1] { (byte)index });
2000
                serialStream.Write(bytes, 0, bytes.Length);
2001
            }
2002
            else
2003
                Log(LogMsgType.Error, "NOT CONNECTED!");
2004
 
2005
        }
2006
        void _getWP()
2007
        {
2008
            //if (_iWP > 0)
2009
            //{
2010
            //    _getWPList();
2011
            //}
2012
            //else
2013
            //{
2014
            //    if (_iWP == -1)
2015
            //    {
2016
            _bGetWPCount = true;
2017
            _getpWP(1);
2018
            while (_bGetWPCount)
2019
                Thread.Sleep(100);
2313 - 2020
            if (_iWPCount > 0)
2287 - 2021
                _getWPList();
2022
            //    }
2023
            //}
2024
        }
2025
        void _getWPList()
2026
        {
2027
            _bGetWP = true;
2313 - 2028
            int iTimeout=0;
2029
            for (int j = 0; j < _iWPCount; j++)
2287 - 2030
            {
2031
                _getpWP(j + 1);
2313 - 2032
                iTimeout = 0;
2033
                while (_iWPIndex != j + 1 & iTimeout < _iWPTimeout * 5)
2034
                {
2035
                    Thread.Sleep(1);
2036
                    iTimeout++;
2037
                }
2287 - 2038
            }
2039
        }
2313 - 2040
        void _sendWPList()
2041
        {
2042
            if (serialPortCtrl.Port.IsOpen)
2043
            {
2044
                byte[] bData = new byte[30];
2045
                for (int i = 0; i < 30; i++)
2046
                    bData[i] = 0;
2047
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2048
                byte[] bytes = FlightControllerMessage.CreateMessage('w', 2, bData); //delete all WP on Copter by sending 'invalid'(==0) at index 0
2049
                serialStream.Write(bytes, 0, bytes.Length);
2287 - 2050
 
2313 - 2051
                _iWPCount = -1;
2052
                while (_iWPCount == -1)
2053
                    Thread.Sleep(10);
2054
                Dispatcher.Invoke(() => lblWPCount.Content = _iWPCount.ToString());
2287 - 2055
 
2313 - 2056
                int iVal;
2057
                double dVal;
2058
                NumberFormatInfo nfi = new NumberFormatInfo();
2059
                nfi.NumberDecimalSeparator = ",";
2060
                for (int i = 0; i < dtWaypoints.Rows.Count; i++)
2061
                {
2062
                    //longitude
2063
                    dVal = Convert.ToDouble(dtWaypoints.Rows[i][4], nfi);
2064
                    iVal = (int)(dVal * Math.Pow(10, 7));
2065
                    bData[0] = (byte)(iVal & 0xff);
2066
                    bData[1] = (byte)((iVal >> 8) & 0xff);
2067
                    bData[2] = (byte)((iVal >> 16) & 0xff);
2068
                    bData[3] = (byte)(iVal >> 24);
2069
                    //latitude
2070
                    dVal = Convert.ToDouble(dtWaypoints.Rows[i][3], nfi);
2071
                    iVal = (int)(dVal * Math.Pow(10, 7));
2072
                    bData[4] = (byte)(iVal & 0xff);
2073
                    bData[5] = (byte)((iVal >> 8) & 0xff);
2074
                    bData[6] = (byte)((iVal >> 16) & 0xff);
2075
                    bData[7] = (byte)(iVal >> 24);
2076
                    //altitude
2077
                    dVal = Convert.ToDouble(dtWaypoints.Rows[i][5], nfi);
2078
                    iVal = (int)(dVal * 10);
2079
                    bData[8] = (byte)(iVal & 0xff);
2080
                    bData[9] = (byte)((iVal >> 8) & 0xff);
2081
                    bData[10] = (byte)((iVal >> 16) & 0xff);
2082
                    bData[11] = (byte)(iVal >> 24);
2083
                    //Status 'NEWDATA'
2084
                    bData[12] = 1;
2085
                    //heading
2086
                    iVal = Convert.ToInt16(dtWaypoints.Rows[i][6]);
2087
                    bData[13] = (byte)(iVal & 0xff);
2088
                    bData[14] = (byte)((iVal >> 8) & 0xff);
2089
                    //ToleranceRadius
2090
                    bData[15] = Convert.ToByte(dtWaypoints.Rows[i][9]);
2091
                    //HoldTime
2092
                    bData[16] = Convert.ToByte(dtWaypoints.Rows[i][10]);
2093
                    //Event_Flag
2094
                    bData[17] = Convert.ToByte(dtWaypoints.Rows[i][13]);
2095
                    //Index
2096
                    bData[18] = Convert.ToByte((int)dtWaypoints.Rows[i][0]);
2097
                    //Type
2098
                    bData[19] = Convert.ToByte(dtWaypoints.Rows[i][1]);
2099
                    //WP_EventChannelValue
2100
                    bData[20] = Convert.ToByte(dtWaypoints.Rows[i][14]);
2101
                    //AltitudeRate
2102
                    bData[21] = Convert.ToByte(dtWaypoints.Rows[i][8]);
2103
                    //Speed
2104
                    bData[22] = Convert.ToByte(dtWaypoints.Rows[i][7]);
2105
                    //CamAngle
2106
                    bData[23] = (byte)Convert.ToInt16(dtWaypoints.Rows[i][12]);
2107
                    //Name
2108
                    byte[] name = ASCIIEncoding.ASCII.GetBytes((string)dtWaypoints.Rows[i][2]);
2109
                    bData[24] = name.Length > 0 ? name[0] : (byte)0;
2110
                    bData[25] = name.Length > 1 ? name[1] : (byte)0;
2111
                    bData[26] = name.Length > 2 ? name[2] : (byte)0;
2112
                    bData[27] = name.Length > 3 ? name[3] : (byte)0;
2113
                    //Autotrigger
2114
                    bData[28] = Convert.ToByte(dtWaypoints.Rows[i][11]);
2115
 
2116
                    bytes = FlightControllerMessage.CreateMessage('w', 2, bData);
2117
                    serialStream.Write(bytes, 0, bytes.Length);
2118
 
2119
                    _iWPCount = -1;
2120
                    while (_iWPCount == -1)
2121
                        Thread.Sleep(10);
2122
                    Dispatcher.Invoke(() => lblWPCount.Content = _iWPCount.ToString());
2123
                }
2124
            }
2125
            else
2126
                Log(LogMsgType.Error, "NOT CONNECTED!");
2127
 
2128
        }
2129
 
2287 - 2130
        #region OSD-Menue
2131
 
2132
        /// <summary>
2133
        /// one time query of the OSD Menue with pagenumber
2134
        /// </summary>
2135
        /// <param name="iMenue">Menue page</param>
2136
        void _OSDMenue(int iMenue)
2137
        {
2138
            if (serialPortCtrl.Port.IsOpen)
2139
            {
2140
                if (iMenue > iOSDMax)
2141
                    iMenue = 0;
2142
                Stream serialStream = serialPortCtrl.Port.BaseStream;
2143
                byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue });
2144
                serialStream.Write(bytes, 0, bytes.Length);
2145
            }
2146
            else
2147
                Log(LogMsgType.Error, "NOT CONNECTED!");
2148
 
2149
        }
2150
        /// <summary>
2151
        /// call the OSDMenue and start autorefresh
2152
        ///  usually by sending a menuekey
2153
        /// 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)
2154
        /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle
2155
        /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33
2156
        /// </summary>
2157
        void _OSDMenueAutoRefresh()
2158
        {
2159
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)), OSDInterval });
2160
        }
2161
        void _OSDMenueAutoRefresh(byte key)
2162
        {
2163
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval });
2164
        }
2165
        /// <summary>
2166
        /// initialize the OSD menue combobox
2167
        /// combox is filled by numbers from 0 to max pagenumber
2168
        /// </summary>
2169
        void _initOSDCB()
2170
        {
2171
            _bCBInit = true;
2172
            if (iOSDMax == 0)
2173
            {
2174
                _OSDMenue(0);
2175
                Thread.Sleep(10);
2176
            }
2177
            Dispatcher.Invoke((Action)(() => cbOSD.Items.Clear()));
2178
            for (int i = 0; i <= iOSDMax; i++)
2179
            {
2180
                Dispatcher.Invoke((Action)(() => cbOSD.Items.Add(i)));
2181
            }
2182
            Dispatcher.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage));
2183
            _bCBInit = false;
2184
        }
2185
        private void btnOSDForward_Click(object sender, RoutedEventArgs e)
2186
        {
2187
            iOSDPage++;
2188
            if (iOSDPage > iOSDMax)
2189
                iOSDPage = 0;
2190
 
2191
            _OSDMenue(iOSDPage);
2192
        }
2193
        private void btnOSDBackward_Click(object sender, RoutedEventArgs e)
2194
        {
2195
            iOSDPage--;
2196
            if (iOSDPage < 0)
2197
                iOSDPage = iOSDMax;
2198
 
2199
            _OSDMenue(iOSDPage);
2200
        }
2201
        private void btnOSDLeave_Click(object sender, RoutedEventArgs e)
2202
        {
2203
            _OSDMenueAutoRefresh(8);
2204
        }
2205
        private void btnOSDEnter_Click(object sender, RoutedEventArgs e)
2206
        {
2207
            _OSDMenueAutoRefresh(4);
2208
        }
2209
        private void cbOSD_DropDownClosing(object sender, EventArgs e)
2210
        {
2211
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
2212
                _OSDMenue(cbOSD.SelectedIndex);
2213
        }
2214
        private void OSD(LogMsgType msgtype, string msg, bool bNew)
2215
        {
2216
            Dispatcher.Invoke(() =>
2217
            {
2218
                TextRange tr;
2219
                if (bNew)
2220
                {
2221
                    rtfOSD.SelectAll();
2222
                    rtfOSD.Selection.Text = string.Empty;
2223
                }
2224
                tr = new TextRange(rtfOSD.Document.ContentEnd, rtfOSD.Document.ContentEnd);
2225
                tr.Text = msg;
2226
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(LogMsgTypeColor[(int)msgtype]));
2227
                //     rtfOSD.AppendText("\r");
2228
                rtfOSD.ScrollToEnd();
2229
 
2230
            });
2231
        }
2232
 
2233
        #endregion OSD-Menue
2234
 
2235
        #endregion controller messages
2236
 
2295 - 2237
        void _mediaPlayer(string file)
2238
        {
2239
            if (File.Exists(file))
2240
            {
2241
                MediaPlayer.MediaPlayer mp = new MediaPlayer.MediaPlayer();
2242
                mp.Open(file);
2243
                mp.Play();
2244
            }
2245
        }
2310 - 2246
 
2247
        /// <summary>
2248
        /// Switch between fullscreen and normal window mode
2249
        /// save & restore scaling settings
2250
        /// </summary>
2251
        /// <param name="sender"></param>
2252
        /// <param name="e"></param>
2287 - 2253
        private void imageFullscreen_MouseDown(object sender, MouseButtonEventArgs e)
2254
        {
2255
            if (winState.isMaximized)
2256
            {
2257
                winState.Restore(this);
2258
                imageFullscreen.Source = new BitmapImage(new Uri("Images/Fullscreen.png", UriKind.Relative));
2259
                if(_bSaveWinStateFull)
2260
                    _saveScaleSliders(true);
2261
                if(_bSaveWinStateNormal)
2262
                    _setScaleSliders(false);
2263
            }
2264
            else
2265
            {
2266
                winState.Maximize(this);
2267
                imageFullscreen.Source = new BitmapImage(new Uri("Images/RestoreScreen.png", UriKind.Relative));
2268
                if(_bSaveWinStateNormal)
2269
                    _saveScaleSliders(false);
2270
                if(_bSaveWinStateFull)
2271
                    _setScaleSliders(true);
2272
            }
2273
        }
2274
 
2275
        /// <summary>
2276
        /// reset the scaling of all UI elements to default
2277
        /// </summary>
2278
        /// <param name="sender"></param>
2279
        /// <param name="e"></param>
2280
        private void buttonUIScaleReset_Click(object sender, RoutedEventArgs e)
2281
        {
2282
            UIScaleHorizonSlider.Value =
2283
                UIScaleLOGSlider.Value =
2284
                UIScaleMotorsSlider.Value =
2285
                UIScaleOSDSlider.Value =
2286
                UIScaleSlider.Value =
2287
                UIScaleTopSlider.Value = 1;
2288
        }
2289
        /// <summary>
2290
        /// adjust the top postion of UI elements below the top bar to fit the bottom position of the bar when scaling the top bar
2291
        /// </summary>
2292
        /// <param name="sender"></param>
2293
        /// <param name="e"></param>
2294
        private void UIScaleTopSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
2295
        {
2296
            GridSettings.Margin = new Thickness(GridSettings.Margin.Left, 36 * UIScaleTopSlider.Value, GridSettings.Margin.Right, GridSettings.Margin.Bottom);
2297
            if (GridMotors != null)
2298
                GridMotors.Margin = new Thickness(GridMotors.Margin.Left, 36 * UIScaleTopSlider.Value, GridMotors.Margin.Right, GridMotors.Margin.Bottom);
2299
            if (GridSideBar != null)
2300
                GridSideBar.Margin = new Thickness(GridSideBar.Margin.Left, 36 * UIScaleTopSlider.Value, GridSideBar.Margin.Right, GridSideBar.Margin.Bottom);
2301
            if (GridOSD != null)
2302
                GridOSD.Margin = new Thickness(GridOSD.Margin.Left, 36 * UIScaleTopSlider.Value, GridOSD.Margin.Right, GridOSD.Margin.Bottom);
2303
            if (GridData != null)
2304
                GridData.Margin = new Thickness(GridData.Margin.Left, 36 * UIScaleTopSlider.Value, GridData.Margin.Right, GridData.Margin.Bottom);
2305
            if (GridWP != null)
2306
                GridWP.Margin = new Thickness(GridWP.Margin.Left, 36 * UIScaleTopSlider.Value, GridWP.Margin.Right, GridWP.Margin.Bottom);
2307
        }
2310 - 2308
        /// <summary>
2309
        /// restore the saved scalings for the fullscreen/normal window mode
2310
        /// </summary>
2311
        /// <param name="bFull">the mode the window is set to</param>
2287 - 2312
        void _setScaleSliders(bool bFull)
2313
        {
2314
            if(bFull)
2315
            {
2316
                UIScaleSlider.Value = scaleFullAll;
2317
                UIScaleTopSlider.Value = scaleFullTopBar;
2318
                UIScaleMotorsSlider.Value = scaleFullMotors;
2319
                UIScaleOSDSlider.Value = scaleFullOSD;
2320
                UIScaleLOGSlider.Value = scaleFullLOG;
2321
                UIScaleHorizonSlider.Value = scaleFullHorizon;
2322
            }
2323
            else
2324
            {
2325
                UIScaleSlider.Value = scaleNormalAll;
2326
                UIScaleTopSlider.Value = scaleNormalTopBar;
2327
                UIScaleMotorsSlider.Value = scaleNormalMotors;
2328
                UIScaleOSDSlider.Value = scaleNormalOSD;
2329
                UIScaleLOGSlider.Value = scaleNormalLOG;
2330
                UIScaleHorizonSlider.Value = scaleNormalHorizon;
2331
            }
2332
        }
2333
 
2310 - 2334
        /// <summary>
2335
        /// save the scalings for the actual window mode
2336
        /// </summary>
2337
        /// <param name="bFull">the actual window mode</param>
2287 - 2338
        void _saveScaleSliders(bool bFull)
2339
        {
2340
 
2341
            if (bFull)
2342
            {
2343
                scaleFullAll = UIScaleSlider.Value;
2344
                scaleFullTopBar = UIScaleTopSlider.Value;
2345
                scaleFullMotors = UIScaleMotorsSlider.Value;
2346
                scaleFullOSD = UIScaleOSDSlider.Value;
2347
                scaleFullLOG = UIScaleLOGSlider.Value;
2348
                scaleFullHorizon = UIScaleHorizonSlider.Value;
2349
            }
2350
            else
2351
            {
2352
                scaleNormalAll = UIScaleSlider.Value;
2353
                scaleNormalTopBar = UIScaleTopSlider.Value;
2354
                scaleNormalMotors = UIScaleMotorsSlider.Value;
2355
                scaleNormalOSD = UIScaleOSDSlider.Value;
2356
                scaleNormalLOG = UIScaleLOGSlider.Value;
2357
                scaleNormalHorizon = UIScaleHorizonSlider.Value;
2358
            }
2359
        }
2360
 
2361
        /// <summary>
2362
        /// read settings from ini-file
2363
        /// </summary>
2364
        void _readIni()
2365
        {
2366
            if (!File.Exists(filePath + "\\MKLiveViewSettings.ini"))
2367
                _writeIni();
2368
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
2369
            ini.path = filePath + "\\MKLiveViewSettings.ini";
2312 - 2370
            try
2371
            {
2287 - 2372
 
2312 - 2373
                string sVal = ini.IniReadValue("timings", "AutorefreshDebugData");
2374
                if (sVal != "") _debugDataAutorefresh = Convert.ToBoolean(sVal);
2375
                sVal = ini.IniReadValue("timings", "AutorefreshNavCtrlData");
2376
                if (sVal != "") _navCtrlDataAutorefresh = Convert.ToBoolean(sVal);
2377
                sVal = ini.IniReadValue("timings", "AutorefreshBLCtrlData");
2378
                if (sVal != "") _blctrlDataAutorefresh = Convert.ToBoolean(sVal);
2379
                sVal = ini.IniReadValue("timings", "AutorefreshOSDData");
2380
                if (sVal != "") _OSDAutorefresh = Convert.ToBoolean(sVal);
2287 - 2381
 
2312 - 2382
                sVal = ini.IniReadValue("timings", "IntervalDebugData");
2383
                if (sVal != "") debugInterval = (byte)Convert.ToInt16(sVal);
2384
                sVal = ini.IniReadValue("timings", "IntervalNavCtrlData");
2385
                if (sVal != "") navctrlInterval = (byte)Convert.ToInt16(sVal);
2386
                sVal = ini.IniReadValue("timings", "IntervalBLCtrlData");
2387
                if (sVal != "") blctrlInterval = (byte)Convert.ToInt16(sVal);
2388
                sVal = ini.IniReadValue("timings", "IntervalOSDData");
2389
                if (sVal != "") OSDInterval = (byte)Convert.ToInt16(sVal);
2287 - 2390
 
2312 - 2391
                sVal = ini.IniReadValue("topBar", "voltage");
2392
                if (sVal != "") chkBoxTopBarShowVoltage.IsChecked = Convert.ToBoolean(sVal);
2393
                sVal = ini.IniReadValue("topBar", "capacity");
2394
                if (sVal != "") chkBoxTopBarShowCapacity.IsChecked = Convert.ToBoolean(sVal);
2395
                sVal = ini.IniReadValue("topBar", "current");
2396
                if (sVal != "") chkBoxTopBarShowCurrent.IsChecked = Convert.ToBoolean(sVal);
2397
                sVal = ini.IniReadValue("topBar", "flightTime");
2398
                if (sVal != "") chkBoxTopBarShowFlightTime.IsChecked = Convert.ToBoolean(sVal);
2399
                sVal = ini.IniReadValue("topBar", "distanceHP");
2400
                if (sVal != "") chkBoxTopBarShowDistanceHP.IsChecked = Convert.ToBoolean(sVal);
2401
                sVal = ini.IniReadValue("topBar", "height");
2402
                if (sVal != "") chkBoxTopBarShowHeight.IsChecked = Convert.ToBoolean(sVal);
2403
                sVal = ini.IniReadValue("topBar", "speed");
2404
                if (sVal != "") chkBoxTopBarShowSpeed.IsChecked = Convert.ToBoolean(sVal);
2405
                sVal = ini.IniReadValue("topBar", "magneticField");
2406
                if (sVal != "") chkBoxTopBarShowMF.IsChecked = Convert.ToBoolean(sVal);
2407
                sVal = ini.IniReadValue("topBar", "satellites");
2408
                if (sVal != "") chkBoxTopBarShowSatellites.IsChecked = Convert.ToBoolean(sVal);
2409
                sVal = ini.IniReadValue("topBar", "rc");
2410
                if (sVal != "") chkBoxTopBarShowRC.IsChecked = Convert.ToBoolean(sVal);
2287 - 2411
 
2312 - 2412
                sVal = ini.IniReadValue("style", "saveFullScreen");
2413
                if (sVal != "") chkBoxSaveFullScreenState.IsChecked = Convert.ToBoolean(sVal);
2414
                sVal = ini.IniReadValue("style", "saveNormalState");
2415
                if (sVal != "") chkBoxSaveNormalState.IsChecked = Convert.ToBoolean(sVal);
2287 - 2416
 
2312 - 2417
                sVal = ini.IniReadValue("style", "scaleNormalAll");
2418
                if (sVal != "") scaleNormalAll = Convert.ToDouble(sVal);
2419
                sVal = ini.IniReadValue("style", "scaleNormalTopBar");
2420
                if (sVal != "") scaleNormalTopBar = Convert.ToDouble(sVal);
2421
                sVal = ini.IniReadValue("style", "scaleNormalMotors");
2422
                if (sVal != "") scaleNormalMotors = Convert.ToDouble(sVal);
2423
                sVal = ini.IniReadValue("style", "scaleNormalOSD");
2424
                if (sVal != "") scaleNormalOSD = Convert.ToDouble(sVal);
2425
                sVal = ini.IniReadValue("style", "scaleNormalLOG");
2426
                if (sVal != "") scaleNormalLOG = Convert.ToDouble(sVal);
2427
                sVal = ini.IniReadValue("style", "scaleNormalHorizon");
2428
                if (sVal != "") scaleNormalHorizon = Convert.ToDouble(sVal);
2287 - 2429
 
2312 - 2430
                sVal = ini.IniReadValue("style", "scaleFullAll");
2431
                if (sVal != "") scaleFullAll = Convert.ToDouble(sVal);
2432
                sVal = ini.IniReadValue("style", "scaleFullTopBar");
2433
                if (sVal != "") scaleFullTopBar = Convert.ToDouble(sVal);
2434
                sVal = ini.IniReadValue("style", "scaleFullMotors");
2435
                if (sVal != "") scaleFullMotors = Convert.ToDouble(sVal);
2436
                sVal = ini.IniReadValue("style", "scaleFullOSD");
2437
                if (sVal != "") scaleFullOSD = Convert.ToDouble(sVal);
2438
                sVal = ini.IniReadValue("style", "scaleFullLOG");
2439
                if (sVal != "") scaleFullLOG = Convert.ToDouble(sVal);
2440
                sVal = ini.IniReadValue("style", "scaleFullHorizon");
2441
                if (sVal != "") scaleFullHorizon = Convert.ToDouble(sVal);
2287 - 2442
 
2312 - 2443
                sVal = ini.IniReadValue("general", "LiPoCells");
2444
                _LipoCells = Convert.ToInt16(sVal);
2445
                sVal = ini.IniReadValue("general", "Motors");
2446
                if (sVal != "") _iMotors = Convert.ToInt16(sVal);
2287 - 2447
 
2312 - 2448
                sVal = ini.IniReadValue("map", "followMe");
2449
                if (sVal != "") _bFollowCopter = Convert.ToBoolean(sVal);
2450
                sVal = ini.IniReadValue("map", "AutoSetHome");
2451
                if (sVal != "") _bAutoHome = Convert.ToBoolean(sVal);
2287 - 2452
 
2312 - 2453
                sVal = ini.IniReadValue("threshold", "VoltageWarning");
2454
                if(sVal != "") _dThresholdVoltageWarn = Convert.ToDouble(sVal);
2455
                sVal = ini.IniReadValue("threshold", "VoltageCritical");
2456
                if(sVal != "") _dThresholdVoltageCrit = Convert.ToDouble(sVal);
2457
                sVal = ini.IniReadValue("threshold", "VoiceVoltageEnable");
2458
                if(sVal != "") _bVoiceVoltPlay = Convert.ToBoolean(sVal);
2459
                sVal = ini.IniReadValue("threshold", "VoiceSatFixEnable");
2460
                if(sVal != "") _bVoiceSatFixPlay = Convert.ToBoolean(sVal);
2461
                sVal = ini.IniReadValue("threshold", "VoiceMagFieldEnable");
2462
                if(sVal != "") _bVoiceMagneticFieldPlay = Convert.ToBoolean(sVal);
2463
                sVal = ini.IniReadValue("threshold", "DistanceWarning");
2464
                if(sVal != "") _dThresholdDistanceWarn = Convert.ToDouble(sVal);
2465
                sVal = ini.IniReadValue("threshold", "VoiceDistanceWarnEnable");
2466
                if(sVal != "") _bVoiceDistancePlay = Convert.ToBoolean(sVal);
2467
                sVal = ini.IniReadValue("threshold", "VoiceRCLevelWarnEnable");
2468
                if(sVal != "") _bVoiceRCLevelPlay = Convert.ToBoolean(sVal);
2469
                sVal = ini.IniReadValue("threshold", "MaxDistance");
2470
                if(sVal != "") _dThresholdDistanceMax = Convert.ToDouble(sVal);
2471
                sVal = ini.IniReadValue("threshold", "RCThreshold");
2472
                if(sVal != "") _iThresholdRC = Convert.ToInt32(sVal);
2473
                sVal = ini.IniReadValue("threshold", "MagFieldThreshold");
2474
                if(sVal != "") _iThresholdMagField = Convert.ToInt32(sVal);
2475
            }
2476
            catch (Exception e)
2477
            {
2287 - 2478
 
2312 - 2479
                MessageBox.Show("Error parsing ini-file!" + Environment.NewLine + e.Message,"Read ini-file" ,MessageBoxButton.OK,MessageBoxImage.Error);
2480
            }
2481
 
2287 - 2482
        }
2483
 
2484
        /// <summary>
2485
        /// save settings to ini-file
2486
        /// </summary>
2487
        void _writeIni()
2488
        {
2489
 
2490
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
2491
            ini.path = filePath + "\\MKLiveViewSettings.ini";
2492
 
2312 - 2493
            try
2494
            {
2287 - 2495
 
2312 - 2496
                ini.IniWriteValue("timings", "AutorefreshDebugData", _debugDataAutorefresh ? "true" : "false");
2497
                ini.IniWriteValue("timings", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true" : "false");
2498
                ini.IniWriteValue("timings", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true" : "false");
2499
                ini.IniWriteValue("timings", "AutorefreshOSDData", _OSDAutorefresh ? "true" : "false");
2287 - 2500
 
2312 - 2501
                ini.IniWriteValue("timings", "IntervalDebugData", debugInterval.ToString());
2502
                ini.IniWriteValue("timings", "IntervalNavCtrlData", navctrlInterval.ToString());
2503
                ini.IniWriteValue("timings", "IntervalBLCtrlData", blctrlInterval.ToString());
2504
                ini.IniWriteValue("timings", "IntervalOSDData", OSDInterval.ToString());
2287 - 2505
 
2312 - 2506
                ini.IniWriteValue("general", "LiPoCells", _LipoCells.ToString());
2507
                ini.IniWriteValue("general", "Motors", _iMotors.ToString());
2287 - 2508
 
2312 - 2509
                ini.IniWriteValue("map", "followMe", _bFollowCopter.ToString());
2510
                ini.IniWriteValue("map", "AutoSetHome", _bAutoHome.ToString());
2287 - 2511
 
2312 - 2512
                ini.IniWriteValue("topBar", "voltage", chkBoxTopBarShowVoltage.IsChecked.ToString());
2513
                ini.IniWriteValue("topBar", "capacity", chkBoxTopBarShowCapacity.IsChecked.ToString());
2514
                ini.IniWriteValue("topBar", "current", chkBoxTopBarShowCurrent.IsChecked.ToString());
2515
                ini.IniWriteValue("topBar", "flightTime", chkBoxTopBarShowFlightTime.IsChecked.ToString());
2516
                ini.IniWriteValue("topBar", "distanceHP", chkBoxTopBarShowDistanceHP.IsChecked.ToString());
2517
                ini.IniWriteValue("topBar", "height", chkBoxTopBarShowHeight.IsChecked.ToString());
2518
                ini.IniWriteValue("topBar", "speed", chkBoxTopBarShowSpeed.IsChecked.ToString());
2519
                ini.IniWriteValue("topBar", "magneticField", chkBoxTopBarShowMF.IsChecked.ToString());
2520
                ini.IniWriteValue("topBar", "satellites", chkBoxTopBarShowSatellites.IsChecked.ToString());
2521
                ini.IniWriteValue("topBar", "rc", chkBoxTopBarShowRC.IsChecked.ToString());
2287 - 2522
 
2312 - 2523
                ini.IniWriteValue("style", "saveFullScreen", chkBoxSaveFullScreenState.IsChecked.ToString());
2524
                ini.IniWriteValue("style", "saveNormalState", chkBoxSaveNormalState.IsChecked.ToString());
2287 - 2525
 
2312 - 2526
                ini.IniWriteValue("style", "scaleNormalAll", scaleNormalAll.ToString());
2527
                ini.IniWriteValue("style", "scaleNormalTopBar", scaleNormalTopBar.ToString());
2528
                ini.IniWriteValue("style", "scaleNormalMotors", scaleNormalMotors.ToString());
2529
                ini.IniWriteValue("style", "scaleNormalOSD", scaleNormalOSD.ToString());
2530
                ini.IniWriteValue("style", "scaleNormalLOG", scaleNormalLOG.ToString());
2531
                ini.IniWriteValue("style", "scaleNormalHorizon", scaleNormalHorizon.ToString());
2287 - 2532
 
2312 - 2533
                ini.IniWriteValue("style", "scaleFullAll", scaleFullAll.ToString());
2534
                ini.IniWriteValue("style", "scaleFullTopBar", scaleFullTopBar.ToString());
2535
                ini.IniWriteValue("style", "scaleFullMotors", scaleFullMotors.ToString());
2536
                ini.IniWriteValue("style", "scaleFullOSD", scaleFullOSD.ToString());
2537
                ini.IniWriteValue("style", "scaleFullLOG", scaleFullLOG.ToString());
2538
                ini.IniWriteValue("style", "scaleFullHorizon", scaleFullHorizon.ToString());
2287 - 2539
 
2312 - 2540
                ini.IniWriteValue("style", "horizon", chkBoxShowHorizon.IsChecked.ToString());
2541
 
2542
                ini.IniWriteValue("threshold", "VoltageWarning", _dThresholdVoltageWarn.ToString());
2543
                ini.IniWriteValue("threshold", "VoltageCritical", _dThresholdVoltageCrit.ToString());
2544
                ini.IniWriteValue("threshold", "VoiceVoltageEnable", _bVoiceVoltPlay.ToString());
2545
                ini.IniWriteValue("threshold", "VoiceSatFixEnable", _bVoiceSatFixPlay.ToString());
2546
                ini.IniWriteValue("threshold", "VoiceMagFieldEnable", _bVoiceMagneticFieldPlay.ToString());
2547
                ini.IniWriteValue("threshold", "VoiceDistanceWarnEnable", _bVoiceDistancePlay.ToString());
2548
                ini.IniWriteValue("threshold", "VoiceRCLevelWarnEnable", _bVoiceRCLevelPlay.ToString());
2549
                ini.IniWriteValue("threshold", "DistanceWarning", _dThresholdDistanceWarn.ToString());
2550
                ini.IniWriteValue("threshold", "MaxDistance", _dThresholdDistanceMax.ToString());
2551
                ini.IniWriteValue("threshold", "RCThreshold", _iThresholdRC.ToString());
2552
                ini.IniWriteValue("threshold", "MagFieldThreshold", _iThresholdMagField.ToString());
2553
            }
2554
            catch (Exception e)
2555
            {
2556
 
2557
                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);
2558
            }
2287 - 2559
        }
2312 - 2560
 
2561
        void _readWPLFile()
2562
        {
2563
            Microsoft.Win32.OpenFileDialog fd = new Microsoft.Win32.OpenFileDialog();
2564
            fd.Filter = "Waypointlists | *.wpl";
2565
            fd.Multiselect = false;
2566
            if (fd.ShowDialog().Value)
2567
            {
2568
                string file = fd.SafeFileName;
2569
                IniFile ini = new IniFile(fd.SafeFileName);
2570
                ini.path = fd.FileName;
2571
 
2572
                try
2573
                {
2574
                    string sVal = ini.IniReadValue("General", "FileVersion");
2575
                    if (sVal == "")
2576
                        MessageBox.Show("The file has no version declared - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2577
                    else
2578
                    {
2579
                        if (Convert.ToInt16(sVal) < 3)
2580
                            MessageBox.Show("The file version is not supported - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2581
                        else
2582
                        {
2583
                            sVal = ini.IniReadValue("General", "NumberOfWaypoints");
2584
                            if (sVal == "")
2585
                                MessageBox.Show("The file has no number of waypoints declared - can't go on...", "", MessageBoxButton.OK, MessageBoxImage.Information);
2586
                            else
2587
                            {
2588
                                int wpnum = Convert.ToInt16(sVal);
2589
                                string wp;
2590
                                int i;
2313 - 2591
                                NumberFormatInfo nfi = new NumberFormatInfo();
2592
                                nfi.NumberDecimalSeparator = ".";
2312 - 2593
                                dtWaypoints.Rows.Clear();
2594
                                for (int k = 1; k <= wpnum; k++)
2595
                                {
2596
                                    DataRow dr = dtWaypoints.NewRow();
2597
                                    dr.ItemArray = new object[16];
2598
                                    object[] o = new object[16];
2599
                                    i = 0;
2600
                                    wp = "Point" + k.ToString();
2601
                                    o[i] = k;
2602
                                    i++;
2603
                                    sVal = ini.IniReadValue(wp, "Type");
2604
                                    if (sVal != "")
2313 - 2605
                                        o[i] = Convert.ToInt16(sVal) -1;
2312 - 2606
                                    i++;
2607
                                    sVal = ini.IniReadValue(wp, "Prefix");
2608
                                    if (sVal != "")
2609
                                        o[i] = sVal == "0" ? "P" + k.ToString() : sVal + k.ToString();
2610
                                    i++;
2313 - 2611
                                    sVal = ini.IniReadValue(wp, "Latitude");
2312 - 2612
                                    if (sVal != "")
2313 - 2613
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2614
                                    i++;
2313 - 2615
                                    sVal = ini.IniReadValue(wp, "Longitude");
2312 - 2616
                                    if (sVal != "")
2313 - 2617
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2618
                                    i++;
2619
                                    sVal = ini.IniReadValue(wp, "Altitude");
2620
                                    if (sVal != "")
2313 - 2621
                                        o[i] = Convert.ToDouble(sVal, nfi);
2312 - 2622
                                    i++;
2623
                                    sVal = ini.IniReadValue(wp, "Heading");
2624
                                    if (sVal != "")
2625
                                        o[i] = Convert.ToInt16(sVal);
2626
                                    i++;
2627
                                    sVal = ini.IniReadValue(wp, "Speed");
2628
                                    if (sVal != "")
2313 - 2629
                                        o[i] =Convert.ToInt16(sVal);
2312 - 2630
                                    i++;
2631
                                    sVal = ini.IniReadValue(wp, "ClimbRate");
2632
                                    if (sVal != "")
2633
                                        o[i] = Convert.ToInt16(sVal);
2634
                                    i++;
2635
                                    sVal = ini.IniReadValue(wp, "Radius");
2636
                                    if (sVal != "")
2637
                                        o[i] = Convert.ToInt16(sVal);
2638
                                    i++;
2639
                                    sVal = ini.IniReadValue(wp, "DelayTime");
2640
                                    if (sVal != "")
2641
                                        o[i] = Convert.ToInt16(sVal);
2642
                                    i++;
2643
                                    sVal = ini.IniReadValue(wp, "AutoTrigger");
2644
                                    if (sVal != "")
2645
                                        o[i] = Convert.ToInt16(sVal); ;
2646
                                    i++;
2647
                                    sVal = ini.IniReadValue(wp, "CAM-Nick");
2648
                                    if (sVal != "")
2313 - 2649
                                        o[i] = Convert.ToInt16(sVal);
2312 - 2650
                                    i++;
2651
                                    o[i] = 0;
2652
                                    i++;
2653
                                    sVal = ini.IniReadValue(wp, "WP_Event_Channel_Value");
2654
                                    if (sVal != "")
2655
                                        o[i] = Convert.ToInt16(sVal);
2656
                                    i++;
2657
                                    o[i] = "New";
2658
 
2659
                                    dr.ItemArray = o;
2313 - 2660
                                  //  dtWaypoints.Rows.Add(dr);
2312 - 2661
                                    dtWaypoints.Rows.Add(dr);
2662
                                    Dispatcher.Invoke(() => lblWPIndex.Content = k.ToString());
2663
                                    Dispatcher.Invoke(() => lblWPCount.Content = k.ToString());
2664
                                    Dispatcher.Invoke(() => dgvWP.Items.Refresh());
2665
                                    Thread.Sleep(10);
2666
                                }
2667
                            }
2668
                        }
2669
                    }
2670
 
2671
                }
2672
                catch (Exception e)
2673
                {
2674
 
2675
                    MessageBox.Show("Error parsing wpl-file!" + Environment.NewLine + e.Message, "Read wpl-file", MessageBoxButton.OK, MessageBoxImage.Error);
2676
                }
2677
 
2678
            }
2679
        }
2680
 
2681
        private void btnLoadWPLFile_Click(object sender, RoutedEventArgs e)
2682
        {
2683
            _readWPLFile();
2684
        }
2287 - 2685
        #endregion functions
2686
    }
2687
    public class BooleanToVisibilityConverter : IValueConverter
2688
    {
2689
 
2690
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
2691
        {
2692
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
2693
        }
2694
 
2695
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
2696
        {
2697
            throw new NotImplementedException();
2698
        }
2699
    }
2700
 
2313 - 2701
    public class waypointsConverter : IValueConverter
2702
    {
2703
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
2704
        {
2705
            if (value != null)
2706
            {
2707
                switch ((string)parameter)
2708
                {
2709
                    case "Latitude":
2710
                        return value.ToString() + " °";
2711
                    case "Longitude":
2712
                        return value.ToString() + " °";
2713
                    case "Radius":
2714
                        return value.ToString() + " m";
2715
                    case "Altitude":
2716
                        return value.ToString() + " m";
2717
                    case "ClimbRate":
2718
                        return value.ToString() == "255" ? "Auto" : (System.Convert.ToDouble(value) / 10).ToString("0.0 m/s");
2719
                    case "DelayTime":
2720
                        return value.ToString() + " s";
2721
                    case "Heading":
2722
                        return Waypoints.Heading(System.Convert.ToInt32(value));
2723
                    case "Speed":
2724
                        return Waypoints.WPSpeed(System.Convert.ToInt16(value));
2725
                    case "CamAngle":
2726
                        return Waypoints.CAMAngle(System.Convert.ToInt16(value));
2727
                    case "Type":
2728
                        return ((Waypoints.pointType)(System.Convert.ToInt16(value))).ToString();
2729
                    case "AutoTrigger":
2730
                        return value.ToString() == "0" ? "- - -" : value.ToString() + " m";
2731
                    case "Status":
2732
                        return Waypoints.WPSpeed(System.Convert.ToInt16(value));
2733
                }
2734
 
2735
                return value.ToString();
2736
            }
2737
            else return value;
2738
        }
2739
 
2740
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
2741
        {
2742
            throw new NotImplementedException();
2743
        }
2744
    }
2745
 
2287 - 2746
    public class IniFile
2747
    {
2748
        public string path;
2749
 
2750
        [DllImport("kernel32")]
2751
        private static extern long WritePrivateProfileString(string section,
2752
          string key, string val, string filePath);
2753
 
2754
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
2755
        static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,
2756
               uint nSize, string lpFileName);
2757
 
2758
        [DllImport("kernel32")]
2759
        private static extern int GetPrivateProfileString(string section,
2760
          string key, string def, StringBuilder retVal,
2761
          int size, string filePath);
2762
 
2763
        public IniFile(string INIPath)
2764
        {
2765
            path = INIPath;
2766
        }
2767
 
2768
        public void IniWriteValue(string Section, string Key, string Value)
2769
        {
2770
            WritePrivateProfileString(Section, Key, Value, this.path);
2771
        }
2772
 
2773
        public string IniReadValue(string Section, string Key)
2774
        {
2775
            StringBuilder temp = new StringBuilder(255);
2776
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
2777
            return temp.ToString();
2778
        }
2779
        //Ini_sections auslesen in String-Array
2780
        public string[] IniSectionNames()
2781
        {
2782
 
2783
            //  uint MAX_BUFFER = 32767;
2784
            uint MAX_BUFFER = 8388608;
2785
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
2786
            uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
2787
            if (bytesReturned == 0)
2788
            {
2789
                Marshal.FreeCoTaskMem(pReturnedString);
2790
                return null;
2791
            }
2792
            string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
2793
            Marshal.FreeCoTaskMem(pReturnedString);
2794
            //use of Substring below removes terminating null for split
2795
            return local.Substring(0, local.Length - 1).Split('\0');
2796
 
2797
 
2798
        }
2799
    }
2800
 
2801
    /// <summary>
2802
    /// Selected Win AI Function Calls
2803
    /// </summary>
2804
    public class WinApi
2805
    {
2806
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
2807
        public static extern int GetSystemMetrics(int which);
2808
        [DllImport("user32.dll")]
2809
        public static extern void
2810
                SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
2811
                             int X, int Y, int width, int height, uint flags);
2812
 
2813
        private const int SM_CXSCREEN = 0;
2814
        private const int SM_CYSCREEN = 1;
2815
        private static IntPtr HWND_TOP = IntPtr.Zero;
2816
        private const int SWP_SHOWWINDOW = 64; // 0x0040
2817
 
2818
        public static int ScreenX
2819
        {
2820
            get { return GetSystemMetrics(SM_CXSCREEN); }
2821
        }
2822
 
2823
        public static int ScreenY
2824
        {
2825
            get { return GetSystemMetrics(SM_CYSCREEN); }
2826
        }
2827
 
2828
        public static void SetWinFullScreen(IntPtr hwnd)
2829
        {
2830
            SetWindowPos(hwnd, HWND_TOP, -8, -7, ScreenX+15, ScreenY+14, SWP_SHOWWINDOW);
2831
        }
2832
    }
2833
    /// <summary>
2834
    /// Class used to preserve / restore state of the window
2835
    /// </summary>
2836
    public class WinState
2837
    {
2838
        private WindowState winState;
2839
        private WindowStyle brdStyle;
2840
        private bool topMost;
2841
        private Rect restore;
2842
        private bool IsMaximized = false;
2843
 
2844
        public bool isMaximized
2845
        {
2846
            get { return IsMaximized; }
2847
        }
2848
        public void Maximize(Window targetForm)
2849
        {
2850
            if (!IsMaximized)
2851
            {
2852
                IsMaximized = true;
2853
                Save(targetForm);
2854
                targetForm.WindowState = WindowState.Maximized;
2855
                targetForm.WindowStyle = WindowStyle.None;
2856
                targetForm.Topmost = true;
2857
                WinApi.SetWinFullScreen(new WindowInteropHelper(targetForm).Handle);
2858
            }
2859
        }
2860
 
2861
        public void Save(Window targetForm)
2862
        {
2863
            winState = targetForm.WindowState;
2864
            brdStyle = targetForm.WindowStyle;
2865
            topMost = targetForm.Topmost;
2866
            restore = targetForm.RestoreBounds;
2867
        }
2868
        public void Restore(Window targetForm)
2869
        {
2870
            targetForm.WindowState = winState;
2871
            targetForm.WindowStyle = brdStyle;
2872
            targetForm.Topmost = topMost;
2873
 
2874
            targetForm.Left = restore.Left;
2875
            targetForm.Top = restore.Top;
2876
            targetForm.Height = restore.Height;
2877
            targetForm.Width = restore.Width;
2878
            IsMaximized = false;
2879
        }
2880
    }
2881
 
2882
}