Subversion Repositories Projects

Rev

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