public static double Convert(double val, Unit from, Unit to)
        {
            UnitConversion conv = GetConversion(from, to);
            if (conv == null)
                throw new ArgumentException("Unknown unit");

            return conv.Convert(val);
        }
 public UnitConversion(Unit from, Unit to, double preAdjust, double scale, double postAdjust)
 {
     this.from = from;
     this.to = to;
     this.preAdjust = preAdjust;
     this.postAdjust = postAdjust;
     this.scale = scale;
 }
        public static UnitConversion GetConversion(Unit from, Unit to)
        {
            foreach (UnitConversion conv in conversions) {
                if (object.Equals(from, conv.From) && object.Equals(to, conv.To))
                    return conv;
            }

            return null;
        }
        public GraphItemAdapter(string name, Color color, double windowSize, DataItemAdapter dataItemAdapter)
        {
            // store values
            this.dataItemAdapter = dataItemAdapter;
            this.name = name;

            // get the source units
            this.sourceUnits = UnitConverter.GetUnit(dataItemAdapter.DataItemUnits);

            // create the queues
            plotQueue = new TimeWindowQueue(windowSize);
            recvQueue = new TimeWindowQueue(windowSize);

            // create the plot object
            plot = new LinePlot(plotQueue.ValueList, plotQueue.TimestampList);
            plot.Color = color;
            plot.Label = name;

            // subscribe to relevant events
            Services.RunControlService.RenderCycle += RunControlService_RenderCycle;
            dataItemAdapter.DataValueReceived += dataItemAdapter_DataValueReceived;
        }
        private void PopulateDestUnits(Unit source)
        {
            comboDestUnits.Items.Clear();
            if (source != null) {
                List<string> destUnits = new List<string>();

                foreach (UnitConversion conv in UnitConverter.GetConversions(source)) {
                    destUnits.Add(conv.To.Abbreviation ?? conv.To.Name);
                }

                destUnits.Sort();

                // find the source name again
                string sourceName = source.Abbreviation ?? source.Name;
                int identityIndex = destUnits.FindIndex(delegate(string s) { return s == sourceName; });
                if (identityIndex < 0) identityIndex = 0;

                comboDestUnits.Items.AddRange(destUnits.ToArray());

                comboDestUnits.Enabled = true;
                comboDestUnits.SelectedIndex = identityIndex;
            }
            else {
                comboDestUnits.Enabled = false;
            }
        }
 private void SelectUnit(Unit unit, ComboBox combo)
 {
     string name = unit.Abbreviation ?? unit.Name;
     for (int i = 0; i < combo.Items.Count; i++) {
         if (object.Equals(name, combo.Items[i])) {
             combo.SelectedIndex = i;
             break;
         }
     }
 }
 private void SelectSourceUnit(Unit source)
 {
     SelectUnit(source, comboSourceUnits);
 }
 private void SelectDestUnit(Unit dest)
 {
     SelectUnit(dest, comboDestUnits);
 }
        public static IEnumerable<UnitConversion> GetConversions(Unit uo)
        {
            if (uo == null)
                yield break;

            foreach (UnitConversion u in conversions) {
                if (object.Equals(u.From, uo))
                    yield return u;
            }

            yield break;
        }
 public static void AddUnit(Unit unit)
 {
     units.Add(unit);
 }
        private static void HandleUnit(XmlReader reader)
        {
            string name = reader.GetAttribute("name", "http://www.cornellracing.com/Units.xsd");
            string abbrev = reader.GetAttribute("abbrev", "http://www.cornellracing.com/Units.xsd");
            string category = reader.GetAttribute("category", "http://www.cornellracing.com/Units.xsd");

            Unit u = new Unit(name, abbrev, category);
            AddUnit(u);
        }