Skip to content

Commit

Permalink
Refactor dsp visualizers to use buffered updates
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jul 9, 2023
1 parent 4ada1bb commit e6523e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
37 changes: 17 additions & 20 deletions Bonsai.Dsp.Design/MatVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.InteropServices;
using System.Reactive.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reactive;

[assembly: TypeVisualizer(typeof(MatVisualizer), Target = typeof(Mat))]

Expand All @@ -26,13 +28,13 @@ public class MatVisualizer : MatVisualizer<WaveformView>
/// <typeparam name="TWaveformView">
/// A type derived from <see cref="WaveformView"/> which will control how data is displayed.
/// </typeparam>
public class MatVisualizer<TWaveformView> : DialogTypeVisualizer where TWaveformView : WaveformView, new()
public class MatVisualizer<TWaveformView> : BufferedVisualizer where TWaveformView : WaveformView, new()
{
const int TargetElapsedTime = (int)(1000.0 / 30);
bool requireInvalidate;
Timer updateTimer;
TWaveformView graph;

/// <inheritdoc/>
protected override int TargetInterval => 1000 / 30;

/// <summary>
/// Gets or sets the lower bound of the x-axis displayed in the graph.
/// </summary>
Expand Down Expand Up @@ -131,9 +133,9 @@ protected internal TWaveformView Graph
/// Invalidates the entire graph display at the next data update.
/// This will send a paint message to the graph control.
/// </summary>
[Obsolete]
protected void InvalidateGraph()
{
requireInvalidate = true;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -194,27 +196,13 @@ public override void Load(IServiceProvider provider)
if (visualizerService != null)
{
visualizerService.AddControl(graph);
updateTimer = new System.Windows.Forms.Timer();
updateTimer.Interval = TargetElapsedTime;
updateTimer.Tick += (sender, e) =>
{
if (requireInvalidate)
{
graph.InvalidateWaveform();
requireInvalidate = false;
}
};
updateTimer.Start();
}
}

/// <inheritdoc/>
public override void Unload()
{
updateTimer.Stop();
updateTimer.Dispose();
graph.Dispose();
updateTimer = null;
graph = null;
}

Expand All @@ -235,7 +223,16 @@ public override void Show(object value)
sampleHandle.Free();

graph.UpdateWaveform(samples, rows, columns);
InvalidateGraph();
}

/// <inheritdoc/>
protected override void ShowBuffer(IList<Timestamped<object>> values)
{
base.ShowBuffer(values);
if (values.Count > 0)
{
graph.InvalidateWaveform();
}
}
}
}
2 changes: 0 additions & 2 deletions Bonsai.Dsp.Design/SpikeWaveformCollectionVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public override void Show(object value)

Graph.UpdateWaveform(spike.ChannelIndex, samples, bufferSize.Height, samples.Length);
}

InvalidateGraph();
}
}
}

0 comments on commit e6523e5

Please sign in to comment.