Home AMX User Forum NetLinx Studio

IF_Defined

jjamesjjames Posts: 2,908
Okay, I need some real world examples of IF_DEFINED, and want to know the limitations of it.

I can use it on variables? Devices? Arrays? How do I use it? The "Help" seems a bit . . . unhelpful?

Comments

  • viningvining Posts: 4,368
    As far as I know IF_DEFINED or IF_NOT_DEFINED are only useful for including or excluding segments of code during the compiling. It allows you to pull in certain sections of other .axi or .axs files so you don't need to delcare them again in your modules which helps to eliminate mistakes when changes are made since they are only made in one location.

    In my module I delcare:
                  
    #DEFINE VAV_CATV_INCLUDE 		               
    #INCLUDE 'VAV_CATV'  
    #INCLUDE 'VAV_NP_Auto_Update'   
    #DEFINE VAV_AUTOUPDATE_INCLUDE                          
    
    
    The defines are just any arbitrary name you make up and now that name is defined.

    The include files contains section of code I want to pull into my module which could be constants, variable , functions or the entire file or any part of the file if you want.

    In the one file I'm including I do:
    #IF_DEFINED VAV_CATV_INCLUDE
    
    section of code you want excluded
    
    #END_IF
    
    In the other file I'm including I do:
    #IF_NOT_DEFINED VAV_AUTOUPDATE_INCLUDE
    
    section code or multiple sections to exclude from being included
    
    #END_IF
    
    I the first example the code is excluded because I don't define VAV_AUTOUPDATE_INCLUDE until after I do the include which I think means I really didn't need to define afterwards. I may have realized that back then and was just to lazy to remove it. That was the only example with #IF_DEFINED used with includes probably because it doesn't really make sense.

    The second and for me the most common use is to do the include after you define a name.

    Other that that #IF_DEFINE can be used in include file around device declarations so they can be listed in the include file and the main file so you have them in both files but you don't get a compiler error since they are only included in the first instance.

    In the include file:
    #IF_NOT_DEFINED dvTPCATV
    dvTPCATV   =    10001:8:0
    #END_IF
    
    #IF_NOT_DEFINED dvCATV
    dvCATV     = 5001:11:0// IR 3 NI-3000/4000
    #END_IF
    
    This should also allow these devices to be high lighted and auto-completed in the local code even though they are really read into the compiler in your main code.
  • shr00m-dewshr00m-dew Posts: 394
    vining wrote: »
    This should also allow these devices to be high lighted and auto-completed in the local code even though they are really read into the compiler in your main code.

    Ok, never thought of that. One of the reason's I don't use a lot of include files is because I still need the color coordination and auto-complete to be efficient. This will help out a lot..

    Just don't show examples out of your viewstat schedule module! I'm still not quite sure how it compiles. Throw an include in, it calls two modules, one of which includes the original include you included in your main code! All them full of #IF's and #IF_NOT's.

    Kevin D.
  • viningvining Posts: 4,368
    shr00m-dew wrote:
    Throw an include in, it calls two modules, one of which includes the original include you included in your main code! All them full of #IF's and #IF_NOT's.
    Actually it's the modules that calls the other files to pull in the portion of those files that it needs. The biggest problem with doiing this with modules is the you need the .axs version of the module so it can be re-compiled when any changes are made in the other file from which it is pulling parts of the code. I've often made changes in the included file to only forget to re-compile the module and wonder why things weren't working as expected.

    I don't presently use the #IF_NOT's or #IF_'s to actually include vars, constant, function etc in my .axi files for the editor high-lites and auto-complete but in giving it some more thought last night when I should have been sleeping made me wonder why I wasn't. A simple #DEFINE Global Declarations at the begining of the main code and then I can repeat any var, constant, structure, function I want in any include file for referance, hi-lighting & auto-complete and it will only get pulled in by the compiler once after placing the #IF_NOT's where needed.
  • shr00m-dewshr00m-dew Posts: 394
    vining wrote: »
    shr00m-dew wrote:

    Actually it's the modules that calls the other files to pull in the portion of those files that it needs. The biggest problem with doiing this with modules is the you need the .axs version of the module so it can be re-compiled when any changes are made in the other file from which it is pulling parts of the code. I've often made changes in the included file to only forget to re-compile the module and wonder why things weren't working as expected.


    I'll be damn.. I was confused in the begining and after I got it working I never tried removing the include from my main code. What do you know, it still compiled after I removed it. Well the good news is your IFs and IF_NOTs will still let it compile if you double up your includes..

    Kevin D.
  • jjamesjjames Posts: 2,908
    Okay, so can I define devices, and then do an IF_DEFINED on an array and if the array is defined define further code?

    Something like this:
    DEFINE# dvDTR_LGR = 05001:01:00

    #IF_DEFINED dvDTR_LGR
    dv_DTR[]=
    {
    dvDTR_LGR
    }
    #END_IF

    Somewhere in code.....
    #IF_DEFINED dv_DTR
    DATA_EVENT[dv_DTR]......

    That's kinda what I was looking for. But if all these definitions only work for includes, etc. I'll have to change the way I do code. *Sigh*
  • viningvining Posts: 4,368
    Should work on anything. In the first example we were pulling existing portions of files into a module but you can use it to choose which section of your main code to include in itself as well.
    //comment or un-comment as required!!
    DEFINE dvSatellite =  5001:1:0
    //DEFINE dvCable    =  5001:1:0
    
    
    later in your code:
    
    #IF_NOT_DEFINED dvCABLE
    
    include section of code related to satellite
    
    #END_IF
    
    #IF_NOT_DEFINED dvSatellite
    
    include section of code related to catv
    
    #END_IF
    
    

    This may also work using #IF_DEFINED
    #IF_DEFINED dvCABLE
    
    include section of code related to catv
    
    #END_IF
    
    #IF_DEFINED dvSatellite
    
    include section of code related to satellite
    
    #END_IF
    
    
    Play around and see what happens.
  • annuelloannuello Posts: 294
    I use the #if_defined for three different purposes:

    1. Debugging on different model master. My testbed is a NI-3100, but I have many real-world NI-700s. Would also be useful if I had to deal with NXI masters.
    #define NI_3100  //Comment this out for NI-700 code.
    
    #if_defined NI_3100
    dvRelays = 5001:8:0
    dvIO = 5001:17:0
    #else
    dvRelays =5001:4:0  //Driven off the I/O port
    dvIO = 5001:4:0
    #end_if
    

    2. Configuring a generic program for specific installations.
    #if_defined PROJ_EPSON
    define_module 'Epson EMP-821' modProjEpson(vdvProj1, dvProj1)
    #end_if
    #if_defined PROJ_HITACHI
    define_module 'Hitachi CP-X385' modProjHitachi(vdvProj1, dvProj1)
    #end_if
    

    3. Managing common include files where the compiler can't cope with it being included from more than one file. ("Device/variable already declared..." errors.) Particularly useful on my global file.
    PROGRAM_NAME='Global'
    #if_not_defined GLOBAL_AXI
    #define GLOBAL_AXI
    ...
    ...  //All my global devices & variables go here.
    ...
    #end_if
    
    Roger McLean
    Swinburne University
Sign In or Register to comment.