Search code examples
c#wpfwpf-grid

Oscilloscope Display


I am trying to design a GUI for an oscilloscope as a part of my MTech project. While I have no problem setting the scale after taking the input from the user for the X and Y axes, I am facing trouble displaying the waveform. Right now, I am using a PolyLine. Every time a new value is received, the disp array is updated, the current contents are cleared and the wave is displayed. This is the piece of code I wrote, just to add elements and clear the Grid.

public partial class MainWindow : Window
{
    int[] disp = new int[500];
    private int x = 0;
    Polyline Wave=new Polyline();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void SetValue_Click(object sender, RoutedEventArgs e)
    {
        x++;
        SetValue_disp(x);
    }

    private void SetValue_disp(int x)
    {
        for (int i = 0; i < disp.Length; i++)
            disp[i] = x;
    }

    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        TheDisp.Children.Clear();
    }

    private void Apply_Click(object sender, RoutedEventArgs e)
    {
        Wave.StrokeThickness = 2;
        int i = 0;
        foreach (int isp in disp)
        {
            Wave.Points.Add(new Point(i, isp));
            i++;
        }
        TheDisp.Children.Add(Wave);
    }
}

I have the following doubts 1) Why is the element not displayed when it click on the apply button 2) There has to be a better way than to clear the entire grid and re-displaying the entire data all over again

Any form of help appreciated.


Solution

  • 1) Why is the element not displayed when it click on the apply button

    because you did not set the Stroke property of the PolyLine. Please add this line into your Apply_Click method:

    Wave.Stroke = Brushes.Red;
    

    2) There has to be a better way than to clear the entire grid and re-displaying the entire data all over again

    Since the Points are actually in the object PolyLine it should suffice to handle them there. Add the Wave right away to the Graph.Children in the constructor:

    public MainWindow()
    {
        InitializeComponent();
    
        TheDisp.Children.Add(Wave);
    }
    

    When you now add values to the Wave.Points they will be displayed in the graph. Now as time passes and new values come in add just the new ones and remove the first for each value that you add.

    Wave.Points.Add(new Point(X, Y));
    // remove the first value
    Wave.Points.RemoveAt(0);
    

    The number of points would remain constant. When you handle the X-Axis correctly you can simulate an oscilloscope flow.