예제 #1
0
        void createValueControl(KeyValuePair<string, string> x)
        {

            string value = ((float)MainV2.comPort.MAV.param[x.Key]).ToString("0.###", CultureInfo.InvariantCulture);
            string description = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Description);
            string displayName = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.DisplayName) + " (" + x.Key + ")";
            string units = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Units);
            string rangeRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Range);
            string incrementRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Increment);
            string availableValuesRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Values);

            float displayscale = 1;

            float increment, intValue;
            float.TryParse(incrementRaw, NumberStyles.Float, CultureInfo.InvariantCulture, out increment);
            // this is in local culture
            float.TryParse(value, out intValue);

            string[] rangeParts = rangeRaw.Split(new[] { ' ' });
            if (rangeParts.Count() == 2 && increment > 0)
            {
                float lowerRange;
                float.TryParse(rangeParts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out lowerRange);
                float upperRange;
                float.TryParse(rangeParts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out upperRange);

                var valueControl = new RangeControl(x.Key, FitDescriptionText(units, description), displayName, increment, displayscale, lowerRange, upperRange, value);

                ThemeManager.ApplyThemeTo(valueControl);

                valueControl.ValueChanged += valueControl_ValueChanged;

                flowLayoutPanel1.Controls.Add(valueControl);
            }
            else if (availableValuesRaw.Length > 0)
            {
                var valueControl = new ValuesControl();
                valueControl.Name = x.Key;
                valueControl.DescriptionText = FitDescriptionText(units, description);
                valueControl.LabelText = displayName;

                ThemeManager.ApplyThemeTo(valueControl);

                valueControl.ComboBoxControl.DisplayMember = "Value";
                valueControl.ComboBoxControl.ValueMember = "Key";
                valueControl.ComboBoxControl.DataSource = ParameterMetaDataRepository.GetParameterOptionsInt(x.Key);
                valueControl.ComboBoxControl.SelectedItem = value;

                valueControl.ValueChanged += valueControl_ValueChanged;

                flowLayoutPanel1.Controls.Add(valueControl);
            }
            else { Console.WriteLine("No valid param metadata for " + x.Key); }


        }
        private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //, ref int ypos)
        {
            if (!string.IsNullOrEmpty(x.Key))
            {
                try
                {
                    var controlAdded = false;

                    var value = ((float) MainV2.comPort.MAV.param[x.Key]).ToString("0.###");

                    var items = Controls.Find(x.Key, true);
                    if (items.Length > 0)
                    {
                        if (items[0].GetType() == typeof (RangeControl))
                        {
                            ((RangeControl) items[0]).ValueChanged -= Control_ValueChanged;
                            ((RangeControl) items[0]).DeAttachEvents();
                            ((RangeControl) items[0]).Value = value;
                            ThemeManager.ApplyThemeTo(((RangeControl) items[0]));
                            ((RangeControl) items[0]).AttachEvents();
                            ((RangeControl) items[0]).ValueChanged += Control_ValueChanged;
                            return;
                        }
                        if (items[0].GetType() == typeof (ValuesControl))
                        {
                            ((ValuesControl) items[0]).ValueChanged -= Control_ValueChanged;
                            ((ValuesControl) items[0]).Value = value;
                            ((ValuesControl) items[0]).ValueChanged += Control_ValueChanged;
                            return;
                        }
                        if (items[0].GetType() == typeof (MavlinkCheckBoxBitMask))
                        {
                            ((MavlinkCheckBoxBitMask) items[0]).ValueChanged -= Control_ValueChanged;
                            ((MavlinkCheckBoxBitMask) items[0]).setup(x.Key, MainV2.comPort.MAV.param);
                            ((MavlinkCheckBoxBitMask) items[0]).ValueChanged += Control_ValueChanged;
                            return;
                        }
                    }

                    var description = ParameterMetaDataRepository.GetParameterMetaData(x.Key,
                        ParameterMetaDataConstants.Description, MainV2.comPort.MAV.cs.firmware.ToString());
                    var displayName = x.Value + " (" + x.Key + ")";
                    var units = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Units,
                        MainV2.comPort.MAV.cs.firmware.ToString());

                    // If this is a range
                    var rangeRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key,
                        ParameterMetaDataConstants.Range, MainV2.comPort.MAV.cs.firmware.ToString());
                    var incrementRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key,
                        ParameterMetaDataConstants.Increment, MainV2.comPort.MAV.cs.firmware.ToString());

                    if (!string.IsNullOrEmpty(rangeRaw) && !string.IsNullOrEmpty(incrementRaw))
                    {
                        float increment, intValue;
                        float.TryParse(incrementRaw, NumberStyles.Float, CultureInfo.InvariantCulture, out increment);
                        // this is in local culture
                        float.TryParse(value, out intValue);

                        var rangeParts = rangeRaw.Split(' ');
                        if (rangeParts.Count() == 2 && increment > 0)
                        {
                            float lowerRange;
                            float.TryParse(rangeParts[0], NumberStyles.Float, CultureInfo.InvariantCulture,
                                out lowerRange);
                            float upperRange;
                            float.TryParse(rangeParts[1], NumberStyles.Float, CultureInfo.InvariantCulture,
                                out upperRange);

                            float displayscale = 1;

                            //    var rangeControl = new RangeControl();

                            if (units.ToLower() == "centi-degrees")
                            {
                                //Console.WriteLine(x.Key + " scale");
                                displayscale = 100;
                                units = "Degrees (Scaled)";
                                increment /= 100;
                            }
                            else if (units.ToLower() == "centimeters")
                            {
                                //Console.WriteLine(x.Key + " scale");
                                //  displayscale = 100;
                                //  units = "Meters (Scaled)";
                                //  increment /= 100;
                            }

                            var desc = FitDescriptionText(units, description, tableLayoutPanel1.Width);

                            var rangeControl = new RangeControl(x.Key, desc, displayName, increment, displayscale,
                                lowerRange, upperRange, value);

                            rangeControl.Width = tableLayoutPanel1.Width - 50;

                            //Console.WriteLine("{0} {1} {2} {3} {4}", x.Key, increment, lowerRange, upperRange, value);

                            ThemeManager.ApplyThemeTo(rangeControl);

                            if (intValue < lowerRange)
                                rangeControl.NumericUpDownControl.BackColor = Color.Orange;

                            if (intValue > upperRange)
                                rangeControl.NumericUpDownControl.BackColor = Color.Orange;

                            rangeControl.AttachEvents();

                            rangeControl.ValueChanged += Control_ValueChanged;

                            // set pos
                            rangeControl.Location = new Point(0, y);
                            // add control - let it autosize height
                            toadd.Add(rangeControl);
                            // add height for next control
                            y += rangeControl.Height;

                            controlAdded = true;
                        }
                    }

                    // try bitmask next
                    if (!controlAdded)
                    {
                        var availableBitMask = ParameterMetaDataRepository.GetParameterBitMaskInt(x.Key,
                            MainV2.comPort.MAV.cs.firmware.ToString());
                        if (availableBitMask.Count > 0)
                        {
                            var bitmask = new MavlinkCheckBoxBitMask();
                            bitmask.Name = x.Key;
                            bitmask.setup(x.Key, MainV2.comPort.MAV.param);

                            bitmask.myLabel1.Text = displayName;
                            bitmask.label1.Text = FitDescriptionText(units, description, bitmask.Width - 100);

                            ThemeManager.ApplyThemeTo(bitmask);

                            // set pos
                            bitmask.Location = new Point(0, y);
                            // add control - let it autosize height
                            toadd.Add(bitmask);
                            // add height for next control
                            y += bitmask.Height;

                            bitmask.ValueChanged += Control_ValueChanged;

                            controlAdded = true;
                        }
                    }

                    if (!controlAdded)
                    {
                        // If this is a subset of values
                        var availableValuesRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key,
                            ParameterMetaDataConstants.Values, MainV2.comPort.MAV.cs.firmware.ToString());
                        if (!string.IsNullOrEmpty(availableValuesRaw))
                        {
                            var availableValues = availableValuesRaw.Split(',');
                            if (availableValues.Any())
                            {
                                var valueControl = new ValuesControl();
                                valueControl.Width = tableLayoutPanel1.Width - 50;
                                valueControl.Name = x.Key;
                                valueControl.DescriptionText = FitDescriptionText(units, description,
                                    tableLayoutPanel1.Width);
                                valueControl.LabelText = displayName;

                                ThemeManager.ApplyThemeTo(valueControl);

                                var splitValues = new List<KeyValuePair<string, string>>();
                                // Add the values to the ddl
                                foreach (var val in availableValues)
                                {
                                    var valParts = val.Split(':');
                                    splitValues.Add(new KeyValuePair<string, string>(valParts[0].Trim(),
                                        (valParts.Length > 1) ? valParts[1].Trim() : valParts[0].Trim()));
                                }
                                ;
                                valueControl.ComboBoxControl.DisplayMember = "Value";
                                valueControl.ComboBoxControl.ValueMember = "Key";
                                valueControl.ComboBoxControl.DataSource = splitValues;
                                valueControl.ComboBoxControl.SelectedValue = value;

                                valueControl.ValueChanged += Control_ValueChanged;

                                // set pos
                                valueControl.Location = new Point(0, y);
                                // add control - let it autosize height
                                toadd.Add(valueControl);
                                // add height for next control
                                y += valueControl.Height;
                            }
                        }
                    }
                } // if there is an error simply dont show it, ie bad pde file, bad scale etc
                catch (Exception ex)
                {
                    log.Error(ex);
                }
            }
        }
        //, ref int ypos)
        void AddControl(KeyValuePair<string,string> x)
        {
            if (!String.IsNullOrEmpty(x.Key))
            {
                try
                {
                    bool controlAdded = false;

                    string value = ((float)MainV2.comPort.MAV.param[x.Key]).ToString("0.###", CultureInfo.InvariantCulture);
                    string description = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Description);
                    string displayName = x.Value + " (" + x.Key + ")";
                    string units = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Units);

                    var items = this.Controls.Find(x.Key,true);
                    if (items.Length > 0)
                    {
                        if (items[0].GetType() == typeof(RangeControl)) {
                            ((RangeControl)items[0]).ValueChanged -= Control_ValueChanged;
                            ((RangeControl)items[0]).DeAttachEvents();
                            ((RangeControl)items[0]).Value = value;
                            ThemeManager.ApplyThemeTo(((RangeControl)items[0]));
                            ((RangeControl)items[0]).AttachEvents();
                            ((RangeControl)items[0]).ValueChanged += Control_ValueChanged;
                            return;
                        } else
                        if (items[0].GetType() == typeof(ValuesControl))
                        {
                            ((ValuesControl)items[0]).ValueChanged -= Control_ValueChanged;
                            ((ValuesControl)items[0]).Value = value;
                            ((ValuesControl)items[0]).ValueChanged += Control_ValueChanged;
                            return;
                        }
                    }

                    // If this is a range
                    string rangeRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Range);
                    string incrementRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Increment);

                    if (!String.IsNullOrEmpty(rangeRaw) && !String.IsNullOrEmpty(incrementRaw))
                    {
                        float increment, intValue;
                        float.TryParse(incrementRaw, out increment);
                        float.TryParse(value, out intValue);

                        string[] rangeParts = rangeRaw.Split(new[] { ' ' });
                        if (rangeParts.Count() == 2 && increment > 0)
                        {
                            float lowerRange;
                            float.TryParse(rangeParts[0], out lowerRange);
                            float upperRange;
                            float.TryParse(rangeParts[1], out upperRange);

                            float displayscale = 1;

                        //    var rangeControl = new RangeControl();

                            if (units.ToLower() == "centi-degrees")
                            {
                                //Console.WriteLine(x.Key + " scale");
                                displayscale = 100;
                                units = "Degrees (Scaled)";
                                increment /= 100;
                            } else if (units.ToLower() == "centimeters")
                            {
                                //Console.WriteLine(x.Key + " scale");
                              //  displayscale = 100;
                              //  units = "Meters (Scaled)";
                              //  increment /= 100;
                            }

                            var rangeControl = new RangeControl(x.Key, FitDescriptionText(units, description), displayName, increment, displayscale, lowerRange, upperRange, value);

                            //Console.WriteLine("{0} {1} {2} {3} {4}", x.Key, increment, lowerRange, upperRange, value);

                            ThemeManager.ApplyThemeTo(rangeControl);

                            if (intValue < lowerRange)
                                rangeControl.NumericUpDownControl.BackColor = Color.Orange;

                            if (intValue > upperRange)
                                rangeControl.NumericUpDownControl.BackColor = Color.Orange;

                            rangeControl.AttachEvents();

                            rangeControl.ValueChanged += Control_ValueChanged;

                            // set pos
                            rangeControl.Location = new Point(0, y);
                            // add control - let it autosize height
                            tableLayoutPanel1.Controls.Add(rangeControl);
                            // add height for next control
                            y += rangeControl.Height;

                            controlAdded = true;
                        }
                    }

                    if (!controlAdded)
                    {
                        // If this is a subset of values
                        string availableValuesRaw = ParameterMetaDataRepository.GetParameterMetaData(x.Key, ParameterMetaDataConstants.Values);
                        if (!String.IsNullOrEmpty(availableValuesRaw))
                        {
                            string[] availableValues = availableValuesRaw.Split(new[] { ',' });
                            if (availableValues.Any())
                            {
                                var valueControl = new ValuesControl();
                                valueControl.Name = x.Key;
                                valueControl.DescriptionText = FitDescriptionText(units, description);
                                valueControl.LabelText = displayName;

                                ThemeManager.ApplyThemeTo(valueControl);

                                var splitValues = new List<KeyValuePair<string, string>>();
                                // Add the values to the ddl
                                foreach (string val in availableValues)
                                {
                                    string[] valParts = val.Split(new[] { ':' });
                                    splitValues.Add(new KeyValuePair<string, string>(valParts[0].Trim(), (valParts.Length > 1) ? valParts[1].Trim() : valParts[0].Trim()));
                                };
                                valueControl.ComboBoxControl.DisplayMember = "Value";
                                valueControl.ComboBoxControl.ValueMember = "Key";
                                valueControl.ComboBoxControl.DataSource = splitValues;
                                valueControl.ComboBoxControl.SelectedValue = value;

                                valueControl.ValueChanged += Control_ValueChanged;

                                // set pos
                                valueControl.Location = new Point(0, y);
                                // add control - let it autosize height
                                tableLayoutPanel1.Controls.Add(valueControl);
                                // add height for next control
                                y += valueControl.Height;
                            }
                        }
                    }
                } // if there is an error simply dont show it, ie bad pde file, bad scale etc
                catch (Exception ex) { log.Error(ex); }
            }
        }
예제 #4
0
        private void ProcessItem(configitem item)
        {
            try
            {
                if (!MainV2.comPort.MAV.param.ContainsKey(item.paramname))
                    return;

                var value = (float) MainV2.comPort.MAV.param[item.paramname];

                if (value < item.min)
                    item.min = value;
                if (value > item.max)
                    item.max = value;

                var range = ParameterMetaDataRepository.GetParameterMetaData(item.paramname,
                    ParameterMetaDataConstants.Range, MainV2.comPort.MAV.cs.firmware.ToString());
                var increment = ParameterMetaDataRepository.GetParameterMetaData(item.paramname,
                    ParameterMetaDataConstants.Increment, MainV2.comPort.MAV.cs.firmware.ToString());

                var rangeopt = range.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                if (rangeopt.Length > 1)
                {
                    item.min = float.Parse(rangeopt[0], CultureInfo.InvariantCulture);
                    item.max = float.Parse(rangeopt[1], CultureInfo.InvariantCulture);
                }

                var incrementf = 0.01f;
                if (increment.Length > 0)
                    float.TryParse(increment, NumberStyles.Float, CultureInfo.InvariantCulture, out incrementf);

                var RNG = new RangeControl(item.paramname, item.desc, item.title, incrementf, 1, item.min, item.max,
                    value.ToString());
                RNG.Tag = item;

                RNG.Location = new Point(10, y);

                RNG.AttachEvents();

                RNG.ValueChanged += RNG_ValueChanged;

                ThemeManager.ApplyThemeTo(RNG);

                panel1.Controls.Add(RNG);

                y += RNG.Height;
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show("Failed to process " + item.paramname + "\n" + ex);
            }
        }