Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Data; |
||
5 | using System.Drawing; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Windows.Forms; |
||
9 | using System.Runtime.InteropServices; |
||
10 | using System.IO.Ports; |
||
11 | using System.IO; |
||
12 | using ArdupilotMega; |
||
13 | using System.Xml; |
||
14 | |||
15 | namespace OSD |
||
16 | { |
||
17 | public partial class OSD : Form |
||
18 | { |
||
19 | //max 7456 datasheet pg 10 |
||
20 | //pal = 16r 30 char |
||
21 | //ntsc = 13r 30 char |
||
22 | Size basesize = new Size(30, 16); |
||
23 | /// <summary> |
||
24 | /// the un-scaled font render image |
||
25 | /// </summary> |
||
26 | Bitmap screen = new Bitmap(30 * 12, 16 * 18); |
||
27 | /// <summary> |
||
28 | /// the scaled to size background control |
||
29 | /// </summary> |
||
30 | Bitmap image = new Bitmap(30 * 12, 16 * 18); |
||
31 | /// <summary> |
||
32 | /// Bitmaps of all the chars created from the mcm |
||
33 | /// </summary> |
||
34 | Bitmap[] chars; |
||
35 | /// <summary> |
||
36 | /// record of what panel is using what squares |
||
37 | /// </summary> |
||
38 | string[][] usedPostion = new string[30][]; |
||
39 | /// <summary> |
||
40 | /// used to track currently selected panel across calls |
||
41 | /// </summary> |
||
42 | string currentlyselected = ""; |
||
43 | /// <summary> |
||
44 | /// used to track current processing panel across calls (because i maintained the original code for panel drawing) |
||
45 | /// </summary> |
||
46 | string processingpanel = ""; |
||
47 | /// <summary> |
||
48 | /// use to draw the red outline box is currentlyselected matchs |
||
49 | /// </summary> |
||
50 | bool selectedrectangle = false; |
||
51 | /// <summary> |
||
52 | /// use to as a invalidator |
||
53 | /// </summary> |
||
54 | bool startup = false; |
||
55 | /// <summary> |
||
56 | /// 328 eeprom memory |
||
57 | /// </summary> |
||
58 | byte[] eeprom = new byte[1024]; |
||
59 | /// <summary> |
||
60 | /// background image |
||
61 | /// </summary> |
||
62 | Image bgpicture; |
||
63 | |||
64 | bool mousedown = false; |
||
65 | |||
66 | SerialPort comPort = new SerialPort(); |
||
67 | |||
68 | Panels pan; |
||
69 | |||
70 | Tuple<string, Func<int, int, int>, int, int, int, int, int>[] panelItems = new Tuple<string, Func<int, int, int>, int, int, int, int, int>[30]; |
||
71 | |||
72 | Graphics gr; |
||
73 | |||
74 | // in pixels |
||
75 | int x = 0, y = 0; |
||
76 | |||
77 | public OSD() |
||
78 | { |
||
79 | InitializeComponent(); |
||
80 | |||
81 | // load default font |
||
82 | chars = mcm.readMCM("OSD_SA_v5.mcm"); |
||
83 | // load default bg picture |
||
84 | try |
||
85 | { |
||
86 | bgpicture = Image.FromFile("vlcsnap-2012-01-28-07h46m04s95.png"); |
||
87 | } |
||
88 | catch { } |
||
89 | |||
90 | gr = Graphics.FromImage(screen); |
||
91 | |||
92 | pan = new Panels(this); |
||
93 | |||
94 | // setup all panel options |
||
95 | setupFunctions(); |
||
96 | } |
||
97 | |||
98 | void changeToPal(bool pal) |
||
99 | { |
||
100 | if (pal) |
||
101 | { |
||
102 | basesize = new Size(30, 16); |
||
103 | |||
104 | screen = new Bitmap(30 * 12, 16 * 18); |
||
105 | image = new Bitmap(30 * 12, 16 * 18); |
||
106 | |||
107 | NUM_X.Maximum = 29; |
||
108 | NUM_Y.Maximum = 15; |
||
109 | } |
||
110 | else |
||
111 | { |
||
112 | basesize = new Size(30, 13); |
||
113 | |||
114 | screen = new Bitmap(30 * 12, 13 * 18); |
||
115 | image = new Bitmap(30 * 12, 13 * 18); |
||
116 | |||
117 | NUM_X.Maximum = 29; |
||
118 | NUM_Y.Maximum = 15; |
||
119 | } |
||
120 | |||
121 | |||
122 | } |
||
123 | |||
124 | void setupFunctions() |
||
125 | { |
||
126 | currentlyselected = ""; |
||
127 | processingpanel = ""; |
||
128 | |||
129 | int a = 0; |
||
130 | |||
131 | for (a = 0; a < usedPostion.Length; a++) |
||
132 | { |
||
133 | usedPostion[a] = new string[16]; |
||
134 | } |
||
135 | |||
136 | a = 0; |
||
137 | |||
138 | // first 8 |
||
139 | // Display name,printfunction,X,Y,ENaddress,Xaddress,Yaddress |
||
140 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Center", pan.panCenter, 13, 8, panCenter_en_ADDR, panCenter_x_ADDR, panCenter_y_ADDR); |
||
141 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Pitch", pan.panPitch, 22, 9, panPitch_en_ADDR, panPitch_x_ADDR, panPitch_y_ADDR); |
||
142 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Roll", pan.panRoll, 11, 1, panRoll_en_ADDR, panRoll_x_ADDR, panRoll_y_ADDR); |
||
143 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Battery A", pan.panBatt_A, 21, 1, panBatt_A_en_ADDR, panBatt_A_x_ADDR, panBatt_A_y_ADDR); |
||
144 | //items[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Battery B", pan.panBatt_B, 21, 3, panBatt_B_en_ADDR, panBatt_B_x_ADDR, panBatt_B_y_ADDR); |
||
145 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Visible Sats", pan.panGPSats, 2, 13, panGPSats_en_ADDR, panGPSats_x_ADDR, panGPSats_y_ADDR); |
||
146 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("GPS Lock", pan.panGPL, 5, 13, panGPL_en_ADDR, panGPL_x_ADDR, panGPL_y_ADDR); |
||
147 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("GPS Coord", pan.panGPS, 2, 14, panGPS_en_ADDR, panGPS_x_ADDR, panGPS_y_ADDR); |
||
148 | |||
149 | //second 8 |
||
150 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Heading Rose", pan.panRose, 16, 14, panRose_en_ADDR, panRose_x_ADDR, panRose_y_ADDR); |
||
151 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Heading", pan.panHeading, 24, 13, panHeading_en_ADDR, panHeading_x_ADDR, panHeading_y_ADDR); |
||
152 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Heart Beat", pan.panMavBeat, 2, 9, panMavBeat_en_ADDR, panMavBeat_x_ADDR, panMavBeat_y_ADDR); |
||
153 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Home Direction", pan.panHomeDir, 14, 3, panHomeDir_en_ADDR, panHomeDir_x_ADDR, panHomeDir_y_ADDR); |
||
154 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Home Distance", pan.panHomeDis, 2, 1, panHomeDis_en_ADDR, panHomeDis_x_ADDR, panHomeDis_y_ADDR); |
||
155 | //items[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("WP Dir", pan.panWPDir, 14, 4, panWPDir_en_ADDR, panWPDir_x_ADDR, panWPDir_y_ADDR); |
||
156 | //items[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("WP Dir", pan.panWPDis, 14, 4, panWPDis_en_ADDR, panWPDis_x_ADDR, panWPDis_y_ADDR); |
||
157 | // rssi |
||
158 | |||
159 | // third 8 |
||
160 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Altitude", pan.panAlt, 2, 2, panAlt_en_ADDR, panAlt_x_ADDR, panAlt_y_ADDR); |
||
161 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Velocity", pan.panVel, 2, 3, panVel_en_ADDR, panVel_x_ADDR, panVel_y_ADDR); |
||
162 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Throttle", pan.panThr, 2, 4, panThr_en_ADDR, panThr_x_ADDR, panThr_y_ADDR); |
||
163 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Flight Mode", pan.panFlightMode, 17, 13, panFMod_en_ADDR, panFMod_x_ADDR, panFMod_y_ADDR); |
||
164 | panelItems[a++] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>("Horizon", pan.panHorizon, 8, 7, panHorizon_en_ADDR, panHorizon_x_ADDR, panHorizon_y_ADDR); |
||
165 | |||
166 | LIST_items.Items.Clear(); |
||
167 | |||
168 | startup = true; |
||
169 | |||
170 | foreach (var thing in panelItems) |
||
171 | { |
||
172 | if (thing != null) |
||
173 | { |
||
174 | if (thing.Item1 == "Center") |
||
175 | { |
||
176 | LIST_items.Items.Add(thing.Item1, false); |
||
177 | } |
||
178 | else |
||
179 | { |
||
180 | LIST_items.Items.Add(thing.Item1, true); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | startup = false; |
||
186 | |||
187 | osdDraw(); |
||
188 | } |
||
189 | |||
190 | private string[] GetPortNames() |
||
191 | { |
||
192 | string[] devs = new string[0]; |
||
193 | |||
194 | if (Directory.Exists("/dev/")) |
||
195 | devs = Directory.GetFiles("/dev/", "*ACM*"); |
||
196 | |||
197 | string[] ports = SerialPort.GetPortNames(); |
||
198 | |||
199 | string[] all = new string[devs.Length + ports.Length]; |
||
200 | |||
201 | devs.CopyTo(all, 0); |
||
202 | ports.CopyTo(all, devs.Length); |
||
203 | |||
204 | return all; |
||
205 | } |
||
206 | |||
207 | public void setPanel(int x, int y) |
||
208 | { |
||
209 | this.x = x * 12; |
||
210 | this.y = y * 18; |
||
211 | } |
||
212 | |||
213 | public void openPanel() |
||
214 | { |
||
215 | d = 0; |
||
216 | r = 0; |
||
217 | } |
||
218 | |||
219 | public void openSingle(int x, int y) |
||
220 | { |
||
221 | setPanel(x, y); |
||
222 | openPanel(); |
||
223 | } |
||
224 | |||
225 | public int getCenter() |
||
226 | { |
||
227 | if (CHK_pal.Checked) |
||
228 | return 8; |
||
229 | return 6; |
||
230 | } |
||
231 | |||
232 | // used for printf tracking line and row |
||
233 | int d = 0, r = 0; |
||
234 | |||
235 | public void printf(string format, params object[] args) |
||
236 | { |
||
237 | StringBuilder sb = new StringBuilder(); |
||
238 | |||
239 | sb = new StringBuilder(AT.MIN.Tools.sprintf(format, args)); |
||
240 | |||
241 | //sprintf(sb, format, __arglist(args)); |
||
242 | |||
243 | //Console.WriteLine(sb.ToString()); |
||
244 | |||
245 | foreach (char ch in sb.ToString().ToCharArray()) |
||
246 | { |
||
247 | if (ch == '|') |
||
248 | { |
||
249 | d += 1; |
||
250 | r = 0; |
||
251 | continue; |
||
252 | } |
||
253 | |||
254 | try |
||
255 | { |
||
256 | // draw red boxs |
||
257 | if (selectedrectangle) |
||
258 | { |
||
259 | gr.DrawRectangle(Pens.Red, (this.x + r * 12) % screen.Width, (this.y + d * 18), 12, 18); |
||
260 | } |
||
261 | |||
262 | int w1 = (this.x / 12 + r) % basesize.Width; |
||
263 | int h1 = (this.y / 18 + d); |
||
264 | |||
265 | if (w1 < basesize.Width && h1 < basesize.Height) |
||
266 | { |
||
267 | // check if this box has bene used |
||
268 | if (usedPostion[w1][h1] != null) |
||
269 | { |
||
270 | //System.Diagnostics.Debug.WriteLine("'" + used[this.x / 12 + r * 12 / 12][this.y / 18 + d * 18 / 18] + "'"); |
||
271 | } |
||
272 | else |
||
273 | { |
||
274 | gr.DrawImage(chars[ch], (this.x + r * 12) % screen.Width, (this.y + d * 18), 12, 18); |
||
275 | } |
||
276 | |||
277 | usedPostion[w1][h1] = processingpanel; |
||
278 | } |
||
279 | } |
||
280 | catch { System.Diagnostics.Debug.WriteLine("printf exception"); } |
||
281 | r++; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | string getMouseOverItem(int x, int y) |
||
286 | { |
||
287 | int ansW,ansH; |
||
288 | |||
289 | getCharLoc(x, y, out ansW, out ansH); |
||
290 | |||
291 | if (usedPostion[ansW][ansH] != null && usedPostion[ansW][ansH] != "") |
||
292 | { |
||
293 | LIST_items.SelectedIndex = LIST_items.Items.IndexOf(usedPostion[ansW][ansH]); |
||
294 | return usedPostion[ansW][ansH]; |
||
295 | } |
||
296 | |||
297 | return ""; |
||
298 | } |
||
299 | |||
300 | void getCharLoc(int x, int y,out int xpos, out int ypos) |
||
301 | { |
||
302 | |||
303 | x = Constrain(x, 0, pictureBox1.Width - 1); |
||
304 | y = Constrain(y, 0, pictureBox1.Height - 1); |
||
305 | |||
306 | float scaleW = pictureBox1.Width / (float)screen.Width; |
||
307 | float scaleH = pictureBox1.Height / (float)screen.Height; |
||
308 | |||
309 | int ansW = (int)((x / scaleW / 12) % 30); |
||
310 | int ansH = 0; |
||
311 | if (CHK_pal.Checked) |
||
312 | { |
||
313 | ansH = (int)((y / scaleH / 18) % 16); |
||
314 | } |
||
315 | else |
||
316 | { |
||
317 | ansH = (int)((y / scaleH / 18) % 13); |
||
318 | } |
||
319 | |||
320 | xpos = Constrain(ansW,0,30 -1); |
||
321 | ypos = Constrain(ansH,0,16 - 1); |
||
322 | } |
||
323 | |||
324 | public void printf_P(string format, params object[] args) |
||
325 | { |
||
326 | printf(format, args); |
||
327 | } |
||
328 | |||
329 | public void closePanel() |
||
330 | { |
||
331 | x = 0; |
||
332 | y = 0; |
||
333 | } |
||
334 | |||
335 | void osdDraw() |
||
336 | { |
||
337 | if (startup) |
||
338 | return; |
||
339 | |||
340 | for (int b = 0; b < usedPostion.Length; b++) |
||
341 | { |
||
342 | usedPostion[b] = new string[16]; |
||
343 | } |
||
344 | |||
345 | image = new Bitmap(pictureBox1.Width, pictureBox1.Height); |
||
346 | |||
347 | float scaleW = pictureBox1.Width / (float)screen.Width; |
||
348 | float scaleH = pictureBox1.Height / (float)screen.Height; |
||
349 | |||
350 | screen = new Bitmap(screen.Width, screen.Height); |
||
351 | |||
352 | gr = Graphics.FromImage(screen); |
||
353 | |||
354 | image = new Bitmap(image.Width, image.Height); |
||
355 | |||
356 | Graphics grfull = Graphics.FromImage(image); |
||
357 | |||
358 | try |
||
359 | { |
||
360 | grfull.DrawImage(bgpicture, 0, 0, pictureBox1.Width, pictureBox1.Height); |
||
361 | } |
||
362 | catch { } |
||
363 | |||
364 | if (checkBox1.Checked) |
||
365 | { |
||
366 | for (int b = 1; b < 16; b++) |
||
367 | { |
||
368 | for (int a = 1; a < 30; a++) |
||
369 | { |
||
370 | grfull.DrawLine(new Pen(Color.Gray, 1), a * 12 * scaleW, 0, a * 12 * scaleW, pictureBox1.Height); |
||
371 | grfull.DrawLine(new Pen(Color.Gray, 1), 0, b * 18 * scaleH, pictureBox1.Width, b * 18 * scaleH); |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | |||
376 | pan.setHeadingPatern(); |
||
377 | pan.setBatteryPic(); |
||
378 | |||
379 | List<string> list = new List<string>(); |
||
380 | |||
381 | foreach (string it in LIST_items.CheckedItems) |
||
382 | { |
||
383 | list.Add(it); |
||
384 | } |
||
385 | |||
386 | list.Reverse(); |
||
387 | |||
388 | foreach (string it in list) |
||
389 | { |
||
390 | foreach (var thing in panelItems) |
||
391 | { |
||
392 | selectedrectangle = false; |
||
393 | if (thing != null) |
||
394 | { |
||
395 | if (thing.Item1 == it) |
||
396 | { |
||
397 | if (thing.Item1 == currentlyselected) |
||
398 | { |
||
399 | selectedrectangle = true; |
||
400 | } |
||
401 | |||
402 | processingpanel = thing.Item1; |
||
403 | |||
404 | // ntsc and below the middle line |
||
405 | if (thing.Item4 >= getCenter() && !CHK_pal.Checked) |
||
406 | { |
||
407 | thing.Item2(thing.Item3, thing.Item4 - 3); |
||
408 | } |
||
409 | else // pal and no change |
||
410 | { |
||
411 | thing.Item2(thing.Item3, thing.Item4); |
||
412 | } |
||
413 | |||
414 | } |
||
415 | } |
||
416 | } |
||
417 | } |
||
418 | |||
419 | grfull.DrawImage(screen, 0, 0, image.Width, image.Height); |
||
420 | |||
421 | pictureBox1.Image = image; |
||
422 | } |
||
423 | |||
424 | int Constrain(double value, double min, double max) |
||
425 | { |
||
426 | if (value < min) |
||
427 | return (int)min; |
||
428 | if (value > max) |
||
429 | return (int)max; |
||
430 | |||
431 | return (int)value; |
||
432 | } |
||
433 | |||
434 | private void OSD_Load(object sender, EventArgs e) |
||
435 | { |
||
436 | |||
437 | string strVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); |
||
438 | this.Text = this.Text + " " + strVersion; |
||
439 | |||
440 | CMB_ComPort.Items.AddRange(GetPortNames()); |
||
441 | |||
442 | if (CMB_ComPort.Items.Count > 0) |
||
443 | CMB_ComPort.SelectedIndex = 0; |
||
444 | |||
445 | xmlconfig(false); |
||
446 | |||
447 | osdDraw(); |
||
448 | } |
||
449 | |||
450 | private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) |
||
451 | { |
||
452 | string item = ((CheckedListBox)sender).SelectedItem.ToString(); |
||
453 | |||
454 | currentlyselected = item; |
||
455 | |||
456 | osdDraw(); |
||
457 | |||
458 | foreach (var thing in panelItems) |
||
459 | { |
||
460 | if (thing != null && thing.Item1 == item) |
||
461 | { |
||
462 | NUM_X.Value = Constrain(thing.Item3,0,basesize.Width -1); |
||
463 | NUM_Y.Value = Constrain(thing.Item4,0,16 -1); |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 | |||
468 | private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e) |
||
469 | { |
||
470 | if (((CheckedListBox)sender).Text == "Horizon") |
||
471 | { |
||
472 | //groupBox1.Enabled = false; |
||
473 | } |
||
474 | else |
||
475 | { |
||
476 | //groupBox1.Enabled = true; |
||
477 | } |
||
478 | } |
||
479 | |||
480 | private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) |
||
481 | { |
||
482 | // if (((CheckedListBox)sender).SelectedItem != null && ((CheckedListBox)sender).SelectedItem.ToString() == "Horizon") |
||
483 | if (((CheckedListBox)sender).SelectedItem != null) |
||
484 | { |
||
485 | if (((CheckedListBox)sender).SelectedItem.ToString() == "Horizon" && e.NewValue == CheckState.Checked) |
||
486 | { |
||
487 | int index = LIST_items.Items.IndexOf("Center"); |
||
488 | LIST_items.SetItemChecked(index, false); |
||
489 | } |
||
490 | else if (((CheckedListBox)sender).SelectedItem.ToString() == "Center" && e.NewValue == CheckState.Checked) |
||
491 | { |
||
492 | int index = LIST_items.Items.IndexOf("Horizon"); |
||
493 | LIST_items.SetItemChecked(index, false); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | // add a delay to this so it runs after the control value has been defined. |
||
498 | if (this.IsHandleCreated) |
||
499 | this.BeginInvoke((MethodInvoker)delegate { osdDraw(); }); |
||
500 | } |
||
501 | |||
502 | private void numericUpDown1_ValueChanged(object sender, EventArgs e) |
||
503 | { |
||
504 | string item; |
||
505 | try |
||
506 | { |
||
507 | item = LIST_items.SelectedItem.ToString(); |
||
508 | } |
||
509 | catch { return; } |
||
510 | |||
511 | for (int a = 0; a < panelItems.Length; a++) |
||
512 | { |
||
513 | if (panelItems[a] != null && panelItems[a].Item1 == item) |
||
514 | { |
||
515 | panelItems[a] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>(panelItems[a].Item1, panelItems[a].Item2, (int)NUM_X.Value, panelItems[a].Item4, panelItems[a].Item5, panelItems[a].Item6, panelItems[a].Item7); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | osdDraw(); |
||
520 | } |
||
521 | |||
522 | private void numericUpDown2_ValueChanged(object sender, EventArgs e) |
||
523 | { |
||
524 | string item; |
||
525 | try |
||
526 | { |
||
527 | item = LIST_items.SelectedItem.ToString(); |
||
528 | } |
||
529 | catch { return; } |
||
530 | |||
531 | for (int a = 0; a < panelItems.Length; a++) |
||
532 | { |
||
533 | if (panelItems[a] != null && panelItems[a].Item1 == item) |
||
534 | { |
||
535 | panelItems[a] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>(panelItems[a].Item1, panelItems[a].Item2, panelItems[a].Item3, (int)NUM_Y.Value, panelItems[a].Item5, panelItems[a].Item6, panelItems[a].Item7); |
||
536 | |||
537 | } |
||
538 | } |
||
539 | |||
540 | osdDraw(); |
||
541 | } |
||
542 | |||
543 | private void BUT_WriteOSD_Click(object sender, EventArgs e) |
||
544 | { |
||
545 | toolStripProgressBar1.Style = ProgressBarStyle.Continuous; |
||
546 | this.toolStripStatusLabel1.Text = ""; |
||
547 | |||
548 | foreach (string str in this.LIST_items.Items) |
||
549 | { |
||
550 | foreach (var tuple in this.panelItems) |
||
551 | { |
||
552 | if ((tuple != null) && ((tuple.Item1 == str)) && tuple.Item5 != -1) |
||
553 | { |
||
554 | eeprom[tuple.Item5] = (byte)(this.LIST_items.CheckedItems.Contains(str) ? 1 : 0); |
||
555 | eeprom[tuple.Item6] = (byte)tuple.Item3; // x |
||
556 | eeprom[tuple.Item7] = (byte)tuple.Item4; // y |
||
557 | |||
558 | Console.WriteLine(str); |
||
559 | } |
||
560 | } |
||
561 | } |
||
562 | |||
563 | ArduinoSTK sp; |
||
564 | |||
565 | try |
||
566 | { |
||
567 | if (comPort.IsOpen) |
||
568 | comPort.Close(); |
||
569 | |||
570 | sp = new ArduinoSTK(); |
||
571 | sp.PortName = CMB_ComPort.Text; |
||
572 | sp.BaudRate = 57600; |
||
573 | sp.DtrEnable = true; |
||
574 | |||
575 | sp.Open(); |
||
576 | } |
||
577 | catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } |
||
578 | |||
579 | if (sp.connectAP()) |
||
580 | { |
||
581 | try |
||
582 | { |
||
583 | if (sp.upload(eeprom, 0, 200, 0)) |
||
584 | { |
||
585 | MessageBox.Show("Done!"); |
||
586 | } |
||
587 | else |
||
588 | { |
||
589 | MessageBox.Show("Failed to upload new settings"); |
||
590 | } |
||
591 | } |
||
592 | catch (Exception ex) { |
||
593 | MessageBox.Show(ex.Message); |
||
594 | } |
||
595 | } |
||
596 | else |
||
597 | { |
||
598 | MessageBox.Show("Failed to talk to bootloader"); |
||
599 | } |
||
600 | |||
601 | sp.Close(); |
||
602 | } |
||
603 | |||
604 | private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) |
||
605 | { |
||
606 | |||
607 | } |
||
608 | |||
609 | private void comboBox1_Click(object sender, EventArgs e) |
||
610 | { |
||
611 | CMB_ComPort.Items.Clear(); |
||
612 | CMB_ComPort.Items.AddRange(GetPortNames()); |
||
613 | } |
||
614 | |||
615 | |||
616 | |||
617 | /* *********************************************** */ |
||
618 | // EEPROM Storage addresses |
||
619 | |||
620 | // First of 8 panels |
||
621 | const int panCenter_en_ADDR = 0; |
||
622 | const int panCenter_x_ADDR = 2; |
||
623 | const int panCenter_y_ADDR = 4; |
||
624 | const int panPitch_en_ADDR = 6; |
||
625 | const int panPitch_x_ADDR = 8; |
||
626 | const int panPitch_y_ADDR = 10; |
||
627 | const int panRoll_en_ADDR = 12; |
||
628 | const int panRoll_x_ADDR = 14; |
||
629 | const int panRoll_y_ADDR = 16; |
||
630 | const int panBatt_A_en_ADDR = 18; |
||
631 | const int panBatt_A_x_ADDR = 20; |
||
632 | const int panBatt_A_y_ADDR = 22; |
||
633 | const int panBatt_B_en_ADDR = 24; |
||
634 | const int panBatt_B_x_ADDR = 26; |
||
635 | const int panBatt_B_y_ADDR = 28; |
||
636 | const int panGPSats_en_ADDR = 30; |
||
637 | const int panGPSats_x_ADDR = 32; |
||
638 | const int panGPSats_y_ADDR = 34; |
||
639 | const int panGPL_en_ADDR = 36; |
||
640 | const int panGPL_x_ADDR = 38; |
||
641 | const int panGPL_y_ADDR = 40; |
||
642 | const int panGPS_en_ADDR = 42; |
||
643 | const int panGPS_x_ADDR = 44; |
||
644 | const int panGPS_y_ADDR = 46; |
||
645 | |||
646 | // Second set of 8 panels |
||
647 | const int panRose_en_ADDR = 48; |
||
648 | const int panRose_x_ADDR = 50; |
||
649 | const int panRose_y_ADDR = 52; |
||
650 | const int panHeading_en_ADDR = 54; |
||
651 | const int panHeading_x_ADDR = 56; |
||
652 | const int panHeading_y_ADDR = 58; |
||
653 | const int panMavBeat_en_ADDR = 60; |
||
654 | const int panMavBeat_x_ADDR = 62; |
||
655 | const int panMavBeat_y_ADDR = 64; |
||
656 | const int panHomeDir_en_ADDR = 66; |
||
657 | const int panHomeDir_x_ADDR = 68; |
||
658 | const int panHomeDir_y_ADDR = 70; |
||
659 | const int panHomeDis_en_ADDR = 72; |
||
660 | const int panHomeDis_x_ADDR = 74; |
||
661 | const int panHomeDis_y_ADDR = 76; |
||
662 | const int panWPDir_en_ADDR = 80; |
||
663 | const int panWPDir_x_ADDR = 82; |
||
664 | const int panWPDir_y_ADDR = 84; |
||
665 | const int panWPDis_en_ADDR = 86; |
||
666 | const int panWPDis_x_ADDR = 88; |
||
667 | const int panWPDis_y_ADDR = 90; |
||
668 | const int panRSSI_en_ADDR = 92; |
||
669 | const int panRSSI_x_ADDR = 94; |
||
670 | const int panRSSI_y_ADDR = 96; |
||
671 | |||
672 | |||
673 | // Third set of 8 panels |
||
674 | const int panCurA_en_ADDR = 98; |
||
675 | const int panCurA_x_ADDR = 100; |
||
676 | const int panCurA_y_ADDR = 102; |
||
677 | const int panCurB_en_ADDR = 104; |
||
678 | const int panCurB_x_ADDR = 106; |
||
679 | const int panCurB_y_ADDR = 108; |
||
680 | const int panAlt_en_ADDR = 110; |
||
681 | const int panAlt_x_ADDR = 112; |
||
682 | const int panAlt_y_ADDR = 114; |
||
683 | const int panVel_en_ADDR = 116; |
||
684 | const int panVel_x_ADDR = 118; |
||
685 | const int panVel_y_ADDR = 120; |
||
686 | const int panThr_en_ADDR = 122; |
||
687 | const int panThr_x_ADDR = 124; |
||
688 | const int panThr_y_ADDR = 126; |
||
689 | const int panFMod_en_ADDR = 128; |
||
690 | const int panFMod_x_ADDR = 130; |
||
691 | const int panFMod_y_ADDR = 132; |
||
692 | const int panHorizon_en_ADDR = 134; |
||
693 | const int panHorizon_x_ADDR = 136; |
||
694 | const int panHorizon_y_ADDR = 138; |
||
695 | |||
696 | const int CHK1 = 1000; |
||
697 | const int CHK2 = 1006; |
||
698 | |||
699 | private void checkBox1_CheckedChanged(object sender, EventArgs e) |
||
700 | { |
||
701 | osdDraw(); |
||
702 | } |
||
703 | |||
704 | private void OSD_Resize(object sender, EventArgs e) |
||
705 | { |
||
706 | try |
||
707 | { |
||
708 | osdDraw(); |
||
709 | } |
||
710 | catch { } |
||
711 | } |
||
712 | |||
713 | private void BUT_ReadOSD_Click(object sender, EventArgs e) |
||
714 | { |
||
715 | toolStripProgressBar1.Style = ProgressBarStyle.Continuous; |
||
716 | this.toolStripStatusLabel1.Text = ""; |
||
717 | |||
718 | bool fail = false; |
||
719 | ArduinoSTK sp; |
||
720 | |||
721 | try |
||
722 | { |
||
723 | if (comPort.IsOpen) |
||
724 | comPort.Close(); |
||
725 | |||
726 | sp = new ArduinoSTK(); |
||
727 | sp.PortName = CMB_ComPort.Text; |
||
728 | sp.BaudRate = 57600; |
||
729 | sp.DtrEnable = true; |
||
730 | |||
731 | sp.Open(); |
||
732 | } |
||
733 | catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } |
||
734 | |||
735 | if (sp.connectAP()) |
||
736 | { |
||
737 | try |
||
738 | { |
||
739 | eeprom = sp.download(1024); |
||
740 | } |
||
741 | catch (Exception ex) { |
||
742 | fail = true; |
||
743 | MessageBox.Show(ex.Message); |
||
744 | } |
||
745 | } |
||
746 | else |
||
747 | { |
||
748 | MessageBox.Show("Failed to talk to bootloader"); |
||
749 | fail = true; |
||
750 | } |
||
751 | |||
752 | sp.Close(); |
||
753 | |||
754 | if (!fail) |
||
755 | { |
||
756 | |||
757 | for (int a = 0; a < panelItems.Length; a++) |
||
758 | { |
||
759 | if (panelItems[a] != null) |
||
760 | { |
||
761 | if (panelItems[a].Item5 >= 0) |
||
762 | LIST_items.SetItemCheckState(a, eeprom[panelItems[a].Item5] == 0 ? CheckState.Unchecked : CheckState.Checked); |
||
763 | |||
764 | if (panelItems[a].Item7 >= 0 || panelItems[a].Item6 >= 0) |
||
765 | panelItems[a] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>(panelItems[a].Item1, panelItems[a].Item2, eeprom[panelItems[a].Item6], eeprom[panelItems[a].Item7], panelItems[a].Item5, panelItems[a].Item6, panelItems[a].Item7); |
||
766 | } |
||
767 | } |
||
768 | } |
||
769 | |||
770 | osdDraw(); |
||
771 | |||
772 | if (!fail) |
||
773 | MessageBox.Show("Done!"); |
||
774 | } |
||
775 | |||
776 | |||
777 | byte[] readIntelHEXv2(StreamReader sr) |
||
778 | { |
||
779 | byte[] FLASH = new byte[1024 * 1024]; |
||
780 | |||
781 | int optionoffset = 0; |
||
782 | int total = 0; |
||
783 | bool hitend = false; |
||
784 | |||
785 | while (!sr.EndOfStream) |
||
786 | { |
||
787 | toolStripProgressBar1.Value = (int)(((float)sr.BaseStream.Position / (float)sr.BaseStream.Length) * 100); |
||
788 | |||
789 | string line = sr.ReadLine(); |
||
790 | |||
791 | if (line.StartsWith(":")) |
||
792 | { |
||
793 | int length = Convert.ToInt32(line.Substring(1, 2), 16); |
||
794 | int address = Convert.ToInt32(line.Substring(3, 4), 16); |
||
795 | int option = Convert.ToInt32(line.Substring(7, 2), 16); |
||
796 | Console.WriteLine("len {0} add {1} opt {2}", length, address, option); |
||
797 | |||
798 | if (option == 0) |
||
799 | { |
||
800 | string data = line.Substring(9, length * 2); |
||
801 | for (int i = 0; i < length; i++) |
||
802 | { |
||
803 | byte byte1 = Convert.ToByte(data.Substring(i * 2, 2), 16); |
||
804 | FLASH[optionoffset + address] = byte1; |
||
805 | address++; |
||
806 | if ((optionoffset + address) > total) |
||
807 | total = optionoffset + address; |
||
808 | } |
||
809 | } |
||
810 | else if (option == 2) |
||
811 | { |
||
812 | optionoffset = (int)Convert.ToUInt16(line.Substring(9, 4), 16) << 4; |
||
813 | } |
||
814 | else if (option == 1) |
||
815 | { |
||
816 | hitend = true; |
||
817 | } |
||
818 | int checksum = Convert.ToInt32(line.Substring(line.Length - 2, 2), 16); |
||
819 | |||
820 | byte checksumact = 0; |
||
821 | for (int z = 0; z < ((line.Length - 1 - 2) / 2); z++) // minus 1 for : then mins 2 for checksum itself |
||
822 | { |
||
823 | checksumact += Convert.ToByte(line.Substring(z * 2 + 1, 2), 16); |
||
824 | } |
||
825 | checksumact = (byte)(0x100 - checksumact); |
||
826 | |||
827 | if (checksumact != checksum) |
||
828 | { |
||
829 | MessageBox.Show("The hex file loaded is invalid, please try again."); |
||
830 | throw new Exception("Checksum Failed - Invalid Hex"); |
||
831 | } |
||
832 | } |
||
833 | //Regex regex = new Regex(@"^:(..)(....)(..)(.*)(..)$"); // length - address - option - data - checksum |
||
834 | } |
||
835 | |||
836 | if (!hitend) |
||
837 | { |
||
838 | MessageBox.Show("The hex file did no contain an end flag. aborting"); |
||
839 | throw new Exception("No end flag in file"); |
||
840 | } |
||
841 | |||
842 | Array.Resize<byte>(ref FLASH, total); |
||
843 | |||
844 | return FLASH; |
||
845 | } |
||
846 | |||
847 | void sp_Progress(int progress) |
||
848 | { |
||
849 | toolStripStatusLabel1.Text = "Uploading " + progress + " %"; |
||
850 | toolStripProgressBar1.Value = progress; |
||
851 | |||
852 | statusStrip1.Refresh(); |
||
853 | } |
||
854 | |||
855 | private void CHK_pal_CheckedChanged(object sender, EventArgs e) |
||
856 | { |
||
857 | changeToPal(CHK_pal.Checked); |
||
858 | |||
859 | osdDraw(); |
||
860 | } |
||
861 | |||
862 | private void pALToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) |
||
863 | { |
||
864 | nTSCToolStripMenuItem.Checked = !CHK_pal.Checked; |
||
865 | } |
||
866 | |||
867 | private void nTSCToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) |
||
868 | { |
||
869 | CHK_pal.Checked = !nTSCToolStripMenuItem.Checked; |
||
870 | } |
||
871 | |||
872 | private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e) |
||
873 | { |
||
874 | SaveFileDialog sfd = new SaveFileDialog() { Filter = "*.osd|*.osd" }; |
||
875 | |||
876 | sfd.ShowDialog(); |
||
877 | |||
878 | if (sfd.FileName != "") |
||
879 | { |
||
880 | try |
||
881 | { |
||
882 | using (StreamWriter sw = new StreamWriter(sfd.OpenFile())) |
||
883 | { |
||
884 | |||
885 | foreach (var item in panelItems) |
||
886 | { |
||
887 | if (item != null) |
||
888 | sw.WriteLine("{0}\t{1}\t{2}\t{3}", item.Item1, item.Item3, item.Item4, LIST_items.GetItemChecked(LIST_items.Items.IndexOf(item.Item1)).ToString()); |
||
889 | } |
||
890 | sw.Close(); |
||
891 | } |
||
892 | } |
||
893 | catch |
||
894 | { |
||
895 | MessageBox.Show("Error writing file"); |
||
896 | } |
||
897 | } |
||
898 | } |
||
899 | |||
900 | private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) |
||
901 | { |
||
902 | OpenFileDialog ofd = new OpenFileDialog() { Filter = "*.osd|*.osd" }; |
||
903 | |||
904 | ofd.ShowDialog(); |
||
905 | |||
906 | if (ofd.FileName != "") |
||
907 | { |
||
908 | try |
||
909 | { |
||
910 | using (StreamReader sr = new StreamReader(ofd.OpenFile())) |
||
911 | { |
||
912 | while (!sr.EndOfStream) |
||
913 | { |
||
914 | string[] strings = sr.ReadLine().Split(new char[] {'\t'},StringSplitOptions.RemoveEmptyEntries); |
||
915 | |||
916 | for (int a = 0; a < panelItems.Length; a++) |
||
917 | { |
||
918 | if (panelItems[a] != null && panelItems[a].Item1 == strings[0]) |
||
919 | { |
||
920 | // incase there is an invalid line number or to shore |
||
921 | try |
||
922 | { |
||
923 | panelItems[a] = new Tuple<string, Func<int, int, int>, int, int, int, int, int>(panelItems[a].Item1, panelItems[a].Item2, int.Parse(strings[1]), int.Parse(strings[2]), panelItems[a].Item5, panelItems[a].Item6, panelItems[a].Item7); |
||
924 | |||
925 | LIST_items.SetItemChecked(a, strings[3] == "True"); |
||
926 | } |
||
927 | catch { } |
||
928 | } |
||
929 | } |
||
930 | } |
||
931 | } |
||
932 | } |
||
933 | catch |
||
934 | { |
||
935 | MessageBox.Show("Error Reading file"); |
||
936 | } |
||
937 | } |
||
938 | |||
939 | osdDraw(); |
||
940 | } |
||
941 | |||
942 | private void loadDefaultsToolStripMenuItem_Click(object sender, EventArgs e) |
||
943 | { |
||
944 | setupFunctions(); |
||
945 | } |
||
946 | |||
947 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) |
||
948 | { |
||
949 | Application.Exit(); |
||
950 | } |
||
951 | |||
952 | private void pictureBox1_MouseUp(object sender, MouseEventArgs e) |
||
953 | { |
||
954 | getMouseOverItem(e.X, e.Y); |
||
955 | |||
956 | mousedown = false; |
||
957 | } |
||
958 | |||
959 | private void pictureBox1_MouseMove(object sender, MouseEventArgs e) |
||
960 | { |
||
961 | if (e.Button == System.Windows.Forms.MouseButtons.Left && mousedown == true) |
||
962 | { |
||
963 | int ansW, ansH; |
||
964 | getCharLoc(e.X, e.Y, out ansW, out ansH); |
||
965 | if (ansH >= getCenter() && !CHK_pal.Checked) |
||
966 | { |
||
967 | ansH += 3; |
||
968 | } |
||
969 | |||
970 | NUM_X.Value = Constrain(ansW, 0, basesize.Width - 1); |
||
971 | NUM_Y.Value = Constrain(ansH, 0, 16 - 1); |
||
972 | |||
973 | pictureBox1.Focus(); |
||
974 | } |
||
975 | else |
||
976 | { |
||
977 | mousedown = false; |
||
978 | } |
||
979 | } |
||
980 | |||
981 | private void pictureBox1_MouseDown(object sender, MouseEventArgs e) |
||
982 | { |
||
983 | currentlyselected = getMouseOverItem(e.X, e.Y); |
||
984 | |||
985 | mousedown = true; |
||
986 | } |
||
987 | |||
988 | private void updateFirmwareToolStripMenuItem_Click(object sender, EventArgs e) |
||
989 | { |
||
990 | toolStripProgressBar1.Style = ProgressBarStyle.Continuous; |
||
991 | this.toolStripStatusLabel1.Text = ""; |
||
992 | |||
993 | OpenFileDialog ofd = new OpenFileDialog(); |
||
994 | ofd.Filter = "*.hex|*.hex"; |
||
995 | |||
996 | ofd.ShowDialog(); |
||
997 | |||
998 | if (ofd.FileName != "") |
||
999 | { |
||
1000 | byte[] FLASH; |
||
1001 | try |
||
1002 | { |
||
1003 | toolStripStatusLabel1.Text = "Reading Hex File"; |
||
1004 | |||
1005 | statusStrip1.Refresh(); |
||
1006 | |||
1007 | FLASH = readIntelHEXv2(new StreamReader(ofd.FileName)); |
||
1008 | } |
||
1009 | catch { MessageBox.Show("Bad Hex File"); return; } |
||
1010 | |||
1011 | bool fail = false; |
||
1012 | ArduinoSTK sp; |
||
1013 | |||
1014 | try |
||
1015 | { |
||
1016 | if (comPort.IsOpen) |
||
1017 | comPort.Close(); |
||
1018 | |||
1019 | sp = new ArduinoSTK(); |
||
1020 | sp.PortName = CMB_ComPort.Text; |
||
1021 | sp.BaudRate = 57600; |
||
1022 | sp.DtrEnable = true; |
||
1023 | |||
1024 | sp.Open(); |
||
1025 | } |
||
1026 | catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } |
||
1027 | |||
1028 | toolStripStatusLabel1.Text = "Connecting to Board"; |
||
1029 | |||
1030 | if (sp.connectAP()) |
||
1031 | { |
||
1032 | sp.Progress += new ArduinoSTK.ProgressEventHandler(sp_Progress); |
||
1033 | try |
||
1034 | { |
||
1035 | if (!sp.uploadflash(FLASH, 0, FLASH.Length, 0)) |
||
1036 | { |
||
1037 | if (sp.IsOpen) |
||
1038 | sp.Close(); |
||
1039 | |||
1040 | MessageBox.Show("Upload failed. Lost sync. Try using Arduino to upload instead", |
||
1041 | "Error", |
||
1042 | MessageBoxButtons.OK, |
||
1043 | MessageBoxIcon.Warning); |
||
1044 | } |
||
1045 | } |
||
1046 | catch (Exception ex) |
||
1047 | { |
||
1048 | fail = true; |
||
1049 | MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
1050 | } |
||
1051 | |||
1052 | } |
||
1053 | else |
||
1054 | { |
||
1055 | MessageBox.Show("Failed to talk to bootloader"); |
||
1056 | } |
||
1057 | |||
1058 | sp.Close(); |
||
1059 | |||
1060 | if (!fail) |
||
1061 | { |
||
1062 | |||
1063 | toolStripStatusLabel1.Text = "Done"; |
||
1064 | |||
1065 | MessageBox.Show("Done!"); |
||
1066 | } |
||
1067 | else |
||
1068 | { |
||
1069 | toolStripStatusLabel1.Text = "Failed"; |
||
1070 | } |
||
1071 | } |
||
1072 | } |
||
1073 | |||
1074 | private void customBGPictureToolStripMenuItem_Click(object sender, EventArgs e) |
||
1075 | { |
||
1076 | OpenFileDialog ofd = new OpenFileDialog(); |
||
1077 | ofd.Filter = "jpg or bmp|*.jpg;*.bmp"; |
||
1078 | |||
1079 | ofd.ShowDialog(); |
||
1080 | |||
1081 | if (ofd.FileName != "") |
||
1082 | { |
||
1083 | try |
||
1084 | { |
||
1085 | bgpicture = Image.FromFile(ofd.FileName); |
||
1086 | |||
1087 | } |
||
1088 | catch { MessageBox.Show("Bad Image"); } |
||
1089 | |||
1090 | osdDraw(); |
||
1091 | } |
||
1092 | } |
||
1093 | |||
1094 | private void sendTLogToolStripMenuItem_Click(object sender, EventArgs e) |
||
1095 | { |
||
1096 | |||
1097 | OpenFileDialog ofd = new OpenFileDialog(); |
||
1098 | ofd.Filter = "Tlog|*.tlog"; |
||
1099 | |||
1100 | ofd.ShowDialog(); |
||
1101 | |||
1102 | if (ofd.FileName != "") |
||
1103 | { |
||
1104 | if (comPort.IsOpen) |
||
1105 | comPort.Close(); |
||
1106 | |||
1107 | try |
||
1108 | { |
||
1109 | |||
1110 | comPort.PortName = CMB_ComPort.Text; |
||
1111 | comPort.BaudRate = 57600; |
||
1112 | comPort.Open(); |
||
1113 | |||
1114 | } |
||
1115 | catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } |
||
1116 | |||
1117 | BinaryReader br = new BinaryReader(ofd.OpenFile()); |
||
1118 | |||
1119 | this.toolStripProgressBar1.Style = ProgressBarStyle.Marquee; |
||
1120 | this.toolStripStatusLabel1.Text = "Sending TLOG data..."; |
||
1121 | |||
1122 | while (br.BaseStream.Position < br.BaseStream.Length && !this.IsDisposed) |
||
1123 | { |
||
1124 | try |
||
1125 | { |
||
1126 | byte[] bytes = br.ReadBytes(20); |
||
1127 | |||
1128 | comPort.Write(bytes, 0, bytes.Length); |
||
1129 | |||
1130 | System.Threading.Thread.Sleep(5); |
||
1131 | |||
1132 | Console.Write(comPort.ReadExisting()); |
||
1133 | |||
1134 | } |
||
1135 | catch { break; } |
||
1136 | |||
1137 | Application.DoEvents(); |
||
1138 | } |
||
1139 | |||
1140 | try |
||
1141 | { |
||
1142 | toolStripProgressBar1.Style = ProgressBarStyle.Continuous; |
||
1143 | toolStripStatusLabel1.Text = ""; |
||
1144 | |||
1145 | comPort.Close(); |
||
1146 | } |
||
1147 | catch { } |
||
1148 | } |
||
1149 | } |
||
1150 | |||
1151 | private void OSD_FormClosed(object sender, FormClosedEventArgs e) |
||
1152 | { |
||
1153 | xmlconfig(true); |
||
1154 | |||
1155 | } |
||
1156 | |||
1157 | private void xmlconfig(bool write) |
||
1158 | { |
||
1159 | if (write || !File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"config.xml")) |
||
1160 | { |
||
1161 | try |
||
1162 | { |
||
1163 | XmlTextWriter xmlwriter = new XmlTextWriter(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"config.xml", Encoding.ASCII); |
||
1164 | xmlwriter.Formatting = Formatting.Indented; |
||
1165 | |||
1166 | xmlwriter.WriteStartDocument(); |
||
1167 | |||
1168 | xmlwriter.WriteStartElement("Config"); |
||
1169 | |||
1170 | xmlwriter.WriteElementString("comport", CMB_ComPort.Text); |
||
1171 | |||
1172 | xmlwriter.WriteElementString("Pal", CHK_pal.Checked.ToString()); |
||
1173 | |||
1174 | xmlwriter.WriteEndElement(); |
||
1175 | |||
1176 | xmlwriter.WriteEndDocument(); |
||
1177 | xmlwriter.Close(); |
||
1178 | |||
1179 | //appconfig.Save(); |
||
1180 | } |
||
1181 | catch (Exception ex) { MessageBox.Show(ex.ToString()); } |
||
1182 | } |
||
1183 | else |
||
1184 | { |
||
1185 | try |
||
1186 | { |
||
1187 | using (XmlTextReader xmlreader = new XmlTextReader(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"config.xml")) |
||
1188 | { |
||
1189 | while (xmlreader.Read()) |
||
1190 | { |
||
1191 | xmlreader.MoveToElement(); |
||
1192 | try |
||
1193 | { |
||
1194 | switch (xmlreader.Name) |
||
1195 | { |
||
1196 | case "comport": |
||
1197 | string temp = xmlreader.ReadString(); |
||
1198 | CMB_ComPort.Text = temp; |
||
1199 | break; |
||
1200 | case "Pal": |
||
1201 | string temp2 = xmlreader.ReadString(); |
||
1202 | CHK_pal.Checked = (temp2 == "True"); |
||
1203 | break; |
||
1204 | case "Config": |
||
1205 | break; |
||
1206 | case "xml": |
||
1207 | break; |
||
1208 | default: |
||
1209 | if (xmlreader.Name == "") // line feeds |
||
1210 | break; |
||
1211 | break; |
||
1212 | } |
||
1213 | } |
||
1214 | catch (Exception ee) { Console.WriteLine(ee.Message); } // silent fail on bad entry |
||
1215 | } |
||
1216 | } |
||
1217 | } |
||
1218 | catch (Exception ex) { Console.WriteLine("Bad Config File: " + ex.ToString()); } // bad config file |
||
1219 | } |
||
1220 | } |
||
1221 | |||
1222 | private void updateFontToolStripMenuItem_Click(object sender, EventArgs e) |
||
1223 | { |
||
1224 | toolStripProgressBar1.Style = ProgressBarStyle.Continuous; |
||
1225 | toolStripStatusLabel1.Text = ""; |
||
1226 | |||
1227 | OpenFileDialog ofd = new OpenFileDialog(); |
||
1228 | ofd.Filter = "mcm|*.mcm"; |
||
1229 | |||
1230 | ofd.ShowDialog(); |
||
1231 | |||
1232 | if (ofd.FileName != "") |
||
1233 | { |
||
1234 | if (comPort.IsOpen) |
||
1235 | comPort.Close(); |
||
1236 | |||
1237 | try |
||
1238 | { |
||
1239 | |||
1240 | comPort.PortName = CMB_ComPort.Text; |
||
1241 | comPort.BaudRate = 57600; |
||
1242 | |||
1243 | comPort.Open(); |
||
1244 | |||
1245 | comPort.DtrEnable = false; |
||
1246 | comPort.RtsEnable = false; |
||
1247 | |||
1248 | System.Threading.Thread.Sleep(50); |
||
1249 | |||
1250 | comPort.DtrEnable = true; |
||
1251 | comPort.RtsEnable = true; |
||
1252 | |||
1253 | System.Threading.Thread.Sleep(2000); |
||
1254 | |||
1255 | comPort.ReadExisting(); |
||
1256 | |||
1257 | comPort.WriteLine(""); |
||
1258 | comPort.WriteLine(""); |
||
1259 | comPort.WriteLine(""); |
||
1260 | comPort.WriteLine(""); |
||
1261 | comPort.WriteLine(""); |
||
1262 | |||
1263 | int timeout = 0; |
||
1264 | |||
1265 | while (comPort.BytesToRead == 0) |
||
1266 | { |
||
1267 | System.Threading.Thread.Sleep(500); |
||
1268 | Console.WriteLine("Waiting..."); |
||
1269 | timeout++; |
||
1270 | |||
1271 | if (timeout > 6) |
||
1272 | { |
||
1273 | MessageBox.Show("Error entering font mode - No Data"); |
||
1274 | comPort.Close(); |
||
1275 | return; |
||
1276 | } |
||
1277 | } |
||
1278 | |||
1279 | if (!comPort.ReadLine().Contains("Ready for Font")) |
||
1280 | { |
||
1281 | MessageBox.Show("Error entering CharSet upload mode - invalid data"); |
||
1282 | comPort.Close(); |
||
1283 | return; |
||
1284 | } |
||
1285 | } |
||
1286 | catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } |
||
1287 | |||
1288 | using (var stream = ofd.OpenFile()) |
||
1289 | { |
||
1290 | |||
1291 | BinaryReader br = new BinaryReader(stream); |
||
1292 | StreamReader sr2 = new StreamReader(br.BaseStream); |
||
1293 | |||
1294 | string device = sr2.ReadLine(); |
||
1295 | |||
1296 | if (device != "MAX7456") |
||
1297 | { |
||
1298 | MessageBox.Show("Invalid MCM"); |
||
1299 | comPort.Close(); |
||
1300 | return; |
||
1301 | } |
||
1302 | |||
1303 | br.BaseStream.Seek(0, SeekOrigin.Begin); |
||
1304 | |||
1305 | long length = br.BaseStream.Length; |
||
1306 | |||
1307 | while (br.BaseStream.Position < br.BaseStream.Length && !this.IsDisposed) |
||
1308 | { |
||
1309 | try |
||
1310 | { |
||
1311 | toolStripProgressBar1.Value = (int)((br.BaseStream.Position / (float)br.BaseStream.Length) * 100); |
||
1312 | toolStripStatusLabel1.Text = "CharSet Uploading"; |
||
1313 | |||
1314 | |||
1315 | int read = 256 * 3;// 163847 / 256 + 1; // 163,847 font file |
||
1316 | if ((br.BaseStream.Position + read) > br.BaseStream.Length) |
||
1317 | { |
||
1318 | read = (int)(br.BaseStream.Length - br.BaseStream.Position); |
||
1319 | } |
||
1320 | length -= read; |
||
1321 | |||
1322 | byte[] buffer = br.ReadBytes(read); |
||
1323 | |||
1324 | comPort.Write(buffer, 0, buffer.Length); |
||
1325 | |||
1326 | int timeout = 0; |
||
1327 | |||
1328 | while (comPort.BytesToRead == 0 && read == 768) |
||
1329 | { |
||
1330 | System.Threading.Thread.Sleep(10); |
||
1331 | timeout++; |
||
1332 | |||
1333 | if (timeout > 10) |
||
1334 | { |
||
1335 | MessageBox.Show("CharSet upload failed - no response"); |
||
1336 | comPort.Close(); |
||
1337 | return; |
||
1338 | } |
||
1339 | } |
||
1340 | |||
1341 | Console.WriteLine(comPort.ReadExisting()); |
||
1342 | |||
1343 | } |
||
1344 | catch { break; } |
||
1345 | |||
1346 | Application.DoEvents(); |
||
1347 | } |
||
1348 | |||
1349 | comPort.WriteLine("\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); |
||
1350 | |||
1351 | comPort.DtrEnable = false; |
||
1352 | comPort.RtsEnable = false; |
||
1353 | |||
1354 | System.Threading.Thread.Sleep(50); |
||
1355 | |||
1356 | comPort.DtrEnable = true; |
||
1357 | comPort.RtsEnable = true; |
||
1358 | |||
1359 | System.Threading.Thread.Sleep(50); |
||
1360 | |||
1361 | comPort.Close(); |
||
1362 | |||
1363 | comPort.DtrEnable = false; |
||
1364 | comPort.RtsEnable = false; |
||
1365 | |||
1366 | toolStripProgressBar1.Value = 100; |
||
1367 | toolStripStatusLabel1.Text = "CharSet Done"; |
||
1368 | } |
||
1369 | } |
||
1370 | } |
||
1371 | |||
1372 | private void helpToolStripMenuItem_Click(object sender, EventArgs e) |
||
1373 | { |
||
1374 | try |
||
1375 | { |
||
1376 | System.Diagnostics.Process.Start("https://code.google.com/p/arducam-osd/wiki/arducam_osd?tm=6"); |
||
1377 | } |
||
1378 | catch { MessageBox.Show("Webpage open failed... do you have a virus?"); } |
||
1379 | } |
||
1380 | } |
||
1381 | } |