| Wheelchair Control for Windows Mobile Diploma Thesis, Author: Ondrej Vozda, FEEC BUT, 2013

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
FormMain.cs
1 ﻿using System;
2 using System.Drawing;
3 using System.IO;
4 using System.Net;
5 using System.Net.Sockets;
6 using System.Windows.Forms;
7 using System.Threading;
8
9 namespace WheelchairControlWM
10 {
14 public partial class FormMain : Form
15 {
16 #region Constants
17
21 private const Byte SLIDE_TO_FORM_WIDTH_RATIO = 4;
22
26 private const UInt16 SEND_ERROR_LABEL_BLINK_TIMEOUT_MS = 1000;
27
31 private const UInt16 FRAME_RECEIVE_TIMEOUT_MS = 2000;
32
36 private static readonly Color PRESSED_BUTON_BACKGROUD_COLOR = Color.DarkBlue;
37
41 private const Byte ARROW_BORDER_THICKNESS = 5;
42
46 private const UInt16 UDP_PORT = 50005;
47
51 private const Byte CAMERA_FRAME_OFFSET = 1;
52
56 private enum PacketHeader : byte
57 {
61 WATCHDOG_NOP = 0xC0,
62
66 JOYSTICK_COMMAND = 0xC1,
67
71 STOP_BUTTON = 0xC2,
72
76 START_BUTTON = 0xC3,
77
81 AUTO_PARKING_START_BUTTON = 0xC4,
82
86 CAMERA_FRAME_REQUEST = 0xC5,
87
91 USER_RESPONSE = 0xC6,
92
96 AUTOPARKING_PHASE = 0xC7,
97
101 CAMERA_FRAME_RESPONSE = 0xD0,
102
106 CAMERA_FRAME_TO_BE_CONFIRMED = 0xD1
107 }
108
112 public enum UserResponseCode : byte
113 {
117 END = 0,
118
122 LOOK_FURTHER,
123
127 MARKS_FOUND
128 }
129
133 public enum AutoParkingPhase : byte
134 {
138 WAITING_FOR_START = 0,
139
143 WALL_FOLLOWING,
144
148 GOING_BACKWARDS,
149
153 MARKS_TRACKING,
154
158 PATH_FOLLOWING,
159
163 END
164 }
165
169 public enum PhaseError : byte
170 {
174 ERROR = 0,
175
179 NO_ERROR
180 }
181
185 private enum JoystickAxisValue : byte
186 {
190 MAX = 255,
191
195 MIN = 0,
196
200 ZERO = 127
201 }
202
206 private enum DetectedMarksTransmit : byte
207 {
211 DONT_TRANSMIT = 0,
212
216 TRANSMIT
217 }
218
222 private enum MainTabIndexes : byte
223 {
227 MAIN,
228
232 CAMERA,
233
237 PARKING,
238
242 SETTINGS,
243
244 TABS_COUNT
245 }
246
247 #endregion
248
249 # region Private variables
250
254 private Thread UDPreceiveThread;
255
259 private IPEndPoint ipEp = new IPEndPoint(IPAddress.Any, UDP_PORT);
260
264 private UdpClient incomingUDPClient;
265
269 private UdpClient outgoingUDPClient;
270
274 private AutoResetEvent resetEventFrameSync;
275
279 delegate void InvokeThreadSafeDelegate();
280
284 private Int32 slideDetectXcoordinate = -1;
285
289 private Byte slideLength = 0;
290
294 private SettingsClass mySettings;
295
299 private Bitmap cameraFrame;
300
304 private System.Threading.Timer deviceIdleTimer;
305
306 #endregion
307
308 #region Miscelaneous methods
309
315 private void VisualizePhase(AutoParkingPhase phase, Boolean isError)
316 {
317 //Invoke this function in controls' owner thread
318 //Method called from UDPReceiverThread - invoke always
319 this.Invoke((InvokeThreadSafeDelegate)delegate()
320 {
321 Color lblColor = isError ? Color.Red : Color.Lime;
322
323 //Erase all labels
324 foreach (Control ctrl in tabPageAutoParking.Controls)
325 if (ctrl is Label)
326 ((Label)ctrl).BackColor = tabPageAutoParking.BackColor;
327
328 //Light up correct label
329 switch (phase)
330 {
331 default:
332 case AutoParkingPhase.WAITING_FOR_START:
333
334 labelPhaseWaitToStart.BackColor = lblColor;
335
336 break;
337
338 case AutoParkingPhase.WALL_FOLLOWING:
339
340 labelPhaseWallFollowing.BackColor = lblColor;
341
342 break;
343
344 case AutoParkingPhase.GOING_BACKWARDS:
345
346 labelPhaseBackwardDrive.BackColor = lblColor;
347
348 break;
349
350 case AutoParkingPhase.MARKS_TRACKING:
351
352 labelPhaseDetectingMarks.BackColor = lblColor;
353
354 break;
355
356 case AutoParkingPhase.PATH_FOLLOWING:
357
358 labelPhaseNextWaypoint.BackColor = lblColor;
359
360 break;
361
362 case AutoParkingPhase.END:
363
364 labelPhaseEnd.BackColor = lblColor;
365
366 break;
367 }
368 });
369 }
370
375 private void GetCameraFrame(Boolean includeDetectedMarks)
376 {
377 //Reset ARevent first
378 resetEventFrameSync.Reset();
379
380 //Send frame request
381 SendUDPPacket(new Byte[] { (Byte)PacketHeader.CAMERA_FRAME_REQUEST, includeDetectedMarks ? (Byte)DetectedMarksTransmit.TRANSMIT : (Byte)DetectedMarksTransmit.DONT_TRANSMIT });
382
383 //Wait for synchronization (ARevent is set within ReceiveCameraFrame() method)
384 if (resetEventFrameSync.WaitOne(FRAME_RECEIVE_TIMEOUT_MS, false))
385 {
386 //Success, draw frame
387 Graphics pbGraph = pictureBoxCameraFrame.CreateGraphics();
388
389 pbGraph.DrawImage(cameraFrame,
390 new Rectangle(0, 0, pictureBoxCameraFrame.Width, pictureBoxCameraFrame.Height),
391 new Rectangle(0, 0, cameraFrame.Width, cameraFrame.Height),
392 GraphicsUnit.Pixel);
393 }
394 else
395 {
396 MessageBox.Show(FRAME_RECEIVE_TIMEOUT_MS.ToString() + " ms timeout has expired!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
397 }
398 }
399
403 private void UDPDataReceiver()
404 {
405 Byte[] inBuffer;
406
407 while (true)
408 {
409 try
410 {
411 try
412 {
413 //Receive
414 inBuffer = incomingUDPClient.Receive(ref ipEp);
415 }
416 catch (SocketException)
417 {
418 //Exit the loop on SocketException
419 break;
420 }
421
422 //Parse header
423 switch (inBuffer[0])
424 {
425 case (Byte)PacketHeader.WATCHDOG_NOP:
426 //No action
427 break;
428
429 case (Byte)PacketHeader.CAMERA_FRAME_RESPONSE:
430 //Camera frame received
431 using (MemoryStream inStream = new MemoryStream(inBuffer, CAMERA_FRAME_OFFSET /*Omit packet header*/, inBuffer.Length - CAMERA_FRAME_OFFSET - 1))
432 {
433 cameraFrame = new Bitmap(inStream);
434
435 //Operation complete signal
436 resetEventFrameSync.Set();
437 }
438
439 break;
440
441 case (Byte)PacketHeader.CAMERA_FRAME_TO_BE_CONFIRMED:
442
443 //Camera frame received, switch to camera tab and display confirmation panel
444 using (MemoryStream inStream = new MemoryStream(inBuffer, CAMERA_FRAME_OFFSET /*Omit packet header*/, inBuffer.Length - CAMERA_FRAME_OFFSET - 1))
445 {
446 //Invoke always
447 this.Invoke((InvokeThreadSafeDelegate)delegate()
448 {
449 //Switch to camera tab
450 tabControlMain.SelectedIndex = (Int32)MainTabIndexes.CAMERA;
451
452 cameraFrame = new Bitmap(inStream);
453
454 panelUserConfirmation.Visible = true;
455
456 tabPageCamera.Refresh();
457
458 //Draw frame
459 using(Graphics pbGraph = pictureBoxCameraFrame.CreateGraphics())
460 pbGraph.DrawImage(cameraFrame,
461 new Rectangle(0, 0, pictureBoxCameraFrame.Width, pictureBoxCameraFrame.Height),
462 new Rectangle(0, 0, cameraFrame.Width, cameraFrame.Height),
463 GraphicsUnit.Pixel);
464 });
465 }
466
467 break;
468
469 case (Byte)PacketHeader.AUTOPARKING_PHASE:
470 //Auto parking phase received
471 VisualizePhase((AutoParkingPhase)Enum.Parse(typeof(AutoParkingPhase),inBuffer[1].ToString(),true),
472 (inBuffer[2] > 0) ? true: false);
473
474 break;
475
476 default:
477 //Next loop
478 continue;
479 }
480 }
481 catch (Exception)
482 {
483 //Log error and blink indicator
484 }
485 }
486 }
487
492 private void EnableArrows(Boolean enableState)
493 {
494 //Go through all controls and enable/disable them
495 foreach (Control controlItem in panelArrowContainer.Controls)
496 if (controlItem is PictureBox)
497 controlItem.Enabled = enableState;
498 }
499
503 private void RightSlideHandler()
504 {
505 //Switch to previous tab page
506 if (tabControlMain.SelectedIndex > 0)
507 tabControlMain.SelectedIndex--;
508 }
509
514 private void SendUDPPacket(Byte[] data)
515 {
516 try
517 {
518 outgoingUDPClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse(textBoxTargetIP.Text), UDP_PORT));
519 }
520 catch (Exception)
521 {
522 //Visualize error
523 new Thread(new ThreadStart(delegate() {
524
525 BlinkSendErrorLabel();
526
527 Thread.Sleep(SEND_ERROR_LABEL_BLINK_TIMEOUT_MS);
528
529 BlinkSendErrorLabel();
530
531 })).Start();
532 }
533 }
534
538 private void BlinkSendErrorLabel()
539 {
540 if (this.InvokeRequired)
541 {
542 //Invoke this function in controls' owner thread
543 this.Invoke(new InvokeThreadSafeDelegate(BlinkSendErrorLabel));
544 }
545 else
546 {
547 lock(labelSendError)
548 {
549 if (labelSendError.Text == "Send error")
550 {
551 //Erase label
552 labelSendError.Text = "";
553 labelSendError.BackColor = panelArrowContainer.BackColor;
554 }
555 else
556 {
557 //Blink label
558 labelSendError.Text = "Send error";
559 labelSendError.BackColor = Color.Red;
560 }
561 }
562 }
563 }
564
569 private void DeviceIdleResetTimerCallback(Object param)
570 {
571 //Invoke
572 try
573 {
574 this.Invoke(new InvokeThreadSafeDelegate(delegate()
575 {
576 if (checkBoxResetIdleTimer.Checked)
577 DevicePowerManagement.ResetIdleTimer();
578 }));
579 }
580 catch (Exception)
581 {/*Nothing to be done*/}
582 }
583
587 private void LeftSlideHandler()
588 {
589 //Switch to next tab page
590 if (tabControlMain.SelectedIndex < (tabControlMain.TabPages.Count - 1))
591 tabControlMain.SelectedIndex++;
592 }
593
594 #endregion
595
599 public FormMain()
600 {
601 InitializeComponent();
602
603 //Initialize UDP clients
604 outgoingUDPClient = new UdpClient();
605 incomingUDPClient = new UdpClient(ipEp);
606
607 //Init resetevent
608 resetEventFrameSync = new AutoResetEvent(false);
609
610 //Run UDP receiver thread
611 UDPreceiveThread = new Thread(new ThreadStart(UDPDataReceiver));
612 UDPreceiveThread.IsBackground = true;
613 UDPreceiveThread.Priority = ThreadPriority.Normal;
614 UDPreceiveThread.Start();
615
616 //Init device idle reset timer
617 deviceIdleTimer = new System.Threading.Timer(new TimerCallback(DeviceIdleResetTimerCallback), null, 0 /*Start immediately*/, 5000 /*Invoke callback every 5s*/);
618 }
619
620 #region Auto generated controls events handlers
621
622 private void buttonExit_Click(object sender, EventArgs e)
623 {
624 //Try to save settings
625 try
626 {
627 mySettings.Save();
628 }
629 catch (Exception)
630 {
631 //Inform user
632 MessageBox.Show("Failed to save settings!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
633 }
634 finally
635 {
636 //Exit with native exit code
637 Application.Exit();
638 }
639 }
640
641 private void FormMain_MouseDown(object sender, MouseEventArgs e)
642 {
643 //Store initial point
644 if (e.X >= 0 && e.X < this.Width)
645 slideDetectXcoordinate = e.X;
646 else
647 slideDetectXcoordinate = -1; //Error, store fake point
648 }
649
650 private void FormMain_MouseUp(object sender, MouseEventArgs e)
651 {
652 //Try to detect slide
653 if (e.X >= 0 && e.X < this.Width)
654 {
655 if (Math.Abs(e.X - slideDetectXcoordinate) >= slideLength)
656 {
657 if (e.X > slideDetectXcoordinate)
658 {
659 //Slide right
660 RightSlideHandler();
661 }
662 else
663 {
664 //Slide left
665 LeftSlideHandler();
666 }
667 }
668 }
669 }
670
671 private void FormMain_Load(object sender, EventArgs e)
672 {
673 #region Read settings
674
675 //New settings class instance -> load from default file location.
676 mySettings = new SettingsClass();
677
678 //Load IP address
679 textBoxTargetIP.Text = mySettings.TargetIP.ToString();
680
681 //Load gain
682 trackBarGain.Value = mySettings.Gain;
683
684 //Load checkboxes' checked states
685 checkBoxEnableRemoteStart.Checked = mySettings.RemoteStartEnabled;
686 checkBoxResetIdleTimer.Checked = mySettings.ResetIdleTimerEnabled;
687
688 #endregion
689
690 #region Initialize variables
691
692 try
693 {
694 slideLength = (Byte)(this.Width / SLIDE_TO_FORM_WIDTH_RATIO);
695 }
696 catch (ArgumentOutOfRangeException)
697 {
698 slideLength = 0;
699 }
700
701 #endregion
702
703 //Try to get local IP
704 try
705 {
706 IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
707
708 foreach (IPAddress ip in host.AddressList)
709 if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
710 labelIP.Text = ip.ToString();
711 }
712 catch (Exception)
713 {
714 labelIP.Text = "Failed to get local IP.";
715 }
716
717 //Erase status labels
718 labelWatchdogIndicator.Text = "";
719 labelSendError.Text = "";
720
721 //Disable arrows
722 EnableArrows(false);
723 }
724
725 private void buttonRefreshIP_Click(object sender, EventArgs e)
726 {
727 //Try to get local IP
728 try
729 {
730 IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
731
732 foreach (IPAddress ip in host.AddressList)
733 if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
734 labelIP.Text = ip.ToString();
735 }
736 catch (Exception)
737 {
738 labelIP.Text = "Failed to get local IP.";
739 }
740 }
741
742 private void pbButtonStart_Click(object sender, EventArgs e)
743 {
744 //Toggle components' visibility
745 pbButtonStart.Visible = false;
746 menuItemStart.Enabled = false;
747 pbButtonStop.Visible = true;
748 menuItemStop.Enabled = true;
749
750 //Enable watchdog
751 timerWifiWatchdog.Enabled = true;
752
753 //Enable buttons
754 EnableArrows(true);
755
756 //Enable AutoParking start button
757 pbButtonStartII.Enabled = true;
758
759 //optionally click PC app's START button remotely
760 SendUDPPacket(new Byte[] { (Byte)PacketHeader.START_BUTTON });
761 }
762
763 private void pbButtonStop_Click(object sender, EventArgs e)
764 {
765 //Toggle components' visibility
766 pbButtonStop.Visible = false;
767 menuItemStop.Enabled = false;
768 pbButtonStart.Visible = true;
769 menuItemStart.Enabled = true;
770
771 //Disable watchdog
772 timerWifiWatchdog.Enabled = false;
773
774 //Disable AutoParking start button
775 pbButtonStartII.Enabled = false;
776
777 //Disable buttons
778 EnableArrows(false);
779
780 //Erase label
781 if (labelWatchdogIndicator.Text == "WIFI COP")
782 {
783 labelWatchdogIndicator.BackColor = panelArrowContainer.BackColor;
784 labelWatchdogIndicator.Text = "";
785 }
786
787 //click PC app's STOP button remotely
788 SendUDPPacket(new Byte[] { (Byte)PacketHeader.STOP_BUTTON });
789 }
790
791 private void pbIcon_MouseDown(object sender, MouseEventArgs e)
792 {
793 //Show menu
794 contextMenuMain.Show(panelTopStrip, new Point(0, 0));
795 }
796
797 private void menuItemViewMain_Click(object sender, EventArgs e)
798 {
799 //Switch tab
800 tabControlMain.SelectedIndex = (Int32)MainTabIndexes.MAIN;
801 }
802
803 private void ViewSettings_Click(object sender, EventArgs e)
804 {
805 //Switch tab
806 tabControlMain.SelectedIndex = (Int32)MainTabIndexes.SETTINGS;
807 }
808
809 private void menuItemViewCamera_Click(object sender, EventArgs e)
810 {
811 //Switch tab
812 tabControlMain.SelectedIndex = (Int32)MainTabIndexes.CAMERA;
813 }
814
815 private void tabControlMain_SelectedIndexChanged(object sender, EventArgs e)
816 {
817 //First enable/disable related menu items
818 menuItemManualControl.Enabled = false;
819 menuItemViewMain.Enabled = true;
820 menuItemViewCamera.Enabled = true;
821 menuItemViewParking.Enabled = true;
822 menuItemViewSettings.Enabled = true;
823
824 //Switch action according to selected index
825 switch (tabControlMain.SelectedIndex)
826 {
827 default:
828 case (Int32)MainTabIndexes.MAIN:
829
830 menuItemManualControl.Enabled = true;
831 menuItemViewMain.Enabled = false;
832
833 break;
834
835 case (Int32)MainTabIndexes.CAMERA:
836
837 menuItemViewCamera.Enabled = false;
838
839 break;
840
841 case (Int32)MainTabIndexes.PARKING:
842
843 menuItemViewParking.Enabled = false;
844
845 break;
846
847 case (Int32)MainTabIndexes.SETTINGS:
848
849 menuItemViewSettings.Enabled = false;
850
851 break;
852 }
853 }
854
855 private void pbButtonStop_MouseDown(object sender, MouseEventArgs e)
856 {
857 pbButtonStop.BackColor = PRESSED_BUTON_BACKGROUD_COLOR;
858 }
859
860 private void pbButtonStop_MouseUp(object sender, MouseEventArgs e)
861 {
862 pbButtonStop.BackColor = tabPageMain.BackColor;
863 }
864
865 private void pbButtonStart_MouseDown(object sender, MouseEventArgs e)
866 {
867 pbButtonStart.BackColor = PRESSED_BUTON_BACKGROUD_COLOR;
868 }
869
870 private void pbButtonStart_MouseUp(object sender, MouseEventArgs e)
871 {
872 pbButtonStart.BackColor = tabPageMain.BackColor;
873 }
874
875 private void pbUL_MouseDown(object sender, MouseEventArgs e)
876 {
877 Graphics gr = panelArrowContainer.CreateGraphics();
878 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
879 new Rectangle(pbUL.Left - ARROW_BORDER_THICKNESS, pbUL.Top - ARROW_BORDER_THICKNESS, pbUL.Width + 2 * ARROW_BORDER_THICKNESS, pbUL.Height + 2 * ARROW_BORDER_THICKNESS));
880
881 gr.Dispose();
882
883 //Create and send UDP packet
884 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MIN, (Byte)JoystickAxisValue.MAX, (Byte)trackBarGain.Value };
885 SendUDPPacket(packetOut);
886 }
887
888 private void pbUL_MouseUp(object sender, MouseEventArgs e)
889 {
890 Graphics gr = panelArrowContainer.CreateGraphics();
891 gr.Clear(panelArrowContainer.BackColor);
892
893 gr.Dispose();
894
895 //Create and send UDP packet
896 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
897 SendUDPPacket(packetOut);
898 }
899
900 private void pbU_MouseDown(object sender, MouseEventArgs e)
901 {
902 //Visualize
903 Graphics gr = panelArrowContainer.CreateGraphics();
904 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
905 new Rectangle(pbU.Left - ARROW_BORDER_THICKNESS, pbU.Top - ARROW_BORDER_THICKNESS, pbU.Width + 2 * ARROW_BORDER_THICKNESS, pbU.Height + 2 * ARROW_BORDER_THICKNESS));
906
907 gr.Dispose();
908
909 //Create and send UDP packet
910 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND,(Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.MAX, (Byte)trackBarGain.Value };
911 SendUDPPacket(packetOut);
912 }
913
914 private void pbU_MouseUp(object sender, MouseEventArgs e)
915 {
916 //Visualize
917 Graphics gr = panelArrowContainer.CreateGraphics();
918 gr.Clear(panelArrowContainer.BackColor);
919
920 gr.Dispose();
921
922 //Create and send UDP packet
923 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
924 SendUDPPacket(packetOut);
925 }
926
927 private void pbUR_MouseDown(object sender, MouseEventArgs e)
928 {
929 Graphics gr = panelArrowContainer.CreateGraphics();
930 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
931 new Rectangle(pbUR.Left - ARROW_BORDER_THICKNESS, pbUR.Top - ARROW_BORDER_THICKNESS, pbUR.Width + 2 * ARROW_BORDER_THICKNESS, pbUR.Height + 2 * ARROW_BORDER_THICKNESS));
932
933 gr.Dispose();
934
935 //Create and send UDP packet
936 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MAX, (Byte)JoystickAxisValue.MAX, (Byte)trackBarGain.Value };
937 SendUDPPacket(packetOut);
938 }
939
940 private void pbUR_MouseUp(object sender, MouseEventArgs e)
941 {
942 Graphics gr = panelArrowContainer.CreateGraphics();
943 gr.Clear(panelArrowContainer.BackColor);
944
945 gr.Dispose();
946
947 //Create and send UDP packet
948 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
949 SendUDPPacket(packetOut);
950 }
951
952 private void pbL_MouseDown(object sender, MouseEventArgs e)
953 {
954 Graphics gr = panelArrowContainer.CreateGraphics();
955 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
956 new Rectangle(pbL.Left - ARROW_BORDER_THICKNESS, pbL.Top - ARROW_BORDER_THICKNESS, pbL.Width + 2 * ARROW_BORDER_THICKNESS, pbL.Height + 2 * ARROW_BORDER_THICKNESS));
957
958 gr.Dispose();
959
960 //Create and send UDP packet
961 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MIN, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
962 SendUDPPacket(packetOut);
963 }
964
965 private void pbL_MouseUp(object sender, MouseEventArgs e)
966 {
967 Graphics gr = panelArrowContainer.CreateGraphics();
968 gr.Clear(panelArrowContainer.BackColor);
969
970 gr.Dispose();
971
972 //Create and send UDP packet
973 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
974 SendUDPPacket(packetOut);
975 }
976
977 private void pbStop_MouseDown(object sender, MouseEventArgs e)
978 {
979 Graphics gr = panelArrowContainer.CreateGraphics();
980 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
981 new Rectangle(pbStop.Left - ARROW_BORDER_THICKNESS, pbStop.Top - ARROW_BORDER_THICKNESS, pbStop.Width + 2 * ARROW_BORDER_THICKNESS, pbStop.Height + 2 * ARROW_BORDER_THICKNESS));
982
983 gr.Dispose();
984
985 //Create and send UDP packet
986 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.STOP_BUTTON };
987 SendUDPPacket(packetOut);
988 }
989
990 private void pbStop_MouseUp(object sender, MouseEventArgs e)
991 {
992 Graphics gr = panelArrowContainer.CreateGraphics();
993 gr.Clear(panelArrowContainer.BackColor);
994
995 gr.Dispose();
996 }
997
998 private void pbR_MouseDown(object sender, MouseEventArgs e)
999 {
1000 Graphics gr = panelArrowContainer.CreateGraphics();
1001 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
1002 new Rectangle(pbR.Left - ARROW_BORDER_THICKNESS, pbR.Top - ARROW_BORDER_THICKNESS, pbR.Width + 2 * ARROW_BORDER_THICKNESS, pbR.Height + 2 * ARROW_BORDER_THICKNESS));
1003
1004 gr.Dispose();
1005
1006 //Create and send UDP packet
1007 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MAX, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
1008 SendUDPPacket(packetOut);
1009 }
1010
1011 private void pbR_MouseUp(object sender, MouseEventArgs e)
1012 {
1013 Graphics gr = panelArrowContainer.CreateGraphics();
1014 gr.Clear(panelArrowContainer.BackColor);
1015
1016 gr.Dispose();
1017
1018 //Create and send UDP packet
1019 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
1020 SendUDPPacket(packetOut);
1021 }
1022
1023 private void pbDL_MouseDown(object sender, MouseEventArgs e)
1024 {
1025 Graphics gr = panelArrowContainer.CreateGraphics();
1026 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
1027 new Rectangle(pbDL.Left - ARROW_BORDER_THICKNESS, pbDL.Top - ARROW_BORDER_THICKNESS, pbDL.Width + 2 * ARROW_BORDER_THICKNESS, pbDL.Height + 2 * ARROW_BORDER_THICKNESS));
1028
1029 gr.Dispose();
1030
1031 //Create and send UDP packet
1032 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MAX, (Byte)JoystickAxisValue.MIN, (Byte)trackBarGain.Value };
1033 SendUDPPacket(packetOut);
1034 }
1035
1036 private void pbDL_MouseUp(object sender, MouseEventArgs e)
1037 {
1038 Graphics gr = panelArrowContainer.CreateGraphics();
1039 gr.Clear(panelArrowContainer.BackColor);
1040
1041 gr.Dispose();
1042
1043 //Create and send UDP packet
1044 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
1045 SendUDPPacket(packetOut);
1046 }
1047
1048 private void pbD_MouseDown(object sender, MouseEventArgs e)
1049 {
1050 Graphics gr = panelArrowContainer.CreateGraphics();
1051 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
1052 new Rectangle(pbD.Left - ARROW_BORDER_THICKNESS, pbD.Top - ARROW_BORDER_THICKNESS, pbD.Width + 2 * ARROW_BORDER_THICKNESS, pbD.Height + 2 * ARROW_BORDER_THICKNESS));
1053
1054 gr.Dispose();
1055
1056 //Create and send UDP packet
1057 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.MIN, (Byte)trackBarGain.Value };
1058 SendUDPPacket(packetOut);
1059 }
1060
1061 private void pbD_MouseUp(object sender, MouseEventArgs e)
1062 {
1063 Graphics gr = panelArrowContainer.CreateGraphics();
1064 gr.Clear(panelArrowContainer.BackColor);
1065
1066 gr.Dispose();
1067
1068 //Create and send UDP packet
1069 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
1070 SendUDPPacket(packetOut);
1071 }
1072
1073 private void pbDR_MouseDown(object sender, MouseEventArgs e)
1074 {
1075 Graphics gr = panelArrowContainer.CreateGraphics();
1076 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
1077 new Rectangle(pbDR.Left - ARROW_BORDER_THICKNESS, pbDR.Top - ARROW_BORDER_THICKNESS, pbDR.Width + 2 * ARROW_BORDER_THICKNESS, pbDR.Height + 2 * ARROW_BORDER_THICKNESS));
1078
1079 gr.Dispose();
1080
1081 //Create and send UDP packet
1082 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.MIN, (Byte)JoystickAxisValue.MIN, (Byte)trackBarGain.Value };
1083 SendUDPPacket(packetOut);
1084 }
1085
1086 private void pbDR_MouseUp(object sender, MouseEventArgs e)
1087 {
1088 Graphics gr = panelArrowContainer.CreateGraphics();
1089 gr.Clear(panelArrowContainer.BackColor);
1090
1091 gr.Dispose();
1092
1093 //Create and send UDP packet
1094 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.JOYSTICK_COMMAND, (Byte)JoystickAxisValue.ZERO, (Byte)JoystickAxisValue.ZERO, (Byte)trackBarGain.Value };
1095 SendUDPPacket(packetOut);
1096 }
1097
1098 private void buttonSaveSettings_Click(object sender, EventArgs e)
1099 {
1100 //Save IP using SettingsClass
1101 try
1102 {
1103 //set values
1104 mySettings.TargetIP = IPAddress.Parse(textBoxTargetIP.Text);
1105 mySettings.ResetIdleTimerEnabled = checkBoxResetIdleTimer.Checked;
1106 mySettings.RemoteStartEnabled = checkBoxEnableRemoteStart.Checked;
1107
1108 //Save settings
1109 mySettings.Save();
1110 }
1111 catch (Exception)
1112 {
1113 MessageBox.Show("Failed to save settings!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
1114 }
1115 }
1116
1117 private void textBoxTargetIP_GotFocus(object sender, EventArgs e)
1118 {
1119 //Show keyboard
1120 inputPanelKeyboard.Enabled = true;
1121 }
1122
1123 private void textBoxTargetIP_LostFocus(object sender, EventArgs e)
1124 {
1125 //Hide keyboard
1126 inputPanelKeyboard.Enabled = false;
1127 }
1128
1129 private void trackBarGain_ValueChanged(object sender, EventArgs e)
1130 {
1131 //Refresh label
1132 labelGain.Text = trackBarGain.Value.ToString();
1133
1134 //Save to settings
1135 mySettings.Gain = (Byte)trackBarGain.Value;
1136 }
1137
1138 private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
1139 {
1140 //Save settings
1141 try
1142 {
1143 mySettings.Save();
1144 }
1145 catch (Exception)
1146 { /*Nothing to be done*/ }
1147 }
1148
1149 private void timerWifiWatchdog_Tick(object sender, EventArgs e)
1150 {
1151 //Create and send UDP watchdog packet
1152 Byte[] packetOut = new Byte[] { (Byte)PacketHeader.WATCHDOG_NOP };
1153 SendUDPPacket(packetOut);
1154
1155 //Blink label
1156 if (labelWatchdogIndicator.Text == "WIFI COP")
1157 {
1158 labelWatchdogIndicator.BackColor = panelArrowContainer.BackColor;
1159 labelWatchdogIndicator.Text = "";
1160 }
1161 else
1162 {
1163 labelWatchdogIndicator.BackColor = Color.LimeGreen;
1164 labelWatchdogIndicator.Text = "WIFI COP";
1165 }
1166 }
1167
1168 private void buttonReceiveFrame_Click(object sender, EventArgs e)
1169 {
1170 GetCameraFrame(false);
1171 }
1172
1173 private void buttonReceiveFrameMarks_Click(object sender, EventArgs e)
1174 {
1175 GetCameraFrame(true);
1176 }
1177
1178 private void menuItemCameraFrameWithoutMarks_Click(object sender, EventArgs e)
1179 {
1180 //Switch tabs and get frame
1181 tabControlMain.SelectedIndex = (Byte)MainTabIndexes.CAMERA;
1182
1183 //Force tabcontrol to redraw first
1184 tabControlMain.Refresh();
1185
1186 GetCameraFrame(false);
1187 }
1188
1189 private void menuItemCameraFrameWithMarks_Click(object sender, EventArgs e)
1190 {
1191 //Switch tabs and get frame
1192 tabControlMain.SelectedIndex = (Byte)MainTabIndexes.CAMERA;
1193
1194 //Force tabcontrol to redraw first
1195 tabControlMain.Refresh();
1196
1197 GetCameraFrame(true);
1198 }
1199
1200 private void checkBoxEnableRemoteStart_CheckStateChanged(object sender, EventArgs e)
1201 {
1202 //Save setting
1203 mySettings.RemoteStartEnabled = checkBoxEnableRemoteStart.Checked;
1204 }
1205
1206 private void checkBoxResetIdleTimer_CheckStateChanged(object sender, EventArgs e)
1207 {
1208 //Save setting
1209 mySettings.ResetIdleTimerEnabled = checkBoxResetIdleTimer.Checked;
1210 }
1211
1212 private void menuItemViewParking_Click(object sender, EventArgs e)
1213 {
1214 //Switch tab
1215 tabControlMain.SelectedIndex = (Byte)MainTabIndexes.PARKING;
1216 }
1217
1218 private void pictureBoxStartII_MouseDown(object sender, MouseEventArgs e)
1219 {
1220 //Visualize
1221 using (Graphics gr = tabPageAutoParking.CreateGraphics())
1222 gr.FillRectangle(new SolidBrush(PRESSED_BUTON_BACKGROUD_COLOR),
1223 new Rectangle(pbButtonStartII.Left - ARROW_BORDER_THICKNESS, pbButtonStartII.Top - ARROW_BORDER_THICKNESS, pbButtonStartII.Width + 2 * ARROW_BORDER_THICKNESS, pbButtonStartII.Height + 2 * ARROW_BORDER_THICKNESS));
1224 }
1225
1226 private void pictureBoxStartII_MouseUp(object sender, MouseEventArgs e)
1227 {
1228 //Visualize
1229 using (Graphics gr = tabPageAutoParking.CreateGraphics())
1230 gr.Clear(tabPageAutoParking.BackColor);
1231
1232 //Send start command
1233 SendUDPPacket(new Byte[] {(Byte)PacketHeader.AUTO_PARKING_START_BUTTON });
1234 }
1235
1236 private void buttonStopDetection_Click(object sender, EventArgs e)
1237 {
1238 //Send confirmation and disable the panel
1239 SendUDPPacket(new Byte[] {(Byte)PacketHeader.USER_RESPONSE, (Byte)UserResponseCode.END });
1240
1241 panelUserConfirmation.Visible = false;
1242 }
1243
1244 private void buttonRedetect_Click(object sender, EventArgs e)
1245 {
1246 //Send confirmation and disable the panel
1247 SendUDPPacket(new Byte[] { (Byte)PacketHeader.USER_RESPONSE, (Byte)UserResponseCode.LOOK_FURTHER });
1248
1249 panelUserConfirmation.Visible = false;
1250 }
1251
1252 private void buttonMarksDetectedOK_Click(object sender, EventArgs e)
1253 {
1254 //Send confirmation and disable the panel
1255 SendUDPPacket(new Byte[] { (Byte)PacketHeader.USER_RESPONSE, (Byte)UserResponseCode.MARKS_FOUND});
1256
1257 panelUserConfirmation.Visible = false;
1258 }
1259
1260 #endregion
1261 }
1262 }