9.2: FgfsMainForm


#region Usings

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.IO.Ports;
#endregion

namespace FgfsSharp
{
public partial class FgfsMainForm : Form, IObserver
{
public delegate void UpdateObserverDelegate(FgfsDataObject dataObject);

#region Privates

private FgfsServer _server;
private FgfsDataObject _dataObject;
private FgfsDataHelper _dataHelper;
private ArrayList _registredDisplays;
private Point _mousePosition;
private SpeechLib.SpVoice _speechObj;
private SerialPort _sPort;

private byte[] _byteArray = new byte[3];
private int _pitchLevel;
private int _leftRoll;
private int _rightRoll;

#endregion

#region Properties

private FgfsServer Server
{
get { return _server; }
set { _server = value; }
}

private FgfsDataObject DataObject
{
get { return _dataObject; }
set { _dataObject = value; }
}

private FgfsDataHelper DataHelper
{
get { return _dataHelper; }
}

#endregion

#region Constructor

public FgfsMainForm()
{
Console.WriteLine("Constructor: FgfsMainForm");

InitializeComponent();
InitializePrivates();

//register the window as observer
DataObject.RegisterObserver(this);
}

#endregion

#region IObserver- Methods
public void UpdateObserver(FgfsDataObject dataObject)
{
if (this.InvokeRequired == false)
{
DataObject = dataObject;
DisplayData();

}
else
{
UpdateObserverDelegate updateDelegate = new UpdateObserverDelegate(UpdateObserver);
this.BeginInvoke(updateDelegate, new object[] { dataObject });
}
}

public void DisplayData()
{
try
{
//update the visual displays
SpeedBox.Text = DataObject.Speed.ToString();
AltitudeBox.Text = DataObject.Altitude.ToString();
SetBar(DataObject.Roll, ref RollBar);
SetBar(DataObject.Pitch, ref PitchBar);

if (this._sPort != null)
{
this.setModelOrientation(RollBar.Value, PitchBar.Value, ref this._byteArray);
this._sPort.Write(this._byteArray, 0, 3);
}

}
catch (Exception portException)
{
Console.WriteLine(portException.Message);
}
}

#endregion

#region Private Methods

private void InitializePrivates()
{
//init the privates
_dataObject = new FgfsDataObject();
_dataHelper = new FgfsDataHelper(_dataObject);
_registredDisplays = new ArrayList();
//_display = new FgfsDisplay();

this._speechObj = null;
this._sPort = null;

//prepare the gui
IpComboBox.DataSource = FgfsServer.DeterminePossibleIPs();

}

#endregion

#region UI- HandlerMethods

private void StartBtn_Click(object sender, EventArgs e)
{
if (PortBox.Text != string.Empty)
{
ChoosePortLabel.Visible = false;
string ip = IpComboBox.SelectedItem.ToString();
int port = Convert.ToInt32(PortBox.Text);

Server = FgfsServer.GetInstance();
Server.StartServer(ip, port, DataHelper);

if (this._soundCheckBox.Checked == true)
{
string speechOut = string.Format("Server IP is {0} running on Port {1}", ip, port);
this._speechObj.Speak(speechOut, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
this._speechObj.WaitUntilDone(5000000);
}

this._startBtn.Enabled = false;
}
else
{
ChoosePortLabel.Visible = true;
}
}

#endregion

#region Menu
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
Console.Write("Bye!");
}

private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
FgfsOptionView OptionView = new FgfsOptionView();
OptionView.ShowDialog();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
this.TrayIcon.Visible = true;
}

private void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.TrayIcon.Visible = false;
}

private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
AboutDialog aboutDialog = new AboutDialog();
aboutDialog.Show();
}

#endregion

private void SoundCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (this._soundCheckBox.Checked == true)
{
try
{
this._speechObj = new SpeechLib.SpVoice();
}
catch (Exception SpeechException)
{
string failureMessage = string.Format("Something with the Speech-Output ran out of control: {0}", SpeechException.Message);
this._soundCheckBox.Checked = false;
MessageBox.Show(failureMessage);
}
}
else
{
this._speechObj = null;
}

}

private void rs232CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (this._rs232CheckBox.Checked == true)
{
try
{
this._sPort = new SerialPort();
this._sPort.BaudRate = 9600;
this._sPort.Open();

}
catch (Exception portException)
{
string failureMessage = string.Format("Something with your COM-Port ran out of control: {0}", portException.Message);
this._rs232CheckBox.Checked = false;
MessageBox.Show(failureMessage);

}
}
else
{
if (this._sPort != null)
{
this._sPort.Close();
this._sPort.Dispose();
this._sPort = null;
}
}
}

private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
{

if (value >= 0)
{
if (value bar.Minimum)
{
bar.Value = value;
}
else { bar.Value = bar.Minimum; }
}
}

private void setModelOrientation(int roll, int pitch, ref byte[] byteArray)
{

//right turn, so rigth side must be pulled down

if (roll > 0)
{
_rightRoll = (int)160 - roll / 2;
_leftRoll = (int)225 + roll / 2;

}
else if (roll 0)
{
_pitchLevel = (int)95 + pitch / 2;
_leftRoll -= (int)pitch / 2;
_rightRoll -= (int)pitch / 2;
}
//and if the value equals null, set the motor to mid-position
else
{
_pitchLevel = 95;
}

byteArray[0] = byte.Parse(_leftRoll.ToString());
byteArray[1] = byte.Parse(_rightRoll.ToString());
byteArray[2] = byte.Parse(_pitchLevel.ToString());
}

}
}

7 Responses to “9.2: FgfsMainForm”

  1. Dustin Says:

    There is a problem in one of your if statements in this code.
    Below is what is given in the code above.

    private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
    {

    if (value >= 0)
    {
    if (value bar.Minimum)
    {
    bar.Value = value;
    }
    else { bar.Value = bar.Minimum; }
    }
    }

    The Following is what I believe is should be

    private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
    {

    if (value >= 0)
    {
    if (value bar.Minimum)
    {
    bar.Value = value;
    }
    else { bar.Value = bar.Minimum; }
    }
    }

  2. Dustin Says:

    That didn’t seem to work, I’ll try again

    There is a problem in one of your if statements in this code.
    Below is what is given in the code above.

    private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
    {

    if (value >= 0)
    {
    if (value bar.Minimum)
    {
    bar.Value = value;
    }
    else { bar.Value = bar.Minimum; }
    }
    }

    The Following is what I believe is should be

    private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
    {

    if (value >= 0)
    {
    if (value bar.Minimum)
    {
    bar.Value = value;
    }
    else { bar.Value = bar.Minimum; }
    }
    }

  3. Dustin Says:

    private void SetBar(int value, ref System.Windows.Forms.TrackBar bar)
    {

    if (value >= 0)
    {
    if (value bar.Minimum)
    {
    bar.Value = value;
    }
    else { bar.Value = bar.Minimum; }
    }
    }

  4. Dustin Says:

    if (value >= 0) {

    if (value bar.Minimum) {
    bar.Value=value;
    }
    else { bar.Value = bar.Minimum; }

  5. Dustin Says:

    Sorry for all the posts, from this end it doesn’t seem to be posting the code I am typing.

  6. PEter Says:

    Where’s the code to make the start button do something?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: