Define macro


dle

Recommended Posts

There are two ways to avoid creating a whole bunch of sequences.  The first is an undocumented trick that a customer discovered and showed me.  If you create a sequence and put just one line of code outside a function block, you can then create multiple function blocks and they'll show up as global functions:

? "test"
function A()
   ..// do stuff
endfunction

function B()
   .. // do stuff
endfunction

All your function code has to be between function and endfunction or it will run as part of the main sequence code (the ? "test").  The problem with this method, however, is two fold:

a. since its undocumented, its possible, though highly unlikely, that it might break in a future DAQFactory release
b. it clutters the global namespace with your function names which can cause collisions.

Because the global namespace gets cluttered enough with channel names and sequence names and internal functions, I prefer the second method, which is to create a class and instatiate it once:

class globalFunctions

   local someGlobalVariable = 2
   local anotherGlobalVariable = 4

   function A()
      ..// do stuff
   endfunction

   function B()
      .. // do stuff
   endfunction

endclass

Note as a bonus I can stuff some global variables that I need for application in the class and keep those more organized and out of the global namespace.

Then I'd instantiate it once in my startup sequence, usually as "g", because its short and obvious.  Assuming the above code was in a sequence called myGlobalClass:

myglobalClass()  // run the sequence, even though there is no runable code, to create the class
global g = new(globalFunctions) // instatiate the class and store a reference to it in "g"

Then I can access the functions and variables using g.:

g.A()

g.someGlobalVariable = 5

Of course you don't have to use one class, you could use multiple classes / objects if you wanted to organize your functions/variables, or nest your classes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.