00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Data;
00005 using System.Drawing;
00006 using System.Text;
00007 using System.Windows.Forms;
00008
00009
00010 namespace PlatformController {
00011 public partial class PlatformControllerMainForm : Form {
00012
00013
00014 PlatformStrategy _strategy;
00015 public PlatformStrategy Strategy {
00016 get { return _strategy; }
00017 }
00018
00019 bool _silent = true;
00020 public bool Silent {
00021 get { return _silent; }
00022 set {
00023 _silent = value;
00024 if (_silent) {
00025 _strategy.updateUiEvent -= updateUi;
00026 _strategy.setSilentModeEvent -= setSilentMode;
00027 } else {
00028 _strategy.updateUiEvent += updateUi;
00029 _strategy.setSilentModeEvent += setSilentMode;
00030 }
00031 }
00032 }
00033
00034 string _status = "ok";
00035 public string Status {
00036 get { return _status; }
00037 }
00038
00039
00040 public PlatformControllerMainForm() {
00041 InitializeComponent();
00042 _strategy = new PlatformStrategy("COM3", 0);
00043 initializeObject();
00044 }
00045
00046
00047 public PlatformControllerMainForm(ref PlatformStrategy strategy) {
00048 InitializeComponent();
00049 _strategy = strategy;
00050 initializeObject();
00051 onInitialize();
00052 }
00053
00054 private void initializeObject() {
00055 _strategy.updateStatusEvent += onInitialize;
00056 toolStripStatusLabel2.Text = _strategy.Status;
00057 }
00058
00059 private void onInitialize() {
00060 if (!_silent) {
00061 toolStripStatusLabel1.Text = _strategy.Data.Status;
00062 if (_strategy.Data.Status == "plat_init")
00063 toolStripStatusLabel1.BackColor = Color.Green;
00064 else
00065 toolStripStatusLabel1.BackColor = Color.Red;
00066 }
00067 }
00068
00069
00070 private void setSilentMode() {
00071 if (Silent != _strategy.Silent)
00072 Silent = _strategy.Silent;
00073 }
00074
00075 private void hideButton_Click(object sender, EventArgs e) {
00076 this.Hide();
00077 }
00078
00079 private void updateUi() {
00080 debugTextBox.Text = _strategy.Data.PlatformBuffer;
00081 systemVoltageTextBox.Text = Math.Round(_strategy.Data.SystemBatteryVolts, 2).ToString();
00082 microVoltageTextBox.Text = Math.Round(_strategy.Data.MicroBatteryVolts, 2).ToString();
00083 infraredLeftTextBox.Text = Math.Round(_strategy.Data.InfraredLeftCm, 2).ToString();
00084 infraredRightTextBox.Text = Math.Round(_strategy.Data.InfraredRightCm, 2).ToString();
00085 sonarLeftTextBox.Text = Math.Round(_strategy.Data.SonarRangefinderLeftCm, 2).ToString();
00086 sonarCenterTextBox.Text = Math.Round(_strategy.Data.SonarRangefinderCenterCm, 2).ToString();
00087 sonarRightTextBox.Text = Math.Round(_strategy.Data.SonarRangefinderRightCm, 2).ToString();
00088 bumperLeftTextBox.Text = _strategy.Data.BumperLeft.ToString();
00089 bumperRightTextBox.Text = _strategy.Data.BumperRight.ToString();
00090 resetSwitchCheckBox.Checked = _strategy.Data.ResetSwitch;
00091 radioControlModeCheckBox.Checked = _strategy.Data.RadioControlModeSwitch;
00092 redLedCheckBox.Checked = _strategy.Data.RedLedValue;
00093 greenLedCheckBox.Checked = _strategy.Data.GreenLedValue;
00094 steeringCalculatedTextBox.Text = _strategy.Data.SteeringCalculatedValue.ToString();
00095 throttleCalculatedTextBox.Text = _strategy.Data.ThrottleCalculatedValue.ToString();
00096 autoAvoidingCheckBox.Checked = _strategy.Data.AutoAvoiding;
00097 compassHeadingTextBox.Text = Math.Round(_strategy.Data.CompassHeadingDouble, 2).ToString();
00098 dataUpdateRateTextBox.Text = _strategy.WatchdogTimer.ElapsedMilliseconds.ToString();
00099 }
00100
00101 private void openCom1Button_Click(object sender, EventArgs e) {
00102 toolStripStatusLabel2.Text = _strategy.openPort();
00103 toolStripStatusLabel1.Text = _strategy.Data.Status;
00104 if (_strategy.Data.Status == "plat_init")
00105 toolStripStatusLabel1.BackColor = Color.Green;
00106 }
00107
00108 private void closeCom1Button_Click(object sender, EventArgs e) {
00109 toolStripStatusLabel2.Text = _strategy.closePort();
00110 toolStripStatusLabel1.BackColor = Color.Red;
00111 toolStripStatusLabel1.Text = "not initialized";
00112 }
00113
00114 private void clearDebugWindowButton_Click(object sender, EventArgs e) {
00115 debugTextBox.Clear();
00116 }
00117
00118 private void platformControllerSerialPortToolStripMenuItem_Click(object sender, EventArgs e) {
00119 _strategy.showSerialPort();
00120 }
00121
00122 private void silentCheckBox_CheckedChanged(object sender, EventArgs e) {
00123 _strategy.setSilentMode(silentCheckBox.Checked);
00124 Silent = silentCheckBox.Checked;
00125 onInitialize();
00126 }
00127
00128 private void makeItCLaunchToolStripMenuItem_Click(object sender, EventArgs e) {
00129 System.Diagnostics.Process makeItC = new System.Diagnostics.Process();
00130 makeItC.StartInfo.FileName = "C:\\Program Files\\Coridium\\bin\\MakeItC.exe";
00131 makeItC.StartInfo.WorkingDirectory = "C:\\Program Files\\Coridium\\bin\\";
00132 makeItC.Start();
00133 System.Diagnostics.Process npp = new System.Diagnostics.Process();
00134 npp.StartInfo.FileName = "C:\\Program Files\\Notepad++\\notepad++.exe";
00135 npp.Start();
00136 }
00137
00138 private void platformControllerResetToolStripMenuItem_Click(object sender, EventArgs e) {
00139 _strategy.resetPlatformController();
00140 }
00141
00142 private void sendCommandEventRaised(object sender, EventArgs e) {
00143 Control cntrl = (Control)sender;
00144 if (cntrl.Text == "Enabled")
00145 _strategy.sendSerialCommand('q', (byte)((CheckBox)cntrl).Checked.CompareTo(false));
00146 else if (cntrl.Text == "Red LED On") _strategy.redLedOn();
00147 else if (cntrl.Text == "Red LED Off") _strategy.redLedOff();
00148 else if (cntrl.Text == "Green LED On") _strategy.greenLedOn();
00149 else if (cntrl.Text == "Green LED Off") _strategy.greenLedOff();
00150 }
00151
00152 }
00153 }