private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text == "" || textBoxAperture.Text == "" || textBoxFocalLength.Text == "" || textBoxObstruction.Text == "")
            {
                MessageBox.Show("Please fill in all entries.");
                
            }
            else
            {
                OpticsInfo oi = new OpticsInfo();
                oi.Name = textBoxName.Text;

                if (radioButtonInches.Checked)
                {
                    oi.FocalLength = double.Parse(textBoxFocalLength.Text) * 25.4;
                    oi.ApertureDiameter = double.Parse(textBoxAperture.Text) * 25.4;
                    oi.ObstructionDiameter = double.Parse(textBoxObstruction.Text) * 25.4;
                    oi.UnitOfMeasure = "inches";
                }
                else
                {
                    oi.FocalLength = double.Parse(textBoxFocalLength.Text);
                    oi.ApertureDiameter = double.Parse(textBoxAperture.Text);
                    oi.ObstructionDiameter = double.Parse(textBoxObstruction.Text);
                    oi.UnitOfMeasure = "millimeters";
                }

                m_OpticInfos.Add(oi);
                listBoxOptics.Items.Add(oi.Name);
                textBoxAperture.Text = "";
                textBoxName.Text = "";
                textBoxFocalLength.Text = "";
            }
        }
 public void AddOpticsInfo(OpticsInfo oi)
 {
     m_OpticsInfos.Add(oi);
     comboBoxSavedOptics.Items.Add(oi.Name);
 }
        private void _DoSetupTelescopeDialog()
        {

            Speech.SayIt(Resources.SetupTelescope, Speech.SpeechType.Command);


            TelescopeSetupDialogForm setupForm = new TelescopeSetupDialogForm();

            setupForm.ComPort = GeminiHardware.Instance.ComPort;
            setupForm.BaudRate = GeminiHardware.Instance.BaudRate.ToString();

            setupForm.Elevation = GeminiHardware.Instance.Elevation;
            setupForm.Latitude = GeminiHardware.Instance.Latitude;
            setupForm.Longitude = GeminiHardware.Instance.Longitude;
            setupForm.TZ = -GeminiHardware.Instance.UTCOffset;

            setupForm.UseDriverSite = GeminiHardware.Instance.UseDriverSite;
            setupForm.UseDriverTime = GeminiHardware.Instance.UseDriverTime;

            setupForm.ShowHandbox = GeminiHardware.Instance.ShowHandbox;

            setupForm.BootMode = GeminiHardware.Instance.BootMode;
            setupForm.UseSpeech = GeminiHardware.Instance.UseSpeech;
            setupForm.SpeechFlags = GeminiHardware.Instance.SpeechFilter;
            setupForm.SpeechVoice = GeminiHardware.Instance.SpeechVoice;
            setupForm.Sites = GeminiHardware.Instance.Sites;
            setupForm.AllowPortScan = GeminiHardware.Instance.ScanCOMPorts;

            setupForm.TraceLevel = GeminiHardware.Instance.TraceLevel;

            setupForm.AsyncPulseGuide = GeminiHardware.Instance.AsyncPulseGuide;
            setupForm.ReportPierSide = GeminiHardware.Instance.ReportPierSide;
            setupForm.PrecisionPulseGuide = GeminiHardware.Instance.PrecisionPulseGuide;

            string[] optics = GeminiHardware.Instance.OpticsNames.Split('~');
            string[] focallengths = GeminiHardware.Instance.FocalLength.Split('~');
            string[] apertures = GeminiHardware.Instance.ApertureDiameter.Split('~');
            string[] uoms = GeminiHardware.Instance.OpticsUnitOfMeasure.Split('~');
            string[] obstructions = GeminiHardware.Instance.OpticsObstruction.Split('~');

            setupForm.ClearOpticsInfos();

            for (int i = 0; i < optics.Length; i++)
            {
                OpticsInfo oi = new OpticsInfo();
                try
                {
                    oi.Name = optics[i];
                    oi.FocalLength = double.Parse(focallengths[i]);
                    oi.ApertureDiameter = double.Parse(apertures[i]);
                    oi.ObstructionDiameter = double.Parse(obstructions[i]);
                    oi.UnitOfMeasure = uoms[i];
                    setupForm.AddOpticsInfo(oi);
                }
                catch
                {
                    setupForm.ClearOpticsInfos();
                    oi = new OpticsInfo();
                    oi.Name = "";
                    oi.FocalLength = 0;
                    oi.UnitOfMeasure = "millimeters";
                    oi.ApertureDiameter = 0;
                    oi.ObstructionDiameter = 0;
                    setupForm.AddOpticsInfo(oi);
                }
            }

            setupForm.SelectedOptic = GeminiHardware.Instance.OpticsValueIndex;

            DialogResult ans;
            if (this.Visible == false)
                ans = setupForm.ShowDialog(this);
            else
                ans = setupForm.ShowDialog(null);

            if (ans == DialogResult.OK)
            {
                string error = "";

                try
                {
                    GeminiHardware.Instance.ComPort = setupForm.ComPort;
                }
                catch
                {
                    error += Resources.COMport + ", ";
                }

                try { GeminiHardware.Instance.BaudRate = int.Parse(setupForm.BaudRate); }
                catch { if (setupForm.ComPort != "Ethernet") error += Resources.BaudRate + ", "; }

                try { GeminiHardware.Instance.Elevation = setupForm.Elevation; }
                catch { error += Resources.Elevation + ", "; }

                try { GeminiHardware.Instance.Latitude = setupForm.Latitude; }
                catch { error += Resources.Latitude + ", "; }

                try { GeminiHardware.Instance.Longitude = setupForm.Longitude; }
                catch { error += Resources.Longitude + ", "; }

                try { GeminiHardware.Instance.UTCOffset = -setupForm.TZ; }
                catch { error += "Timezone" + ", "; }



                GeminiHardware.Instance.UseDriverTime = setupForm.UseDriverTime;
                GeminiHardware.Instance.UseDriverSite = setupForm.UseDriverSite;

                GeminiHardware.Instance.ShowHandbox = setupForm.ShowHandbox;

                GeminiHardware.Instance.BootMode = setupForm.BootMode;

                GeminiHardware.Instance.TraceLevel = setupForm.TraceLevel;

                GeminiHardware.Instance.AsyncPulseGuide = setupForm.AsyncPulseGuide;

                GeminiHardware.Instance.ReportPierSide = setupForm.ReportPierSide;

                GeminiHardware.Instance.PrecisionPulseGuide = setupForm.PrecisionPulseGuide;

                GeminiHardware.Instance.ScanCOMPorts = setupForm.AllowPortScan;

                if (setupForm.UseJoystick && !string.IsNullOrEmpty(setupForm.JoystickName))
                {
                    GeminiHardware.Instance.UseJoystick = true;
                    GeminiHardware.Instance.JoystickName = setupForm.JoystickName;
                }
                else
                {
                    GeminiHardware.Instance.UseJoystick = false;
                }

                GeminiHardware.Instance.UseSpeech = setupForm.UseSpeech;
                GeminiHardware.Instance.SpeechVoice = setupForm.SpeechVoice;
                GeminiHardware.Instance.SpeechFilter = setupForm.SpeechFlags;

                //Get the Optics Data
                GeminiHardware.Instance.OpticsValueIndex = setupForm.SelectedOptic;
                string focallength = "";
                string aperture = "";
                string uom = "";
                string name = "";
                string obstruction = "";
                for (int i = 0; i < setupForm.OpticsInfos.Count; i++)
                {
                    OpticsInfo oi = setupForm.GetOpticsInfo(i);

                    // always store in mm
                    if (uom == "inches")
                    {
                        oi.FocalLength *= 25.4;
                        oi.ApertureDiameter *= 25.4;
                        oi.ObstructionDiameter *= 25.4;
                    }

                    if (focallength == "") focallength += oi.FocalLength.ToString();
                    else focallength += "~" + oi.FocalLength.ToString();
                    if (aperture == "") aperture += oi.ApertureDiameter.ToString();
                    else aperture += "~" + oi.ApertureDiameter.ToString();
                    if (uom == "") uom += oi.UnitOfMeasure;
                    else uom += "~" + oi.UnitOfMeasure;
                    if (oi.Name != "") name += "~" + oi.Name;
                    if (obstruction == "") obstruction += oi.ObstructionDiameter.ToString();
                    else obstruction += "~" + oi.ObstructionDiameter.ToString();

                }

                GeminiHardware.Instance.FocalLength = focallength;
                GeminiHardware.Instance.ApertureDiameter = aperture;
                GeminiHardware.Instance.OpticsUnitOfMeasure = uom;
                GeminiHardware.Instance.OpticsNames = name;
                GeminiHardware.Instance.OpticsObstruction = obstruction;


                if (error != "")
                {
                    if (error.EndsWith(", ")) error = error.Substring(0, error.Length - 2);
                    MessageBox.Show(Resources.InvalidSettings + "\r\n" + error, SharedResources.TELESCOPE_DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                tmrJoystick.Stop();
                StartJoystick();    //restart if settings have changed

            }

            Speech.SpeechInitialize(this.Handle, GeminiHardware.Instance.UseSpeech ? GeminiHardware.Instance.SpeechVoice : null);
            Speech.Filter = GeminiHardware.Instance.SpeechFilter;

            setupForm.Dispose();
            GeminiHardware.Instance.Profile = null;
        }