public GraphView()
        {
            InitializeComponent();
            GraphGrid = new Path();
            GraphGrid.StrokeThickness = 0.5;
            GraphGrid.Stroke          = Brushes.Gray;
            GraphArea.Children.Add(GraphGrid);

            SecPerDiv                 = new Quantity("spd", "Time per div", "s");
            SecPerDiv.AllowZero       = false;
            SecPerDiv.AllowNegative   = false;
            VoltsPerDiv               = new Quantity("vpd", "Volts per div", "V");
            VoltsPerDiv.AllowZero     = false;
            VoltsPerDiv.AllowNegative = false;
            VoltsOffset               = -2;

            SecPerDiv.Val   = 1;
            VoltsPerDiv.Val = 2;

            for (int i = 0; i < MaxNumberOfTraces; i++)
            {
                Traces[i] = null;
            }
        }
Exemple #2
0
        //Called so that a component can update its appearance based on the simulation. Used, for example, by LEDs.
        //Passes what type of event has happened, and the total number of updates that have occurred if it is a tick event
        public virtual void UpdateFromSimulation(int numberOfUpdates, Simulator sim, SimulationEvent eventType)
        {
            if (eventType == SimulationEvent.STARTED)
            {
                ConnectedPinVariables.Clear();
                //Generate list of pin variables
                //Using ConnectedNets as a source of pin numbers
                foreach (int pin in ConnectedNets.Keys)
                {
                    ConnectedPinVariables.Add(pin, new List <int>());
                }
                Regex regex = new Regex(@"\{([0-9]+)\}");
                foreach (string netlistLine in UnmergedNetlist.Split(new char[] { '\n' }))
                {
                    string[] parts = netlistLine.Trim().Split(new char[] { ' ' });
                    if (parts.Length >= 3)
                    {
                        //Ignore fixed voltage nets
                        if (parts[0] != "NET")
                        {
                            for (int i = 2; i < parts.Length; i++)
                            {
                                //Look for connections to pin
                                if (regex.IsMatch(parts[i]))
                                {
                                    int thisPinNum = int.Parse(regex.Matches(parts[i])[0].Groups[1].Value);
                                    //Determine pin number of the subcircuit component based on position
                                    int    simPinNum   = i - 1;
                                    string simCompName = parts[1].Replace("{r}", ID);
                                    int    varId       = sim.GetComponentPinCurrentVarId(simCompName, simPinNum);
                                    if (varId != -1)
                                    {
                                        ConnectedPinVariables[thisPinNum].Add(varId);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (eventType == SimulationEvent.TICK)
            {
                if ((numberOfUpdates % 2) == 0)
                {
                    Quantity current = new Quantity("I", "Current", "A");
                    Quantity voltage = new Quantity("V", "Voltage", "V");
                    foreach (var pin in ConnectedNets.Keys)
                    {
                        string label = "";
                        if (PinNames.ContainsKey(pin))
                        {
                            label = PinNames[pin] + "\r\n";
                        }
                        else
                        {
                            label = "Pin " + pin + "\r\n";
                        }
                        int netVarId = sim.GetNetVoltageVarId(ConnectedNets[pin]);
                        if (netVarId != -1)
                        {
                            voltage.Val = sim.GetValueOfVar(netVarId, 0);
                            label      += voltage.ToFixedString();
                        }
                        if (ConnectedPinVariables.ContainsKey(pin))
                        {
                            if (ConnectedPinVariables[pin].Count > 0)
                            {
                                current.Val = 0;
                                foreach (int pinVar in ConnectedPinVariables[pin])
                                {
                                    current.Val += sim.GetValueOfVar(pinVar, 0);
                                }

                                string dir = "";
                                if (current.Val < 0)
                                {
                                    dir = "out";
                                }
                                if (current.Val > 0)
                                {
                                    dir = "in";
                                }
                                current.Val = Math.Abs(current.Val);
                                label      += "\r\n" + current.ToFixedString() + " " + dir;
                            }
                        }

                        foreach (Path p in Children.OfType <Path>())
                        {
                            if (p.Name == ("pin" + pin))
                            {
                                p.ToolTip = label;
                            }
                        }
                    }
                }
            }
            else if (eventType == SimulationEvent.STOPPED)
            {
                foreach (int pin in ConnectedNets.Keys)
                {
                    if (PinNames.ContainsKey(pin))
                    {
                        foreach (Path p in Children.OfType <Path>())
                        {
                            if (p.Name == ("pin" + pin))
                            {
                                p.ToolTip = PinNames[pin];
                            }
                        }
                    }
                    else
                    {
                        foreach (Path p in Children.OfType <Path>())
                        {
                            if (p.Name == ("pin" + pin))
                            {
                                p.ToolTip = "Pin " + pin;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
 public virtual void SetComponentValue(Quantity newValue)
 {
     ComponentValue = newValue;
     UpdateText();
 }
        public void UpdateGrid()
        {
            GeometryGroup gridLines   = new GeometryGroup();
            double        lineX       = 0;
            Quantity      currentTime = new Quantity();

            currentTime.Val = 0;
            HLabels.Children.Clear();
            while (lineX >= -GraphArea.ActualWidth)
            {
                gridLines.Children.Add(new LineGeometry(new Point(GraphArea.ActualWidth + lineX, 0), new Point(GraphArea.ActualWidth + lineX, GraphArea.ActualHeight)));
                TextBlock label = new TextBlock();
                label.HorizontalAlignment = HorizontalAlignment.Left;

                label.TextAlignment         = TextAlignment.Center;
                label.Text                  = currentTime.ToString();
                label.RenderTransformOrigin = new Point(0.5, 0.5);
                label.RenderTransform       = new TranslateTransform(lineX + GraphArea.ActualWidth + 50, 0);
                HLabels.Children.Add(label);
                lineX -= VerticalGridSpacing;

                currentTime.Val -= SecPerDiv.Val;
            }


            double   lineY         = GraphArea.ActualHeight / 2;
            Quantity currentVoltsA = new Quantity();

            currentVoltsA.Val = -VoltsOffset;
            Quantity currentVoltsB = new Quantity();

            currentVoltsB.Val = -VoltsOffset;

            VLabels.Children.Clear();

            while (lineY > 0)
            {
                gridLines.Children.Add(new LineGeometry(new Point(0, lineY), new Point(GraphArea.ActualWidth, lineY)));

                TextBlock label1 = new TextBlock();
                label1.HorizontalAlignment = HorizontalAlignment.Right;
                label1.Text = currentVoltsA.ToString();
                label1.RenderTransformOrigin = new Point(1, 0.5);
                label1.RenderTransform       = new TranslateTransform(-5, lineY - 10);

                VLabels.Children.Add(label1);

                /* if (lineY != (GraphArea.ActualHeight - lineY))
                 * {*/
                gridLines.Children.Add(new LineGeometry(new Point(0, GraphArea.ActualHeight - lineY), new Point(GraphArea.ActualWidth, GraphArea.ActualHeight - lineY)));

                TextBlock label2 = new TextBlock();
                label2.HorizontalAlignment = HorizontalAlignment.Right;
                label2.Text = currentVoltsB.ToString();
                label2.RenderTransformOrigin = new Point(1, 0.5);
                label2.RenderTransform       = new TranslateTransform(-5, GraphArea.ActualHeight - lineY - 10);

                VLabels.Children.Add(label2);
                //}
                lineY             -= HorizontalGridSpacing;
                currentVoltsA.Val += VoltsPerDiv.Val;
                currentVoltsB.Val -= VoltsPerDiv.Val;
            }
            GraphGrid.Data = gridLines;
        }