Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2233 | - | 1 | ///============================================================================ |
2 | /// MKLiveView |
||
3 | /// Copyright © 2016 Steph |
||
4 | /// |
||
5 | ///This file is part of MKLiveView. |
||
6 | /// |
||
7 | ///MKLiveView is free software: you can redistribute it and/or modify |
||
8 | ///it under the terms of the GNU General Public License as published by |
||
9 | ///the Free Software Foundation, either version 3 of the License, or |
||
10 | ///(at your option) any later version. |
||
11 | /// |
||
12 | ///MKLiveView is distributed in the hope that it will be useful, |
||
13 | ///but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | ///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | ///GNU General Public License for more details. |
||
16 | /// |
||
17 | ///You should have received a copy of the GNU General Public License |
||
18 | ///along with cssRcon. If not, see <http://www.gnu.org/licenses/>. |
||
19 | /// |
||
20 | ///============================================================================ |
||
21 | ///Credits: |
||
22 | ///Chootair (http://www.codeproject.com/script/Membership/View.aspx?mid=3941737) |
||
23 | ///for his "C# Avionic Instrument Controls" (http://www.codeproject.com/Articles/27411/C-Avionic-Instrument-Controls) |
||
24 | ///I used some of his code for displaying the compass |
||
25 | /// |
||
26 | ///Tom Pyke (http://tom.pycke.be) |
||
27 | ///for his "Artifical horizon" (http://tom.pycke.be/mav/100/artificial-horizon) |
||
28 | ///Great job! |
||
29 | /// |
||
30 | /// and last but most of all to JOHN C. MACDONALD at Ira A. Fulton College of Engineering and Technology |
||
31 | /// for his MIKROKOPTER SERIAL CONTROL TUTORIAL (http://hdl.lib.byu.edu/1877/2747) |
||
32 | /// and the sourcode (http://hdl.lib.byu.edu/1877/2748) |
||
33 | /// By his work I finally managed to get the communication with the Mikrokopter controllers to run |
||
34 | /// Some of his code was used in this programm like the SimpelSerialPort class (with some changes) |
||
35 | /// and the FilghtControllerMessage class |
||
36 | /// |
||
37 | ///============================================================================ |
||
38 | |||
39 | using System; |
||
40 | using System.Data; |
||
41 | using System.Drawing; |
||
42 | using System.Text; |
||
43 | using System.Windows.Forms; |
||
44 | using System.IO; |
||
45 | using System.Threading; |
||
46 | using System.Diagnostics; |
||
47 | using System.Runtime.InteropServices; |
||
48 | |||
49 | namespace MKLiveView |
||
50 | { |
||
51 | public partial class MainForm : Form |
||
52 | { |
||
2250 | - | 53 | String[] NC_Error = new string[44] |
54 | { |
||
55 | "No Error", |
||
56 | "FC not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A1_.22FC_not_compatible_.22", |
||
57 | "MK3Mag not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A2_.22MK3Mag_not_compatible_.22", |
||
58 | "no FC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A3_.22no_FC_communication_.22", |
||
59 | "no compass communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A4_.22no_compass_communication_.22", |
||
60 | "no GPS communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A5_.22no_GPS_communication_.22", |
||
61 | "bad compass value" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A6_.22bad_compass_value.22", |
||
62 | "RC Signal lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A7_.22RC_Signal_lost_.22", |
||
63 | "FC spi rx error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A8_.22FC_spi_rx_error_.22", |
||
64 | "ERR: no NC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A9:_.22ERR:_no_NC_communication.22", |
||
65 | "ERR: FC Nick Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A10_.22ERR:_FC_Nick_Gyro.22", |
||
66 | "ERR: FC Roll Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A11_.22ERR:_FC_Roll_Gyro.22", |
||
67 | "ERR: FC Yaw Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A12_.22ERR:_FC_Yaw_Gyro.22", |
||
68 | "ERR: FC Nick ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A13_.22ERR:_FC_Nick_ACC.22", |
||
69 | "ERR: FC Roll ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A14_.22ERR:_FC_Roll_ACC.22", |
||
70 | "ERR: FC Z-ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A15_.22ERR:_FC_Z-ACC.22", |
||
71 | "ERR: Pressure sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A16_.22ERR:_Pressure_sensor.22", |
||
72 | "ERR: FC I2C" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A17_.22ERR:_FC_I2C.22", |
||
73 | "ERR: Bl Missing" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A18_.22ERR:_Bl_Missing.22", |
||
74 | "Mixer Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A19_.22Mixer_Error.22", |
||
75 | "FC: Carefree Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A20_.22FC:_Carefree_Error.22", |
||
76 | "ERR: GPS lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A21_.22ERR:_GPS_lost.22", |
||
77 | "ERR: Magnet Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A22_.22ERR:_Magnet_Error.22", |
||
78 | "Motor restart" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A23_.22Motor_restart.22", |
||
79 | "BL Limitation" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A24_.22BL_Limitation.22", |
||
80 | "Waypoint range" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A25_.22Waypoint_range.22", |
||
81 | "ERR:No SD-Card" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A26_.22ERR:No_SD-Card.22", |
||
82 | "ERR:SD Logging aborted" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A27_.22ERR:SD_Logging_aborted.22", |
||
83 | "ERR:Flying range!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A28_.22ERR:Flying_range.21.22", |
||
84 | "ERR:Max Altitude" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A29_.22ERR:Max_Altitude.22", |
||
85 | "No GPS Fix" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A30_.22No_GPS_Fix.22", |
||
86 | "compass not calibrated" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A31_.22compass_not_calibrated.22", |
||
87 | "ERR:BL selftest" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A32_.22ERR:BL_selftest.22", |
||
88 | "no ext. compass" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A33_.22no_ext._compass.22", |
||
89 | "compass sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A34_.22compass_sensor.22", |
||
90 | "FAILSAFE pos.!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A35_.22FAILSAFE_pos..21__.22", |
||
91 | "ERR:Redundancy" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A36_.22ERR:Redundancy__.22", |
||
92 | "Redundancy test" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A37_.22Redundancy_test_.22", |
||
93 | "GPS Update rate" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A38_.22GPS_Update_rate.22", |
||
94 | "ERR:Canbus" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A39_.22ERR:Canbus.22", |
||
95 | "ERR: 5V RC-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A40_.22ERR:_5V_RC-Supply.22", |
||
96 | "ERR:Power-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A41_.22ERR:Power-Supply.22", |
||
97 | "ACC not calibr." + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A42_.22ACC_not_calibr..22", |
||
98 | "ERR:Parachute!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A43_.22ERR:Parachute.21.22" |
||
99 | }; |
||
2233 | - | 100 | |
101 | [FlagsAttribute] |
||
102 | enum NC_HWError0 : short |
||
103 | { |
||
104 | None = 0, |
||
105 | SPI_RX = 1, |
||
106 | COMPASS_RX = 2, |
||
107 | FC_INCOMPATIBLE = 4, |
||
108 | COMPASS_INCOMPATIBLE = 8, |
||
109 | GPS_RX = 16, |
||
110 | COMPASS_VALUE = 32 |
||
111 | }; |
||
112 | [FlagsAttribute] |
||
113 | enum FC_HWError0 : short |
||
114 | { |
||
115 | None = 0, |
||
116 | GYRO_NICK = 1, |
||
117 | GYRO_ROLL = 2, |
||
118 | GYRO_YAW = 4, |
||
119 | ACC_NICK = 8, |
||
120 | ACC_ROLL = 16, |
||
121 | ACC_TOP = 32, |
||
122 | PRESSURE = 64, |
||
123 | CAREFREE = 128 |
||
124 | }; |
||
125 | [FlagsAttribute] |
||
126 | enum FC_HWError1 : short |
||
127 | { |
||
128 | None = 0, |
||
129 | I2C = 1, |
||
130 | BL_MISSING = 2, |
||
131 | SPI_RX = 4, |
||
132 | PPM = 8, |
||
133 | MIXER = 16, |
||
134 | RC_VOLTAGE = 32, |
||
135 | ACC_NOT_CAL = 64, |
||
136 | RES3 = 128 |
||
137 | }; |
||
138 | public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error }; |
||
139 | // Various colors for logging info |
||
140 | private Color[] LogMsgTypeColor = { Color.FromArgb(43, 145, 175), Color.Green, Color.Black, Color.Orange, Color.Red }; |
||
141 | |||
142 | string[] sAnalogLabel = new string[32]; |
||
143 | string[] sAnalogData = new string[32]; |
||
144 | bool bReadContinously = false; |
||
145 | bool check_HWError = false; |
||
146 | bool _bCBInit = true; |
||
147 | bool _init = true; |
||
148 | bool _debugDataAutorefresh = true; |
||
149 | bool _navCtrlDataAutorefresh = true; |
||
150 | bool _blctrlDataAutorefresh = true; |
||
151 | bool _OSDAutorefresh = true; |
||
2254 | - | 152 | bool _bErrorLog = false; |
2233 | - | 153 | int crcError = 0; |
154 | int iLableIndex = 0; |
||
155 | string filePath = Directory.GetCurrentDirectory(); |
||
156 | string fileName = "NCLabelTexts.txt"; |
||
157 | int _iCtrlAct = 0; |
||
158 | int _iLifeCounter = 0; |
||
159 | int iOSDPage = 0; |
||
160 | int iOSDMax = 0; |
||
161 | /// <summary> |
||
162 | /// interval for sending debugdata (multiplied by 10ms) |
||
163 | /// </summary> |
||
164 | byte debugInterval = 25; //(=> 250ms) |
||
165 | /// <summary> |
||
166 | /// interval for sending BL-CTRL status (multiplied by 10ms) |
||
167 | /// </summary> |
||
168 | byte blctrlInterval = 45; |
||
169 | /// <summary> |
||
170 | /// interval for sending NAV-CTRL status (multiplied by 10ms) |
||
171 | /// </summary> |
||
172 | byte navctrlInterval = 80; |
||
173 | /// <summary> |
||
174 | /// interval for sending OSD page update (multiplied by 10ms) |
||
175 | /// </summary> |
||
176 | byte OSDInterval = 85; |
||
177 | /// <summary> |
||
178 | /// datatable for the debug data array - displayed on settings tabpage in datagridview |
||
179 | /// </summary> |
||
180 | DataTable dtAnalog = new DataTable(); |
||
2254 | - | 181 | /// <summary> |
182 | /// datatable for motordata (current,temp) |
||
183 | /// </summary> |
||
2250 | - | 184 | DataTable dtMotors1 = new DataTable(); |
185 | DataTable dtMotors2 = new DataTable(); |
||
186 | |||
2233 | - | 187 | public MainForm() |
188 | { |
||
189 | InitializeComponent(); |
||
190 | _readIni(); |
||
191 | dtAnalog.Columns.Add("ID"); |
||
192 | dtAnalog.Columns.Add("Value"); |
||
193 | dataGridView1.DataSource = dtAnalog; |
||
2250 | - | 194 | dtMotors1.Columns.Add("#"); |
195 | dtMotors1.Columns.Add("Current"); |
||
196 | dtMotors1.Columns.Add("Temp"); |
||
197 | dtMotors2.Columns.Add("#"); |
||
198 | dtMotors2.Columns.Add("Current"); |
||
199 | dtMotors2.Columns.Add("Temp"); |
||
200 | dgvMotors1.DataSource = dtMotors1; |
||
201 | dgvMotors2.DataSource = dtMotors2; |
||
202 | _initDTMotors(); |
||
203 | dgvMotors1.Columns[0].Width = 24; |
||
204 | dgvMotors1.Columns[1].Width = 74; |
||
205 | dgvMotors1.Columns[2].Width = 74; |
||
206 | dgvMotors2.Columns[0].Width = 24; |
||
207 | dgvMotors2.Columns[1].Width = 74; |
||
208 | dgvMotors2.Columns[2].Width = 74; |
||
2233 | - | 209 | simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed; |
210 | simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened; |
||
211 | simpleSerialPort.DataReceived += processMessage; |
||
212 | chkbAutoBL.Checked = _blctrlDataAutorefresh; |
||
213 | chkbAutoDbg.Checked = _debugDataAutorefresh; |
||
214 | chkbAutoNav.Checked = _navCtrlDataAutorefresh; |
||
215 | chkbAutoOSD.Checked = _OSDAutorefresh; |
||
216 | labelTimingDebug.Text = (debugInterval * 10).ToString(); |
||
217 | labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString(); |
||
218 | labelTimingNAV.Text = (navctrlInterval * 10).ToString(); |
||
219 | labelTimingOSD.Text = (OSDInterval * 10).ToString(); |
||
220 | tabControl1.TabPages.Remove(tabPageTesting); |
||
221 | } |
||
222 | #region events |
||
223 | private void MainForm_Shown(object sender, EventArgs e) |
||
224 | { |
||
225 | _loadLabelNames(); |
||
226 | _init = false; |
||
2250 | - | 227 | splitContainer1.SplitterDistance = 514; |
2233 | - | 228 | } |
229 | private void MainForm_FormClosed(object sender, FormClosedEventArgs e) |
||
230 | { |
||
231 | _writeIni(); |
||
232 | } |
||
233 | private void SimpleSerialPort_PortOpened() |
||
234 | { |
||
235 | btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(192, 255, 192))); |
||
236 | btnConn.Invoke((Action)(() => btnConn.Text = "close" + Environment.NewLine + "serial port")); |
||
237 | _getVersion(); |
||
238 | Thread.Sleep(100); |
||
239 | _OSDMenue(0); |
||
240 | // _readCont(true); |
||
241 | } |
||
242 | private void SimpleSerialPort_PortClosed() |
||
243 | { |
||
244 | btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(224, 224, 224))); |
||
245 | btnConn.Invoke((Action)(() => btnConn.Text = "open" + Environment.NewLine + "serial port")); |
||
246 | _readCont(false); |
||
247 | } |
||
248 | /// <summary> |
||
249 | /// timer for refreshing subscription of subscribed data |
||
250 | /// query lifecounter for connection failure |
||
251 | /// </summary> |
||
252 | private void timer1_Tick(object sender, EventArgs e) |
||
253 | { |
||
254 | if(bReadContinously) |
||
255 | { |
||
256 | if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
||
257 | |||
2250 | - | 258 | if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
2233 | - | 259 | |
260 | if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
||
261 | check_HWError = true; |
||
262 | _getVersion(); |
||
263 | Thread.Sleep(10); |
||
264 | if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); } |
||
265 | if (_iLifeCounter > 0) |
||
266 | { |
||
267 | lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0); |
||
268 | _iLifeCounter = 0; |
||
269 | } |
||
270 | else |
||
271 | { |
||
272 | Log(LogMsgType.Error, "No communication to NC/FC!"); |
||
273 | lblLifeCounter.BackColor = Color.FromArgb(224, 0, 0); |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | private void cbOSD_SelectedIndexChanged(object sender, EventArgs e) |
||
278 | { |
||
279 | if (!_bCBInit && cbOSD.SelectedIndex > -1) |
||
280 | _OSDMenue(cbOSD.SelectedIndex); |
||
281 | } |
||
282 | private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e) |
||
283 | { |
||
284 | if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked; |
||
285 | } |
||
286 | private void chkbAutoNav_CheckedChanged(object sender, EventArgs e) |
||
287 | { |
||
288 | if (!_init) _navCtrlDataAutorefresh = chkbAutoNav.Checked; |
||
289 | } |
||
290 | private void chkbAutoBL_CheckedChanged(object sender, EventArgs e) |
||
291 | { |
||
292 | if (!_init) _blctrlDataAutorefresh = chkbAutoBL.Checked; |
||
293 | } |
||
294 | private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e) |
||
295 | { |
||
296 | if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked; |
||
297 | } |
||
298 | private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e) |
||
299 | { |
||
300 | if (cbTimingDebug.SelectedIndex > -1) |
||
301 | { |
||
302 | debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10); |
||
303 | labelTimingDebug.Text = (debugInterval * 10).ToString(); |
||
304 | } |
||
305 | } |
||
306 | private void cbTimingNAV_SelectedIndexChanged(object sender, EventArgs e) |
||
307 | { |
||
308 | if (cbTimingNAV.SelectedIndex > -1) |
||
309 | { |
||
310 | navctrlInterval = (byte)(Convert.ToInt16(cbTimingNAV.SelectedItem) / 10); |
||
311 | labelTimingNAV.Text = (navctrlInterval * 10).ToString(); |
||
312 | } |
||
313 | } |
||
314 | private void cbTimingBLCTRL_SelectedIndexChanged(object sender, EventArgs e) |
||
315 | { |
||
316 | if (cbTimingBLCTRL.SelectedIndex > -1) |
||
317 | { |
||
318 | blctrlInterval = (byte)(Convert.ToInt16(cbTimingBLCTRL.SelectedItem) / 10); |
||
319 | labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString(); |
||
320 | } |
||
321 | } |
||
322 | private void cbTimingOSD_SelectedIndexChanged(object sender, EventArgs e) |
||
323 | { |
||
324 | if (cbTimingOSD.SelectedIndex > -1) |
||
325 | { |
||
326 | OSDInterval = (byte)(Convert.ToInt16(cbTimingOSD.SelectedItem) / 10); |
||
327 | labelTimingOSD.Text = (OSDInterval * 10).ToString(); |
||
328 | } |
||
329 | } |
||
2254 | - | 330 | private void rtfError_LinkClicked(object sender, LinkClickedEventArgs e) |
331 | { |
||
332 | System.Diagnostics.Process.Start(e.LinkText); |
||
333 | } |
||
2233 | - | 334 | #endregion events |
335 | |||
336 | /// <summary> Log data to the terminal window. </summary> |
||
337 | /// <param name="msgtype"> The type of message to be written. </param> |
||
338 | /// <param name="msg"> The string containing the message to be shown. </param> |
||
339 | private void Log(LogMsgType msgtype, string msg) |
||
340 | { |
||
341 | rtfTerminal.Invoke(new EventHandler(delegate |
||
342 | { |
||
343 | if (rtfTerminal.Lines.Length >= 1000) //Wenn Terminal mehr als 1000 Zeilen hat |
||
344 | rtfTerminal.Select(42, (500 * 129)); //500 löschen |
||
345 | rtfTerminal.Select(rtfTerminal.Text.Length, 0); |
||
346 | rtfTerminal.SelectedText = string.Empty; |
||
347 | rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular); |
||
348 | rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
349 | rtfTerminal.AppendText(msg + Environment.NewLine); |
||
350 | rtfTerminal.ScrollToCaret(); |
||
351 | })); |
||
352 | } |
||
353 | /// <summary> display the OSD text in 4 lines à 20 chars </summary> |
||
354 | /// <param name="msgtype"> The type of message to be written. </param> |
||
355 | /// <param name="msg"> The string containing the message to be shown. </param> |
||
356 | private void OSD(LogMsgType msgtype, string msg) |
||
357 | { |
||
358 | rtfOSD.Invoke(new EventHandler(delegate |
||
359 | { |
||
360 | if (rtfOSD.Lines.Length > 4) |
||
361 | rtfOSD.Clear(); |
||
362 | rtfOSD.Select(rtfOSD.Text.Length,0); |
||
363 | rtfOSD.SelectedText = string.Empty; |
||
364 | rtfOSD.SelectionFont = new Font(rtfOSD.SelectionFont, FontStyle.Regular); |
||
365 | rtfOSD.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
366 | rtfOSD.AppendText(msg + Environment.NewLine); |
||
367 | if (rtfOSD.Text.IndexOf("ERR") > 0) |
||
368 | { |
||
369 | rtfOSD.Select(rtfOSD.Text.IndexOf("ERR"), 40); |
||
370 | rtfOSD.SelectionColor = LogMsgTypeColor[(int)LogMsgType.Error]; |
||
371 | } |
||
372 | })); |
||
373 | } |
||
374 | private void ErrorLog(LogMsgType msgtype, string msg) |
||
375 | { |
||
376 | rtfError.Invoke(new EventHandler(delegate |
||
377 | { |
||
378 | if (rtfError.Lines.Length > 4) |
||
379 | rtfError.Clear(); |
||
380 | rtfError.Focus(); |
||
381 | rtfError.Select(rtfError.Text.Length, 0); |
||
382 | rtfError.SelectedText = string.Empty; |
||
383 | rtfError.SelectionFont = new Font(rtfError.SelectionFont, FontStyle.Regular); |
||
384 | rtfError.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
385 | rtfError.AppendText(msg + Environment.NewLine); |
||
386 | |||
387 | })); |
||
2254 | - | 388 | _bErrorLog = true; |
2233 | - | 389 | } |
390 | |||
391 | #region functions |
||
392 | |||
2250 | - | 393 | #region processing received data |
394 | /// <summary> Processing the messages and displaying them in the according form controls |
||
395 | /// function called by simpleSerialPort.DataReceived event |
||
396 | /// </summary> |
||
2233 | - | 397 | /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param> |
398 | private void processMessage(byte[] message) |
||
399 | { |
||
400 | if (message.Length > 0) |
||
401 | { |
||
402 | _iLifeCounter++; |
||
403 | //Log(LogMsgType.Incoming, BitConverter.ToString(message)); |
||
404 | //Log(LogMsgType.Incoming, message.Length.ToString()); |
||
405 | string s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, message.Length)); |
||
406 | char cmdID; |
||
407 | byte adr; |
||
408 | byte[] data; |
||
409 | if (message[0] != '#') |
||
410 | Log(LogMsgType.Normal, s.Trim('\0', '\n','\r')); |
||
411 | //Debug.Print(s); |
||
412 | else |
||
413 | { |
||
414 | FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data); |
||
415 | |||
416 | if (adr == 255) { crcError++; } |
||
417 | else crcError = 0; |
||
418 | lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString())); |
||
2250 | - | 419 | //display the active controller (FC / NC) |
2233 | - | 420 | 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...??? |
421 | { |
||
422 | _iCtrlAct = adr; |
||
423 | switch (adr) |
||
424 | { |
||
425 | case 1: |
||
426 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "FC")); |
||
427 | lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "FC")); |
||
428 | _setFieldsNA(); //display fields NA for FC |
||
429 | break; |
||
430 | case 2: |
||
431 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "NC")); |
||
432 | lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "NC")); |
||
433 | break; |
||
434 | case 3: |
||
435 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "MK3MAG")); |
||
436 | break; |
||
437 | case 4: |
||
438 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "BL-CTRL")); |
||
439 | break; |
||
440 | default: |
||
441 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "....")); |
||
442 | break; |
||
443 | } |
||
2250 | - | 444 | _loadLabelNames(); |
2233 | - | 445 | } |
446 | // else |
||
447 | // Debug.Print("Address == 0?"); |
||
448 | |||
449 | if (data != null && data.Length > 0) |
||
450 | { |
||
451 | s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1)); |
||
452 | s = s.Trim('\0', '\n'); |
||
453 | |||
454 | switch (cmdID) |
||
455 | { |
||
2250 | - | 456 | case 'A': //Label names |
457 | _processLabelNames(s); |
||
2233 | - | 458 | break; |
459 | |||
2250 | - | 460 | case 'D': //Debug data |
461 | _processDebugVals(adr,data); |
||
2233 | - | 462 | break; |
463 | |||
2250 | - | 464 | case 'V': //Version |
465 | _processVersion(adr, data); |
||
2233 | - | 466 | break; |
467 | |||
2250 | - | 468 | case 'K'://BL-CTRL data |
469 | _processBLCtrl(data); |
||
2233 | - | 470 | break; |
471 | |||
472 | case 'O': //NC Data |
||
2250 | - | 473 | _processNCData(data); |
2233 | - | 474 | break; |
475 | |||
476 | case 'E': //NC error-string |
||
477 | ErrorLog(LogMsgType.Error, "NC Error: " + s); |
||
478 | break; |
||
479 | |||
2250 | - | 480 | case 'L': //OSD Menue (called by pagenumber) |
481 | _processOSDSingle(data); |
||
2233 | - | 482 | break; |
483 | |||
2250 | - | 484 | case 'H': //OSD Menue (with autoupdate - called by Key) |
485 | _processOSDAuto(data); |
||
2233 | - | 486 | break; |
487 | |||
488 | //default: |
||
489 | // Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString()); |
||
490 | // Log(LogMsgType.Incoming, BitConverter.ToString(data)); |
||
491 | // break; |
||
492 | } |
||
493 | } |
||
494 | //else |
||
495 | //{ |
||
496 | // Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString()); |
||
497 | // Log(LogMsgType.Incoming, BitConverter.ToString(data)); |
||
498 | //} |
||
499 | } |
||
500 | } |
||
501 | } |
||
2250 | - | 502 | /// <summary> |
503 | /// Analog label names 'A' |
||
504 | /// each label name is returned as a single string |
||
505 | /// and added to string array sAnalogLabel[] |
||
506 | /// and the datatable dtAnalog |
||
507 | /// </summary> |
||
508 | /// <param name="s">the label name</param> |
||
509 | void _processLabelNames(string s) |
||
510 | { |
||
511 | if (iLableIndex < 32) |
||
512 | { |
||
513 | sAnalogLabel[iLableIndex] = s; |
||
514 | if (dtAnalog.Rows.Count < 32) |
||
515 | dtAnalog.Rows.Add(s, ""); |
||
516 | else |
||
517 | dtAnalog.Rows[iLableIndex].SetField(0, s); |
||
2233 | - | 518 | |
2250 | - | 519 | _getAnalogLabels(iLableIndex + 1); |
520 | } |
||
521 | Debug.Print(s); |
||
522 | } |
||
523 | /// <summary> |
||
524 | /// Debug values 'D' |
||
525 | /// </summary> |
||
526 | /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
||
527 | /// <param name="data">the received byte array to process</param> |
||
528 | void _processDebugVals(byte adr,byte[] data) |
||
529 | { |
||
530 | if (data.Length == 66) |
||
531 | { |
||
532 | int[] iAnalogData = new int[32]; |
||
533 | |||
534 | int index = 0; |
||
535 | Int16 i16 = 0; |
||
536 | double dTemp = 0; |
||
537 | for (int i = 2; i < 66; i += 2) |
||
538 | { |
||
539 | i16 = data[i + 1]; |
||
540 | i16 = (Int16)(i16 << 8); |
||
541 | iAnalogData[index] = data[i] + i16; |
||
542 | sAnalogData[index] = (data[i] + i16).ToString(); |
||
543 | dtAnalog.Rows[index].SetField(1, sAnalogData[index]); |
||
544 | |||
545 | if (adr == 2) //NC |
||
546 | { |
||
547 | switch (index) |
||
548 | { |
||
549 | case 0: //pitch (German: nick) |
||
550 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
||
551 | lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
552 | break; |
||
553 | case 1: //roll |
||
554 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
||
555 | lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
556 | break; |
||
557 | case 4: //altitude |
||
558 | lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
||
559 | break; |
||
560 | case 7: //Voltage |
||
561 | lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
||
562 | break; |
||
563 | case 8: // Current |
||
564 | lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
||
565 | break; |
||
566 | case 10: //heading |
||
567 | lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
||
568 | headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
||
569 | break; |
||
570 | case 12: // SPI error |
||
571 | lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
||
572 | break; |
||
573 | case 14: //i2c error |
||
574 | lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
||
575 | break; |
||
576 | case 20: //Earthmagnet field |
||
577 | lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%")); |
||
578 | break; |
||
579 | case 21: //GroundSpeed |
||
580 | lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s"))); |
||
581 | break; |
||
582 | case 28: //Distance East from saved home position -> calculate distance with distance N + height |
||
583 | dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2); |
||
584 | dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E |
||
585 | // lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00"))); |
||
586 | dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation |
||
2254 | - | 587 | dTemp = Math.Sqrt(dTemp) / (double)10; |
588 | lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0.0 m"))); |
||
2250 | - | 589 | break; |
590 | case 31: //Sats used |
||
591 | lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index])); |
||
592 | break; |
||
593 | } |
||
594 | } |
||
595 | if (adr == 1) //FC |
||
596 | { |
||
597 | switch (index) |
||
598 | { |
||
599 | case 0: //pitch (German: nick) |
||
600 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
||
601 | lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
602 | break; |
||
603 | case 1: //roll |
||
604 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
||
605 | lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
606 | break; |
||
607 | case 5: //altitude |
||
608 | lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
||
609 | break; |
||
610 | case 8: //heading |
||
611 | lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
||
612 | headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
||
613 | break; |
||
614 | case 9: //Voltage |
||
615 | lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
||
616 | break; |
||
617 | case 10: //Receiver quality |
||
618 | lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index])); |
||
619 | break; |
||
620 | case 22: // Current |
||
621 | lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
||
622 | break; |
||
623 | case 23: //capacity used |
||
624 | lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh"))); |
||
625 | break; |
||
626 | case 27: // SPI error |
||
627 | lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
||
628 | break; |
||
629 | case 28: //i2c error |
||
630 | lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
||
631 | break; |
||
632 | } |
||
633 | } |
||
634 | index++; |
||
635 | } |
||
636 | } |
||
637 | else |
||
638 | Debug.Print("wrong data-length (66): " + data.Length.ToString()); |
||
639 | } |
||
640 | /// <summary> |
||
641 | /// Version string 'V' |
||
642 | /// </summary> |
||
643 | /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
||
644 | /// <param name="data">the received byte array to process</param> |
||
645 | void _processVersion(byte adr,byte[] data) |
||
646 | { |
||
647 | if (data.Length == 12) |
||
648 | { |
||
649 | if (!check_HWError) |
||
650 | { |
||
651 | string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " }; |
||
652 | string sVersion = ""; |
||
653 | //sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b)); |
||
654 | Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: "); |
||
655 | sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString(); |
||
656 | Log(LogMsgType.Incoming, sVersion); |
||
657 | sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString(); |
||
658 | Log(LogMsgType.Incoming, sVersion); |
||
659 | Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString()); |
||
660 | } |
||
661 | if (data[5] > 0) //error0 |
||
662 | { |
||
663 | if (adr == 1) |
||
664 | ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString()); |
||
665 | if (adr == 2) |
||
666 | ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString()); |
||
667 | } |
||
668 | if (data[6] > 0) //error1 |
||
669 | { |
||
670 | if (adr == 1) |
||
671 | ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString()); |
||
672 | if (adr == 2) |
||
673 | ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field |
||
674 | } |
||
2254 | - | 675 | if((data[5] + data[6] == 0) && _bErrorLog) |
676 | _clearErrorLog(adr==1 ? "FC - HW-Error" : "FC - HW-Error"); |
||
2250 | - | 677 | |
678 | } |
||
679 | check_HWError = false; |
||
680 | } |
||
681 | /// <summary> |
||
682 | /// BL-Ctrl data 'K' |
||
683 | /// for FC you have to use a customized firmware |
||
684 | /// </summary> |
||
685 | /// <param name="data">the received byte array to process</param> |
||
686 | void _processBLCtrl(byte[] data) |
||
687 | { |
||
688 | if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard! |
||
689 | { |
||
690 | bool bAvailable = false; |
||
691 | for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors |
||
692 | { |
||
693 | |||
694 | if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available |
||
695 | bAvailable = true; |
||
696 | else |
||
697 | bAvailable = false; |
||
698 | |||
699 | if (data[i] < 4) |
||
700 | { |
||
701 | if (bAvailable) |
||
702 | { |
||
703 | dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
||
704 | dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C")); |
||
705 | } |
||
706 | else |
||
707 | { |
||
708 | dtMotors1.Rows[data[i]].SetField(1, "NA"); |
||
709 | dtMotors1.Rows[data[i]].SetField(2, "NA"); |
||
710 | } |
||
711 | } |
||
712 | if (data[i] > 3 && data[i] < 8) |
||
713 | { |
||
714 | if (bAvailable) |
||
715 | { |
||
716 | dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
||
717 | dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C")); |
||
718 | } |
||
719 | else |
||
720 | { |
||
721 | dtMotors2.Rows[data[i] - 4].SetField(1, "NA"); |
||
722 | dtMotors2.Rows[data[i] - 4].SetField(2, "NA"); |
||
723 | } |
||
724 | } |
||
725 | } |
||
726 | } |
||
727 | |||
728 | } |
||
729 | /// <summary> |
||
730 | /// Navi-Ctrl data 'O' |
||
731 | /// GPS-Position, capacatiy, flying time... |
||
732 | /// </summary> |
||
733 | /// <param name="data">the received byte array to process</param> |
||
734 | void _processNCData(byte[] data) |
||
735 | { |
||
736 | int i_32, i_16, iVal; |
||
737 | double d; |
||
738 | i_32 = data[4]; |
||
739 | iVal = i_32 << 24; |
||
740 | i_32 = data[3]; |
||
741 | iVal += i_32 << 16; |
||
742 | i_32 = data[2]; |
||
743 | iVal += i_32 << 8; |
||
744 | iVal += data[1]; |
||
745 | d = (double)iVal / Math.Pow(10, 7); |
||
746 | lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree |
||
747 | //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds |
||
748 | |||
749 | i_32 = data[8]; |
||
750 | iVal = i_32 << 24; |
||
751 | i_32 = data[7]; |
||
752 | iVal += i_32 << 16; |
||
753 | i_32 = data[6]; |
||
754 | iVal += i_32 << 8; |
||
755 | iVal += data[5]; |
||
756 | d = (double)iVal / Math.Pow(10, 7); |
||
757 | lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree |
||
2254 | - | 758 | //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds |
2250 | - | 759 | |
2254 | - | 760 | i_16 = data[28]; |
761 | i_16 = (Int16)(i_16 << 8); |
||
762 | iVal = data[27] + i_16; |
||
763 | lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP |
||
764 | |||
765 | i_16 = data[45]; |
||
766 | i_16 = (Int16)(i_16 << 8); |
||
767 | iVal = data[44] + i_16; |
||
768 | lblNCDistHP1.Invoke((Action)(() => lblNCDistHP1.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP |
||
769 | |||
770 | lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = data[48].ToString())); //Waypoint index |
||
771 | lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = data[49].ToString())); //Waypoints count |
||
772 | |||
2250 | - | 773 | i_16 = data[81]; |
774 | i_16 = (Int16)(i_16 << 8); |
||
775 | iVal = data[80] + i_16; |
||
776 | lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used |
||
777 | |||
778 | i_16 = data[56]; |
||
779 | i_16 = (Int16)(i_16 << 8); |
||
780 | iVal = data[55] + i_16; |
||
781 | TimeSpan t = TimeSpan.FromSeconds(iVal); |
||
782 | string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2"); |
||
783 | lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time |
||
784 | |||
785 | lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality |
||
786 | lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString())); //NC Errornumber |
||
787 | //if (data[69] > 0) |
||
788 | // _readNCError(); |
||
789 | //break; |
||
790 | if (data[69] > 0 & data[69] < 44) |
||
791 | ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]); |
||
2254 | - | 792 | else |
793 | if(_bErrorLog) _clearErrorLog("NC Error"); |
||
2250 | - | 794 | |
795 | } |
||
796 | /// <summary> |
||
797 | /// OSD Menue 'L' |
||
798 | /// single page called by pagenumber |
||
799 | /// no autoupdate |
||
800 | /// </summary> |
||
801 | /// <param name="data">the received byte array to process</param> |
||
802 | void _processOSDSingle(byte[] data) |
||
803 | { |
||
804 | if (data.Length == 84) |
||
805 | { |
||
806 | string sMessage = ""; |
||
807 | iOSDPage = data[0]; |
||
808 | iOSDMax = data[1]; |
||
809 | if (cbOSD.Items.Count != iOSDMax) _initOSDCB(); |
||
810 | sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4)); |
||
811 | OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
||
812 | OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
||
813 | OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
||
814 | OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
||
815 | lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]"))); |
||
816 | |||
817 | } |
||
818 | else |
||
819 | OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)"); |
||
820 | |||
821 | } |
||
822 | /// <summary> |
||
823 | /// OSD Menue 'H' |
||
824 | /// called by keys (0x01,0x02,0x03,0x04) |
||
825 | /// autoupdate |
||
826 | /// </summary> |
||
827 | /// <param name="data">the received byte array to process</param> |
||
828 | void _processOSDAuto(byte[] data) |
||
829 | { |
||
830 | if (data.Length == 81) |
||
831 | { |
||
832 | string sMessage = ""; |
||
833 | sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1)); |
||
834 | OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
||
835 | OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
||
836 | OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
||
837 | OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
||
838 | |||
839 | } |
||
840 | else |
||
841 | OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)"); |
||
842 | } |
||
843 | #endregion processing received data |
||
844 | |||
2233 | - | 845 | /// <summary> send message to controller to request data |
846 | /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/ |
||
847 | /// </summary> |
||
848 | /// <param name="CMDID"> the command ID </param> |
||
849 | /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param> |
||
850 | private void _sendControllerMessage(char CMDID, byte address) |
||
851 | { |
||
852 | if (simpleSerialPort.Port.IsOpen) |
||
853 | { |
||
854 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
855 | byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address); |
||
856 | serialStream.Write(bytes, 0, bytes.Length); |
||
857 | |||
858 | } |
||
859 | else |
||
860 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
861 | } |
||
862 | /// <summary> send message to controller to request data |
||
863 | /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/ |
||
864 | /// </summary> |
||
865 | /// <param name="CMDID"> the command ID </param> |
||
866 | /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param> |
||
867 | /// <param name="data"> additional data for the request</param> |
||
868 | private void _sendControllerMessage(char CMDID, byte address, byte[]data) |
||
869 | { |
||
870 | if (simpleSerialPort.Port.IsOpen) |
||
871 | { |
||
872 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
873 | byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address,data); |
||
874 | serialStream.Write(bytes, 0, bytes.Length); |
||
875 | |||
876 | } |
||
877 | else |
||
878 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
879 | } |
||
880 | |||
881 | /// <summary> |
||
882 | /// read the analog-label names for the actual controller |
||
883 | /// and load it into listbox |
||
884 | /// </summary> |
||
885 | void _loadLabelNames() |
||
886 | { |
||
887 | if (_iCtrlAct > 0 && _iCtrlAct < 3) |
||
888 | { |
||
889 | switch (_iCtrlAct) |
||
890 | { |
||
891 | case 1: |
||
892 | sAnalogLabel = Properties.Resources.FCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None); |
||
893 | break; |
||
894 | case 2: |
||
895 | sAnalogLabel = Properties.Resources.NCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None); |
||
896 | break; |
||
897 | } |
||
898 | for (int i = 0; i < 32; i++) |
||
899 | { |
||
900 | if (dtAnalog.Rows.Count < 32) |
||
901 | dtAnalog.Rows.Add(sAnalogLabel[i], ""); |
||
902 | else |
||
903 | dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]); |
||
904 | } |
||
905 | dataGridView1.Invoke((Action)(()=>dataGridView1.Refresh())); |
||
906 | } |
||
907 | } |
||
908 | /// <summary> |
||
909 | /// no longer used... |
||
910 | /// read the analog-label textfile for the actual controller |
||
911 | /// </summary> |
||
912 | private void _loadLabelFile() |
||
913 | { |
||
914 | switch (_iCtrlAct) |
||
915 | { |
||
916 | case 1: |
||
917 | fileName = "FCLabelTexts.txt"; |
||
918 | break; |
||
919 | case 2: |
||
920 | fileName = "NCLabelTexts.txt"; |
||
921 | break; |
||
922 | //default: |
||
923 | // fileName = "NCLabelTexts.txt"; |
||
924 | // break; |
||
925 | } |
||
926 | |||
927 | if (File.Exists(filePath + "\\" + fileName)) |
||
928 | { |
||
929 | sAnalogLabel.Initialize(); |
||
930 | sAnalogLabel = File.ReadAllLines(filePath + "\\" + fileName); |
||
931 | lbLabels.Invoke((Action)(() => lbLabels.Items.Clear())); |
||
932 | lbLabels.Invoke((Action)(() => lbLabels.Update())); |
||
933 | lbLabels.Invoke((Action)(() => lbLabels.Items.AddRange(sAnalogLabel))); |
||
934 | Console.WriteLine("Names loaded from file"); |
||
935 | lblFileName.Invoke((Action)(() => lblFileName.Text = fileName)); |
||
936 | } |
||
937 | else |
||
938 | { |
||
939 | _readCont(false); |
||
940 | Log(LogMsgType.Error, "Label-file not found!"); |
||
941 | Log(LogMsgType.Error, "Please go to settings-tab and load the label texts from the copter control (FC & NC)"); |
||
942 | Log(LogMsgType.Error, "When done, you have to save the label texts with the 'save' button!"); |
||
943 | } |
||
944 | } |
||
945 | /// <summary> |
||
946 | /// no longer used... |
||
947 | /// assign the analog-label names from the textfile to the datatable |
||
948 | /// |
||
949 | /// </summary> |
||
950 | private void _assignLabelNames() |
||
951 | { |
||
952 | if (lbLabels.Items.Count == 32) |
||
953 | { |
||
954 | lbLabels.Items.CopyTo(sAnalogLabel, 0); |
||
955 | for (int i = 0; i < 32; i++) |
||
956 | { |
||
957 | if (dtAnalog.Rows.Count < 32) |
||
958 | dtAnalog.Rows.Add(sAnalogLabel[i], ""); |
||
959 | else |
||
960 | dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]); |
||
961 | |||
962 | } |
||
963 | } |
||
964 | } |
||
965 | /// <summary> |
||
966 | /// get the version struct of actual controller |
||
967 | /// </summary> |
||
968 | /// <summary> |
||
969 | /// get the labeltexts for the analog values |
||
970 | /// </summary> |
||
971 | private void _getAnalogLabels() |
||
972 | { |
||
973 | if (simpleSerialPort.Port.IsOpen) |
||
974 | { |
||
975 | iLableIndex = 0; |
||
976 | for (int i = 0; i < 32; i++) |
||
977 | { |
||
978 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
979 | byte[] bytes = FlightControllerMessage.CreateMessage('a', 0, new byte[1] { (byte)i }); |
||
980 | serialStream.Write(bytes, 0, bytes.Length); |
||
981 | Thread.Sleep(10); |
||
982 | } |
||
983 | } |
||
984 | else |
||
985 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
986 | } |
||
987 | /// <summary> |
||
988 | /// get the labeltext for a single label |
||
989 | /// </summary> |
||
990 | /// <param name="iIndex">index of the label</param> |
||
991 | private void _getAnalogLabels(int iIndex) |
||
992 | { |
||
993 | if (simpleSerialPort.Port.IsOpen) |
||
994 | { |
||
995 | if (iIndex < 32) |
||
996 | { |
||
997 | iLableIndex = iIndex; |
||
998 | _sendControllerMessage('a', 0, new byte[1] { (byte)iLableIndex }); |
||
999 | } |
||
1000 | } |
||
1001 | else |
||
1002 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1003 | } |
||
1004 | private void _getVersion() |
||
1005 | { |
||
1006 | _sendControllerMessage('v', 0); |
||
1007 | } |
||
1008 | /// <summary> |
||
1009 | /// get FC version struct via NC |
||
1010 | /// by sending '1' as data (not documented in wiki...) |
||
1011 | /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version) |
||
1012 | /// </summary> |
||
1013 | /// <param name="ctrl">controller number 1=FC</param> |
||
1014 | private void _getVersion(byte ctrl) |
||
1015 | { |
||
1016 | _sendControllerMessage('v', 0, new byte[1] {ctrl}); |
||
1017 | } |
||
1018 | /// <summary> |
||
1019 | /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00 |
||
1020 | /// </summary> |
||
1021 | private void _switchToNC() |
||
1022 | { |
||
1023 | if (simpleSerialPort.Port.IsOpen) |
||
1024 | { |
||
1025 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1026 | byte[] bytes = new byte[5] { 0x1B,0x1B,0x55,0xAA,0x00 }; |
||
1027 | serialStream.Write(bytes, 0, bytes.Length); |
||
1028 | |||
1029 | Thread.Sleep(100); |
||
1030 | _getVersion(); |
||
1031 | Thread.Sleep(100); |
||
1032 | _OSDMenue(0); |
||
1033 | } |
||
1034 | else |
||
1035 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1036 | } |
||
1037 | /// <summary> |
||
1038 | /// switch to FC |
||
1039 | /// </summary> |
||
1040 | private void _switchToFC() |
||
1041 | { |
||
1042 | _sendControllerMessage('u', 2, new byte[1] { (byte)0 }); |
||
1043 | Thread.Sleep(100); |
||
1044 | _getVersion(); |
||
1045 | Thread.Sleep(100); |
||
1046 | _OSDMenue(0); |
||
1047 | } |
||
1048 | /// <summary> |
||
1049 | /// send RESET signal to FC |
||
1050 | /// </summary> |
||
1051 | private void _resetCtrl() |
||
1052 | { |
||
1053 | _sendControllerMessage('R', 1); |
||
1054 | } |
||
1055 | /// <summary> |
||
1056 | /// poll the debug data (4sec subscription) |
||
1057 | /// </summary> |
||
1058 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1059 | private void _readDebugData(bool auto) |
||
1060 | { |
||
1061 | byte interval = auto ? debugInterval : (byte)0; |
||
1062 | _sendControllerMessage('d', 0, new byte[1] { debugInterval }); |
||
1063 | } |
||
1064 | /// <summary> |
||
1065 | /// poll the BL-CTRL status via NC (4sec subscription) |
||
1066 | /// </summary> |
||
1067 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1068 | private void _readBLCtrl(bool auto) |
||
1069 | { |
||
1070 | byte interval = auto ? blctrlInterval : (byte)0; |
||
1071 | _sendControllerMessage('k', 0, new byte[1] { interval }); |
||
1072 | } |
||
1073 | /// <summary> |
||
1074 | /// poll the NC data struct (4sec subscription) |
||
1075 | /// </summary> |
||
1076 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1077 | private void _readNavData(bool auto) |
||
1078 | { |
||
1079 | byte interval = auto ? navctrlInterval : (byte)0; |
||
1080 | _sendControllerMessage('o', 2, new byte[1] { interval }); |
||
1081 | } |
||
1082 | /// <summary> |
||
1083 | /// get the errortext for pending NC error |
||
1084 | /// </summary> |
||
1085 | private void _readNCError() |
||
1086 | { |
||
1087 | _sendControllerMessage('e', 2); |
||
1088 | } |
||
1089 | /// <summary> |
||
1090 | /// start/stop continous polling of controller values |
||
1091 | /// </summary> |
||
1092 | /// <param name="b">start/stop switch</param> |
||
1093 | void _readCont(bool b) |
||
1094 | { |
||
1095 | bReadContinously = b; |
||
1096 | btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh")); |
||
1097 | btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224))); |
||
1098 | if (bReadContinously) |
||
1099 | { |
||
2250 | - | 1100 | if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
1101 | if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
||
1102 | if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
||
1103 | if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);} |
||
2233 | - | 1104 | lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0))); |
1105 | } |
||
1106 | else |
||
1107 | lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224))); |
||
1108 | _iLifeCounter = 0; |
||
1109 | } |
||
1110 | /// <summary> |
||
2250 | - | 1111 | /// set values to "NA" when not available with FC |
2233 | - | 1112 | /// </summary> |
1113 | void _setFieldsNA() |
||
1114 | { |
||
1115 | Thread.Sleep(100); |
||
2250 | - | 1116 | _initDTMotors(); |
2233 | - | 1117 | lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA")); //FlightTime |
2250 | - | 1118 | lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA")); //NC ErrorNr |
1119 | lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA")); //earth magnet field |
||
1120 | lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA")); //GroundSpeed |
||
1121 | lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA")); //Distance to HP |
||
1122 | lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA")); //Sats used |
||
1123 | lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA")); //GPS position - longitude |
||
1124 | lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA")); //GPS position - latitude |
||
2254 | - | 1125 | lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = "NA")); //next WP distance |
1126 | lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = "NA")); //index of actual WP |
||
1127 | lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = "NA")); //count of items in WP list |
||
2233 | - | 1128 | } |
1129 | /// <summary> |
||
1130 | /// one time query of the OSD Menue with pagenumber |
||
1131 | /// </summary> |
||
1132 | /// <param name="iMenue">Menue page</param> |
||
1133 | void _OSDMenue(int iMenue) |
||
1134 | { |
||
1135 | if (simpleSerialPort.Port.IsOpen) |
||
1136 | { |
||
1137 | if (iMenue > iOSDMax) |
||
1138 | iMenue = 0; |
||
1139 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1140 | byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue }); |
||
1141 | serialStream.Write(bytes, 0, bytes.Length); |
||
1142 | } |
||
1143 | else |
||
1144 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1145 | |||
1146 | } |
||
1147 | /// <summary> |
||
1148 | /// call the OSDMenue and start autorefresh |
||
1149 | /// usually by sending a menuekey |
||
1150 | /// 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) |
||
1151 | /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle |
||
1152 | /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33 |
||
1153 | /// </summary> |
||
1154 | void _OSDMenueAutoRefresh() |
||
1155 | { |
||
1156 | _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)),OSDInterval }); |
||
1157 | } |
||
1158 | void _OSDMenueAutoRefresh(byte key) |
||
1159 | { |
||
1160 | _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval }); |
||
1161 | } |
||
1162 | /// <summary> |
||
1163 | /// initialize the OSD menue combobox |
||
1164 | /// combox is filled by numbers from 0 to max pagenumber |
||
1165 | /// </summary> |
||
1166 | void _initOSDCB() |
||
1167 | { |
||
1168 | _bCBInit = true; |
||
1169 | if(iOSDMax == 0) |
||
1170 | { |
||
1171 | _OSDMenue(0); |
||
1172 | Thread.Sleep(10); |
||
1173 | } |
||
1174 | cbOSD.Invoke((Action)(()=>cbOSD.Items.Clear())); |
||
1175 | for(int i = 0; i <= iOSDMax;i++) |
||
1176 | { |
||
1177 | cbOSD.Invoke((Action)(() => cbOSD.Items.Add(i))); |
||
1178 | } |
||
1179 | cbOSD.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage)); |
||
1180 | _bCBInit = false; |
||
1181 | } |
||
1182 | void _readIni() |
||
1183 | { |
||
1184 | if (!File.Exists(filePath + "\\MKLiveViewSettings.ini")) |
||
1185 | _writeIni(); |
||
1186 | IniFile ini = new IniFile("MKLiveViewSettings.ini"); |
||
1187 | ini.path = filePath + "\\MKLiveViewSettings.ini"; |
||
1188 | |||
1189 | string sVal = ini.IniReadValue("default", "AutorefreshDebugData"); |
||
1190 | _debugDataAutorefresh = Convert.ToBoolean(sVal); |
||
1191 | sVal = ini.IniReadValue("default", "AutorefreshNavCtrlData"); |
||
1192 | _navCtrlDataAutorefresh = Convert.ToBoolean(sVal); |
||
1193 | sVal = ini.IniReadValue("default", "AutorefreshBLCtrlData"); |
||
1194 | _blctrlDataAutorefresh = Convert.ToBoolean(sVal); |
||
1195 | sVal = ini.IniReadValue("default", "AutorefreshOSDData"); |
||
1196 | _OSDAutorefresh = Convert.ToBoolean(sVal); |
||
1197 | |||
1198 | sVal = ini.IniReadValue("default", "IntervalDebugData"); |
||
1199 | debugInterval = (byte)Convert.ToInt16(sVal); |
||
1200 | sVal = ini.IniReadValue("default", "IntervalNavCtrlData"); |
||
1201 | navctrlInterval = (byte)Convert.ToInt16(sVal); |
||
1202 | sVal = ini.IniReadValue("default", "IntervalBLCtrlData"); |
||
1203 | blctrlInterval = (byte)Convert.ToInt16(sVal); |
||
1204 | sVal = ini.IniReadValue("default", "IntervalOSDData"); |
||
1205 | OSDInterval = (byte)Convert.ToInt16(sVal); |
||
1206 | } |
||
1207 | void _writeIni() |
||
1208 | { |
||
1209 | |||
1210 | IniFile ini = new IniFile("MKLiveViewSettings.ini"); |
||
1211 | ini.path = filePath + "\\MKLiveViewSettings.ini"; |
||
1212 | |||
1213 | ini.IniWriteValue("default", "AutorefreshDebugData", _debugDataAutorefresh ? "true":"false"); |
||
1214 | ini.IniWriteValue("default", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true":"false"); |
||
1215 | ini.IniWriteValue("default", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true":"false"); |
||
1216 | ini.IniWriteValue("default", "AutorefreshOSDData", _OSDAutorefresh ? "true":"false"); |
||
1217 | |||
1218 | ini.IniWriteValue("default", "IntervalDebugData", debugInterval.ToString()); |
||
1219 | ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString()); |
||
1220 | ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString()); |
||
1221 | ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString()); |
||
1222 | } |
||
1223 | |||
2250 | - | 1224 | /// <summary> |
1225 | /// initialize the 2 datatables for motor values |
||
1226 | /// dtMotors1 - motor 1 - 4 |
||
1227 | /// dtMotors2 - motor 5 - 8 |
||
1228 | /// DataGridView dgvMotors1/2 are bound to dtMotors1/2 |
||
1229 | /// </summary> |
||
1230 | void _initDTMotors() |
||
1231 | { |
||
1232 | for(int i = 0; i < 4; i++) |
||
1233 | { |
||
1234 | if (dtMotors1.Rows.Count < 4) |
||
1235 | dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA"); |
||
1236 | else |
||
1237 | { |
||
1238 | dtMotors1.Rows[i].SetField(1, "NA"); |
||
1239 | dtMotors1.Rows[i].SetField(2, "NA"); |
||
1240 | } |
||
1241 | if (dtMotors2.Rows.Count < 4) |
||
1242 | dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA"); |
||
1243 | else |
||
1244 | { |
||
1245 | dtMotors2.Rows[i].SetField(1, "NA"); |
||
1246 | dtMotors2.Rows[i].SetField(2, "NA"); |
||
1247 | } |
||
1248 | } |
||
1249 | dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh())); |
||
1250 | dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh())); |
||
1251 | } |
||
1252 | |||
1253 | /// <summary> |
||
1254 | /// Convert decimal degrees to degrees, minutes, seconds, milliseconds |
||
1255 | /// </summary> |
||
1256 | /// <param name="coord">the degree value as double</param> |
||
1257 | /// <returns>0° 0' 0,0"</returns> |
||
1258 | string _convertDegree(double coord) |
||
1259 | { |
||
1260 | //double minutes = (degree - Math.Floor(degree)) * 60.0; |
||
1261 | //double seconds = (minutes - Math.Floor(minutes)) * 60.0; |
||
1262 | //double tenths = (seconds - Math.Floor(seconds)) * 10.0; |
||
1263 | //// get rid of fractional part |
||
1264 | //minutes = Math.Floor(minutes); |
||
1265 | //seconds = Math.Floor(seconds); |
||
1266 | //tenths = Math.Floor(tenths); |
||
1267 | |||
1268 | |||
1269 | //int sec = (int)Math.Round(coord * 3600); |
||
1270 | //int deg = sec / 3600; |
||
1271 | //sec = Math.Abs(sec % 3600); |
||
1272 | //int min = sec / 60; |
||
1273 | //sec %= 60; |
||
1274 | |||
1275 | var ts = TimeSpan.FromHours(Math.Abs(coord)); |
||
1276 | double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours); |
||
1277 | int min = ts.Minutes; |
||
1278 | int sec = ts.Seconds; |
||
1279 | int milli = ts.Milliseconds; |
||
1280 | |||
1281 | return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\""; |
||
1282 | } |
||
2254 | - | 1283 | |
1284 | void _clearErrorLog(string s) |
||
1285 | { |
||
1286 | rtfError.Invoke((Action)(() => |
||
1287 | { |
||
1288 | if (rtfError.Text.Contains(s)) |
||
1289 | { |
||
1290 | int iLength = 0; |
||
1291 | int iStart = rtfError.Text.IndexOf(s); |
||
1292 | int iEnd = rtfError.Text.IndexOf('\n', iStart); |
||
1293 | if (iEnd > 0) |
||
1294 | { |
||
1295 | iLength = iEnd + 1; |
||
1296 | int iHttp = rtfError.Text.IndexOf("http", iEnd); |
||
1297 | if (iHttp == iLength) |
||
1298 | { |
||
1299 | int iEnd2 = rtfError.Text.IndexOf('\n', iLength); |
||
1300 | if (iEnd2 > iLength) |
||
1301 | { |
||
1302 | iLength = iEnd2 + 1; |
||
1303 | rtfError.Select(iStart, iLength); |
||
1304 | rtfError.SelectedText = string.Empty; |
||
1305 | if(rtfError.Text.Length < 2) _bErrorLog = false; |
||
1306 | } |
||
1307 | |||
1308 | } |
||
1309 | else |
||
1310 | { |
||
1311 | rtfError.Select(iStart, iLength); |
||
1312 | rtfError.SelectedText = string.Empty; |
||
1313 | if(rtfError.Text.Length < 2) _bErrorLog = false; |
||
1314 | } |
||
1315 | } |
||
1316 | } |
||
1317 | })); |
||
1318 | |||
1319 | } |
||
2233 | - | 1320 | #endregion functions |
1321 | |||
1322 | #region buttons |
||
1323 | private void buttonReset_Click(object sender, EventArgs e) |
||
1324 | { |
||
1325 | _resetCtrl(); |
||
1326 | } |
||
1327 | private void btnVersion_Click(object sender, EventArgs e) |
||
1328 | { |
||
1329 | _getVersion(); |
||
1330 | } |
||
1331 | private void btnAnalogLabels_Click(object sender, EventArgs e) |
||
1332 | { |
||
1333 | _getAnalogLabels(0); |
||
1334 | } |
||
1335 | private void btnDbgData_Click(object sender, EventArgs e) |
||
1336 | { |
||
1337 | _readDebugData(false); //onetime reading of debug data --> subscription lasts 4sec - this means you will receive data for 4 seconds |
||
1338 | } |
||
1339 | private void btnSaveLabels_Click(object sender, EventArgs e) |
||
1340 | { |
||
1341 | switch (_iCtrlAct) |
||
1342 | { |
||
1343 | case 1: |
||
1344 | fileName = "FCLabelTexts.txt"; |
||
1345 | break; |
||
1346 | case 2: |
||
1347 | fileName = "NCLabelTexts.txt"; |
||
1348 | break; |
||
1349 | default: |
||
1350 | fileName = "NCLabelTexts.txt"; |
||
1351 | break; |
||
1352 | } |
||
1353 | if (sAnalogLabel[0] != null) |
||
1354 | { |
||
1355 | File.WriteAllLines(filePath + "\\" + fileName, sAnalogLabel); |
||
1356 | Console.WriteLine("Names saved to file"); |
||
1357 | _loadLabelFile(); |
||
1358 | } |
||
1359 | else |
||
1360 | Log(LogMsgType.Warning, "there's no data -> read first from fc/nc!"); |
||
1361 | } |
||
1362 | private void btnLoadLabels_Click(object sender, EventArgs e) |
||
1363 | { |
||
1364 | _assignLabelNames(); |
||
1365 | } |
||
1366 | private void btnReadLabelFile_Click(object sender, EventArgs e) |
||
1367 | { |
||
1368 | _loadLabelFile(); |
||
1369 | } |
||
1370 | private void btnSwitchFC_Click(object sender, EventArgs e) |
||
1371 | { |
||
1372 | _switchToFC(); |
||
1373 | } |
||
1374 | private void btnSwitchNC_Click(object sender, EventArgs e) |
||
1375 | { |
||
1376 | _switchToNC(); |
||
1377 | } |
||
1378 | private void btnReadDbgCont_Click(object sender, EventArgs e) |
||
1379 | { |
||
1380 | _readCont(!bReadContinously); |
||
1381 | } |
||
1382 | private void btnReadBLCtrl_Click(object sender, EventArgs e) |
||
1383 | { |
||
1384 | |||
1385 | if (_iCtrlAct == 2) _readBLCtrl(false); |
||
1386 | else Log(LogMsgType.Warning, "only available when connected to NC"); |
||
1387 | } |
||
1388 | private void btnGetNaviData_Click(object sender, EventArgs e) |
||
1389 | { |
||
1390 | if (_iCtrlAct == 2) _readNavData(false); |
||
1391 | else Log(LogMsgType.Warning, "only available when connected to NC"); |
||
1392 | } |
||
1393 | private void btnConn_Click(object sender, EventArgs e) |
||
1394 | { |
||
1395 | simpleSerialPort.Connect(!simpleSerialPort.Port.IsOpen); |
||
1396 | } |
||
1397 | private void button3_Click(object sender, EventArgs e) |
||
1398 | { |
||
1399 | _getVersion(1); |
||
1400 | } |
||
1401 | private void button4_Click(object sender, EventArgs e) |
||
1402 | { |
||
1403 | _getVersion(2); |
||
1404 | } |
||
1405 | private void btnOSD_Click(object sender, EventArgs e) |
||
1406 | { |
||
1407 | if (iOSDPage > iOSDMax) |
||
1408 | iOSDPage = 0; |
||
1409 | _OSDMenue(iOSDPage); |
||
1410 | } |
||
1411 | private void btnOSDForward_Click(object sender, EventArgs e) |
||
1412 | { |
||
1413 | iOSDPage++; |
||
1414 | if (iOSDPage > iOSDMax) |
||
1415 | iOSDPage = 0; |
||
1416 | |||
1417 | _OSDMenue(iOSDPage); |
||
1418 | } |
||
1419 | private void btnOSDBackward_Click(object sender, EventArgs e) |
||
1420 | { |
||
1421 | iOSDPage--; |
||
1422 | if (iOSDPage < 0) |
||
1423 | iOSDPage = iOSDMax; |
||
1424 | |||
1425 | _OSDMenue(iOSDPage); |
||
1426 | } |
||
1427 | private void btnOSDAuto_Click(object sender, EventArgs e) |
||
1428 | { |
||
1429 | _OSDMenueAutoRefresh(); |
||
1430 | } |
||
1431 | /// call the OSDMenue with Key 0x8 |
||
1432 | private void btnOSDLeave_Click(object sender, EventArgs e) |
||
1433 | { |
||
1434 | _OSDMenueAutoRefresh(8); |
||
1435 | } |
||
1436 | /// call the OSDMenue with Key 0x4 |
||
1437 | private void btnOSDEnter_Click(object sender, EventArgs e) |
||
1438 | { |
||
1439 | _OSDMenueAutoRefresh(4); |
||
1440 | } |
||
1441 | #endregion buttons |
||
2254 | - | 1442 | |
2233 | - | 1443 | } |
1444 | public class IniFile |
||
1445 | { |
||
1446 | public string path; |
||
1447 | |||
1448 | [DllImport("kernel32")] |
||
1449 | private static extern long WritePrivateProfileString(string section, |
||
1450 | string key, string val, string filePath); |
||
1451 | |||
1452 | [DllImport("kernel32.dll", CharSet = CharSet.Auto)] |
||
1453 | static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, |
||
1454 | uint nSize, string lpFileName); |
||
1455 | |||
1456 | [DllImport("kernel32")] |
||
1457 | private static extern int GetPrivateProfileString(string section, |
||
1458 | string key, string def, StringBuilder retVal, |
||
1459 | int size, string filePath); |
||
1460 | |||
1461 | public IniFile(string INIPath) |
||
1462 | { |
||
1463 | path = INIPath; |
||
1464 | } |
||
1465 | |||
1466 | public void IniWriteValue(string Section, string Key, string Value) |
||
1467 | { |
||
1468 | WritePrivateProfileString(Section, Key, Value, this.path); |
||
1469 | } |
||
1470 | |||
1471 | public string IniReadValue(string Section, string Key) |
||
1472 | { |
||
1473 | StringBuilder temp = new StringBuilder(255); |
||
1474 | int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); |
||
1475 | return temp.ToString(); |
||
1476 | } |
||
1477 | //Ini_sections auslesen in String-Array |
||
1478 | public string[] IniSectionNames() |
||
1479 | { |
||
1480 | |||
1481 | // uint MAX_BUFFER = 32767; |
||
1482 | uint MAX_BUFFER = 8388608; |
||
1483 | IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER); |
||
1484 | uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path); |
||
1485 | if (bytesReturned == 0) |
||
1486 | { |
||
1487 | Marshal.FreeCoTaskMem(pReturnedString); |
||
1488 | return null; |
||
1489 | } |
||
1490 | string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); |
||
1491 | Marshal.FreeCoTaskMem(pReturnedString); |
||
1492 | //use of Substring below removes terminating null for split |
||
1493 | return local.Substring(0, local.Length - 1).Split('\0'); |
||
1494 | |||
1495 | |||
1496 | } |
||
1497 | } |
||
1498 | public static class ControlExtensions |
||
1499 | { |
||
1500 | /// <summary> |
||
1501 | /// Execute a threadsafe operation, when accessing a control via another thread |
||
1502 | /// action is a lamdaexpression |
||
1503 | /// e.g. comboBox1.ExecuteThreadSafe(() => comboBox1.Enabled = true); |
||
1504 | /// </summary> |
||
1505 | /// <param name="control"> The control </param> |
||
1506 | /// <param name="action"> The 'action' to perform </param> |
||
1507 | public static void ExecuteThreadSafe(this Control control, Action action) |
||
1508 | { |
||
1509 | if (control.InvokeRequired) |
||
1510 | { |
||
1511 | control.BeginInvoke(action); //"BeginInvoke" is an async call -> threadsafety error when called to many times successively -> then take "Invoke" |
||
1512 | } |
||
1513 | else |
||
1514 | { |
||
1515 | action.Invoke(); |
||
1516 | } |
||
1517 | } |
||
1518 | } |
||
1519 | |||
1520 | } |