dle Posted April 20, 2017 Share Posted April 20, 2017 Is it possible to define a macro in DF similar to C preprocessor below Define ADC128_REG_IN_MAX(ch) = (42 + (ch) * 2) Link to comment Share on other sites More sharing options...
AzeoTech Posted April 21, 2017 Share Posted April 21, 2017 Sure, just create a sequence function: function adc128_reg_in_max(val) return(42 + val * 2) Link to comment Share on other sites More sharing options...
dle Posted April 24, 2017 Author Share Posted April 24, 2017 I was thinking using sequence function, but I have many macros. Do I have to create one sequence functions for each macro? Link to comment Share on other sites More sharing options...
AzeoTech Posted April 25, 2017 Share Posted April 25, 2017 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 More sharing options...
dle Posted April 26, 2017 Author Share Posted April 26, 2017 Thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.