コード例 #1
0
        public Constant Create(int bitWidth, int value, PinSide pinSide)
        {
            Constant constant = this.CreateItem(Guid.NewGuid(), bitWidth, Constant.Normalize(value, bitWidth), pinSide, ConstantData.NoteField.Field.DefaultValue);

            this.CreateDevicePin(constant);
            return(constant);
        }
コード例 #2
0
        public DialogSensor(Sensor sensor)
        {
            this.sensor = sensor;

            this.SensorTypes = SensorDescriptor.SensorTypes;
            SensorType type = this.sensor.SensorType;

            this.IsLoop = (type != SensorType.Series);
            if (type == SensorType.Loop)
            {
                type = SensorType.Series;
            }
            this.SelectedSensorType = this.SensorTypes.First(t => t.Value == type);

            this.SeriesData = (type == SensorType.Series) ? this.sensor.Data : Sensor.DefaultSeriesData;

            int min = Sensor.DefaultRandomMinInterval;
            int max = Sensor.DefaultRandomMaxInterval;

            if (type == SensorType.Random)
            {
                SensorPoint point;
                if (Sensor.TryParsePoint(this.sensor.Data, 32, out point))
                {
                    min = point.Tick;
                    max = point.Value;
                }
            }
            this.RandomMin = min.ToString(Properties.Resources.Culture);
            this.RandomMax = max.ToString(Properties.Resources.Culture);

            if (type == SensorType.Manual)
            {
                string text = this.sensor.Data;
                int    value;
                if (string.IsNullOrEmpty(text) || !int.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value))
                {
                    value = 0;
                }
                this.ManualInitialValue = Constant.Normalize(value, this.sensor.BitWidth).ToString("X", CultureInfo.InvariantCulture);
            }
            else
            {
                this.ManualInitialValue = "0";
            }

            this.DataContext = this;
            this.InitializeComponent();

            this.bitWidth.ItemsSource  = PinDescriptor.BitWidthRange;
            this.bitWidth.SelectedItem = this.sensor.BitWidth;
            this.side.ItemsSource      = PinDescriptor.PinSideRange;
            this.side.SelectedItem     = PinDescriptor.PinSideDescriptor(this.sensor.PinSide);
            this.notation.Text         = this.sensor.Notation;
            this.note.Text             = this.sensor.Note;
        }
コード例 #3
0
        public static void SetCellValue(byte[] data, int bitWidth, int index, int value)
        {
            int cellSize  = Memory.BytesPerCellFor(bitWidth);
            int cellStart = index * cellSize;

            value = Constant.Normalize(value, bitWidth);
            for (int i = 0; i < cellSize; i++)
            {
                data[cellStart + i] = (byte)(value >> (i * 8));
            }
        }
コード例 #4
0
        public static int CellValue(byte[] data, int bitWidth, int index)
        {
            int cellSize  = Memory.BytesPerCellFor(bitWidth);
            int cellStart = index * cellSize;
            int value     = 0;

            for (int i = 0; i < cellSize; i++)
            {
                value |= ((int)data[cellStart + i]) << (i * 8);
            }
            value = Constant.Normalize(value, bitWidth);
            return(value);
        }
コード例 #5
0
        public static bool TryParsePoint(string data, int bitWidth, out SensorPoint point)
        {
            point = new SensorPoint();
            int tick, value;

            string[] parts = data.Split(':');
            if (parts == null || parts.Length != 2 ||
                !int.TryParse(parts[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out tick) ||
                !int.TryParse(parts[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value)
                )
            {
                return(false);
            }
            point = new SensorPoint(tick, Constant.Normalize(value, bitWidth));
            return(true);
        }
コード例 #6
0
        private void ButtonOkClick(object sender, RoutedEventArgs e)
        {
            try {
                int     bitWidth = (int)this.bitWidth.SelectedItem;
                int     value    = Constant.Normalize(int.Parse(this.value.Text.Trim(), NumberStyles.HexNumber, CultureInfo.InvariantCulture), bitWidth);
                PinSide pinSide  = ((EnumDescriptor <PinSide>) this.side.SelectedItem).Value;
                string  note     = this.note.Text.Trim();

                if (this.constant.BitWidth != bitWidth || this.constant.ConstantValue != value || this.constant.PinSide != pinSide || this.constant.Note != note)
                {
                    this.constant.CircuitProject.InTransaction(() => {
                        this.constant.BitWidth             = bitWidth;
                        this.constant.ConstantValue        = value;
                        this.constant.PinSide              = pinSide;
                        this.constant.Note                 = note;
                        this.constant.Pins.First().PinSide = pinSide;
                    });
                }
                this.Close();
            } catch (Exception exception) {
                App.Mainframe.ReportException(exception);
            }
        }
コード例 #7
0
        private void ButtonOkClick(object sender, RoutedEventArgs e)
        {
            // All the logic of this method assumes that validation prevents this been called if there is an incorrect user input.
            try {
                int     bitWidth = (int)this.bitWidth.SelectedItem;
                PinSide pinSide  = ((EnumDescriptor <PinSide>) this.side.SelectedItem).Value;
                string  notation = this.notation.Text.Trim();
                string  note     = this.note.Text.Trim();
                string  data     = this.SeriesData.Trim();
                string  minText  = this.RandomMin.Trim();
                string  maxText  = this.RandomMax.Trim();
                string  initial  = this.ManualInitialValue.Trim();

                SensorType type = this.SelectedSensorType.Value;
                if (type == SensorType.Series && string.IsNullOrWhiteSpace(data))
                {
                    data = Sensor.DefaultSeriesData;
                }
                if (type == SensorType.Series && this.IsLoop)
                {
                    type = SensorType.Loop;
                }
                else if (type == SensorType.Random)
                {
                    if (int.TryParse(minText, NumberStyles.Integer, Properties.Resources.Culture, out int min) &&
                        int.TryParse(maxText, NumberStyles.Integer, Properties.Resources.Culture, out int max) &&
                        0 < min && min <= max
                        )
                    {
                        data = Sensor.SaveSeries(new List <SensorPoint>()
                        {
                            new SensorPoint(min, max)
                        });
                    }
                    else
                    {
                        data = Sensor.DefaultRandomData;
                    }
                }
                else if (type == SensorType.Manual)
                {
                    int value;
                    if (!int.TryParse(initial, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value))
                    {
                        value = 0;
                    }
                    data = Constant.Normalize(value, bitWidth).ToString("X", CultureInfo.InvariantCulture);
                }

                if (this.sensor.SensorType != type ||
                    this.sensor.BitWidth != bitWidth ||
                    this.sensor.PinSide != pinSide ||
                    this.sensor.Notation != notation ||
                    this.sensor.Note != note ||
                    this.sensor.Data != data
                    )
                {
                    this.sensor.CircuitProject.InTransaction(() => {
                        this.sensor.SensorType           = type;
                        this.sensor.BitWidth             = bitWidth;
                        this.sensor.PinSide              = pinSide;
                        this.sensor.Notation             = notation;
                        this.sensor.Note                 = note;
                        this.sensor.Data                 = data;
                        this.sensor.Pins.First().PinSide = pinSide;
                    });
                }
                this.Close();
            } catch (Exception exception) {
                App.Mainframe.ReportException(exception);
            }
        }