dinsdag 3 oktober 2017

Signature Dataviewer

Following are some (random) notes regarding the creation of a WPF program that is used for reading, logging and displaying data from a 'Signature' series doppler based current profiling sensor ( http://www.nortek-as.com/en/products/current-profilers/signature1000-signature500-en).

Charts

The best solution for displaying charts in both WindowsForms and WPF applications seems to be the OxyPlot package. This is free and open source, available for multiple platforms. It can be installed using NuGet. Note that if you are not using the pre-release versions it might be necessary to switch the .NET target version.
After installing, add the OxyPlot to the toolbox:
Copy OxyPlot.dll to the OxyPlot.WindowsForms.xxx Folder.
  • Right click in the toolbox
  • Select "Choose items..."
  • Press "Browse..." under ".NET framework components
  • Select the OxyPlot.WindowsForms.dll assembly (note that there are versions for both .NET 4.0 and .NET 4.5)
  • The "Plot" control should appear under "OxyPlot.WindowsForms Components" in the ToolBox 
 Do not install 'Oxyplot-Extensions', since that is not using the latest version of OxyPlot.

The is no scrolling Spectrogram type in OxyPlot, although the heatmap comes close.
Scrolling requires fast bitmap blitting, which can be achieved using the 'WriteableBitmap' class.
For creating the palette as required for a spectrograph we can use the 'OxyPalette' function from the OxyPlot package:

var palette = OxyPalette.Interpolate(256, OxyColors.Green, OxyColors.White, OxyColors.Red);

This will return a 256 long array of colors, going from green to white to red, and for every color there is a A,R,G and B component. (Alpha, Red, Green and Blue) 

Theming

WPF Theming is relatively simple if you use the ThemeManager from the WPFToolkit.
Just add the WPF.Themes project to the solution, and create a reference to WPF.Themes.  Then in the Window_Loaded() event handler, set the theme:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ThemeManager.ApplyTheme(this, "ExpressionDark");

        }


Next add the the Background to the XAML file for the window:

        Background="{DynamicResource WindowBackgroundBrush}"

This will change the window background color according to the theme.

Function Keys

Using function keys in WPF seems difficult. But the easiest solution I have found so far is the following.
Add these lines to the XAML file:
   
<Window.Resources>
   <RoutedUICommand x:Key="cmd1"></RoutedUICommand>
</Window.Resources>
<Window.CommandBindings>
   <CommandBinding Command="{StaticResource cmd1}" Executed="PressButtonCommand"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
  <KeyBinding  Key="F12" Command="{StaticResource cmd1}"/>
</Window.InputBindings>

And in the code:
private void PressButtonCommand(object sender,RoutedEventArgs e)
        {
                Console.WriteLine("Button Clicked from F12");
        }


It's explained in this question on StackOverflow. The only problem with this solution is that I don't fully understand what is going on, but it works like a charm.