Adding custom commands in context menu of the Script window


It is also possible to add custom commands applicable to your script to its context menu. For this purpose, two more functions can optionally be added to your script (applies to all types of script windows):


function getCustomActions( )

       return ["CustomFunction1", "Customfunction2"];

end


function customAction( action :: String )

       if( action == "CustomFunction1")

               #do something

       elseif ( action == "Customfunction2")

               #do something else

       end

end


The function getCustomActions returns an array of strings which should be added to the context menu of the window. After the user opens the context menu and selects one of the custom functions, a function customAction will be called with the string parameter equal to the name of the chosen custom command.


This example shows a practical usage of this feature.


Displaying markers in signal


By setting output_window.markers property of your window, you can add visible markers in the resulting signal window. See data types chapter for details about markers array and Marker data structure. Please note that the markers have to be defined on each call of update(...) or process(...) functions, i.e. they are not persistent.


For example, this script processes input signal to calculate a new signal, finds its maximum value and places a marker on maximum position.


function process( input_windows::Array{SigviewWindow}, output_signal::SigviewSignalWindow )

  output_signal.samples = input_windows[1].samples .* 2

  output_signal.samplingRate = input_windows[1].samplingRate

 

  #find max value and index

  found_max = findmax(output_signal.samples) #returns tuple(value, index)

 

  #peak position in x axis units

  peak_position = (found_max[2]-1)/input_windows[1].samplingRate + input_windows[1].xAxisBegin

  #create marker

  new_peak_marker = Marker(peak_position, "Peak")

  #add marker to the list

  push!( output_signal.markers, new_peak_marker )

  return true

end



Displaying Julia plots


Julia includes comprehensive graphical capabilities by using various plotting packages. You will have to add these packages to Julia before using them with SIGVIEW. For example, to add Plots package, you would open Julia console and type


using Pkg; Pkg.add("Plots"); using Plots



After that, you can use plotting in the scripts embedded in SIGVIEW. Plots will not display directly in SIGVIEW but in a separate window. For example, this script displays first parent signal as Julia plot, without displaying anything in SIGVIEW:


using Plots


function process( input_windows::Array{SigviewWindow}, output_signal::SigviewSignalWindow )

  #create plot

  plot( input_windows[1].samples )

  #display Julia plot window

  gui()

 

  #do not provide any data to be displayed in SIGVIEW

  return false

end



Initializing and uninitializing scripts


For some applications, you would need to initialize some external resources once, before executing your update() or process() functions. For this purpose, you can add the following two functions to your script:


function initScript()

begin

  #do some initialization

end


function cleanupScript()

begin

  #do some cleanup

end



For example, if you would develop a script to collect data from a DAQ device, you would use these functions to open and close device communication. Please note that after changing the script content,  cleanupScript() and initScript() will also be called.


Variable visibility, scope


Each script you create for a specific window in SIGVIEW has its own module, i.e. scope. That means that all global variables (i.e. variables outside functions) you create in your scripts, will be visible only from the script for that window, even if there are variables with same names in scripts of other windows.

All global variables you use in your script will be persistent, i.e. will keep their value between consecutive calls to update() or process(). Only if your script has been changed, it will be evaluated again and all global variables will be resetted.

For an example of how a global variable can be used, see this script.