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





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.


Specifying custom tool parameters


If you create a Julia script based window in order to save it as a Custom tool , some functions are provided to define the later behaviour of that tool. You can define how many parent windows will the tool accept by using getMinParentCount/getMaxParentCount functions. If the number of parent windows is outside of this range, the corresponding menu option in a Custom tools menu will be disabled.

By using toolName function, you can define the name of the tool, which will be visible in the Custom tools menu. 


function getMinParentCount()

  return 1

end


function getMaxParentCount()

  return 2

end



function toolName()

  return "My plot"

end


Modifying input windows


In some rare cases, it is needed for the script window to change the values of its parent windows. Please note that it is not possible to change length of input signal or window properties - it is only allowed to change signal values. For example, see Align signals  custom tool. 

If this is needed, a function changesInputWindows must be implemented and must return true. As a result, on each script execution, all parent windows will be redrawn as well.

  

function changesInputWindows()

return true

end

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.